KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 package com.caucho.ejb.protocol;
28
29 import com.caucho.ejb.AbstractContext;
30 import com.caucho.ejb.AbstractEJBObject;
31 import com.caucho.ejb.AbstractServer;
32 import com.caucho.ejb.NoSuchObjectExceptionWrapper;
33 import com.caucho.ejb.RemoteExceptionWrapper;
34 import com.caucho.util.L10N;
35
36 import javax.ejb.EJBHome JavaDoc;
37 import javax.ejb.EJBObject JavaDoc;
38 import javax.ejb.FinderException JavaDoc;
39 import javax.ejb.Handle JavaDoc;
40 import javax.ejb.RemoveException JavaDoc;
41 import java.io.ObjectStreamException JavaDoc;
42 import java.io.Serializable JavaDoc;
43 import java.rmi.NoSuchObjectException JavaDoc;
44 import java.rmi.RemoteException JavaDoc;
45
46 /**
47  * Interface for stubs inside the same JVM.
48  */

49 public abstract class JVMObject extends AbstractEJBObject
50   implements EJBObject, Serializable JavaDoc {
51   protected static final L10N L = new L10N(JVMObject.class);
52
53   protected Object JavaDoc _primaryKey;
54   protected ObjectSkeletonWrapper _skeletonWrapper;
55
56   protected AbstractServer _server;
57   protected Object JavaDoc _object;
58
59   /**
60    * Initialization code.
61    */

62   public void _init(AbstractServer server, Object JavaDoc primaryKey)
63   {
64     if (primaryKey == null)
65       throw new NullPointerException JavaDoc();
66     
67     _server = server;
68     _primaryKey = primaryKey;
69   }
70   
71   /**
72    * Returns the implemented class.
73    */

74   public Class JavaDoc getAPIClass()
75   {
76     return getServer().getRemoteObjectClass();
77   }
78   
79   /**
80    * Returns the URL for the given protocol.
81    */

82   public String JavaDoc getURL(String JavaDoc protocol)
83   {
84     HandleEncoder encoder = getServer().getHandleEncoder(protocol);
85
86     return encoder.getURL(getServer().encodeId(_primaryKey));
87   }
88
89   /**
90    * Returns the serializable handle for the remote object
91    */

92   public Handle JavaDoc getHandle()
93     throws RemoteException JavaDoc
94   {
95     HandleEncoder encoder = getServer().getHandleEncoder();
96
97     return encoder.createHandle(getServer().encodeId(_primaryKey));
98   }
99
100   /**
101    * Returns the EJBHome stub for the remote object.
102    */

103   public EJBHome JavaDoc getEJBHome()
104     throws RemoteException JavaDoc
105   {
106     try {
107       return getServer().getEJBHome();
108     } catch (Exception JavaDoc e) {
109       throw new RemoteExceptionWrapper(e);
110     }
111   }
112
113   /**
114    * Returns the underlying object
115    */

116   protected Object JavaDoc _caucho_getObject()
117     throws RemoteException JavaDoc
118   {
119     if (_server == null || _server.isDead() || _object == null) {
120       try {
121         AbstractContext context;
122         context = getServer().getContext(_primaryKey);
123
124         if (context != null)
125           _object = context.getRemoteView();
126       } catch (FinderException JavaDoc e) {
127         throw new NoSuchObjectExceptionWrapper(e);
128       }
129
130       if (_object == null)
131         throw new NoSuchObjectException JavaDoc(L.l("`{0}' is not a valid bean. The bean may have been deleted or the server moved.", _primaryKey));
132
133       _caucho_init_methods(_object.getClass());
134     }
135
136     return _object;
137   }
138
139   protected void _caucho_init_methods(Class JavaDoc cl)
140   {
141   }
142
143   /**
144    * Returns the server's class loader
145    */

146   protected ClassLoader JavaDoc _caucho_getClassLoader()
147     throws RemoteException JavaDoc
148   {
149     ClassLoader JavaDoc loader = getServer().getClassLoader();
150
151     return loader;
152   }
153
154   /**
155    * Returns the currently active server. The server can change over time
156    * because of class reloading.
157    */

158   private AbstractServer getServer()
159   {
160     if (_server.isDead()) {
161       String JavaDoc serverId = _server.getHandleServerId();
162       _object = null;
163       _server = EjbProtocolManager.getJVMServer(serverId);
164     }
165
166     return _server;
167   }
168
169   /**
170    * Returns true if the test object is identical to this object.
171    *
172    * @param obj the object to test for identity
173    */

174   public boolean isIdentical(EJBObject obj)
175     throws RemoteException JavaDoc
176   {
177     return getHandle().equals(obj.getHandle());
178   }
179
180   public void remove()
181     throws RemoveException JavaDoc, RemoteException JavaDoc
182   {
183   }
184
185   public Object JavaDoc getPrimaryKey()
186     throws RemoteException JavaDoc
187   {
188     return _primaryKey;
189   }
190
191   /**
192    * Serialize the HomeSkeletonWrapper in place of this object.
193    *
194    * @return the matching skeleton wrapper.
195    */

196   public Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
197   {
198     if (_skeletonWrapper == null) {
199       try {
200         _skeletonWrapper = new ObjectSkeletonWrapper(getHandle());
201       } catch (Exception JavaDoc e) {
202       }
203     }
204     
205     return _skeletonWrapper;
206   }
207
208   //
209
// Static utility methods.
210
//
211

212   /**
213    * Convert the object to an boolean.
214    */

215   public static boolean to_boolean(Object JavaDoc o)
216   {
217     if (o == null)
218       return false;
219     else
220       return ((Boolean JavaDoc) o).booleanValue();
221   }
222   
223   /**
224    * Convert the object to an byte.
225    */

226   public static byte to_byte(Object JavaDoc o)
227   {
228     if (o == null)
229       return 0;
230     else
231       return ((Byte JavaDoc) o).byteValue();
232   }
233   
234   /**
235    * Convert the object to an short.
236    */

237   public static short to_short(Object JavaDoc o)
238   {
239     if (o == null)
240       return 0;
241     else
242       return ((Short JavaDoc) o).shortValue();
243   }
244   
245   /**
246    * Convert the object to an char.
247    */

248   public static char to_char(Object JavaDoc o)
249   {
250     if (o == null)
251       return 0;
252     else
253       return ((Character JavaDoc) o).charValue();
254   }
255   
256   /**
257    * Convert the object to an int.
258    */

259   public static int to_int(Object JavaDoc o)
260   {
261     if (o == null)
262       return 0;
263     else
264       return ((Integer JavaDoc) o).intValue();
265   }
266   
267   /**
268    * Convert the object to an long.
269    */

270   public static long to_long(Object JavaDoc o)
271   {
272     if (o == null)
273       return 0;
274     else
275       return ((Long JavaDoc) o).longValue();
276   }
277   
278   /**
279    * Convert the object to an float.
280    */

281   public static float to_float(Object JavaDoc o)
282   {
283     if (o == null)
284       return 0;
285     else
286       return ((Float JavaDoc) o).floatValue();
287   }
288   
289   /**
290    * Convert the object to an double.
291    */

292   public static double to_double(Object JavaDoc o)
293   {
294     if (o == null)
295       return 0;
296     else
297       return ((Double JavaDoc) o).doubleValue();
298   }
299 }
300
Popular Tags