1 /*
2 * Replica is published under the terms
3 * of the Apache Software License.
4 */
5 package replica.command.beans;
6
7 import java.util.HashMap;
8 import java.util.Hashtable;
9
10 import org.springframework.context.ApplicationContext;
11
12 import replica.command.Command;
13 import replica.command.CommandException;
14 import replica.command.Mappings;
15
16 /***
17 * @author Pedro Costa
18 * @author Helder Silva
19 * @since 20/Jan/2004
20 */
21 public class BeansMappings implements Mappings {
22
23
24 // mapps from action to bean name or reference
25 HashMap beansMappings;
26
27 // if the mapping is a bean reference take it from context.
28 ApplicationContext beansContext;
29
30 // if the mapping is a class name create one, put in this
31 // table and reuse it.
32 Hashtable commandsByName = new Hashtable();
33
34
35 /***
36 * Default constructor;
37 */
38 public BeansMappings() {
39 super();
40 }
41
42 /* (non-Javadoc)
43 * @see replica.command.Mappings#getMappedClass(java.lang.String)
44 */
45 public Class getMappedClass(String actionId) throws CommandException {
46 return getNewCommand(actionId).getClass();
47 }
48
49 /* (non-Javadoc)
50 * @see replica.command.Mappings#getNewCommand(java.lang.String)
51 */
52 public Command getNewCommand(String actionId) throws CommandException {
53
54 Object obj = beansMappings.get(actionId.toLowerCase());
55
56 String name = null;
57 if( obj instanceof String )
58 name = obj.toString();
59
60 if( obj == null || obj instanceof String ){
61 // not a bean reference
62 // look on commands table
63 obj = commandsByName.get( actionId );
64
65 if( obj == null ){
66 // not in table , create a new one and put on table
67 try{
68 obj = Class.forName( name ).newInstance();
69 }
70 catch(Exception e){
71 throw new CommandException("Problem creating new instance of [" + name
72 + "] for action [" + actionId + "].", e);
73 }
74 commandsByName.put(actionId, obj);
75 }
76 }
77
78 return (Command)obj;
79 }
80
81 /***
82 * @return
83 */
84 public ApplicationContext getBeansContext() {
85 return beansContext;
86 }
87
88 /***
89 * @return
90 */
91 public HashMap getBeansMappings() {
92 return beansMappings;
93 }
94
95 /***
96 * @param context
97 */
98 public void setBeansContext(ApplicationContext context) {
99 beansContext = context;
100 }
101
102 /***
103 * @param hashtable
104 */
105 public void setBeansMappings(HashMap hashtable) {
106 beansMappings = hashtable;
107 }
108
109 }
This page was automatically generated by Maven