1   /*
2    * Replica is published under the terms
3    * of the Apache Software License.
4    */
5   package replica.command;
6   
7   import java.util.Enumeration;
8   import java.util.Hashtable;
9   
10  import replica.utils.BeanUtils;
11  
12  
13  /***
14   *  This Mappings implementation stores the mapped configuration in a hashtable.
15   * 
16   * @author Pedro Costa
17   * @author Helder Silva
18   * @since 17/Jan/2004
19   */
20  public class HashtableMappings implements Mappings {
21  	
22  	// contain the class names mapped to action ids.
23  	Hashtable classMapping = new Hashtable();
24  	
25  	// contain the initial parameters for a given class name
26  	// the key is the class name
27  	ParametersMappings initParameters = new ParametersMappings();
28  
29  	/***
30  	 * Default constructor. 
31  	 */
32  	public HashtableMappings() {
33  		super();
34  	}
35  	
36  	public void addMapping(String actionId, String className){
37  		classMapping.put(actionId, className);
38  	}
39  	
40  	public void addMapping(String actionId, String className, Hashtable parameters ){
41  		if( parameters == null ){
42  			addMapping(actionId, className);
43  			return;
44  		}
45  		
46  		int size = parameters.size();
47  		addMapping( actionId, className, (String[])parameters.keySet().toArray(new String[size]),
48  								parameters.values().toArray(new Object[size]) );
49  	}
50  	
51  	public void addMapping(String actionId, String className, String[] paramNames, Object[] paramValues){
52  		addMapping(actionId, className);
53  		initParameters.addParameters(className, paramNames, paramValues);
54  	}
55  	
56  	/*** 
57  	 * 
58  	 * @see replica.command.Mappings#getMappedClass(java.lang.String)
59  	 */
60  	public Class getMappedClass(String actionId) throws CommandException{
61  		
62  		try{
63  			Class clazz = Class.forName((String)classMapping.get( actionId ));
64  			
65  			if( clazz == null )
66  				throw new NoSuchCommandException("No command found for action id " + actionId, actionId);
67  				
68  			return clazz;
69  		}
70  		catch(ClassNotFoundException e){
71  			throw new CommandException(e);
72  		}
73  	}
74  
75  	/* (non-Javadoc)
76  	 * @see replica.server.cmd.Mappings#getNewCommand(java.lang.String)
77  	 */
78  	public Command getNewCommand(String actionId) throws CommandException {
79  		
80  		Class clazz = getMappedClass( actionId );
81  		
82  		try{
83  			Command command = (Command)clazz.newInstance();
84  			
85  			initCommand( command );
86  			
87  			return command;
88  		}
89  		catch(IllegalAccessException e){
90  			throw new CommandException("Error creating command from class [" + clazz + 
91  					"] for action id [" + actionId + "]. Look for the root cause on this exception." + e.toString(), e );
92  		}
93  		catch(InstantiationException e ){
94  			throw new CommandException("Error creating command from class [" + clazz + 
95  					"] for action id [" + actionId + "]. Look for the root cause on this exception." + e.toString(), e );
96  		}
97  		 
98  	}
99  	
100 	/***
101 	 * Initialize the given command with the defined parameters (if any).
102 	 * 
103 	 * @param command
104 	 */
105 	protected void initCommand(Command command){
106 		
107 		Hashtable table = initParameters.getParameters(command.getClass().getName());
108 		
109 		if( table == null || table.size() == 0 )
110 			return;
111 		
112 		Enumeration enum = table.keys();
113 		while( enum.hasMoreElements() ){
114 			String key = (String)enum.nextElement();
115 			BeanUtils.writeProperty( command, key, table.get(key).toString() );
116 		}
117 	}
118 	
119 	class ParametersMappings{
120 		
121 		private Hashtable mappings = new Hashtable();
122 		
123 		ParametersMappings(){
124 			super();
125 		}
126 		
127 		/***
128 		 * Adds initial parameter values for the given class.
129 		 * 
130 		 * @param clazz the class
131 		 * @param paramName parameter name
132 		 * @param paramValue parameter value
133 		 */
134 		public void addParameters( String className, String[] paramNames, Object[] paramValues ){
135 			
136 			if( paramNames.length != paramValues.length )
137 				throw new IllegalArgumentException("Parameter Names and values must have the same lenght.");
138 			
139 			Hashtable params = (Hashtable)mappings.get(className);
140 			
141 			if( params == null ){
142 				params = new Hashtable();
143 				mappings.put(className, params);
144 			}
145 			
146 			for (int i = 0; i < paramNames.length; i++)
147 				params.put(paramNames[i], paramValues[i]);
148 		}
149 		
150 		/***
151 		 * Adds initial Parameter values for the given class.
152 		 * 
153 		 * @param clazz the class
154 		 * @param parameters Hashtable containing the parameters.
155 		 * 				 Where the key must the parameter name and the value the parameter value.
156 		 */
157 		public void addParameters( String className, Hashtable parameters){
158 			
159 			int size = parameters.size();
160 			
161 			addParameters( className, (String[])parameters.entrySet().toArray( new String[ size ] ),
162 														parameters.values().toArray( new Object[ size ] ) );
163 		}
164 
165 		/***
166 		 * Shortcut method.
167 		 * 
168 		 * @see #addParameters( Class, String[], Object[])
169 		 * @param clazz
170 		 * @param paramName
171 		 * @param paramValue
172 		 */		
173 		public void addParameter( String className, String paramName, Object paramValue ){
174 			
175 			addParameters(className, new String[]{paramName}, new Object[]{paramValue});
176 		}
177 		
178 		/***
179 		 * Return the initial parameters for the given class.
180 		 * 
181 		 * @param clazz
182 		 * @return table conting as key the parameters names and as values the <i>values</i>.
183 		 */
184 		public Hashtable getParameters( String className  ){
185 			
186 			Hashtable table = (Hashtable)mappings.get( className );
187 			
188 			if( table == null )
189 				return new Hashtable();
190 				
191 			return table;
192 		}
193 	}
194 
195 	/***
196 		 * Adds mappings for the received commands.
197 		 * 
198 		 * @param className the name of the command class to map the commands in the received list.
199 		 * @param paramsTable a table with the initial parameters value for the command class.
200 		 * @param commands a list of commands to map to the received action class.
201 		 */
202 	protected void addMappingsForClass(String className, Hashtable paramsTable, String[] commands) {
203 			
204 		for (int i = 0; i < commands.length; i++) {
205 			addMapping(commands[i], className, paramsTable );
206 		}
207 	}
208 
209 }
This page was automatically generated by Maven