KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > protocol > ClientContainer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.protocol;
30
31 import com.caucho.ejb.AbstractServer;
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34 import com.caucho.util.LruCache;
35
36 import javax.ejb.EJBHome JavaDoc;
37 import javax.ejb.EJBLocalHome JavaDoc;
38 import javax.ejb.EJBObject JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Container for EJB stubs.
43  */

44 public abstract class ClientContainer {
45   protected static L10N L = new L10N(ClientContainer.class);
46   protected static Logger JavaDoc log = Log.open(ClientContainer.class);
47   
48   static final String JavaDoc CLIENT_KEY = "caucho.ejb.client";
49
50   // the unique server id
51
String JavaDoc _serverId;
52   // the home stub
53
EJBLocalHome JavaDoc _localHomeStub;
54   // the home stub
55
EJBHome JavaDoc _remoteHomeStub;
56   // cache of stubs for remote objects
57
LruCache<String JavaDoc,EJBObject JavaDoc> _stubMap = new LruCache<String JavaDoc,EJBObject JavaDoc>(1024);
58   // handler encoder
59
protected HandleEncoder _handleEncoder;
60
61   /**
62    * Creates a new client container
63    *
64    * @param serverId the server's unique ID
65    */

66   protected ClientContainer(String JavaDoc serverId)
67   {
68     _serverId = serverId;
69   }
70   
71   /**
72    * Returns the bean's url prefix
73    */

74   protected String JavaDoc getServerId()
75   {
76     return _serverId;
77   }
78
79   /**
80    * Returns the home stub for the container.
81    *
82    * @return the bean's home stub
83    */

84   public EJBHome JavaDoc getHomeStub()
85     throws Exception JavaDoc
86   {
87     if (_remoteHomeStub != null)
88       return _remoteHomeStub;
89
90     _remoteHomeStub = createHomeStub();
91
92     return _remoteHomeStub;
93   }
94
95   /**
96    * Returns the home stub for the container.
97    *
98    * @return the bean's home stub
99    */

100   public Object JavaDoc getEJBLocalHome()
101   {
102     AbstractServer jvmServer = EjbProtocolManager.getJVMServer(_serverId);
103
104     return jvmServer.getClientLocalHome();
105   }
106
107   /**
108    * Creates the home stub for the container.
109    *
110    * @return the bean's home stub
111    */

112   abstract protected EJBHome JavaDoc createHomeStub()
113     throws Exception JavaDoc;
114
115   public HandleEncoder getHandleEncoder(AbstractHandle handle)
116   {
117     if (_handleEncoder == null)
118       _handleEncoder = new HandleEncoder("foo");
119
120     return _handleEncoder;
121   }
122   /**
123    * Returns the object key from a handle.
124    */

125   public Class JavaDoc getPrimaryKeyClass()
126   {
127     return String JavaDoc.class;
128   }
129
130   /**
131    * Returns a remote stub for the given handle
132    *
133    * @param handle the handle for the remote bean
134    *
135    * @return the bean's remote stub
136    */

137   public EJBObject JavaDoc getObjectStub(String JavaDoc url)
138     throws Exception JavaDoc
139   {
140     EJBObject JavaDoc stub = _stubMap.get(url);
141     if (stub != null)
142       return stub;
143
144     stub = createObjectStub(url);
145
146     _stubMap.put(url, stub);
147
148     return stub;
149   }
150
151   /**
152    * Creates the stub for the remote object for the given Handle.
153    *
154    * @param handle the handle for the remote object
155    *
156    * @return the bean's remote stub
157    */

158   abstract protected EJBObject JavaDoc createObjectStub(String JavaDoc url)
159     throws Exception JavaDoc;
160
161   /**
162    * Returns a printable version of the client container
163    */

164   public String JavaDoc toString()
165   {
166     return "ClientContainer[" + _serverId + "]";
167   }
168 }
169
Popular Tags