001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.session;
031:
032: import com.caucho.config.*;
033: import com.caucho.ejb.AbstractContext;
034: import com.caucho.ejb.AbstractServer;
035: import com.caucho.ejb.cfg.*;
036: import com.caucho.ejb.manager.EjbContainer;
037: import com.caucho.webbeans.component.*;
038: import com.caucho.webbeans.context.*;
039: import com.caucho.webbeans.manager.WebBeansContainer;
040:
041: import javax.ejb.*;
042: import javax.webbeans.*;
043: import java.util.ArrayList;
044: import java.util.HashMap;
045: import java.util.logging.Logger;
046:
047: /**
048: * Server container for a session bean.
049: */
050: abstract public class SessionServer extends AbstractServer {
051: private final static Logger log = Logger
052: .getLogger(SessionServer.class.getName());
053:
054: private HashMap<Class, ComponentImpl> _componentMap = new HashMap<Class, ComponentImpl>();
055:
056: public SessionServer(EjbContainer manager) {
057: super (manager);
058: }
059:
060: @Override
061: protected String getType() {
062: return "session:";
063: }
064:
065: /**
066: * Initialize the server
067: */
068: @Override
069: public void init() throws Exception {
070: Thread thread = Thread.currentThread();
071: ClassLoader oldLoader = thread.getContextClassLoader();
072:
073: try {
074: thread.setContextClassLoader(_loader);
075:
076: super .init();
077:
078: WebBeansContainer webBeans = WebBeansContainer.create();
079:
080: SingletonComponent comp = new SingletonComponent(webBeans,
081: getSessionContext());
082: comp.setTargetType(SessionContext.class);
083: comp.init();
084: webBeans.addComponent(comp);
085:
086: if (_localHomeClass != null)
087: _localHome = (EJBLocalHome) getLocalObject(_localHomeClass);
088: if (_remoteHomeClass != null)
089: _remoteHome = (EJBHome) getRemoteObject(_remoteHomeClass);
090: } finally {
091: thread.setContextClassLoader(oldLoader);
092: }
093:
094: registerWebBeans();
095:
096: log.fine(this + " initialized");
097: }
098:
099: private void registerWebBeans() {
100: Class beanClass = getBeanSkelClass();
101: ArrayList<Class> localApiList = getLocalApiList();
102: ArrayList<Class> remoteApiList = getRemoteApiList();
103:
104: if (beanClass != null
105: && (localApiList != null || remoteApiList != null)) {
106: WebBeansContainer webBeans = WebBeansContainer.create();
107:
108: String beanName = getEJBName();
109: Named named = (Named) beanClass.getAnnotation(Named.class);
110:
111: if (named != null)
112: beanName = named.value();
113:
114: if (localApiList != null) {
115: for (Class api : localApiList) {
116: ComponentImpl comp = createSessionComponent(api);
117:
118: comp.setTargetType(api);
119:
120: comp.setName(beanName);
121: comp.addNameBinding(beanName);
122:
123: comp.init();
124: webBeans.addComponentByName(beanName, comp);
125: webBeans.addComponentByType(api, comp);
126:
127: _componentMap.put(api, comp);
128: }
129: }
130:
131: if (remoteApiList != null) {
132: for (Class api : remoteApiList) {
133: ComponentImpl comp = createSessionComponent(api);
134:
135: comp.setTargetType(api);
136:
137: comp.setName(beanName);
138: comp.addNameBinding(beanName);
139:
140: comp.init();
141: webBeans.addComponentByName(beanName, comp);
142: webBeans.addComponentByType(api, comp);
143:
144: _componentMap.put(api, comp);
145: }
146: }
147: }
148: }
149:
150: protected void bindInjection() {
151: super .bindInjection();
152:
153: for (ComponentImpl comp : _componentMap.values()) {
154: comp.bind();
155: }
156: }
157:
158: abstract protected ComponentImpl createSessionComponent(Class api);
159:
160: protected ComponentImpl getComponent(Class api) {
161: return _componentMap.get(api);
162: }
163:
164: /**
165: * Returns the object key from a handle.
166: */
167: @Override
168: public Class getPrimaryKeyClass() {
169: return null;
170: }
171:
172: /**
173: * Returns the EJBLocalHome stub for the container
174: */
175: @Override
176: public EJBLocalHome getEJBLocalHome() {
177: return _localHome;
178: }
179:
180: @Override
181: public AbstractContext getContext() {
182: return getSessionContext();
183: }
184: }
|