001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3;
023:
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import javax.ejb.EJBContext;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingEnumeration;
029:
030: import org.jboss.aop.metadata.SimpleMetaData;
031: import org.jboss.ejb3.interceptor.InterceptorInfo;
032: import org.jboss.ejb3.interceptor.InterceptorInjector;
033: import org.jboss.logging.Logger;
034: import org.jboss.naming.Util;
035: import org.jboss.security.RealmMapping;
036:
037: /**
038: * Comment
039: *
040: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
041: * @version $Revision: 61173 $
042: */
043: public abstract class BaseContext implements BeanContext {
044: protected static Logger log = Logger.getLogger(BaseContext.class);
045: protected Container container;
046: protected Object bean;
047: protected RealmMapping rm;
048: protected SimpleMetaData metadata;
049: protected EJBContext ejbContext;
050:
051: protected HashMap<Class, Object> interceptorInstances;
052:
053: public BaseContext() {
054: }
055:
056: public Object getId() {
057: return null;
058: }
059:
060: public Object getInstance() {
061: return bean;
062: }
063:
064: public void setInstance(Object instance) {
065: bean = instance;
066: }
067:
068: public Container getContainer() {
069: return container;
070: }
071:
072: public SimpleMetaData getMetaData() {
073: if (metadata == null)
074: metadata = new SimpleMetaData();
075: return metadata;
076: }
077:
078: public void setContainer(Container container) {
079: this .container = container;
080: bindEJBContext();
081: }
082:
083: public void bindEJBContext() {
084: try {
085: Util.rebind(container.getEnc(), "EJBContext",
086: getEJBContext());
087: } catch (javax.naming.NamingException e) {
088: e.printStackTrace();
089: throw new RuntimeException(e);
090: }
091: }
092:
093: public void initialiseInterceptorInstances() {
094: HashSet<InterceptorInfo> interceptors = ((EJBContainer) container)
095: .getApplicableInterceptors();
096: if (interceptors != null && interceptors.size() > 0
097: && interceptorInstances == null) {
098: HashMap<Class, InterceptorInjector> interceptorInjectors = ((EJBContainer) container)
099: .getInterceptorInjectors();
100: interceptorInstances = new HashMap<Class, Object>();
101: for (InterceptorInfo info : interceptors) {
102: try {
103: Object instance = info.getClazz().newInstance();
104: interceptorInstances.put(info.getClazz(), instance);
105: interceptorInjectors.get(info.getClazz()).inject(
106: this , instance);
107: } catch (Exception e) {
108: log
109: .warn("Interceptors must have a public noargs constructor: "
110: + info.getClazz().getName());
111: }
112: }
113: }
114: }
115:
116: public EJBContext getEJBContext() {
117: if (ejbContext == null) {
118: BaseSessionContext bsc = new BaseSessionContext();
119: bsc.setContainer(getContainer());
120: bsc.setBaseContext(this );
121: ejbContext = bsc;
122: }
123: return ejbContext;
124: }
125:
126: public Object[] getInterceptorInstances(
127: InterceptorInfo[] interceptorInfos) {
128: Object[] interceptors = new Object[interceptorInfos.length];
129: int i = 0;
130: for (InterceptorInfo info : interceptorInfos) {
131: interceptors[i++] = interceptorInstances.get(info
132: .getClazz());
133: }
134: return interceptors;
135: }
136:
137: public Object getInvokedMethodKey() {
138: return container;
139: }
140: }
|