1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.jsieve;
21
22 import java.util.Map;
23
24 import org.apache.jsieve.exception.LookupException;
25
26 /**
27 * Maps command names to comman implementations.
28 */
29 public class CommandManagerImpl implements CommandManager {
30
31 private final Map classNameMap;
32
33 /**
34 * Constructor for CommandManager.
35 */
36 public CommandManagerImpl(final Map classNameMap) {
37 super();
38 this.classNameMap = classNameMap;
39 }
40
41 /**
42 * <p>
43 * Method lookup answers the class to which a Command name is mapped.
44 * </p>
45 *
46 * @param name -
47 * The name of the Command
48 * @return Class - The class of the Command
49 * @throws LookupException
50 */
51 private Class lookup(String name) throws LookupException {
52 Class cmdClass = null;
53 try {
54 cmdClass = getClass().getClassLoader()
55 .loadClass(getClassName(name));
56 } catch (ClassNotFoundException e) {
57 throw new LookupException("Command named '" + name + "' not found.");
58 }
59 if (!ExecutableCommand.class.isAssignableFrom(cmdClass))
60 throw new LookupException("Class " + cmdClass.getName()
61 + " must implement " + ExecutableCommand.class.getName());
62 return cmdClass;
63 }
64
65 /**
66 * <p>
67 * Method newInstance answers an instance of the class to which a Command
68 * name is mapped.
69 * </p>
70 *
71 * @param name -
72 * The name of the Command
73 * @return Class - The class of the Command
74 * @throws LookupException
75 */
76 public ExecutableCommand getCommand(String name) throws LookupException {
77 try {
78 return (ExecutableCommand) lookup(name).newInstance();
79 } catch (InstantiationException e) {
80 throw new LookupException(e.getMessage());
81 } catch (IllegalAccessException e) {
82 throw new LookupException(e.getMessage());
83 }
84 }
85
86 /**
87 * Method isSupported answers a boolean indicating if a Command name is
88 * configured.
89 *
90 * @param name -
91 * The Command name
92 * @return boolean - True if the Command name is configured.
93 */
94 public boolean isCommandSupported(String name) {
95 boolean isSupported = false;
96 try {
97 lookup(name);
98 isSupported = true;
99 } catch (LookupException e) {
100 }
101 return isSupported;
102 }
103
104 /**
105 * <p>
106 * Method getClassName answers the name of the class to which a Command name
107 * is mapped.
108 * </p>
109 *
110 * @param name -
111 * The name of the Command
112 * @return String - The name of the class
113 * @throws LookupException
114 */
115 protected String getClassName(String name) throws LookupException {
116 final String className = (String) classNameMap.get(name.toLowerCase());
117 if (null == className)
118 throw new LookupException("Command named '" + name
119 + "' not mapped.");
120 return className;
121 }
122 }