KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > clientmodule > ClientENCInjectionContainer


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.clientmodule;
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.Method JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.management.MalformedObjectNameException JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.NameNotFoundException JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39
40 import org.jboss.deployers.spi.deployer.DeploymentUnit;
41 import org.jboss.ejb3.Container;
42 import org.jboss.ejb3.DependencyPolicy;
43 import org.jboss.ejb3.DeploymentScope;
44 import org.jboss.ejb3.Ejb3Module;
45 import org.jboss.ejb3.deployers.JBoss5DependencyPolicy;
46 import org.jboss.ejb3.deployers.JBoss5DeploymentScope;
47 import org.jboss.ejb3.enc.DeploymentEjbResolver;
48 import org.jboss.ejb3.entity.PersistenceUnitDeployment;
49 import org.jboss.ejb3.metamodel.ApplicationClientDD;
50 import org.jboss.injection.DependsHandler;
51 import org.jboss.injection.EJBHandler;
52 import org.jboss.injection.EncInjector;
53 import org.jboss.injection.InjectionContainer;
54 import org.jboss.injection.InjectionHandler;
55 import org.jboss.injection.InjectionUtil;
56 import org.jboss.injection.Injector;
57 import org.jboss.injection.JndiInjectHandler;
58 import org.jboss.injection.PersistenceContextHandler;
59 import org.jboss.injection.PersistenceUnitHandler;
60 import org.jboss.injection.ResourceHandler;
61 import org.jboss.injection.WebServiceRefHandler;
62 import org.jboss.logging.Logger;
63 import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
64 import org.jboss.naming.Util;
65
66 /**
67  * This class builds up the java:comp namespace for JavaEE 5 application clients.
68  * It uses the existing injection framework to get this done.
69  *
70  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
71  * @version $Revision: 58013 $
72  */

73 public class ClientENCInjectionContainer implements InjectionContainer
74 {
75    private static final Logger log = Logger.getLogger(ClientENCInjectionContainer.class);
76    
77    private ApplicationClientDD xml;
78    private Class JavaDoc<?> mainClass;
79    private String JavaDoc applicationClientName;
80    private ClassLoader JavaDoc classLoader;
81    
82    // TODO: remove injectors, these are not supported
83
private List JavaDoc<Injector> injectors = new ArrayList JavaDoc<Injector>();
84    private Map JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>> encInjections = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>>();
85    private Map JavaDoc<String JavaDoc, EncInjector> encInjectors = new HashMap JavaDoc<String JavaDoc, EncInjector>();
86    
87    private Context JavaDoc enc;
88    private Context JavaDoc encEnv;
89    
90    private DeploymentEjbResolver ejbResolver;
91    private ObjectName JavaDoc objectName;
92    private DependencyPolicy dependencyPolicy = new JBoss5DependencyPolicy();
93
94    public ClientENCInjectionContainer(DeploymentUnit unit, ApplicationClientDD xml, Class JavaDoc<?> mainClass, String JavaDoc applicationClientName, ClassLoader JavaDoc classLoader, Context JavaDoc encCtx) throws NamingException JavaDoc
95    {
96       if(mainClass == null)
97          throw new NullPointerException JavaDoc("mainClass is mandatory");
98       if(applicationClientName == null)
99          throw new NullPointerException JavaDoc("applicationClientName is mandatory");
100       if(classLoader == null)
101          throw new NullPointerException JavaDoc("classLoader is mandatory");
102       
103       this.xml = xml;
104       this.mainClass = mainClass;
105       this.applicationClientName = applicationClientName;
106       this.classLoader = classLoader;
107       
108       this.enc = encCtx;
109       
110       encEnv = Util.createSubcontext(enc, "env");
111       
112       /*
113       EAR ear = null;
114
115       if (di.parent != null)
116       {
117          if (di.parent.shortName.endsWith(".ear") || di.parent.shortName.endsWith(".ear/"))
118          {
119             synchronized (di.parent.context)
120             {
121                ear = (EAR) di.parent.context.get("EJB3_EAR_METADATA");
122                if (ear == null)
123                {
124                   ear = new JmxEARImpl(di.parent.shortName);
125                   di.parent.context.put("EJB3_EAR_METADATA", ear);
126                }
127             }
128          }
129       }
130       */

131       
132       DeploymentScope scope = null;
133       if (unit.getDeploymentContext().getParent() != null)
134       {
135          scope = new JBoss5DeploymentScope(unit.getDeploymentContext().getParent());
136       }
137       
138       ejbResolver = new ClientEjbResolver(scope, unit.getDeploymentContext().getRoot().getName());
139       
140       String JavaDoc on = Ejb3Module.BASE_EJB3_JMX_NAME + createScopeKernelName(unit, scope) + ",name=" + applicationClientName;
141       try
142       {
143          this.objectName = new ObjectName JavaDoc(on);
144       }
145       catch(MalformedObjectNameException JavaDoc e)
146       {
147          // should not happen
148
throw new RuntimeException JavaDoc(e);
149       }
150       
151       processMetaData();
152    }
153    
154    private String JavaDoc createScopeKernelName(DeploymentUnit unit, DeploymentScope ear)
155    {
156       String JavaDoc scopedKernelName = "";
157       if (ear != null) scopedKernelName += ",ear=" + ear.getShortName();
158       scopedKernelName += ",jar=" + unit.getDeploymentContext().getRoot().getName();
159       return scopedKernelName;
160    }
161    
162    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationType, Class JavaDoc<?> clazz)
163    {
164       return clazz.getAnnotation(annotationType);
165    }
166
167    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationType, Class JavaDoc<?> clazz, Method JavaDoc method)
168    {
169       return method.getAnnotation(annotationType);
170    }
171
172    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationType, Method JavaDoc method)
173    {
174       return method.getAnnotation(annotationType);
175    }
176
177    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationType, Class JavaDoc<?> clazz, Field JavaDoc field)
178    {
179       return field.getAnnotation(annotationType);
180    }
181
182    public <T extends Annotation JavaDoc> T getAnnotation(Class JavaDoc<T> annotationType, Field JavaDoc field)
183    {
184       return field.getAnnotation(annotationType);
185    }
186
187    public ClassLoader JavaDoc getClassloader()
188    {
189       return classLoader;
190    }
191
192    public DependencyPolicy getDependencyPolicy()
193    {
194       return dependencyPolicy;
195    }
196
197    public String JavaDoc getDeploymentDescriptorType()
198    {
199       return "application-client.xml";
200    }
201
202    public String JavaDoc getEjbJndiName(Class JavaDoc businessInterface) throws NameNotFoundException JavaDoc
203    {
204       return ejbResolver.getEjbJndiName(businessInterface);
205    }
206
207    public String JavaDoc getEjbJndiName(String JavaDoc link, Class JavaDoc businessInterface)
208    {
209       return ejbResolver.getEjbJndiName(link, businessInterface);
210    }
211
212    public Context JavaDoc getEnc()
213    {
214       return enc;
215    }
216
217    public Context JavaDoc getEncEnv()
218    {
219       return encEnv;
220    }
221
222    public Map JavaDoc<String JavaDoc, Map JavaDoc<AccessibleObject JavaDoc, Injector>> getEncInjections()
223    {
224       return encInjections;
225    }
226
227    public Map JavaDoc<String JavaDoc, EncInjector> getEncInjectors()
228    {
229       return encInjectors;
230    }
231
232    public EnvironmentRefGroup getEnvironmentRefGroup()
233    {
234       return xml;
235    }
236
237    public String JavaDoc getIdentifier()
238    {
239       return applicationClientName;
240    }
241
242    /**
243     * A client enc injection container doesn't support injectors, because
244     * these must be run client side.
245     */

246    public List JavaDoc<Injector> getInjectors()
247    {
248       //throw new RuntimeException("not supported");
249
return injectors;
250    }
251
252    public Class JavaDoc<?> getMainClass()
253    {
254       return mainClass;
255    }
256    
257    public ObjectName JavaDoc getObjectName()
258    {
259       return objectName;
260    }
261    
262    public PersistenceUnitDeployment getPersistenceUnitDeployment(String JavaDoc unitName) throws NameNotFoundException JavaDoc
263    {
264       throw new RuntimeException JavaDoc("NYI");
265    }
266
267    private void populateEnc()
268    {
269       for (EncInjector injector : encInjectors.values())
270       {
271          log.trace("encInjector: " + injector);
272          injector.inject(this);
273       }
274    }
275    
276    private void processMetaData()
277    {
278       for(String JavaDoc dependency : xml.getDependencies())
279       {
280          getDependencyPolicy().addDependency(dependency);
281       }
282       
283       // TODO: check which handlers an application client should support
284
Collection JavaDoc<InjectionHandler> handlers = new ArrayList JavaDoc<InjectionHandler>();
285       handlers.add(new EJBHandler());
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       ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
294       Thread.currentThread().setContextClassLoader(classLoader);
295       try
296       {
297          // EJB container's XML must be processed before interceptor's as it may override interceptor's references
298
for (InjectionHandler handler : handlers) handler.loadXml(xml, this);
299
300          Map JavaDoc<AccessibleObject JavaDoc, Injector> tmp = InjectionUtil.processAnnotations(this, handlers, getMainClass());
301          injectors.addAll(tmp.values());
302
303 // initialiseInterceptors();
304
// for (InterceptorInfo interceptorInfo : applicableInterceptors)
305
// {
306
// for (InjectionHandler handler : handlers)
307
// {
308
// handler.loadXml(interceptorInfo.getXml(), this);
309
// }
310
// }
311
// for (InterceptorInfo interceptorInfo : applicableInterceptors)
312
// {
313
// Map<AccessibleObject, Injector> tmpInterceptor = InjectionUtil.processAnnotations(this, handlers, interceptorInfo.getClazz());
314
// InterceptorInjector injector = new InterceptorInjector(this, interceptorInfo, tmpInterceptor);
315
// interceptorInjectors.put(interceptorInfo.getClazz(), injector);
316
// }
317
}
318       finally
319       {
320          Thread.currentThread().setContextClassLoader(old);
321       }
322    }
323    
324    public Container resolveEjbContainer(String JavaDoc link, Class JavaDoc businessIntf)
325    {
326       return ejbResolver.getEjbContainer(link, businessIntf);
327    }
328
329    public Container resolveEjbContainer(Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
330    {
331       return ejbResolver.getEjbContainer(businessIntf);
332    }
333
334    public void start()
335    {
336       log.trace("start");
337       
338       populateEnc();
339       
340       // Don't run any injectors, they must be run client side
341
}
342    
343    public void stop()
344    {
345       log.trace("stop");
346    }
347 }
348
Popular Tags