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.protocol;
031:
032: import com.caucho.ejb.AbstractServer;
033: import com.caucho.log.Log;
034: import com.caucho.util.L10N;
035: import com.caucho.util.LruCache;
036:
037: import javax.ejb.EJBHome;
038: import javax.ejb.EJBLocalHome;
039: import javax.ejb.EJBObject;
040: import java.util.logging.Logger;
041:
042: /**
043: * Container for EJB stubs.
044: */
045: public abstract class ClientContainer {
046: protected static L10N L = new L10N(ClientContainer.class);
047: protected static Logger log = Logger
048: .getLogger(ClientContainer.class.getName());
049:
050: static final String CLIENT_KEY = "caucho.ejb.client";
051:
052: // the unique server id
053: String _serverId;
054: // the home stub
055: EJBLocalHome _localHomeStub;
056: // the home stub
057: EJBHome _remoteHomeStub;
058: // cache of stubs for remote objects
059: LruCache<String, EJBObject> _stubMap = new LruCache<String, EJBObject>(
060: 1024);
061: // handler encoder
062: protected HandleEncoder _handleEncoder;
063:
064: /**
065: * Creates a new client container
066: *
067: * @param serverId the server's unique ID
068: */
069: protected ClientContainer(String serverId) {
070: _serverId = serverId;
071: }
072:
073: /**
074: * Returns the bean's url prefix
075: */
076: protected String getServerId() {
077: return _serverId;
078: }
079:
080: /**
081: * Returns the home stub for the container.
082: *
083: * @return the bean's home stub
084: */
085: public EJBHome getHomeStub() throws Exception {
086: if (_remoteHomeStub != null)
087: return _remoteHomeStub;
088:
089: _remoteHomeStub = createHomeStub();
090:
091: return _remoteHomeStub;
092: }
093:
094: /**
095: * Returns the home stub for the container.
096: *
097: * @return the bean's home stub
098: */
099: public Object getEJBLocalHome() {
100: AbstractServer jvmServer = EjbProtocolManager
101: .getJVMServer(_serverId);
102:
103: return jvmServer.getLocalObject(jvmServer.getLocalHomeClass());
104: }
105:
106: /**
107: * Creates the home stub for the container.
108: *
109: * @return the bean's home stub
110: */
111: abstract protected EJBHome createHomeStub() throws Exception;
112:
113: public HandleEncoder getHandleEncoder(AbstractHandle handle) {
114: if (_handleEncoder == null)
115: _handleEncoder = new HandleEncoder("foo");
116:
117: return _handleEncoder;
118: }
119:
120: /**
121: * Returns the object key from a handle.
122: */
123: public Class getPrimaryKeyClass() {
124: return String.class;
125: }
126:
127: /**
128: * Returns a remote stub for the given handle
129: *
130: * @param handle the handle for the remote bean
131: *
132: * @return the bean's remote stub
133: */
134: public EJBObject getObjectStub(String url) throws Exception {
135: EJBObject stub = _stubMap.get(url);
136: if (stub != null)
137: return stub;
138:
139: stub = createObjectStub(url);
140:
141: _stubMap.put(url, stub);
142:
143: return stub;
144: }
145:
146: /**
147: * Creates the stub for the remote object for the given Handle.
148: *
149: * @param handle the handle for the remote object
150: *
151: * @return the bean's remote stub
152: */
153: abstract protected EJBObject createObjectStub(String url)
154: throws Exception;
155:
156: /**
157: * Returns a printable version of the client container
158: */
159: public String toString() {
160: return "ClientContainer[" + _serverId + "]";
161: }
162: }
|