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.ejb.AbstractContext;
033: import com.caucho.ejb.EJBExceptionWrapper;
034: import com.caucho.ejb.manager.EjbContainer;
035: import com.caucho.ejb.protocol.*;
036: import com.caucho.webbeans.component.*;
037:
038: import javax.ejb.*;
039: import javax.webbeans.*;
040: import java.lang.reflect.Constructor;
041: import java.util.*;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * Server home container for a stateless session bean
047: */
048: public class StatelessServer extends SessionServer {
049: protected static Logger log = Logger
050: .getLogger(StatelessServer.class.getName());
051:
052: private StatelessContext _homeContext;
053: private StatelessProvider _remoteProvider;
054:
055: /**
056: * Creates a new stateless server.
057: *
058: * @param urlPrefix the url prefix for any request to the server
059: * @param allowJVMCall allows fast calls to the same JVM (with serialization)
060: * @param config the session configuration from the ejb.xml
061: */
062: public StatelessServer(EjbContainer ejbContainer) {
063: super (ejbContainer);
064: }
065:
066: @Override
067: protected String getType() {
068: return "stateless:";
069: }
070:
071: /**
072: * Returns the JNDI proxy object to create instances of the
073: * local interface.
074: */
075: @Override
076: public Object getLocalProxy(Class api) {
077: StatelessProvider provider = getStatelessContext().getProvider(
078: api);
079:
080: return new StatelessProviderProxy(provider);
081: }
082:
083: /**
084: * Returns the object implementation
085: */
086: @Override
087: public Object getLocalObject(Class api) {
088: return getStatelessContext().getProvider(api);
089: }
090:
091: protected ComponentImpl createSessionComponent(Class api) {
092: StatelessProvider provider = getStatelessContext().getProvider(
093: api);
094:
095: return new StatelessComponent(provider, api);
096: }
097:
098: /**
099: * Returns the ejb home.
100: */
101: @Override
102: public EJBHome getEJBHome() {
103: return _remoteHomeView;
104: }
105:
106: /**
107: * Returns the 3.0 remote stub for the container
108: */
109: public Object getRemoteObject() {
110: if (_remoteProvider != null)
111: return _remoteProvider.__caucho_get();
112: else
113: return null;
114: }
115:
116: /**
117: * Returns the remote stub for the container
118: */
119: @Override
120: public Object getRemoteObject(Class api, String protocol) {
121: if (api == null)
122: return null;
123:
124: StatelessProvider provider = getStatelessContext().getProvider(
125: api);
126:
127: if (provider != null) {
128: Object result = provider.__caucho_get();
129:
130: return result;
131: } else {
132: log.fine(this + " unknown api " + api.getName());
133: return null;
134: }
135: }
136:
137: /**
138: * Returns the remote object.
139: */
140: @Override
141: public Object getRemoteObject(Object key) {
142: if (_remoteProvider != null)
143: return _remoteProvider.__caucho_get();
144: else
145: return null;
146: }
147:
148: @Override
149: public void init() throws Exception {
150: super .init();
151:
152: ArrayList<Class> remoteApiList = getRemoteApiList();
153:
154: if (remoteApiList != null && remoteApiList.size() > 0) {
155: Class api = remoteApiList.get(0);
156:
157: _remoteProvider = getStatelessContext().getProvider(api);
158: }
159: }
160:
161: /**
162: * Finds the remote bean by its key.
163: *
164: * @param key the remote key
165: *
166: * @return the remote interface of the entity.
167: */
168: @Override
169: public EJBObject getEJBObject(Object key) throws FinderException {
170: return getStatelessContext().getEJBObject();
171: }
172:
173: public AbstractContext getContext() {
174: return getStatelessContext();
175: }
176:
177: @Override
178: public AbstractContext getContext(Object key, boolean forceLoad) {
179: return getStatelessContext();
180: }
181:
182: @Override
183: public AbstractSessionContext getSessionContext() {
184: return getStatelessContext();
185: }
186:
187: private StatelessContext getStatelessContext() {
188: synchronized (this ) {
189: if (_homeContext == null) {
190: try {
191: Class[] param = new Class[] { StatelessServer.class };
192: Constructor cons = _contextImplClass
193: .getConstructor(param);
194:
195: _homeContext = (StatelessContext) cons
196: .newInstance(this );
197: } catch (Exception e) {
198: throw new EJBExceptionWrapper(e);
199: }
200: }
201: }
202:
203: return _homeContext;
204: }
205:
206: /**
207: * Returns the object serialization handle for the given api
208: */
209: Object getObjectHandle(StatelessObject obj, Class api) {
210: ComponentImpl comp = getComponent(api);
211:
212: // XXX: remote handle differently
213: if (comp != null)
214: return comp.getHandle();
215: else
216: return new ObjectSkeletonWrapper(obj.getHandle());
217: }
218:
219: /**
220: * Creates a handle for a new session.
221: */
222: AbstractHandle createHandle(AbstractContext context) {
223: String key = createSessionKey(context);
224:
225: return getHandleEncoder().createHandle(key);
226: }
227:
228: /**
229: * Creates a handle for a new session.
230: */
231: /*
232: JVMObject createEJBObject(Object primaryKey)
233: {
234: try {
235: JVMObject obj = (JVMObject) _remoteStubClass.newInstance();
236: obj._init(this, createSessionKey(null));
237:
238: return obj;
239: } catch (Exception e) {
240: throw new EJBExceptionWrapper(e);
241: }
242: }
243: */
244:
245: /**
246: * Creates a handle for a new session.
247: */
248: String createSessionKey(AbstractContext context) {
249: return "::ejb:stateless";
250: }
251:
252: /**
253: * Cleans up the entity server nicely.
254: */
255: @Override
256: public void destroy() {
257: if (_homeContext != null) {
258: try {
259: _homeContext.destroy();
260: } catch (Throwable e) {
261: log.log(Level.WARNING, e.toString(), e);
262: }
263: }
264:
265: super.destroy();
266: }
267: }
|