KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > mediator > EjbSvoMediator


1 package org.bsf.smartValueObject.mediator;
2
3 import org.bsf.smartValueObject.SmartAccess;
4 import org.apache.commons.beanutils.BeanUtils;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.naming.NamingException JavaDoc;
11 import java.lang.reflect.Field JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 /**
17  * A mediator for EJBs. This class is to be used by a session facade
18  * to retrieve/update objects based on graph of VOs.
19  * Right now we use a dummy implementation to simulate
20  * real ejb lookups. Implementation is not yet finished !
21  *
22  */

23 public class EjbSvoMediator implements Mediator {
24     private static Log log = LogFactory.getLog(EjbSvoMediator.class);
25     /** The class of the VO. */
26     private Class JavaDoc clazz;
27     /** To config lookups etc. */
28     private Map JavaDoc config;
29     /** The index field of the VO */
30     private String JavaDoc indexField;
31     /* Dummy storage for testing purposes. */
32     private Map JavaDoc objects;
33     /* The JNDI context */
34     private Context JavaDoc context = null;
35     /* The Home Interface */
36     private Class JavaDoc homeClass;
37
38     public static final String JavaDoc CONTEXT = "context";
39     public static final String JavaDoc HOMECLASS = "homeclass";
40
41     /**
42      * Creates a mediator for the given class.
43      *
44      * @param clazz the class of the VO.
45      * @param config configuration parameters.
46      */

47     public EjbSvoMediator(Class JavaDoc clazz, Map JavaDoc config) {
48         this.clazz = clazz;
49         this.config = config;
50         this.objects = new HashMap JavaDoc();
51         readConfig(this.config);
52     }
53
54     /**
55      * Retrieves graph based on given prototype.
56      *
57      * @param prototype
58      * @return
59      * @throws MediatorException
60      */

61     public Object JavaDoc getGraph(Object JavaDoc prototype) throws MediatorException {
62         log.info("getGraph(" + prototype + ")");
63         if (prototype == null || prototype.getClass().getName() != clazz.getName())
64                 throw new MediatorException("Unknown prototype");
65
66         return lookFor(prototype);
67     }
68
69     /**
70      * Stores graph.
71      * @param graph
72      * @throws MediatorException
73      */

74     public ChangeSummary updateGraph(Object JavaDoc graph) throws MediatorException {
75         log.info("updateGraph(" + graph + ")");
76         if (graph == null || graph.getClass().getName() != clazz.getName())
77                  throw new MediatorException("Unknown prototype");
78
79         if (!SmartAccess.isVersionable(graph)) {
80             throw new MediatorException("Object not versionable");
81         }
82
83         if (SmartAccess.isGraphDirty(graph)) {
84             storeEJB(graph);
85         }
86         return new ChangeSummary(null, null);
87     }
88
89     /**
90      * Deletes a graph.
91      * @param graph the root element of the graph.
92      * @throws MediatorException
93      */

94     public void deleteGraph(Object JavaDoc graph)
95             throws MediatorException {
96     }
97
98     private void storeEJB(Object JavaDoc graph) throws MediatorException {
99         log.info("storeEJB(" + graph + ")");
100
101         Object JavaDoc index;
102         try {
103             index = graph.getClass().getField(indexField).get(graph);
104         } catch (Exception JavaDoc e) {
105             throw new MediatorException(e);
106         }
107
108         objects.put(index, graph);
109     }
110
111     private Object JavaDoc lookFor(Object JavaDoc o) throws MediatorException {
112         Field JavaDoc[] fields = o.getClass().getDeclaredFields();
113         for (int i = 0; i < fields.length; i++) {
114             Field JavaDoc field = fields[i];
115             if (field.getName().equals(indexField)) {
116                 Object JavaDoc index;
117                 try {
118                     index = field.get(o);
119                 } catch (Exception JavaDoc e) {
120                     throw new MediatorException(e);
121                 }
122
123                 if (index != null)
124                     return lookForEJB(index);
125             }
126         }
127
128         throw new MediatorException("Object not found");
129     }
130
131     private Object JavaDoc lookForEJB(Object JavaDoc index) throws MediatorException {
132         log.info("lookForEJB: " + index);
133         Object JavaDoc o = createNewVO();
134         Object JavaDoc src = lookForIndex(index);
135
136         try {
137             BeanUtils.copyProperties(o, src);
138         } catch (Exception JavaDoc e) {
139             throw new MediatorException(e);
140         }
141         return o;
142     }
143
144     // fake ejb lookup
145
private Object JavaDoc lookForIndex(Object JavaDoc index) {
146         return objects.get(index);
147     }
148
149     private void readConfig(Map JavaDoc config) {
150         indexField = (String JavaDoc) config.get(INDEXFIELD);
151         context = (Context JavaDoc) config.get(CONTEXT);
152     }
153
154     /**
155      * Gets an 'empty' VO.
156      * @return
157      * @throws MediatorException
158      */

159     private Object JavaDoc createNewVO() throws MediatorException {
160         Object JavaDoc o;
161         try {
162             o = clazz.newInstance();
163         } catch (Exception JavaDoc e) {
164             throw new MediatorException(e);
165         }
166         return o;
167     }
168
169     /** Gets initial context. */
170     private Context JavaDoc getContext() {
171         if (context == null) {
172             try {
173                 Properties JavaDoc props = new Properties JavaDoc();
174                 props.put(Context.INITIAL_CONTEXT_FACTORY,
175                     "org.jnp.interfaces.NamingContextFactory");
176                 props.put(Context.PROVIDER_URL,
177                     "jnp://localhost:1099");
178                 props.put(Context.URL_PKG_PREFIXES,
179                     "org.jboss.naming:org.jnp.interfaces");
180
181                 context = new InitialContext JavaDoc(props);
182             } catch (NamingException JavaDoc e) {
183                 throw new RuntimeException JavaDoc(e.getLocalizedMessage());
184             }
185         }
186
187         return context;
188     }
189 }
190
191
Popular Tags