KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > client > ClientContainer


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ejb3.client;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.lang.reflect.AccessibleObject JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NameClassPair JavaDoc;
38 import javax.naming.NameNotFoundException JavaDoc;
39 import javax.naming.NamingEnumeration JavaDoc;
40
41 import org.jboss.ejb3.Container;
42 import org.jboss.ejb3.DependencyPolicy;
43 import org.jboss.ejb3.entity.PersistenceUnitDeployment;
44 import org.jboss.ejb3.metamodel.ApplicationClientDD;
45 import org.jboss.injection.DependsHandler;
46 import org.jboss.injection.EJBHandler;
47 import org.jboss.injection.EncInjector;
48 import org.jboss.injection.InjectionContainer;
49 import org.jboss.injection.InjectionHandler;
50 import org.jboss.injection.InjectionUtil;
51 import org.jboss.injection.Injector;
52 import org.jboss.injection.JndiInjectHandler;
53 import org.jboss.injection.PersistenceContextHandler;
54 import org.jboss.injection.PersistenceUnitHandler;
55 import org.jboss.injection.ResourceHandler;
56 import org.jboss.injection.WebServiceRefHandler;
57 import org.jboss.logging.Logger;
58 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
59 import org.jboss.util.NotImplementedException;
60
61 /**
62  * Injection of the application client main class is handled from here.
63  *
64  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
65  * @version $Revision: $
66  */

67 public class ClientContainer implements InjectionContainer
68 {
69    private static final Logger log = Logger.getLogger(ClientContainer.class);
70    
71    private Class JavaDoc<?> mainClass;
72    private ApplicationClientDD xml;
73    private String JavaDoc applicationClientName;
74    
75    // for performance there is an array.
76
private List JavaDoc<Injector> injectors = new ArrayList JavaDoc<Injector>();
77    private Map JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>> encInjections = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>>();
78    private Map JavaDoc<String JavaDoc, EncInjector> encInjectors = new HashMap JavaDoc<String JavaDoc, EncInjector>();
79    
80    private Context JavaDoc enc;
81    private Context JavaDoc encEnv;
82
83    public ClientContainer(ApplicationClientDD xml, Class JavaDoc<?> mainClass, String JavaDoc applicationClientName) throws Exception JavaDoc
84    {
85       this.xml = xml;
86       this.mainClass = mainClass;
87       this.applicationClientName = applicationClientName;
88       
89       //Context ctx = getInitialContext();
90
Context JavaDoc ctx = new InitialContext JavaDoc();
91       enc = (Context JavaDoc) ctx.lookup(applicationClientName);
92       NamingEnumeration JavaDoc<NameClassPair JavaDoc> e = enc.list("");
93       while(e.hasMore())
94       {
95          NameClassPair JavaDoc ncp = e.next();
96          log.debug(" " + ncp);
97       }
98       encEnv = (Context JavaDoc) enc.lookup("env");
99 // enc = ThreadLocalENCFactory.create(ctx);
100
// encEnv = Util.createSubcontext(enc, "env");
101

102       processMetadata(null);
103       
104 // for (EncInjector injector : encInjectors.values())
105
// {
106
// log.trace("encInjector: " + injector);
107
// injector.inject(this);
108
// }
109

110       for(Injector injector : injectors)
111       {
112          log.trace("injector: " + injector);
113          injector.inject((Object JavaDoc) null);
114       }
115    }
116    
117    /* (non-Javadoc)
118     * @see org.jboss.injection.InjectionContainer#getAnnotation(java.lang.Class, java.lang.Class)
119     */

120    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationClass, Class JavaDoc<?> clazz)
121    {
122       return clazz.getAnnotation(annotationClass);
123    }
124    
125    /* (non-Javadoc)
126     * @see org.jboss.injection.InjectionContainer#getAnnotation(java.lang.Class, java.lang.Class, java.lang.reflect.Method)
127     */

128    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationClass, Class JavaDoc<?> clazz, Method JavaDoc method)
129    {
130       return method.getAnnotation(annotationClass);
131    }
132
133    /* (non-Javadoc)
134     * @see org.jboss.injection.InjectionContainer#getAnnotation(java.lang.Class, java.lang.reflect.Method)
135     */

136    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationClass, Method JavaDoc method)
137    {
138       return method.getAnnotation(annotationClass);
139    }
140
141    /* (non-Javadoc)
142     * @see org.jboss.injection.InjectionContainer#getAnnotation(java.lang.Class, java.lang.Class, java.lang.reflect.Field)
143     */

144    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationClass, Class JavaDoc<?> clazz, Field JavaDoc field)
145    {
146       return field.getAnnotation(annotationClass);
147    }
148
149    /* (non-Javadoc)
150     * @see org.jboss.injection.InjectionContainer#getAnnotation(java.lang.Class, java.lang.reflect.Field)
151     */

152    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationClass, Field JavaDoc field)
153    {
154       return field.getAnnotation(annotationClass);
155    }
156
157    /* (non-Javadoc)
158     * @see org.jboss.injection.InjectionContainer#getClassloader()
159     */

160    public ClassLoader JavaDoc getClassloader()
161    {
162       throw new RuntimeException JavaDoc("NYI");
163    }
164
165    /* (non-Javadoc)
166     * @see org.jboss.injection.InjectionContainer#getDependencyPolicy()
167     */

168    public DependencyPolicy getDependencyPolicy()
169    {
170       throw new RuntimeException JavaDoc("NYI");
171    }
172
173    /* (non-Javadoc)
174     * @see org.jboss.injection.InjectionContainer#getDeploymentDescriptorType()
175     */

176    public String JavaDoc getDeploymentDescriptorType()
177    {
178       return "application-client.xml";
179    }
180
181    /* (non-Javadoc)
182     * @see org.jboss.injection.InjectionContainer#getEjbJndiName(java.lang.Class)
183     */

184    public String JavaDoc getEjbJndiName(Class JavaDoc businessInterface) throws NameNotFoundException JavaDoc
185    {
186       throw new RuntimeException JavaDoc("NYI");
187       //return null;
188
}
189
190    /* (non-Javadoc)
191     * @see org.jboss.injection.InjectionContainer#getEjbJndiName(java.lang.String, java.lang.Class)
192     */

193    public String JavaDoc getEjbJndiName(String JavaDoc link, Class JavaDoc businessInterface)
194    {
195       throw new NotImplementedException();
196       //return "java:comp/env/" + link + "/remote";
197
//return applicationClientName + "/" + link + "/remote";
198
//return null;
199
}
200
201    /* (non-Javadoc)
202     * @see org.jboss.injection.InjectionContainer#getEnc()
203     */

204    public Context JavaDoc getEnc()
205    {
206       return enc;
207    }
208
209    /* (non-Javadoc)
210     * @see org.jboss.injection.InjectionContainer#getEncEnv()
211     */

212    public Context JavaDoc getEncEnv()
213    {
214       return encEnv;
215    }
216
217    /* (non-Javadoc)
218     * @see org.jboss.injection.InjectionContainer#getEncInjections()
219     */

220    public Map JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>> getEncInjections()
221    {
222       return encInjections;
223    }
224
225    /* (non-Javadoc)
226     * @see org.jboss.injection.InjectionContainer#getEncInjectors()
227     */

228    public Map JavaDoc<String JavaDoc, EncInjector> getEncInjectors()
229    {
230       return encInjectors;
231    }
232
233    /* (non-Javadoc)
234     * @see org.jboss.injection.InjectionContainer#getEnvironmentRefGroup()
235     */

236    public EnvironmentRefGroup getEnvironmentRefGroup()
237    {
238       return xml;
239    }
240
241    /* (non-Javadoc)
242     * @see org.jboss.injection.InjectionContainer#getIdentifier()
243     */

244    public String JavaDoc getIdentifier()
245    {
246 // throw new NotImplementedException;
247
// FIXME: return the real identifier
248
//return "client-identifier";
249
return applicationClientName;
250    }
251
252    /* (non-Javadoc)
253     * @see org.jboss.injection.InjectionContainer#getInjectors()
254     */

255    public List JavaDoc<Injector> getInjectors()
256    {
257       throw new NotImplementedException();
258    }
259
260    public Class JavaDoc<?> getMainClass()
261    {
262       return mainClass;
263    }
264    
265    /* (non-Javadoc)
266     * @see org.jboss.injection.InjectionContainer#getPersistenceUnitDeployment(java.lang.String)
267     */

268    public PersistenceUnitDeployment getPersistenceUnitDeployment(String JavaDoc unitName) throws NameNotFoundException JavaDoc
269    {
270       throw new NotImplementedException();
271    }
272
273    public void invokeMain(String JavaDoc args[]) throws SecurityException JavaDoc, NoSuchMethodException JavaDoc, IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
274    {
275       Class JavaDoc parameterTypes[] = { args.getClass() };
276       Method JavaDoc method = mainClass.getDeclaredMethod("main", parameterTypes);
277       method.invoke(null, (Object JavaDoc) args);
278    }
279    
280    private void processMetadata(DependencyPolicy dependencyPolicy)
281    {
282       // TODO: check which handlers a client container should support
283
Collection JavaDoc<InjectionHandler> handlers = new ArrayList JavaDoc<InjectionHandler>();
284       handlers.add(new EJBHandler());
285       //handlers.add(new ClientEJBHandler());
286
handlers.add(new DependsHandler());
287       handlers.add(new JndiInjectHandler());
288       handlers.add(new PersistenceContextHandler());
289       handlers.add(new PersistenceUnitHandler());
290       handlers.add(new ResourceHandler());
291       handlers.add(new WebServiceRefHandler());
292       
293       // TODO: we're going to use a jar class loader
294
// ClassLoader old = Thread.currentThread().getContextClassLoader();
295
// Thread.currentThread().setContextClassLoader(classloader);
296
try
297       {
298          // EJB container's XML must be processed before interceptor's as it may override interceptor's references
299
for (InjectionHandler handler : handlers) handler.loadXml(xml, this);
300
301          Map JavaDoc<AccessibleObject JavaDoc, Injector> tmp = InjectionUtil.processAnnotations(this, handlers, getMainClass());
302          injectors.addAll(tmp.values());
303
304 // initialiseInterceptors();
305
// for (InterceptorInfo interceptorInfo : applicableInterceptors)
306
// {
307
// for (InjectionHandler handler : handlers)
308
// {
309
// handler.loadXml(interceptorInfo.getXml(), this);
310
// }
311
// }
312
// for (InterceptorInfo interceptorInfo : applicableInterceptors)
313
// {
314
// Map<AccessibleObject, Injector> tmpInterceptor = InjectionUtil.processAnnotations(this, handlers, interceptorInfo.getClazz());
315
// InterceptorInjector injector = new InterceptorInjector(this, interceptorInfo, tmpInterceptor);
316
// interceptorInjectors.put(interceptorInfo.getClazz(), injector);
317
// }
318
}
319       finally
320       {
321 // Thread.currentThread().setContextClassLoader(old);
322
}
323    }
324    
325    /* (non-Javadoc)
326     * @see org.jboss.injection.InjectionContainer#resolveEjbContainer(java.lang.String, java.lang.Class)
327     */

328    public Container resolveEjbContainer(String JavaDoc link, Class JavaDoc businessIntf)
329    {
330       log.warn("resolveEjbContainer(" + link + ", " + businessIntf + ") not implemented");
331       return null;
332    }
333
334    /* (non-Javadoc)
335     * @see org.jboss.injection.InjectionContainer#resolveEjbContainer(java.lang.Class)
336     */

337    public Container resolveEjbContainer(Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
338    {
339       return null;
340    }
341 }
342
Popular Tags