KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > Ejb3AnnotationHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., 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;
23
24 import javassist.bytecode.AnnotationsAttribute;
25 import javassist.bytecode.ClassFile;
26 import javassist.bytecode.annotation.Annotation;
27 import javassist.bytecode.annotation.StringMemberValue;
28 import org.jboss.annotation.ejb.ResourceAdapter;
29 import org.jboss.aop.AspectManager;
30 import org.jboss.aop.DomainDefinition;
31 import org.jboss.ejb3.metamodel.EnterpriseBean;
32 import org.jboss.ejb3.metamodel.MessageDrivenBean;
33 import org.jboss.ejb3.mdb.ConsumerContainer;
34 import org.jboss.ejb3.mdb.MDB;
35 import org.jboss.ejb3.mdb.inflow.JBossMessageEndpointFactory;
36 import org.jboss.ejb3.service.ServiceContainer;
37 import org.jboss.ejb3.stateful.StatefulContainer;
38 import org.jboss.ejb3.stateless.StatelessContainer;
39 import org.jboss.logging.Logger;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
47  * @author <a HREF="mailto:bill@jboss.com">Bill Burke</a>
48  * @version <tt>$Revision: 56592 $</tt>
49  */

50 public class Ejb3AnnotationHandler implements Ejb3Handler
51 {
52    private static final Logger log = Logger.getLogger(Ejb3AnnotationHandler.class);
53
54    protected static enum EJB_TYPE
55    {
56       STATELESS, STATEFUL, MESSAGE_DRIVEN, ENTITY, SERVICE, CONSUMER
57    }
58
59    protected DeploymentUnit di;
60
61    protected ClassFile cf;
62    protected List JavaDoc<String JavaDoc> ejbNames = new ArrayList JavaDoc<String JavaDoc>();
63    protected Class JavaDoc ejbClass;
64    protected String JavaDoc className;
65    protected EJB_TYPE ejbType;
66    protected Annotation annotation;
67    protected AnnotationsAttribute visible;
68    protected Hashtable JavaDoc ctxProperties;
69    protected String JavaDoc defaultSLSBDomain;
70    protected String JavaDoc defaultSFSBDomain;
71    protected String JavaDoc defaultMDBDomain;
72    protected String JavaDoc defaultServiceDomain;
73    protected String JavaDoc defaultConsumerDomain;
74    protected Ejb3Deployment deployment;
75
76    public Ejb3AnnotationHandler(Ejb3Deployment deployment, ClassFile cf)
77    {
78       this.deployment = deployment;
79       this.di = deployment.getDeploymentUnit();
80       this.cf = cf;
81       className = cf.getName();
82       visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
83       defaultSLSBDomain = deployment.getDefaultSLSBDomain();
84       defaultSFSBDomain = deployment.getDefaultSFSBDomain();
85       defaultMDBDomain = deployment.getDefaultMDBDomain();
86       defaultServiceDomain = deployment.getDefaultServiceDomain();
87       defaultConsumerDomain = deployment.getDefaultConsumerDomain();
88    }
89
90    public void setCtxProperties(Hashtable JavaDoc ctxProperties)
91    {
92       this.ctxProperties = ctxProperties;
93    }
94
95    private String JavaDoc getJaccContextId()
96    {
97       return di.getShortName();
98    }
99
100    public boolean isEjb()
101    {
102       if (visible == null) return false;
103
104       if (EJB3Util.isStateless(visible)) return true;
105       if (EJB3Util.isMessageDriven(visible)) return true;
106       if (EJB3Util.isStatefulSession(visible)) return true;
107       return false;
108    }
109
110    public boolean isJBossBeanType()
111    {
112       if (visible == null) return false;
113
114       if (EJB3Util.isService(visible)) return true;
115       if (EJB3Util.isConsumer(visible)) return true;
116       return false;
117    }
118
119    public List JavaDoc getContainers(ClassFile cf, Ejb3Deployment deployment) throws Exception JavaDoc
120    {
121       List JavaDoc containers = new ArrayList JavaDoc();
122
123       populateBaseInfo();
124
125       for (int ejbIndex = 0; ejbIndex < ejbNames.size(); ++ejbIndex)
126       {
127          String JavaDoc ejbName = ejbNames.get(ejbIndex);
128          if (ejbType == EJB_TYPE.STATELESS)
129          {
130             StatelessContainer container = getStatelessContainer(ejbIndex);
131             container.setJaccContextId(getJaccContextId());
132             containers.add(container);
133          }
134          else if (ejbType == EJB_TYPE.STATEFUL)
135          {
136             StatefulContainer container = getStatefulContainer(ejbIndex);
137             container.setJaccContextId(getJaccContextId());
138             containers.add(container);
139          }
140          else if (ejbType == EJB_TYPE.MESSAGE_DRIVEN)
141          {
142             MDB container = getMDB(ejbIndex);
143             container.setJaccContextId(getJaccContextId());
144             containers.add(container);
145          }
146          else if (ejbType == EJB_TYPE.SERVICE)
147          {
148             ServiceContainer container = getServiceContainer(ejbIndex);
149             container.setJaccContextId(getJaccContextId());
150             containers.add(container);
151          }
152          else if (ejbType == EJB_TYPE.CONSUMER)
153          {
154             ConsumerContainer container = getConsumerContainer(ejbIndex);
155             container.setJaccContextId(getJaccContextId());
156             containers.add(container);
157          }
158          log.debug("found EJB3: ejbName=" + ejbName + ", class=" + className + ", type=" + ejbType);
159       }
160
161       return containers;
162    }
163    
164    protected String JavaDoc getAspectDomain(int ejbIndex, String JavaDoc defaultDomain)
165    {
166       return EJB3Util.getAspectDomain(visible, defaultDomain);
167    }
168
169    protected ServiceContainer getServiceContainer(int ejbIndex) throws Exception JavaDoc
170    {
171       String JavaDoc containerName = getAspectDomain(ejbIndex, defaultServiceDomain);
172       DomainDefinition domain = AspectManager.instance().getContainer(containerName);
173
174       if (domain == null)
175          throw new RuntimeException JavaDoc("No container configured with name '"
176                  + containerName + "''");
177
178       return new ServiceContainer(deployment.getMbeanServer(), di.getClassLoader(), cf.getName(),
179               ejbNames.get(ejbIndex), (AspectManager) domain.getManager(), ctxProperties,
180               di.getInterceptorInfoRepository(), deployment);
181
182    }
183
184    protected ConsumerContainer getConsumerContainer(int ejbIndex) throws Exception JavaDoc
185    {
186       String JavaDoc containerName = getAspectDomain(ejbIndex, defaultConsumerDomain);
187       DomainDefinition domain = AspectManager.instance().getContainer(containerName);
188
189       if (domain == null)
190          throw new RuntimeException JavaDoc("No container configured with name '"
191                  + containerName + "''");
192
193       return new ConsumerContainer(ejbNames.get(ejbIndex), (AspectManager) domain.getManager(),
194               di.getClassLoader(), cf.getName(), ctxProperties,
195               di.getInterceptorInfoRepository(), deployment);
196
197    }
198
199    protected StatefulContainer getStatefulContainer(int ejbIndex) throws Exception JavaDoc
200    {
201       String JavaDoc containerName = getAspectDomain(ejbIndex, defaultSFSBDomain);
202       DomainDefinition domain = AspectManager.instance().getContainer(containerName);
203
204       if (domain == null)
205          throw new RuntimeException JavaDoc("No container configured with name '"
206                  + containerName + "''");
207
208       return new StatefulContainer(di.getClassLoader(), cf.getName(),
209               ejbNames.get(ejbIndex), (AspectManager) domain.getManager(), ctxProperties,
210               di.getInterceptorInfoRepository(), deployment);
211
212    }
213
214    protected StatelessContainer getStatelessContainer(int ejbIndex) throws Exception JavaDoc
215    {
216       String JavaDoc containerName = getAspectDomain(ejbIndex, defaultSLSBDomain);
217       
218       DomainDefinition domain = AspectManager.instance().getContainer(containerName);
219
220       if (domain == null)
221          throw new RuntimeException JavaDoc("No container configured with name '"
222                  + containerName + "''");
223
224       return new StatelessContainer(di.getClassLoader(), cf.getName(),
225               ejbNames.get(ejbIndex), (AspectManager) domain.getManager(),
226               ctxProperties, di.getInterceptorInfoRepository(),
227               deployment);
228    }
229
230    protected String JavaDoc getMDBDomainName(int ejbIndex)
231    {
232       return defaultMDBDomain;
233    }
234
235    protected void createProxyFactories()
236    {
237
238    }
239
240    protected MDB getMDB(int ejbIndex) throws Exception JavaDoc
241    {
242       return getMDB(ejbIndex, null);
243    }
244
245    protected MDB getMDB(int ejbIndex, EnterpriseBean xml) throws Exception JavaDoc
246    {
247       String JavaDoc domainName = getMDBDomainName(ejbIndex);
248       
249       String JavaDoc containerName = getAspectDomain(ejbIndex, domainName);
250       DomainDefinition domain = AspectManager.instance().getContainer(containerName);
251
252       if (domain == null)
253          throw new RuntimeException JavaDoc("No container configured with name '"
254                  + containerName + "''");
255
256       MDB container = new MDB(ejbNames.get(ejbIndex), (AspectManager) domain.getManager(), di.getClassLoader(), cf.getName(),
257               ctxProperties, di.getInterceptorInfoRepository(), deployment);
258
259       return container;
260    }
261
262    protected void populateBaseInfo() throws Exception JavaDoc
263    {
264       String JavaDoc ejbName = null;
265       ejbClass = di.getClassLoader().loadClass(className);
266
267       visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
268
269       if (visible != null)
270       {
271          annotation = visible.getAnnotation(javax.ejb.Stateless JavaDoc.class.getName());
272          if (annotation != null)
273          {
274             ejbType = EJB_TYPE.STATELESS;
275          }
276          else
277          {
278             annotation = visible.getAnnotation(javax.ejb.Stateful JavaDoc.class.getName());
279             if (annotation != null)
280             {
281                ejbType = EJB_TYPE.STATEFUL;
282             }
283             else
284             {
285                annotation = visible.getAnnotation(javax.persistence.Entity.class.getName());
286                if (annotation != null)
287                {
288                   ejbType = EJB_TYPE.ENTITY;
289                }
290                else
291                {
292                   annotation = visible.getAnnotation(javax.ejb.MessageDriven JavaDoc.class.getName());
293                   if (annotation != null)
294                   {
295                      ejbType = EJB_TYPE.MESSAGE_DRIVEN;
296                   }
297                   else
298                   {
299                      annotation = visible.getAnnotation(org.jboss.annotation.ejb.Service.class.getName());
300                      if (annotation != null)
301                      {
302                         ejbType = EJB_TYPE.SERVICE;
303                      }
304                      else
305                      {
306                         annotation = visible.getAnnotation(org.jboss.annotation.ejb.Consumer.class.getName());
307                         if (annotation != null)
308                         {
309                            ejbType = EJB_TYPE.CONSUMER;
310                         }
311                      }
312                   }
313                }
314             }
315          }
316
317          if (annotation != null)
318          {
319             StringMemberValue mv = (StringMemberValue) annotation.getMemberValue("name");
320             if (mv != null)
321                ejbName = mv.getValue();
322             else
323                ejbName = ejbClass.getSimpleName();
324          }
325       }
326
327       if (ejbName != null)
328       {
329          ejbNames.add(ejbName);
330       }
331    }
332 }
333
Popular Tags