KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Field JavaDoc;
25
26 import javassist.bytecode.ClassFile;
27 import org.jboss.annotation.IgnoreDependency;
28 import org.jboss.annotation.IgnoreDependencyImpl;
29 import org.jboss.annotation.ejb.Clustered;
30 import org.jboss.annotation.ejb.ClusteredImpl;
31 import org.jboss.annotation.ejb.Consumer;
32 import org.jboss.annotation.ejb.ConsumerImpl;
33 import org.jboss.annotation.ejb.CurrentMessage;
34 import org.jboss.annotation.ejb.CurrentMessageImpl;
35 import org.jboss.annotation.ejb.DefaultActivationSpecs;
36 import org.jboss.annotation.ejb.DefaultActivationSpecsImpl;
37 import org.jboss.annotation.ejb.DeliveryMode;
38 import org.jboss.annotation.ejb.Depends;
39 import org.jboss.annotation.ejb.DependsImpl;
40 import org.jboss.annotation.ejb.ExcludeClassInterceptorsImpl;
41 import org.jboss.annotation.ejb.ExcludeDefaultInterceptorsImpl;
42 import org.jboss.annotation.ejb.LocalBinding;
43 import org.jboss.annotation.ejb.LocalBindingImpl;
44 import org.jboss.annotation.ejb.LocalHomeImpl;
45 import org.jboss.annotation.ejb.Management;
46 import org.jboss.annotation.ejb.ManagementImpl;
47 import org.jboss.annotation.ejb.MessageProperties;
48 import org.jboss.annotation.ejb.MessagePropertiesImpl;
49 import org.jboss.annotation.ejb.PoolClass;
50 import org.jboss.annotation.ejb.PoolClassImpl;
51 import org.jboss.annotation.ejb.Producers;
52 import org.jboss.annotation.ejb.RemoteBinding;
53 import org.jboss.annotation.ejb.RemoteBindingImpl;
54 import org.jboss.annotation.ejb.RemoteBindings;
55 import org.jboss.annotation.ejb.RemoteBindingsImpl;
56 import org.jboss.annotation.ejb.RemoteHomeImpl;
57 import org.jboss.annotation.ejb.ResourceAdapter;
58 import org.jboss.annotation.ejb.ResourceAdapterImpl;
59 import org.jboss.annotation.ejb.SerializedConcurrentAccess;
60 import org.jboss.annotation.ejb.SerializedConcurrentAccessImpl;
61 import org.jboss.annotation.ejb.Service;
62 import org.jboss.annotation.ejb.ServiceImpl;
63 import org.jboss.annotation.ejb.TransactionTimeout;
64 import org.jboss.annotation.ejb.TransactionTimeoutImpl;
65 import org.jboss.annotation.ejb.cache.Cache;
66 import org.jboss.annotation.ejb.cache.CacheImpl;
67 import org.jboss.annotation.ejb.cache.simple.PersistenceManager;
68 import org.jboss.annotation.ejb.cache.simple.PersistenceManagerImpl;
69 import org.jboss.annotation.internal.DefaultInterceptorMarker;
70 import org.jboss.annotation.internal.DefaultInterceptorMarkerImpl;
71 import org.jboss.annotation.security.RunAsPrincipalImpl;
72 import org.jboss.annotation.security.SecurityDomain;
73 import org.jboss.annotation.security.SecurityDomainImpl;
74 import org.jboss.aop.annotation.AnnotationRepository;
75 import org.jboss.ejb.ActivationConfigPropertyImpl;
76 import org.jboss.ejb.AroundInvokeImpl;
77 import org.jboss.ejb.DeclareRolesImpl;
78 import org.jboss.ejb.DenyAllImpl;
79 import org.jboss.ejb.InitImpl;
80 import org.jboss.ejb.InterceptorsImpl;
81 import org.jboss.ejb.LocalImpl;
82 import org.jboss.ejb.MessageDrivenImpl;
83 import org.jboss.ejb.PermitAllImpl;
84 import org.jboss.ejb.PostActivateImpl;
85 import org.jboss.ejb.PostConstructImpl;
86 import org.jboss.ejb.PreDestroyImpl;
87 import org.jboss.ejb.PrePassivateImpl;
88 import org.jboss.ejb.RemoteImpl;
89 import org.jboss.ejb.RemoveImpl;
90 import org.jboss.ejb.RolesAllowedImpl;
91 import org.jboss.ejb.RunAsImpl;
92 import org.jboss.ejb.StatelessImpl;
93 import org.jboss.ejb.TransactionAttributeImpl;
94 import org.jboss.ejb.TransactionManagementImpl;
95 import org.jboss.ejb3.interceptor.InterceptorInfoRepository;
96 import org.jboss.ejb3.mdb.ConsumerContainer;
97 import org.jboss.ejb3.mdb.MDB;
98 import org.jboss.ejb3.mdb.ProducerImpl;
99 import org.jboss.ejb3.mdb.ProducersImpl;
100 import org.jboss.ejb3.metamodel.ActivationConfig;
101 import org.jboss.ejb3.metamodel.AssemblyDescriptor;
102 import org.jboss.ejb3.metamodel.CacheConfig;
103 import org.jboss.ejb3.metamodel.ClusterConfig;
104 import org.jboss.ejb3.metamodel.ContainerTransaction;
105 import org.jboss.ejb3.metamodel.EjbJarDD;
106 import org.jboss.ejb3.metamodel.EnterpriseBean;
107 import org.jboss.ejb3.metamodel.EnterpriseBeans;
108 import org.jboss.ejb3.metamodel.ExcludeList;
109 import org.jboss.ejb3.metamodel.InitMethod;
110 import org.jboss.ejb3.metamodel.InterceptorBinding;
111 import org.jboss.ejb3.metamodel.MessageDestination;
112 import org.jboss.ejb3.metamodel.MessageDrivenBean;
113 import org.jboss.ejb3.metamodel.MessageDrivenDestination;
114 import org.jboss.ejb3.metamodel.Method;
115 import org.jboss.ejb3.metamodel.MethodAttributes;
116 import org.jboss.ejb3.metamodel.MethodPermission;
117 import org.jboss.ejb3.metamodel.PoolConfig;
118 import org.jboss.ejb3.metamodel.RemoveMethod;
119 import org.jboss.ejb3.metamodel.SecurityIdentity;
120 import org.jboss.ejb3.metamodel.SessionEnterpriseBean;
121 import org.jboss.ejb3.metamodel.XmlAnnotation;
122 import org.jboss.ejb3.service.ServiceContainer;
123 import org.jboss.ejb3.stateful.StatefulContainer;
124 import org.jboss.ejb3.stateless.StatelessContainer;
125 import org.jboss.logging.Logger;
126 import org.jboss.metamodel.descriptor.InjectionTarget;
127 import org.jboss.metamodel.descriptor.MessageDestinationRef;
128 import org.jboss.metamodel.descriptor.NameValuePair;
129 import org.jboss.metamodel.descriptor.ResourceRef;
130 import org.jboss.metamodel.descriptor.RunAs;
131 import org.jboss.metamodel.descriptor.SecurityRole;
132
133 import javax.annotation.PostConstruct;
134 import javax.annotation.PreDestroy;
135 import javax.annotation.security.DeclareRoles;
136 import javax.annotation.security.DenyAll;
137 import javax.annotation.security.PermitAll;
138 import javax.annotation.security.RolesAllowed;
139 import javax.ejb.Init JavaDoc;
140 import javax.ejb.Local JavaDoc;
141 import javax.ejb.LocalHome JavaDoc;
142 import javax.ejb.MessageDriven JavaDoc;
143 import javax.ejb.PostActivate JavaDoc;
144 import javax.ejb.PrePassivate JavaDoc;
145 import javax.ejb.Remote JavaDoc;
146 import javax.ejb.RemoteHome JavaDoc;
147 import javax.ejb.Remove JavaDoc;
148 import javax.ejb.Stateless JavaDoc;
149 import javax.ejb.TransactionAttribute JavaDoc;
150 import javax.ejb.TransactionAttributeType JavaDoc;
151 import javax.ejb.TransactionManagement JavaDoc;
152 import javax.interceptor.AroundInvoke;
153 import javax.interceptor.ExcludeClassInterceptors;
154 import javax.interceptor.ExcludeDefaultInterceptors;
155 import javax.interceptor.Interceptors;
156 import java.lang.reflect.Member JavaDoc;
157 import java.util.ArrayList JavaDoc;
158 import java.util.Collection JavaDoc;
159 import java.util.Iterator JavaDoc;
160 import java.util.List JavaDoc;
161 import java.util.StringTokenizer JavaDoc;
162
163 /**
164  * @version <tt>$Revision: 58478 $</tt>
165  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
166  * @author <a HREF="mailto:bill@jboss.com">Bill Burke</a>
167  */

168 public class Ejb3DescriptorHandler extends Ejb3AnnotationHandler
169 {
170    private static final Logger log = Logger
171          .getLogger(Ejb3DescriptorHandler.class);
172
173    protected EjbJarDD dd;
174
175    protected List<EnterpriseBean> ejbs = new ArrayList JavaDoc<EnterpriseBean>();
176
177    private static Class JavaDoc clazz;
178
179    public Ejb3DescriptorHandler(Ejb3Deployment deployment, ClassFile cf,
180          EjbJarDD dd)
181    {
182       super(deployment, cf);
183       this.dd = dd;
184    }
185
186    public boolean isEjb()
187    {
188       if (super.isEjb())
189          return true;
190       EnterpriseBeans enterpriseBeans = dd.getEnterpriseBeans();
191
192       if (enterpriseBeans == null)
193       {
194          return false;
195       }
196       return enterpriseBeans.findEjbsByClass(cf.getName()).size() > 0;
197
198    }
199
200    protected void populateBaseInfo() throws Exception JavaDoc
201    {
202       super.populateBaseInfo();
203
204       EnterpriseBeans enterpriseBeans = (dd.getEnterpriseBeans() != null) ? dd
205             .getEnterpriseBeans() : new EnterpriseBeans();
206
207       List<EnterpriseBean> ejbsByClass = enterpriseBeans.findEjbsByClass(cf
208             .getName());
209
210       for (int i = 0; i < ejbNames.size(); ++i)
211       {
212          String JavaDoc ejbNameFromAnnotation = ejbNames.get(i);
213          EnterpriseBean enterpriseBean = enterpriseBeans
214                .findEjbByEjbName(ejbNameFromAnnotation);
215          ejbs.add(enterpriseBean);
216
217          boolean removed = false;
218          int j = 0;
219          while (!removed && j < ejbsByClass.size())
220          {
221             EnterpriseBean ejbByClass = ejbsByClass.get(j);
222             if (ejbByClass.getEjbName().equals(ejbNameFromAnnotation))
223             {
224                ejbsByClass.remove(j);
225             } else
226                ++j;
227          }
228       }
229
230       for (EnterpriseBean enterpriseBean : ejbsByClass)
231       {
232          String JavaDoc ejbName = enterpriseBean.getEjbName();
233
234          ejbs.add(enterpriseBean);
235          ejbNames.add(ejbName);
236
237          if (enterpriseBean.isSessionBean())
238          {
239             if (((SessionEnterpriseBean) enterpriseBean).isStateless())
240                ejbType = EJB_TYPE.STATELESS;
241             else
242                ejbType = EJB_TYPE.STATEFUL;
243          } else if (enterpriseBean.isEntityBean())
244             ejbType = EJB_TYPE.ENTITY;
245          else if (enterpriseBean.isMessageDrivenBean())
246             ejbType = EJB_TYPE.MESSAGE_DRIVEN;
247          else if (enterpriseBean.isService())
248             ejbType = EJB_TYPE.SERVICE;
249          else if (enterpriseBean.isConsumer())
250             ejbType = EJB_TYPE.CONSUMER;
251       }
252    }
253
254    protected StatefulContainer getStatefulContainer(int ejbIndex)
255          throws Exception JavaDoc
256    {
257       String JavaDoc ejbName = ejbNames.get(ejbIndex);
258
259       EnterpriseBean enterpriseBean = ejbs.get(ejbIndex);
260
261       StatefulContainer container = super.getStatefulContainer(ejbIndex);
262
263       container.setAssemblyDescriptor(dd.getAssemblyDescriptor());
264
265       addInterfaces(container, enterpriseBean);
266
267       addDescriptorAnnotations(container, enterpriseBean, ejbName);
268
269       return container;
270    }
271
272    private void addHomeAnnotations(EJBContainer container,
273          EnterpriseBean enterpriseBean) throws Exception JavaDoc
274    {
275       if (enterpriseBean.getHome() != null)
276       {
277          RemoteHome JavaDoc annotation = new RemoteHomeImpl(di.getClassLoader()
278                .loadClass(enterpriseBean.getHome()));
279          addClassAnnotation(container, annotation.annotationType(), annotation);
280       }
281
282       if (enterpriseBean.getLocalHome() != null)
283       {
284          LocalHome JavaDoc annotation = new LocalHomeImpl(di.getClassLoader()
285                .loadClass(enterpriseBean.getLocalHome()));
286          addClassAnnotation(container, annotation.annotationType(), annotation);
287       }
288    }
289
290    protected StatelessContainer getStatelessContainer(int ejbIndex)
291          throws Exception JavaDoc
292    {
293       String JavaDoc ejbName = ejbNames.get(ejbIndex);
294
295       EnterpriseBean enterpriseBean = ejbs.get(ejbIndex);
296
297       StatelessContainer container = super.getStatelessContainer(ejbIndex);
298
299       container.setAssemblyDescriptor(dd.getAssemblyDescriptor());
300
301       StatelessImpl annotation = new StatelessImpl(ejbName);
302       if (enterpriseBean != null && !isAnnotatedBean())
303       {
304          addClassAnnotation(container, Stateless JavaDoc.class, annotation);
305       }
306
307       addInterfaces(container, enterpriseBean);
308
309       addDescriptorAnnotations(container, enterpriseBean, ejbName);
310
311       return container;
312    }
313
314    protected ServiceContainer getServiceContainer(int ejbIndex)
315          throws Exception JavaDoc
316    {
317       String JavaDoc ejbName = ejbNames.get(ejbIndex);
318
319       org.jboss.ejb3.metamodel.Service service = (org.jboss.ejb3.metamodel.Service) ejbs
320             .get(ejbIndex);
321
322       ServiceContainer container = super.getServiceContainer(ejbIndex);
323       ServiceImpl annotation = new ServiceImpl((Service) container
324             .resolveAnnotation(Service.class));
325
326       container.setAssemblyDescriptor(dd.getAssemblyDescriptor());
327
328       if (service != null && !isAnnotatedBean())
329       {
330          if (service.getObjectName() != null)
331             annotation.setObjectName(service.getObjectName());
332          if (service.getEjbName() != null)
333             annotation.setName(service.getEjbName());
334          addClassAnnotation(container, Service.class, annotation);
335       }
336
337       addInterfaces(container, service);
338
339       addDescriptorAnnotations(container, service, ejbName);
340
341       addServiceAnnotations(container, service);
342
343       return container;
344    }
345
346    protected ConsumerContainer getConsumerContainer(int ejbIndex)
347          throws Exception JavaDoc
348    {
349       String JavaDoc ejbName = ejbNames.get(ejbIndex);
350
351       org.jboss.ejb3.metamodel.Consumer consumer = (org.jboss.ejb3.metamodel.Consumer) ejbs
352             .get(ejbIndex);
353
354       ConsumerContainer container = super.getConsumerContainer(ejbIndex);
355       ConsumerImpl annotation = new ConsumerImpl((Consumer) container
356             .resolveAnnotation(Consumer.class));
357
358       container.setAssemblyDescriptor(dd.getAssemblyDescriptor());
359
360       if (consumer != null && !isAnnotatedBean())
361       {
362          if (consumer.getDestination() != null)
363          {
364             ActivationConfigPropertyImpl property = new ActivationConfigPropertyImpl(
365                   "destination", consumer.getDestination());
366             annotation.addActivationConfig(property);
367          }
368
369          if (consumer.getDestinationType() != null)
370          {
371             ActivationConfigPropertyImpl property = new ActivationConfigPropertyImpl(
372                   "destinationType", consumer.getDestinationType());
373             annotation.addActivationConfig(property);
374          }
375
376          addClassAnnotation(container, Consumer.class, annotation);
377       }
378
379       addInterfaces(container, consumer);
380
381       addDescriptorAnnotations(container, consumer, ejbName);
382
383       addConsumerAnnotations(container, consumer);
384
385       return container;
386    }
387
388    protected String JavaDoc getMDBDomainName(int ejbIndex)
389    {
390       return defaultMDBDomain;
391    }
392
393    protected MDB getMDB(int ejbIndex) throws Exception JavaDoc
394    {
395       String JavaDoc ejbName = ejbNames.get(ejbIndex);
396
397       EnterpriseBean enterpriseBean = ejbs.get(ejbIndex);
398
399       MDB container = super.getMDB(ejbIndex);
400
401       container.setAssemblyDescriptor(dd.getAssemblyDescriptor());
402
403       addMDBAnnotations(container, ejbName, enterpriseBean);
404
405       addInterfaces(container, enterpriseBean);
406
407       addDescriptorAnnotations(container, enterpriseBean, ejbName);
408
409       return container;
410    }
411
412    protected String JavaDoc getAspectDomain(int ejbIndex, String JavaDoc defaultDomain)
413    {
414       EnterpriseBean enterpriseBean = ejbs.get(ejbIndex);
415       if (enterpriseBean != null)
416       {
417          String JavaDoc aopDomainName = enterpriseBean.getAopDomainName();
418          if (aopDomainName != null)
419          {
420             log.debug("Found aop-domain-name element for annotation "
421                   + aopDomainName + " for ejbName "
422                   + enterpriseBean.getEjbName());
423
424             return aopDomainName;
425          }
426       }
427       return super.getAspectDomain(ejbIndex, defaultDomain);
428    }
429
430    protected boolean isAnnotatedBean()
431    {
432       return super.isEjb() || super.isJBossBeanType();
433    }
434
435    private void addMDBAnnotations(MDB container, String JavaDoc ejbName,
436          EnterpriseBean enterpriseBean)
437    {
438       if (enterpriseBean != null)
439       {
440          MessageDrivenBean mdb = (MessageDrivenBean) enterpriseBean;
441
442          ArrayList JavaDoc properties = new ArrayList JavaDoc();
443          if (mdb.getAcknowledgeMode() != null)
444             properties.add(new ActivationConfigPropertyImpl("acknowledgeMode",
445                   mdb.getAcknowledgeMode()));
446
447          if (mdb.getMessageDrivenDestination() != null)
448          {
449             MessageDrivenDestination destination = mdb
450                   .getMessageDrivenDestination();
451             if (destination.getDestinationType() != null)
452                properties.add(new ActivationConfigPropertyImpl(
453                      "destinationType", destination.getDestinationType()));
454             if (destination.getSubscriptionDurability() != null)
455             {
456                String JavaDoc durable = "false";
457                if (destination.getSubscriptionDurability().equals("Durable"))
458                   durable = "true";
459                properties.add(new ActivationConfigPropertyImpl("subscriptionDurability",
460                      durable));
461                if (destination.getSubscriptionDurability().equals("Durable"))
462                   properties.add(new ActivationConfigPropertyImpl(
463                         "subscriptionName", "subscriptionName"));
464
465             }
466          }
467
468          if (mdb.getResourceAdaptorName() != null)
469          {
470             ResourceAdapter adapter = new ResourceAdapterImpl(mdb
471                   .getResourceAdaptorName());
472             addClassAnnotation(container, ResourceAdapter.class, adapter);
473          }
474
475          ActivationConfig activationConfig = mdb.getActivationConfig();
476          if (activationConfig != null)
477          {
478             for (Object JavaDoc o : activationConfig.getActivationConfigProperties())
479             {
480                NameValuePair property = (NameValuePair) o;
481                properties.add(new ActivationConfigPropertyImpl(property
482                      .getName(), property.getValue()));
483             }
484          }
485
486          if (mdb.getDestinationJndiName() != null)
487          {
488             properties.add(new ActivationConfigPropertyImpl("destination", mdb
489                   .getDestinationJndiName()));
490          }
491
492          if (mdb.getMdbUser() != null)
493          {
494             properties.add(new ActivationConfigPropertyImpl("user", mdb
495                   .getMdbUser()));
496          }
497
498          if (mdb.getMdbPassword() != null)
499          {
500             properties.add(new ActivationConfigPropertyImpl("password", mdb
501                   .getMdbPassword()));
502
503          }
504          
505          if (mdb.getMdbSubscriptionId() != null)
506          {
507             properties.add(new ActivationConfigPropertyImpl("subscriptionName", mdb
508                   .getMdbSubscriptionId()));
509
510          }
511
512          ActivationConfigPropertyImpl[] propsArray = new ActivationConfigPropertyImpl[properties
513                .size()];
514          properties.toArray(propsArray);
515          MessageDrivenImpl annotation = new MessageDrivenImpl(ejbName,
516                propsArray);
517          if (mdb.getMessagingType() != null)
518          {
519             try
520             {
521                annotation.setMessageListenerInterface(container
522                      .getClassloader().loadClass(mdb.getMessagingType()));
523             } catch (ClassNotFoundException JavaDoc e)
524             {
525                throw new RuntimeException JavaDoc(e);
526             }
527          }
528
529          if (isAnnotatedBean())
530          {
531             annotation.merge((MessageDriven JavaDoc) ejbClass
532                   .getAnnotation(MessageDriven JavaDoc.class));
533          }
534
535          addClassAnnotation(container, MessageDriven JavaDoc.class, annotation);
536
537          addDefaultActivationConfig(container, mdb);
538       }
539    }
540
541    private void addDefaultActivationConfig(MDB container, MessageDrivenBean mdb)
542    {
543       ActivationConfig defaultActivationConfig = mdb.getDefaultActivationConfig();
544       if (defaultActivationConfig != null)
545       {
546          DefaultActivationSpecsImpl activationAnnotation = new DefaultActivationSpecsImpl();
547          for (Object JavaDoc o : defaultActivationConfig.getActivationConfigProperties())
548          {
549             NameValuePair property = (NameValuePair) o;
550             activationAnnotation.addActivationConfigProperty(new ActivationConfigPropertyImpl(property
551                   .getName(), property.getValue()));
552          }
553
554          DefaultActivationSpecs existingAnnotation = (DefaultActivationSpecs)ejbClass.getAnnotation(DefaultActivationSpecs.class);
555          if (existingAnnotation != null)
556             activationAnnotation.merge(existingAnnotation);
557
558          addClassAnnotation(container, DefaultActivationSpecs.class, activationAnnotation);
559       }
560    }
561
562    private void addInterfaces(EJBContainer container,
563          EnterpriseBean enterpriseBean) throws ClassNotFoundException JavaDoc
564    {
565       if (enterpriseBean != null)
566       {
567          String JavaDoc local = enterpriseBean.getLocal();
568          String JavaDoc remote = enterpriseBean.getRemote();
569
570          if (remote != null)
571          {
572             StringTokenizer JavaDoc classes = new StringTokenizer JavaDoc(remote, ",");
573             ArrayList JavaDoc<Class JavaDoc> remoteClasses = new ArrayList JavaDoc<Class JavaDoc>();
574             while (classes.hasMoreTokens())
575             {
576                String JavaDoc token = classes.nextToken();
577                String JavaDoc classname = token.trim();
578                remoteClasses.add(di.getClassLoader().loadClass(classname));
579
580             }
581             Class JavaDoc[] intfs = new Class JavaDoc[remoteClasses.size()];
582             intfs = remoteClasses.toArray(intfs);
583             addClassAnnotation(container, Remote JavaDoc.class, new RemoteImpl(intfs));
584          }
585
586          if (local != null)
587          {
588             StringTokenizer JavaDoc classes = new StringTokenizer JavaDoc(local, ",");
589             ArrayList JavaDoc<Class JavaDoc> localClasses = new ArrayList JavaDoc<Class JavaDoc>();
590             while (classes.hasMoreTokens())
591             {
592                String JavaDoc token = classes.nextToken();
593                String JavaDoc classname = token.trim();
594                localClasses.add(di.getClassLoader().loadClass(classname));
595
596             }
597             Class JavaDoc[] intfs = new Class JavaDoc[localClasses.size()];
598             intfs = localClasses.toArray(intfs);
599             addClassAnnotation(container, Local JavaDoc.class, new LocalImpl(intfs));
600          }
601       }
602    }
603
604    private void addDescriptorAnnotations(EJBContainer container,
605          EnterpriseBean enterpriseBean, String JavaDoc ejbName) throws Exception JavaDoc
606    {
607       container.setXml(enterpriseBean);
608
609       addTransactionAnnotations(container, enterpriseBean, ejbName);
610
611       addAssemblyAnnotations(container, enterpriseBean, ejbName);
612
613       addSecurityAnnotations(container, enterpriseBean, ejbName);
614
615       addEjbAnnotations(container, enterpriseBean);
616
617       addEjb21Annotations(container);
618    }
619
620    private void addEjb21Annotations(EJBContainer container) throws Exception JavaDoc
621    {
622       Class JavaDoc[] interfaces = ejbClass.getInterfaces();
623       for (Class JavaDoc beanInterface : interfaces)
624       {
625          if (beanInterface.equals(javax.ejb.SessionBean JavaDoc.class))
626          {
627             Method method = new Method();
628             method.setEjbName(container.getEjbName());
629
630             Object JavaDoc annotation = new PostConstructImpl();
631             Class JavaDoc annotationClass = javax.annotation.PostConstruct.class;
632             method.setMethodName("ejbCreate");
633             addAnnotations(annotationClass, annotation, container, method);
634
635             annotation = new PostActivateImpl();
636             annotationClass = javax.ejb.PostActivate JavaDoc.class;
637             method.setMethodName("ejbActivate");
638             addAnnotations(annotationClass, annotation, container, method);
639
640             annotation = new PrePassivateImpl();
641             annotationClass = javax.ejb.PrePassivate JavaDoc.class;
642             method.setMethodName("ejbPassivate");
643             addAnnotations(annotationClass, annotation, container, method);
644
645             annotation = new PreDestroyImpl();
646             annotationClass = javax.annotation.PreDestroy.class;
647             method.setMethodName("ejbRemove");
648             addAnnotations(annotationClass, annotation, container, method);
649          }
650       }
651    }
652
653    private void addAssemblyAnnotations(EJBContainer container,
654          EnterpriseBean enterpriseBean, String JavaDoc ejbName) throws Exception JavaDoc
655    {
656       AssemblyDescriptor assembly = dd.getAssemblyDescriptor();
657       if (assembly != null)
658       {
659          addExcludeAnnotations(container, assembly.getExcludeList(), ejbName);
660
661          addInterceptorBindingAnnotations(container, enterpriseBean, ejbName);
662       }
663
664       if (enterpriseBean instanceof SessionEnterpriseBean)
665       {
666          addInitAnnotations(container, ((SessionEnterpriseBean) enterpriseBean)
667                .getInitMethods(), ejbName);
668          addRemoveAnnotations(container,
669                ((SessionEnterpriseBean) enterpriseBean).getRemoveMethods(),
670                ejbName);
671       }
672    }
673
674    private void addExcludeAnnotations(EJBContainer container, ExcludeList list,
675          String JavaDoc ejbName) throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
676          NoSuchFieldException JavaDoc
677    {
678       if (list != null)
679       {
680          for (Object JavaDoc o : list.getMethods())
681          {
682             Method method = (Method) o;
683             if (method.getEjbName().equals(ejbName))
684             {
685                DenyAllImpl annotation = new DenyAllImpl();
686                addAnnotations(DenyAll.class, annotation, container, method);
687             }
688          }
689       }
690    }
691
692    private void addInitAnnotations(EJBContainer container,
693          List<InitMethod> list, String JavaDoc ejbName) throws ClassNotFoundException JavaDoc,
694          NoSuchMethodException JavaDoc, NoSuchFieldException JavaDoc
695    {
696       if (list != null)
697       {
698          for (InitMethod initMethod : list)
699          {
700             Method method = initMethod.getBeanMethod();
701             InitImpl annotation = new InitImpl();
702             addAnnotations(Init JavaDoc.class, annotation, container, method);
703          }
704       }
705    }
706
707    private void addRemoveAnnotations(EJBContainer container,
708          List<RemoveMethod> list, String JavaDoc ejbName)
709          throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
710          NoSuchFieldException JavaDoc
711    {
712       if (list != null)
713       {
714          for (RemoveMethod removeMethod : list)
715          {
716             Method method = removeMethod.getBeanMethod();
717             RemoveImpl annotation = new RemoveImpl(removeMethod
718                   .isRetainIfException());
719             addAnnotations(Remove JavaDoc.class, annotation, container, method);
720          }
721       }
722    }
723
724    private void addSecurityAnnotations(EJBContainer container,
725          EnterpriseBean enterpriseBean, String JavaDoc ejbName)
726          throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
727          NoSuchFieldException JavaDoc
728    {
729       AssemblyDescriptor assembly = dd.getAssemblyDescriptor();
730       if (assembly != null)
731       {
732          List securityRoles = assembly.getSecurityRoles();
733
734          if (securityRoles.size() > 0)
735          {
736             ArrayList JavaDoc roleList = new ArrayList JavaDoc();
737             for (Object JavaDoc securityRole : securityRoles)
738             {
739                SecurityRole role = (SecurityRole) securityRole;
740                roleList.add(role.getRoleName());
741
742             }
743             DeclareRolesImpl annotation = new DeclareRolesImpl(
744                   (String JavaDoc[]) roleList.toArray(new String JavaDoc[roleList.size()]));
745             addClassAnnotation(container, DeclareRoles.class, annotation);
746          }
747
748          List methodPermissions = assembly.getMethodPermissions();
749          for (Object JavaDoc methodPermission : methodPermissions)
750          {
751             MethodPermission permission = (MethodPermission) methodPermission;
752             for (Method method : permission.getMethods())
753             {
754                if (method.getEjbName().equals(ejbName))
755                {
756                   if (permission.isUnchecked())
757                   {
758                      PermitAllImpl annotation = new PermitAllImpl();
759                      addAnnotations(PermitAll.class, annotation, container,
760                            method);
761                   } else
762                   {
763                      RolesAllowedImpl annotation = new RolesAllowedImpl();
764
765                      for (Object JavaDoc o : permission.getRoleNames())
766                      {
767                         String JavaDoc roleName = (String JavaDoc) o;
768                         annotation.addValue(roleName);
769                      }
770                      addAnnotations(RolesAllowed.class, annotation, container,
771                            method);
772                   }
773                }
774             }
775          }
776       }
777
778       if (enterpriseBean != null && enterpriseBean.getSecurityDomain() != null)
779       {
780          String JavaDoc securityDomain = enterpriseBean.getSecurityDomain();
781
782          SecurityDomainImpl annotation = new SecurityDomainImpl(securityDomain);
783
784          if (dd.getUnauthenticatedPrincipal() != null)
785             annotation.setUnauthenticatedPrincipal(dd
786                   .getUnauthenticatedPrincipal());
787
788          addClassAnnotation(container, annotation.annotationType(), annotation);
789       } else if (dd.getSecurityDomain() != null)
790       {
791          String JavaDoc securityDomain = dd.getSecurityDomain();
792
793          SecurityDomainImpl annotation = new SecurityDomainImpl(securityDomain);
794
795          if (dd.getUnauthenticatedPrincipal() != null)
796             annotation.setUnauthenticatedPrincipal(dd
797                   .getUnauthenticatedPrincipal());
798
799          addClassAnnotation(container, annotation.annotationType(), annotation);
800       } else if (dd.getUnauthenticatedPrincipal() != null)
801       {
802          SecurityDomain annotation = (SecurityDomain) ejbClass
803                .getAnnotation(SecurityDomain.class);
804          if (annotation != null)
805          {
806             SecurityDomainImpl override = new SecurityDomainImpl(annotation
807                   .value());
808             override.setUnauthenticatedPrincipal(dd
809                   .getUnauthenticatedPrincipal());
810
811             addClassAnnotation(container, override.annotationType(), override);
812          }
813       }
814    }
815
816    private void addTransactionAnnotations(EJBContainer container,
817          EnterpriseBean enterpriseBean, String JavaDoc ejbName)
818          throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
819          NoSuchFieldException JavaDoc
820    {
821       if (enterpriseBean != null)
822       {
823          if (enterpriseBean.getTransactionManagementType() != null)
824          {
825             TransactionManagementImpl annotation = new TransactionManagementImpl();
826             annotation.setValue(enterpriseBean.getTransactionManagementType());
827             addClassAnnotation(container, TransactionManagement JavaDoc.class,
828                   annotation);
829          }
830
831          MethodAttributes attributes = enterpriseBean.getMethodAttributes();
832          if (attributes != null)
833          {
834             Iterator JavaDoc methods = attributes.getMethods().iterator();
835             while (methods.hasNext())
836             {
837                Method method = (Method) methods.next();
838                if (method.getTransactionTimeout() != null)
839                {
840                   TransactionTimeout timeoutAnnotation = new TransactionTimeoutImpl(
841                         Integer.parseInt(method.getTransactionTimeout()));
842                   addAnnotations(TransactionTimeout.class, timeoutAnnotation,
843                         container, method);
844                }
845             }
846          }
847       }
848
849       AssemblyDescriptor descriptor = dd.getAssemblyDescriptor();
850       if (descriptor != null)
851       {
852          Iterator JavaDoc transactions = descriptor.getContainerTransactions()
853                .iterator();
854          while (transactions.hasNext())
855          {
856             ContainerTransaction transaction = (ContainerTransaction) transactions
857                   .next();
858             if (transaction.getMethod().getEjbName().equals(ejbName))
859             {
860                String JavaDoc transAttribute = transaction.getTransAttribute();
861                TransactionAttributeImpl annotation = new TransactionAttributeImpl();
862                if (transAttribute.equals("Mandatory"))
863                   annotation.setType(TransactionAttributeType.MANDATORY);
864                else if (transAttribute.equals("Required"))
865                   annotation.setType(TransactionAttributeType.REQUIRED);
866                else if (transAttribute.equals("RequiresNew"))
867                   annotation.setType(TransactionAttributeType.REQUIRES_NEW);
868                else if (transAttribute.equals("Supports"))
869                   annotation.setType(TransactionAttributeType.SUPPORTS);
870                else if (transAttribute.equals("NotSupported"))
871                   annotation.setType(TransactionAttributeType.NOT_SUPPORTED);
872                else if (transAttribute.equals("Never"))
873                   annotation.setType(TransactionAttributeType.NEVER);
874
875                addAnnotations(TransactionAttribute JavaDoc.class, annotation,
876                      container, transaction.getMethod());
877             }
878          }
879       }
880    }
881
882    /**
883     * Interceptors are additive. What's in the annotations and in the XML is
884     * merged
885     */

886    private void addInterceptorBindingAnnotations(EJBContainer container,
887          EnterpriseBean enterpriseBean, String JavaDoc ejbName)
888          throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
889          NoSuchFieldException JavaDoc
890    {
891       boolean definesInterceptors = false;
892
893       List<InterceptorBinding> interceptorBindings = dd.getAssemblyDescriptor()
894             .getInterceptorBindings();
895       for (InterceptorBinding binding : interceptorBindings)
896       {
897          if (binding.isOrdered())
898          {
899             continue;
900          }
901          if (binding.getEjbName().equals(ejbName))
902          {
903             if (binding.getMethodName() == null
904                   || binding.getMethodName().trim().length() == 0)
905             {
906                addClassLevelInterceptorBindingAnnotations(container, binding);
907                definesInterceptors = true;
908             } else
909             {
910                definesInterceptors = addMethodLevelInterceptorBindingAnnotations(
911                      container, binding);
912             }
913
914          }
915       }
916
917       if (!definesInterceptors
918             && di.getInterceptorInfoRepository().hasDefaultInterceptors())
919       {
920          addClassAnnotation(container, DefaultInterceptorMarker.class,
921                new DefaultInterceptorMarkerImpl());
922       }
923    }
924
925    /**
926     * Interceptors are additive. What's in the annotations and in the XML is
927     * merged
928     */

929    private void addClassLevelInterceptorBindingAnnotations(
930          EJBContainer container, InterceptorBinding binding)
931          throws ClassNotFoundException JavaDoc
932    {
933       Interceptors interceptors = (Interceptors) container
934             .resolveAnnotation(Interceptors.class);
935       InterceptorsImpl impl = InterceptorsImpl.getImpl(interceptors);
936       for (String JavaDoc name : binding.getInterceptorClasses())
937       {
938          Class JavaDoc clazz = di.getClassLoader().loadClass(name);
939          impl.addValue(clazz);
940       }
941
942       addClassAnnotation(container, impl.annotationType(), impl);
943
944       boolean exclude = binding.getExcludeDefaultInterceptors();
945       if (exclude
946             && container.resolveAnnotation(ExcludeDefaultInterceptors.class) == null)
947       {
948          addClassAnnotation(container, ExcludeDefaultInterceptors.class,
949                new ExcludeDefaultInterceptorsImpl());
950       }
951
952    }
953
954    /**
955     * Interceptors are additive. What's in the annotations and in the XML is
956     * merged
957     */

958    private boolean addMethodLevelInterceptorBindingAnnotations(
959          EJBContainer container, InterceptorBinding binding)
960          throws ClassNotFoundException JavaDoc
961    {
962       boolean addedAnnotations = false;
963       for (java.lang.reflect.Method JavaDoc method : container.getBeanClass()
964             .getMethods())
965       {
966          boolean matches = false;
967          if (method.getName().equals(binding.getMethodName()))
968          {
969             if (binding.getMethodParams() == null)
970             {
971                matches = true;
972             } else
973             {
974                Class JavaDoc[] methodParams = method.getParameterTypes();
975                List<String JavaDoc> bindingParams = binding.getMethodParams();
976
977                if (methodParams.length == bindingParams.size())
978                {
979                   matches = true;
980                   int i = 0;
981                   for (String JavaDoc paramName : bindingParams)
982                   {
983                      String JavaDoc methodParamName = InterceptorInfoRepository
984                            .simpleType(methodParams[i++]);
985                      if (!paramName.equals(methodParamName))
986                      {
987                         matches = false;
988                         break;
989                      }
990                   }
991                }
992             }
993          }
994
995          if (matches)
996          {
997             Interceptors interceptors = (Interceptors) container
998                   .resolveAnnotation(method, Interceptors.class);
999             InterceptorsImpl impl = InterceptorsImpl.getImpl(interceptors);
1000            for (String JavaDoc name : binding.getInterceptorClasses())
1001            {
1002               Class JavaDoc clazz = di.getClassLoader().loadClass(name);
1003               impl.addValue(clazz);
1004            }
1005            log.debug("adding " + Interceptors.class.getName()
1006                  + " method annotation to " + ejbClass.getName() + "."
1007                  + method.getName() + "(" + getParameters(method) + ")");
1008            container.getAnnotations().addAnnotation(method,
1009                  Interceptors.class, impl);
1010
1011            boolean excludeDefault = binding.getExcludeDefaultInterceptors();
1012            if (excludeDefault
1013                  && container.resolveAnnotation(method,
1014                        ExcludeDefaultInterceptors.class) == null)
1015            {
1016               log.debug("adding " + ExcludeDefaultInterceptors.class.getName()
1017                     + " method annotation to " + ejbClass.getName() + "."
1018                     + method.getName() + "(" + getParameters(method) + ")");
1019               container.getAnnotations().addAnnotation(method,
1020                     ExcludeDefaultInterceptors.class,
1021                     new ExcludeDefaultInterceptorsImpl());
1022            }
1023
1024            boolean excludeClass = binding.getExcludeClassInterceptors();
1025            if (excludeClass
1026                  && container.resolveAnnotation(method,
1027                        ExcludeClassInterceptors.class) == null)
1028            {
1029               log.debug("adding " + ExcludeClassInterceptors.class.getName()
1030                     + " method annotation to " + ejbClass.getName() + "."
1031                     + method.getName() + "(" + getParameters(method) + ")");
1032               container.getAnnotations().addAnnotation(method,
1033                     ExcludeClassInterceptors.class,
1034                     new ExcludeClassInterceptorsImpl());
1035            }
1036            matches = false;
1037            addedAnnotations = true;
1038         }
1039      }
1040
1041      return addedAnnotations;
1042   }
1043
1044   private void addEjbAnnotations(EJBContainer container,
1045         EnterpriseBean enterpriseBean) throws Exception JavaDoc
1046   {
1047      if (enterpriseBean != null)
1048      {
1049         addHomeAnnotations(container, enterpriseBean);
1050
1051         addJndiAnnotations(container, enterpriseBean);
1052
1053         addInterceptorMethodAnnotations(container, enterpriseBean);
1054
1055         handleResourceRefs(container, enterpriseBean.getResourceRefs());
1056
1057         addMessageDestinationAnnotations(container, enterpriseBean.getMessageDestinationRefs());
1058
1059         addSecurityIdentityAnnotation(container, enterpriseBean
1060               .getSecurityIdentity());
1061
1062         addDependencies(container, enterpriseBean);
1063
1064         addPoolAnnotations(container, enterpriseBean);
1065         
1066         addXmlAnnotations(container, enterpriseBean);
1067
1068         if (enterpriseBean instanceof SessionEnterpriseBean)
1069         {
1070            addConcurrentAnnotations(container, (SessionEnterpriseBean)enterpriseBean);
1071            addClusterAnnotations(container, (SessionEnterpriseBean)enterpriseBean);
1072            addCacheAnnotations(container, (SessionEnterpriseBean)enterpriseBean);
1073         }
1074      }
1075   }
1076
1077   private void addConcurrentAnnotations(EJBContainer container,
1078         SessionEnterpriseBean enterpriseBean) throws Exception JavaDoc
1079   {
1080      if (enterpriseBean.getConcurrent() != null)
1081      {
1082         boolean concurrent = Boolean.getBoolean(enterpriseBean.getConcurrent());
1083         if (concurrent)
1084         {
1085            SerializedConcurrentAccessImpl annotation = new SerializedConcurrentAccessImpl();
1086            addClassAnnotation(container, SerializedConcurrentAccess.class, annotation);
1087         }
1088         else
1089         {
1090            container.getAnnotations().disableAnnotation(SerializedConcurrentAccess.class.getName());
1091         }
1092      }
1093   }
1094
1095   private void addPoolAnnotations(EJBContainer container,
1096         EnterpriseBean enterpriseBean) throws Exception JavaDoc
1097   {
1098      if (enterpriseBean.getPoolConfig() != null)
1099      {
1100         PoolConfig config = enterpriseBean.getPoolConfig();
1101
1102         PoolClassImpl poolAnnotation = new PoolClassImpl();
1103
1104         if (config.getPoolClass() != null)
1105            poolAnnotation.setValue(di.getClassLoader().loadClass(config.getPoolClass()));
1106
1107         if (config.getMaxSize() != null)
1108            poolAnnotation.setMaxSize(Integer.parseInt(config.getMaxSize()));
1109
1110         if (config.getTimeout() != null)
1111            poolAnnotation.setTimeout(Long.parseLong(config.getTimeout()));
1112
1113         addClassAnnotation(container, PoolClass.class, poolAnnotation);
1114      }
1115   }
1116   
1117   private void addXmlAnnotations(EJBContainer container,
1118         EnterpriseBean enterpriseBean) throws Exception JavaDoc
1119   {
1120      Iterator JavaDoc xmlAnnotations = enterpriseBean.getXmlAnnotations().iterator();
1121      while (xmlAnnotations.hasNext())
1122      {
1123         XmlAnnotation xmlAnnotation = (XmlAnnotation)xmlAnnotations.next();
1124
1125         Class JavaDoc annotationClass = di.getClassLoader().loadClass(xmlAnnotation.getAnnotationClass());
1126         Class JavaDoc annotationImplementationClass = di.getClassLoader().loadClass(xmlAnnotation.getAnnotationImplementationClass());
1127         Object JavaDoc annotation = annotationImplementationClass.newInstance();
1128         
1129         Iterator JavaDoc properties = xmlAnnotation.getProperties().iterator();
1130         while (properties.hasNext())
1131         {
1132            NameValuePair property = (NameValuePair)properties.next();
1133            Field JavaDoc field = annotationImplementationClass.getDeclaredField(property.getName());
1134            setAnnotationPropertyField(field, annotation, property.getValue());
1135         }
1136            
1137         if (xmlAnnotation.getInjectionTarget() == null)
1138         {
1139            addClassAnnotation(container, annotationClass, annotation);
1140         }
1141         else
1142         {
1143            Method method = new Method();
1144            method.setMethodName(xmlAnnotation.getInjectionTarget().getTargetName());
1145            addAnnotations(annotationClass, annotation, container, method);
1146         }
1147      }
1148   }
1149   
1150   protected void setAnnotationPropertyField(Field JavaDoc field, Object JavaDoc annotation, String JavaDoc value) throws Exception JavaDoc
1151   {
1152      if (field.getType() == String JavaDoc.class)
1153         field.set(annotation, value);
1154      else if (field.getType() == Long JavaDoc.class)
1155         field.setLong(annotation, Long.parseLong(value));
1156      else if (field.getType() == Integer JavaDoc.class)
1157         field.setInt(annotation, Integer.parseInt(value));
1158      else if (field.getType() == Class JavaDoc.class)
1159         field.set(annotation, di.getClassLoader().loadClass(value));
1160      else if (field.getType() == Boolean JavaDoc.class)
1161         field.setBoolean(annotation, Boolean.parseBoolean(value));
1162   }
1163
1164   private void addCacheAnnotations(EJBContainer container,
1165         SessionEnterpriseBean enterpriseBean) throws Exception JavaDoc
1166   {
1167      if (enterpriseBean.getCacheConfig() != null)
1168      {
1169         CacheConfig config = enterpriseBean.getCacheConfig();
1170         if (config.getCacheClass() != null)
1171         {
1172            Class JavaDoc cacheClass = di.getClassLoader().loadClass(config.getCacheClass());
1173            CacheImpl cacheAnnotation = new CacheImpl(cacheClass);
1174            addClassAnnotation(container, Cache.class, cacheAnnotation);
1175
1176            if (cacheClass == org.jboss.ejb3.cache.simple.SimpleStatefulCache.class)
1177            {
1178               if (!ejbClass.isAnnotationPresent(PersistenceManager.class))
1179               {
1180                  PersistenceManagerImpl persistenceAnnotation = new PersistenceManagerImpl();
1181                  if (config.getPersistenceManager() != null)
1182                     persistenceAnnotation.setValue(di.getClassLoader().loadClass(config.getPersistenceManager()));
1183                  addClassAnnotation(container, PersistenceManager.class, persistenceAnnotation);
1184               }
1185            }
1186         }
1187
1188         if (config.getName() != null)
1189         {
1190            org.jboss.annotation.ejb.cache.tree.CacheConfigImpl configAnnotation = new org.jboss.annotation.ejb.cache.tree.CacheConfigImpl();
1191
1192            configAnnotation.setName(config.getName());
1193
1194            if (config.getMaxSize() != null)
1195               configAnnotation.setMaxSize(Integer.parseInt(config.getMaxSize()));
1196
1197            if (config.getIdleTimeoutSeconds() != null)
1198               configAnnotation.setIdleTimeoutSeconds(Long.parseLong(config.getIdleTimeoutSeconds()));
1199
1200            addClassAnnotation(container, org.jboss.annotation.ejb.cache.tree.CacheConfig.class, configAnnotation);
1201         }
1202         else
1203         {
1204            org.jboss.annotation.ejb.cache.simple.CacheConfigImpl configAnnotation = new org.jboss.annotation.ejb.cache.simple.CacheConfigImpl();
1205
1206            if (config.getMaxSize() != null)
1207               configAnnotation.setMaxSize(Integer.parseInt(config.getMaxSize()));
1208
1209            if (config.getIdleTimeoutSeconds() != null)
1210               configAnnotation.setIdleTimeoutSeconds(Long.parseLong(config.getIdleTimeoutSeconds()));
1211
1212            addClassAnnotation(container, org.jboss.annotation.ejb.cache.simple.CacheConfig.class, configAnnotation);
1213         }
1214      }
1215
1216   }
1217
1218   private void addClusterAnnotations(EJBContainer container,
1219         SessionEnterpriseBean enterpriseBean) throws Exception JavaDoc
1220   {
1221      ClusteredImpl clusteredAnnotation = null;
1222
1223      if (enterpriseBean.getClustered() != null)
1224      {
1225         Clustered existingAnnotation = (Clustered)ejbClass.getAnnotation(Clustered.class);
1226
1227         boolean clustered = Boolean.parseBoolean(enterpriseBean.getClustered());
1228         if (!clustered)
1229         {
1230            if (existingAnnotation != null)
1231              container.getAnnotations().disableAnnotation(Clustered.class.getName());
1232
1233            return;
1234         }
1235         else
1236         {
1237            if (existingAnnotation == null)
1238               clusteredAnnotation = new ClusteredImpl();
1239         }
1240      }
1241
1242      ClusterConfig config = enterpriseBean.getClusterConfig();
1243      if (config != null)
1244      {
1245         if (clusteredAnnotation == null)
1246            clusteredAnnotation = new ClusteredImpl();
1247
1248         if (config.getLoadBalancePolicy() != null)
1249         {
1250            Class JavaDoc policy = di.getClassLoader().loadClass(config.getLoadBalancePolicy());
1251            clusteredAnnotation.setLoadBalancePolicy(policy);
1252         }
1253
1254         if (config.getPartition() != null)
1255         {
1256            clusteredAnnotation.setPartition(config.getPartition());
1257         }
1258      }
1259
1260      if (clusteredAnnotation != null)
1261      {
1262         addClassAnnotation(container, Clustered.class, clusteredAnnotation);
1263      }
1264   }
1265
1266   private void addDependencies(EJBContainer container,
1267         EnterpriseBean enterpriseBean) throws Exception JavaDoc
1268   {
1269      if (enterpriseBean.getDependencies().size() > 0)
1270      {
1271         DependsImpl annotation = new DependsImpl();
1272         Iterator JavaDoc<String JavaDoc> dependencies = enterpriseBean.getDependencies()
1273               .iterator();
1274         while (dependencies.hasNext())
1275         {
1276            annotation.addDependency(dependencies.next());
1277         }
1278
1279         addClassAnnotation(container, Depends.class, annotation);
1280      }
1281
1282      if (enterpriseBean.getIgnoreDependencies().size() > 0)
1283      {
1284         Iterator JavaDoc<InjectionTarget> ignores = enterpriseBean.getIgnoreDependencies().iterator();
1285         while (ignores.hasNext())
1286         {
1287            InjectionTarget ignore = ignores.next();
1288            IgnoreDependencyImpl annotation = new IgnoreDependencyImpl();
1289
1290            Method method = new Method();
1291            method.setMethodName(ignore.getTargetName());
1292
1293            addAnnotations(IgnoreDependency.class, annotation, container, method);
1294         }
1295      }
1296   }
1297
1298   private void addServiceAnnotations(EJBContainer container, EnterpriseBean ejb)
1299         throws ClassNotFoundException JavaDoc
1300   {
1301      org.jboss.ejb3.metamodel.Service service = (org.jboss.ejb3.metamodel.Service) ejb;
1302
1303      if (service == null)
1304         return;
1305
1306      String JavaDoc management = service.getManagement();
1307
1308      if (management != null)
1309      {
1310         ManagementImpl annotation = new ManagementImpl(di.getClassLoader()
1311               .loadClass(management));
1312         addClassAnnotation(container, Management.class, annotation);
1313      }
1314   }
1315
1316   private void addConsumerAnnotations(EJBContainer container,
1317         EnterpriseBean ejb) throws ClassNotFoundException JavaDoc,
1318         NoSuchFieldException JavaDoc, NoSuchMethodException JavaDoc
1319   {
1320      org.jboss.ejb3.metamodel.Consumer consumer = (org.jboss.ejb3.metamodel.Consumer) ejb;
1321
1322      if (consumer == null)
1323         return;
1324
1325      if (consumer.getProducers().size() > 0
1326            || consumer.getLocalProducers().size() > 0)
1327      {
1328         ProducersImpl producersAnnotation = new ProducersImpl();
1329
1330         Iterator JavaDoc producers = consumer.getProducers().iterator();
1331         while (producers.hasNext())
1332         {
1333            org.jboss.ejb3.metamodel.Producer producer = (org.jboss.ejb3.metamodel.Producer) producers
1334                  .next();
1335            ProducerImpl annotation = new ProducerImpl(di.getClassLoader()
1336                  .loadClass(producer.getClassName()));
1337            if (producer.getConnectionFactory() != null)
1338               annotation.setConnectionFactory(producer.getConnectionFactory());
1339            producersAnnotation.addProducer(annotation);
1340         }
1341
1342         producers = consumer.getLocalProducers().iterator();
1343         while (producers.hasNext())
1344         {
1345            org.jboss.ejb3.metamodel.Producer producer = (org.jboss.ejb3.metamodel.Producer) producers
1346                  .next();
1347            ProducerImpl annotation = new ProducerImpl(di.getClassLoader()
1348                  .loadClass(producer.getClassName()));
1349            if (producer.getConnectionFactory() != null)
1350               annotation.setConnectionFactory(producer.getConnectionFactory());
1351            producersAnnotation.addProducer(annotation);
1352         }
1353         addClassAnnotation(container, Producers.class, producersAnnotation);
1354      }
1355
1356      org.jboss.ejb3.metamodel.CurrentMessage currentMessage = consumer
1357            .getCurrentMessage();
1358      if (currentMessage != null)
1359      {
1360         List methods = currentMessage.getMethods();
1361         CurrentMessageImpl annotation = new CurrentMessageImpl();
1362         for (int i = 0; i < methods.size(); ++i)
1363         {
1364            Method method = (Method) methods.get(i);
1365            addAnnotations(CurrentMessage.class, annotation, container, method);
1366         }
1367      }
1368
1369      org.jboss.ejb3.metamodel.MessageProperties properties = consumer
1370            .getMessageProperties();
1371      if (properties != null)
1372      {
1373         List methods = properties.getMethods();
1374
1375         MessagePropertiesImpl annotation = new MessagePropertiesImpl();
1376
1377         String JavaDoc delivery = properties.getDelivery();
1378         if (delivery != null && delivery.equals("Persistent"))
1379            annotation.setDelivery(DeliveryMode.PERSISTENT);
1380         else
1381            annotation.setDelivery(DeliveryMode.NON_PERSISTENT);
1382
1383         String JavaDoc priority = properties.getPriority();
1384         if (priority != null)
1385            annotation.setDelivery(DeliveryMode.PERSISTENT);
1386
1387         String JavaDoc interfac = properties.getClassName();
1388         if (interfac != null)
1389         {
1390            Class JavaDoc clazz = di.getClassLoader().loadClass(interfac);
1391            annotation.setInterface(clazz);
1392         }
1393
1394         for (int i = 0; i < methods.size(); ++i)
1395         {
1396            Method method = (Method) methods.get(i);
1397            addAnnotations(MessageProperties.class, annotation, container,
1398                  method);
1399         }
1400      }
1401   }
1402
1403   private void addJndiAnnotations(EJBContainer container,
1404         EnterpriseBean enterpriseBean) throws ClassNotFoundException JavaDoc
1405   {
1406      addLocalJndiAnnotations(container, enterpriseBean);
1407      addRemoteJndiAnnotations(container, enterpriseBean);
1408   }
1409
1410   private void addLocalJndiAnnotations(EJBContainer container,
1411         EnterpriseBean enterpriseBean) throws ClassNotFoundException JavaDoc
1412   {
1413      String JavaDoc localJndiName = enterpriseBean.getLocalJndiName();
1414      if (localJndiName != null)
1415      {
1416         LocalBindingImpl localBinding = new LocalBindingImpl(localJndiName);
1417         addClassAnnotation(container, LocalBinding.class, localBinding);
1418      }
1419   }
1420
1421   private void addRemoteJndiAnnotations(EJBContainer container,
1422         EnterpriseBean enterpriseBean) throws ClassNotFoundException JavaDoc
1423   {
1424      List<org.jboss.ejb3.metamodel.RemoteBinding> bindingsList = enterpriseBean.getRemoteBindings();
1425      if (bindingsList.size() == 0)
1426      {
1427         addSimpleJndiAnnotations(container, enterpriseBean);
1428         return;
1429      }
1430
1431      AnnotationRepository annotations = container.getAnnotations();
1432
1433      annotations.disableAnnotation(RemoteBinding.class.getName());
1434
1435      List<RemoteBindingImpl> bindingAnnotationsList = new ArrayList JavaDoc();
1436
1437      Iterator JavaDoc bindings = bindingsList.iterator();
1438      while(bindings.hasNext())
1439      {
1440         org.jboss.ejb3.metamodel.RemoteBinding binding = (org.jboss.ejb3.metamodel.RemoteBinding)bindings.next();
1441         RemoteBindingImpl bindingAnnotation = new RemoteBindingImpl();
1442
1443         if (binding.getJndiName() != null)
1444            bindingAnnotation.setJndiBinding(binding.getJndiName());
1445
1446         if (binding.getClientBindUrl() != null)
1447            bindingAnnotation.setBindUrl(binding.getClientBindUrl());
1448
1449         if (binding.getInterceptorStack() != null)
1450            bindingAnnotation.setStack(binding.getInterceptorStack());
1451
1452         if (binding.getProxyFactory() != null)
1453            bindingAnnotation.setFactory(di.getClassLoader().loadClass(binding.getProxyFactory()));
1454
1455         bindingAnnotationsList.add(bindingAnnotation);
1456
1457      }
1458
1459      RemoteBindingsImpl bindingsAnnotation = new RemoteBindingsImpl(bindingAnnotationsList);
1460      addClassAnnotation(container, RemoteBindings.class, bindingsAnnotation);
1461   }
1462
1463   private void addSimpleJndiAnnotations(EJBContainer container,
1464         EnterpriseBean enterpriseBean) throws ClassNotFoundException JavaDoc
1465   {
1466      RemoteBindingImpl remoteBinding = null;
1467
1468      String JavaDoc jndiName = enterpriseBean.getJndiName();
1469      if (jndiName != null)
1470      {
1471         remoteBinding = new RemoteBindingImpl();
1472         remoteBinding.setJndiBinding(jndiName);
1473         addClassAnnotation(container, RemoteBinding.class, remoteBinding);
1474      }
1475
1476      if (remoteBinding != null)
1477      {
1478         RemoteBinding existingBinding = (RemoteBinding)ejbClass.getAnnotation(RemoteBinding.class);
1479         if (existingBinding != null)
1480            remoteBinding.merge(existingBinding);
1481
1482         addClassAnnotation(container, RemoteBinding.class, remoteBinding);
1483      }
1484   }
1485
1486   private void handleResourceRefs(EJBContainer container,
1487         Collection JavaDoc<ResourceRef> resourceRefList)
1488   {
1489      Iterator JavaDoc refs = resourceRefList.iterator();
1490      while (refs.hasNext())
1491      {
1492         ResourceRef ref = (ResourceRef) refs.next();
1493
1494         if (ref.getResourceName() != null)
1495         {
1496            // for <resource-manager>
1497
ref.setJndiName(dd.resolveResourceManager(ref.getResourceName()));
1498            ref.setMappedName(dd.resolveResourceManager(ref.getResourceName()));
1499         }
1500      }
1501   }
1502
1503   private void addMessageDestinationAnnotations(EJBContainer container,
1504         Collection JavaDoc destinationRefList)
1505
1506   {
1507      Iterator JavaDoc refs = destinationRefList.iterator();
1508      while (refs.hasNext())
1509      {
1510         MessageDestinationRef ref = (MessageDestinationRef) refs.next();
1511
1512         if (ref.getMappedName() == null || ref.getMappedName().equals(""))
1513         {
1514            AssemblyDescriptor descriptor = dd.getAssemblyDescriptor();
1515            if (descriptor != null)
1516            {
1517               MessageDestination destination = descriptor
1518                     .findMessageDestination(ref.getMessageDestinationLink());
1519               if (destination != null)
1520               {
1521                  ref.setMappedName(destination.getJndiName());
1522               }
1523            }
1524         }
1525      }
1526   }
1527
1528   private void addInterceptorMethodAnnotations(EJBContainer container,
1529         EnterpriseBean enterpriseBean)
1530   {
1531      if (enterpriseBean instanceof SessionEnterpriseBean)
1532      {
1533         addInterceptorMethodAnnotation(container, enterpriseBean,
1534               ((SessionEnterpriseBean) enterpriseBean).getAroundInvoke(),
1535               AroundInvoke.class, "around-invoke-method");
1536         addInterceptorMethodAnnotation(container, enterpriseBean,
1537               ((SessionEnterpriseBean) enterpriseBean).getPostConstruct(),
1538               PostConstruct.class, "post-construct-method");
1539         addInterceptorMethodAnnotation(container, enterpriseBean,
1540               ((SessionEnterpriseBean) enterpriseBean).getPostActivate(),
1541               PostActivate JavaDoc.class, "post-activate-method");
1542         addInterceptorMethodAnnotation(container, enterpriseBean,
1543               ((SessionEnterpriseBean) enterpriseBean).getPrePassivate(),
1544               PrePassivate JavaDoc.class, "pre-passivate-method");
1545         addInterceptorMethodAnnotation(container, enterpriseBean,
1546               ((SessionEnterpriseBean) enterpriseBean).getPreDestroy(),
1547               PreDestroy.class, "pre-destroy-method");
1548      } else if (enterpriseBean instanceof MessageDrivenBean)
1549      {
1550         addInterceptorMethodAnnotation(container, enterpriseBean,
1551               ((MessageDrivenBean) enterpriseBean).getAroundInvoke(),
1552               AroundInvoke.class, "around-invoke-method");
1553         addInterceptorMethodAnnotation(container, enterpriseBean,
1554               ((MessageDrivenBean) enterpriseBean).getPostConstruct(),
1555               PostConstruct.class, "post-construct-method");
1556         addInterceptorMethodAnnotation(container, enterpriseBean,
1557               ((MessageDrivenBean) enterpriseBean).getPreDestroy(),
1558               PreDestroy.class, "pre-destroy-method");
1559      }
1560   }
1561
1562   private void addInterceptorMethodAnnotation(EJBContainer container,
1563         EnterpriseBean enterpriseBean, Method method, Class JavaDoc ann, String JavaDoc xmlName)
1564   {
1565      if (method == null)
1566         return;
1567
1568      java.lang.reflect.Method JavaDoc found = null;
1569      for (java.lang.reflect.Method JavaDoc rm : container.getBeanClass()
1570            .getDeclaredMethods())
1571      {
1572         if (rm.getName().equals(method.getMethodName()))
1573         {
1574            if (ann == AroundInvoke.class)
1575            {
1576               if (InterceptorInfoRepository.checkValidBusinessSignature(rm))
1577               {
1578                  found = rm;
1579                  break;
1580               }
1581            } else
1582            {
1583               if (InterceptorInfoRepository
1584                     .checkValidBeanLifecycleSignature(rm))
1585               {
1586                  found = rm;
1587                  break;
1588               }
1589            }
1590         }
1591      }
1592
1593      if (found == null)
1594      {
1595         log.warn("No method found within " + container.getBeanClassName()
1596               + " with name " + method.getMethodName()
1597               + " with the right signature for " + xmlName + "was found");
1598         return;
1599      }
1600
1601      if (container.resolveAnnotation(found, ann) == null)
1602      {
1603         log.debug("adding " + ann.getName() + " method annotation to "
1604               + ejbClass.getName() + "." + found.getName());
1605
1606         container.getAnnotations().addAnnotation(found, ann,
1607               getInterceptorImpl(ann));
1608      }
1609   }
1610
1611   private Object JavaDoc getInterceptorImpl(Class JavaDoc ann)
1612   {
1613      if (ann == AroundInvoke.class)
1614      {
1615         return new AroundInvokeImpl();
1616      } else if (ann == PostConstruct.class)
1617      {
1618         return new PostConstructImpl();
1619      } else if (ann == PostActivate JavaDoc.class)
1620      {
1621         return new PostActivateImpl();
1622      } else if (ann == PrePassivate JavaDoc.class)
1623      {
1624         return new PrePassivateImpl();
1625      } else if (ann == PreDestroy.class)
1626      {
1627         return new PreDestroyImpl();
1628      }
1629
1630      return null;
1631   }
1632
1633   private void addSecurityIdentityAnnotation(EJBContainer container,
1634         SecurityIdentity identity)
1635   {
1636      if (identity != null && !identity.isUseCallerIdentity())
1637      {
1638         RunAs runAs = identity.getRunAs();
1639         if (runAs != null)
1640         {
1641            RunAsImpl annotation = new RunAsImpl(runAs.getRoleName());
1642            addClassAnnotation(container, annotation.annotationType(),
1643                  annotation);
1644
1645            String JavaDoc runAsPrincipal = identity.getRunAsPrincipal();
1646            if (runAsPrincipal != null)
1647            {
1648               RunAsPrincipalImpl principalAnnotation = new RunAsPrincipalImpl(
1649                     runAs.getRoleName());
1650               addClassAnnotation(container, principalAnnotation
1651                     .annotationType(), principalAnnotation);
1652            }
1653         }
1654      }
1655   }
1656
1657   protected void overrideAnnotations(EJBContainer container, Member JavaDoc m,
1658         String JavaDoc annotation, Object JavaDoc value)
1659   {
1660      AnnotationRepository annotations = container.getAnnotations();
1661
1662      if (value instanceof javax.annotation.security.DenyAll)
1663      {
1664         annotations.disableAnnotation(m,
1665               javax.annotation.security.PermitAll.class.getName());
1666         annotations.disableAnnotation(m,
1667               javax.annotation.security.RolesAllowed.class.getName());
1668      } else if (value instanceof javax.annotation.security.PermitAll)
1669      {
1670         annotations.disableAnnotation(m,
1671               javax.annotation.security.DenyAll.class.getName());
1672         annotations.disableAnnotation(m,
1673               javax.annotation.security.RolesAllowed.class.getName());
1674      } else if (value instanceof javax.annotation.security.RolesAllowed)
1675      {
1676         annotations.disableAnnotation(m,
1677               javax.annotation.security.PermitAll.class.getName());
1678         annotations.disableAnnotation(m,
1679               javax.annotation.security.DenyAll.class.getName());
1680      }
1681   }
1682
1683   private void addClassAnnotation(EJBContainer container,
1684         Class JavaDoc annotationClass, Object JavaDoc annotation)
1685   {
1686      log.debug("adding class annotation " + annotationClass.getName() + " to "
1687            + ejbClass.getName() + " " + annotation);
1688      log.debug("adding class annotation " + annotationClass.getName() + " to "
1689            + ejbClass.getName() + " " + annotation);
1690      container.getAnnotations()
1691            .addClassAnnotation(annotationClass, annotation);
1692   }
1693
1694   private void addAnnotations(Class JavaDoc annotationClass, Object JavaDoc annotation,
1695         EJBContainer container, Method method) throws ClassNotFoundException JavaDoc,
1696         NoSuchMethodException JavaDoc, NoSuchFieldException JavaDoc
1697   {
1698      String JavaDoc methodName = method.getMethodName();
1699
1700      AnnotationRepository annotations = container.getAnnotations();
1701      if (methodName.equals("*"))
1702      {
1703         log.debug("adding " + annotationClass.getName() + " annotation to "
1704               + ejbClass.getName() + "." + methodName);
1705
1706         for (java.lang.reflect.Method JavaDoc declaredMethod : ejbClass
1707               .getDeclaredMethods())
1708         {
1709            annotations.addAnnotation(declaredMethod, annotationClass,
1710                  annotation);
1711            overrideAnnotations(container, declaredMethod, annotationClass
1712                  .getName(), annotation);
1713         }
1714      } else
1715      {
1716         List params = method.getMethodParams();
1717         if (params == null)
1718         {
1719            java.lang.reflect.Method JavaDoc[] methods = ejbClass.getMethods();
1720            boolean foundMethod = false;
1721            for (int methodIndex = 0; methodIndex < methods.length; ++methodIndex)
1722            {
1723               if (methods[methodIndex].getName().equals(methodName))
1724               {
1725                  log.debug("adding " + annotationClass.getName()
1726                        + " method annotation to " + ejbClass.getName() + "."
1727                        + methodName);
1728                  annotations.addAnnotation(methods[methodIndex],
1729                        annotationClass, annotation);
1730                  overrideAnnotations(container, methods[methodIndex],
1731                        annotationClass.getName(), annotation);
1732                  foundMethod = true;
1733
1734               }
1735            }
1736
1737            if (!foundMethod)
1738            {
1739               methods = ejbClass.getDeclaredMethods();
1740               for (int methodIndex = 0; methodIndex < methods.length; ++methodIndex)
1741               {
1742                  if (methods[methodIndex].getName().equals(methodName))
1743                  {
1744                     log.debug("adding " + annotationClass.getName()
1745                           + " method annotation to " + ejbClass.getName()
1746                           + "." + methodName);
1747                     annotations.addAnnotation(methods[methodIndex],
1748                           annotationClass, annotation);
1749                     overrideAnnotations(container, methods[methodIndex],
1750                           annotationClass.getName(), annotation);
1751                     foundMethod = true;
1752
1753                  }
1754               }
1755            }
1756
1757            if (!foundMethod)
1758            {
1759               java.lang.reflect.Field JavaDoc member = ejbClass
1760                     .getDeclaredField(methodName);
1761               if (member != null)
1762               {
1763                  log.debug("adding " + annotationClass.getName()
1764                        + " field annotation to " + ejbClass.getName() + "."
1765                        + methodName);
1766                  annotations
1767                        .addAnnotation(member, annotationClass, annotation);
1768                  overrideAnnotations(container, member, annotationClass
1769                        .getName(), annotation);
1770               }
1771            }
1772         } else
1773         {
1774            Class JavaDoc[] methodSignature = new Class JavaDoc[params.size()];
1775            Iterator JavaDoc paramIterator = params.iterator();
1776            int paramIndex = 0;
1777            while (paramIterator.hasNext())
1778            {
1779               String JavaDoc param = (String JavaDoc) paramIterator.next();
1780               Class JavaDoc paramClass = null;
1781               if (param.equals("boolean"))
1782                  paramClass = boolean.class;
1783               else if (param.equals("int"))
1784                  paramClass = int.class;
1785               else if (param.equals("long"))
1786                  paramClass = long.class;
1787               else if (param.equals("short"))
1788                  paramClass = short.class;
1789               else if (param.equals("byte"))
1790                  paramClass = byte.class;
1791               else if (param.equals("char"))
1792                  paramClass = char.class;
1793               else
1794                  paramClass = di.getClassLoader().loadClass(param);
1795               methodSignature[paramIndex++] = paramClass;
1796            }
1797            java.lang.reflect.Member JavaDoc member = ejbClass.getMethod(methodName,
1798                  methodSignature);
1799            log.debug("adding " + annotationClass.getName()
1800                  + " method annotation to " + ejbClass.getName() + "."
1801                  + methodName);
1802            annotations.addAnnotation(member, annotationClass, annotation);
1803            overrideAnnotations(container, member, annotationClass.getName(),
1804                  annotation);
1805         }
1806      }
1807   }
1808
1809   private static String JavaDoc getParameters(java.lang.reflect.Method JavaDoc m)
1810   {
1811      if (m.getParameterTypes().length == 0)
1812      {
1813         return "";
1814      }
1815      StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1816      boolean first = true;
1817      for (Class JavaDoc param : m.getParameterTypes())
1818      {
1819         if (!first)
1820         {
1821            sb.append(", ");
1822         } else
1823         {
1824            first = false;
1825         }
1826         sb.append(InterceptorInfoRepository.simpleType(param));
1827      }
1828      return sb.toString();
1829   }
1830
1831}
1832
Popular Tags