KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import javax.ejb.EJBContext JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingEnumeration JavaDoc;
29
30 import org.jboss.aop.metadata.SimpleMetaData;
31 import org.jboss.ejb3.interceptor.InterceptorInfo;
32 import org.jboss.ejb3.interceptor.InterceptorInjector;
33 import org.jboss.logging.Logger;
34 import org.jboss.naming.Util;
35 import org.jboss.security.RealmMapping;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 55144 $
42  */

43 public abstract class BaseContext implements BeanContext
44 {
45    protected static Logger log = Logger.getLogger(BaseContext.class);
46    protected Container container;
47    protected Object JavaDoc bean;
48    protected RealmMapping rm;
49    protected SimpleMetaData metadata;
50    protected EJBContext JavaDoc ejbContext;
51    
52    protected HashMap JavaDoc<Class JavaDoc, Object JavaDoc> interceptorInstances;
53    
54    public BaseContext()
55    {
56    }
57
58    public Object JavaDoc getInstance()
59    {
60       return bean;
61    }
62
63    public void setInstance(Object JavaDoc instance)
64    {
65       bean = instance;
66    }
67
68    public Container getContainer()
69    {
70       return container;
71    }
72
73    public SimpleMetaData getMetaData()
74    {
75       if (metadata == null) metadata = new SimpleMetaData();
76       return metadata;
77    }
78
79    public void setContainer(Container container)
80    {
81       this.container = container;
82       bindEJBContext();
83    }
84    
85    public void bindEJBContext()
86    {
87       try
88       {
89          Util.rebind(container.getEnc(), "EJBContext", getEJBContext());
90       }
91       catch (javax.naming.NamingException JavaDoc e)
92       {
93          e.printStackTrace();
94          throw new RuntimeException JavaDoc(e);
95       }
96    }
97    
98    public void initialiseInterceptorInstances()
99    {
100       HashSet JavaDoc<InterceptorInfo> interceptors = ((EJBContainer)container).getApplicableInterceptors();
101       if (interceptors != null && interceptors.size() > 0 && interceptorInstances == null)
102       {
103          HashMap JavaDoc<Class JavaDoc,InterceptorInjector> interceptorInjectors = ((EJBContainer)container).getInterceptorInjectors();
104          interceptorInstances = new HashMap JavaDoc<Class JavaDoc, Object JavaDoc>();
105          for (InterceptorInfo info : interceptors)
106          {
107             try
108             {
109                Object JavaDoc instance = info.getClazz().newInstance();
110                interceptorInstances.put(info.getClazz(), instance);
111                interceptorInjectors.get(info.getClazz()).inject(this, instance);
112             }
113             catch (Exception JavaDoc e)
114             {
115                log.warn("Interceptors must have a public noargs constructor: " + info.getClazz().getName());
116             }
117          }
118       }
119    }
120
121    public EJBContext JavaDoc getEJBContext()
122    {
123       if (ejbContext == null)
124       {
125          BaseSessionContext bsc = new BaseSessionContext();
126          bsc.setContainer(getContainer());
127          bsc.setBaseContext(this);
128          ejbContext = bsc;
129       }
130       return ejbContext;
131    }
132
133    public Object JavaDoc[] getInterceptorInstances(InterceptorInfo[] interceptorInfos)
134    {
135       Object JavaDoc[] interceptors = new Object JavaDoc[interceptorInfos.length];
136       int i = 0;
137       for (InterceptorInfo info : interceptorInfos)
138       {
139          interceptors[i++] = interceptorInstances.get(info.getClazz());
140       }
141       return interceptors;
142    }
143    
144    public Object JavaDoc getInvokedMethodKey()
145    {
146       return container;
147    }
148 }
149
Popular Tags