KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > ObjectRef


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop;
19
20 import java.util.Hashtable JavaDoc;
21
22 import org.apache.geronimo.interop.rmi.iiop.client.ClientNamingContext;
23 import org.apache.geronimo.interop.rmi.iiop.client.Connection;
24 import org.apache.geronimo.interop.security.SimpleSubject;
25 import org.apache.geronimo.interop.util.Base16Binary;
26 import org.apache.geronimo.interop.util.FutureObject;
27 import org.apache.geronimo.interop.util.StringUtil;
28 import org.apache.geronimo.interop.util.ThreadContext;
29 import org.apache.geronimo.interop.util.UTF8;
30 import org.apache.geronimo.interop.util.UnsignedShort;
31 import org.omg.IIOP.ProfileBody_1_1;
32 import org.omg.IIOP.ProfileBody_1_1Helper;
33 import org.omg.IIOP.Version;
34 import org.omg.IOP.IOR JavaDoc;
35 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc;
36 import org.omg.IOP.TaggedComponent JavaDoc;
37 import org.omg.IOP.TaggedProfile JavaDoc;
38
39 public class ObjectRef extends CorbaObject
40 {
41
42     public static ObjectRef _getInstance()
43     {
44         return new ObjectRef();
45     }
46
47     // -----------------------------------------------------------------------
48
// public data
49
// -----------------------------------------------------------------------
50

51     public static final int IIOP_VERSION_1_0 = 0;
52     public static final int IIOP_VERSION_1_1 = 1;
53     public static final int IIOP_VERSION_1_2 = 2;
54
55     // -----------------------------------------------------------------------
56
// private data
57
// -----------------------------------------------------------------------
58

59     private static FutureObject _defaultNamingContext = new FutureObject()
60     {
61         public Object JavaDoc evaluate()
62         {
63             Hashtable JavaDoc env = new Hashtable JavaDoc();
64             return ClientNamingContext.getInstance(env);
65         }
66     };
67
68     private static Version VERSION_1_1 = new Version((byte)1, (byte)1);
69
70     private static Version VERSION_1_2 = new Version((byte)1, (byte)2);
71
72     private static TaggedComponent JavaDoc[] NO_PROFILE_COMPONENTS = {};
73
74     private static TaggedProfile JavaDoc AUTOMATIC_FAILOVER_PROFILE;
75
76
77     private int _iiopVersion = IIOP_VERSION_1_2;
78
79     private IOR JavaDoc _ior;
80
81     private ClientNamingContext _namingContext;
82
83     private ObjectRef _forwardingAddress;
84
85     private String JavaDoc _repositoryID; // CORBA repository ID e.g. "RMI:xyz:0000000000000000"
86

87     private int _protocol = Protocol.IIOP;
88
89     private String JavaDoc _endpoint;
90
91     private String JavaDoc _host;
92
93     private int _port = -1;
94
95     public byte[] _objectKey;
96
97     public byte[] _objectState;
98
99     public long _objectVersion;
100
101     // public methods
102

103    public ObjectRef()
104    {
105    }
106
107     public Connection $connect()
108     {
109         if(_forwardingAddress != null)
110         {
111             return _forwardingAddress.$connect();
112         }
113         try
114         {
115             Connection conn = $getNamingContext().getConnectionPool().get(_protocol, $getEndpoint(), this);
116             conn.beforeInvoke();
117             return conn;
118         }
119         catch (RuntimeException JavaDoc ex)
120         {
121             throw ex;
122         }
123     }
124
125     public ObjectRef $getForwardingAddress()
126     {
127         return _forwardingAddress;
128     }
129
130     public void $setForwardingAddress(ObjectRef ref)
131     {
132         _forwardingAddress = ref;
133     }
134
135     public int $getIiopVersion()
136     {
137         return _iiopVersion;
138     }
139
140     public void $setIiopVersion(int version)
141     {
142         _iiopVersion = version;
143     }
144
145     public String JavaDoc $getID()
146     {
147         if (_repositoryID == null)
148         {
149             return "";
150         }
151         else
152         {
153             return _repositoryID;
154         }
155     }
156
157     public void $setID(String JavaDoc id)
158     {
159         _repositoryID = id;
160     }
161
162     public IOR JavaDoc $getIOR()
163     {
164         if (_ior == null)
165         {
166             ProfileBody_1_1 profileBody = new ProfileBody_1_1();
167             profileBody.iiop_version = _iiopVersion == IIOP_VERSION_1_1
168                 ? VERSION_1_1 : VERSION_1_2;
169             if (_host == null || _host.length() == 0)
170             {
171                 profileBody.host = ThreadContext.getDefaultRmiHost();
172             }
173             else
174             {
175                 profileBody.host = _host;
176             }
177             if (_port == -1)
178             {
179                 profileBody.port = (short)ThreadContext.getDefaultRmiPort();
180             }
181             else
182             {
183                 profileBody.port = (short)_port;
184             }
185             profileBody.object_key = _objectKey;
186             // TODO: if protocol using SSL, set port to 0 and set components
187
profileBody.components = NO_PROFILE_COMPONENTS;
188
189             TaggedProfile JavaDoc profile = new TaggedProfile JavaDoc();
190             profile.tag = TAG_INTERNET_IOP.value;
191             CdrOutputStream output = CdrOutputStream.getInstanceForEncapsulation();
192             ProfileBody_1_1Helper.write(output, profileBody);
193             profile.profile_data = output.getEncapsulation();
194
195             IOR JavaDoc ior = new IOR JavaDoc();
196             ior.type_id = $getID();
197             ior.profiles = new TaggedProfile JavaDoc[] { profile };
198             return ior;
199         }
200         return _ior;
201     }
202
203     public void $setIOR(IOR JavaDoc ior)
204     {
205         _ior = ior;
206         _endpoint = null;
207         _objectKey = null;
208         $getObjectKey(); // set _protocol, _host, _port, _objectKey
209
}
210
211     public ClientNamingContext $getNamingContext()
212     {
213         if (_namingContext == null)
214         {
215             _namingContext = (ClientNamingContext)_defaultNamingContext.getValue();
216         }
217         return _namingContext;
218     }
219
220     public void $setNamingContext(ClientNamingContext namingContext)
221     {
222         _namingContext = namingContext;
223     }
224
225     public int $getProtocol()
226     {
227         if (_objectKey == null)
228         {
229             $getObjectKey(); // to set _protocol
230
}
231         return _protocol;
232     }
233
234     public void $setProtocol(int protocol)
235     {
236         _protocol = protocol;
237         _ior = null;
238     }
239
240     public String JavaDoc $getHost()
241     {
242         if (_objectKey == null)
243         {
244             $getObjectKey(); // to set _host
245
}
246         return _host;
247     }
248
249     public void $setHost(String JavaDoc host)
250     {
251         _host = host;
252         _endpoint = null;
253         _ior = null;
254     }
255
256     public int $getPort()
257     {
258         if (_objectKey == null)
259         {
260             $getObjectKey(); // to set _port
261
}
262         return _port;
263     }
264
265     public void $setPort(int port)
266     {
267         _port = port;
268         _endpoint = null;
269         _ior = null;
270     }
271
272     public String JavaDoc $getEndpoint()
273     {
274         if (_endpoint == null)
275         {
276             _endpoint = _host + ":" + _port;
277         }
278         return _endpoint;
279     }
280
281     public byte[] $getObjectKey()
282     {
283         if (_objectKey == null)
284         {
285             if (_ior == null)
286             {
287                 throw new IllegalStateException JavaDoc("$getObjectKey: _ior == null && _objectKey = null");
288             }
289             int n = _ior.profiles.length;
290             for (int i = 0; i < n; i++)
291             {
292                 TaggedProfile JavaDoc profile = _ior.profiles[i];
293                 if (profile.tag == TAG_INTERNET_IOP.value)
294                 {
295                     ProfileBody_1_1 profileBody;
296                     CdrInputStream input = CdrInputStream.getInstanceForEncapsulation();
297                     input.setEncapsulation(profile.profile_data);
298                     profileBody = ProfileBody_1_1Helper.read(input);
299                     input.recycle();
300
301                     _protocol = Protocol.IIOP; // TODO: IIOP/SSL etc.
302
_iiopVersion = profileBody.iiop_version.minor;
303                     _host = profileBody.host;
304                     _port = UnsignedShort.intValue(profileBody.port);
305                     _objectKey = profileBody.object_key;
306                 }
307             }
308         }
309         return _objectKey;
310     }
311
312     public String JavaDoc $getObjectKeyString()
313     {
314         return UTF8.toString($getObjectKey());
315     }
316
317     public String JavaDoc $getObjectName()
318     {
319         byte[] objectKey = $getObjectKey();
320         int keyLength = objectKey.length;
321
322         for (int colonPos = 0; colonPos < keyLength; colonPos++)
323         {
324             if (objectKey[colonPos] == ':')
325             {
326                 return UTF8.toString(objectKey, 0, colonPos);
327             }
328         }
329         return UTF8.toString(objectKey);
330     }
331
332     public void $setObjectKey(byte[] objectKey)
333     {
334         _objectKey = objectKey;
335         _ior = null;
336     }
337
338     public void $setObjectKey(String JavaDoc objectKey)
339     {
340         $setObjectKey(UTF8.fromString(objectKey));
341     }
342
343     public void $setObjectKey(String JavaDoc prefix, byte[] suffixBytes)
344     {
345         byte[] prefixBytes = UTF8.fromString(prefix);
346         int p = prefixBytes.length;
347         int s = suffixBytes.length;
348         byte[] objectKey = new byte[p + 1 + s];
349         System.arraycopy(prefixBytes, 0, objectKey, 0, p);
350         objectKey[p] = (byte)':';
351         System.arraycopy(suffixBytes, 0, objectKey, p + 1, s);
352         $setObjectKey(objectKey);
353     }
354
355     public void $setObjectKey(Class JavaDoc compClass)
356     {
357         SimpleSubject subject = SimpleSubject.getCurrent();
358         if (subject != null
359             && (subject.getFlags() & SimpleSubject.FLAG_SESSION_MANAGER) != 0)
360         {
361             // Initialize for simple IDL interoperability.
362
ObjectKey objectKey = new ObjectKey();
363             objectKey.component = compClass.getName();
364             objectKey.username = subject.getUsername();
365             objectKey.password = subject.getPassword();
366             byte[] key = objectKey.encode();
367             key[0] = 'I';
368             _iiopVersion = IIOP_VERSION_1_1;
369             $setObjectKey(key);
370         }
371         else
372         {
373             // Initialize for RMI-IIOP.
374
$setObjectKey(compClass.getName());
375         }
376     }
377
378     public Object JavaDoc $getRequestKey()
379     {
380         return null;
381     }
382
383     public String JavaDoc $getIORString()
384     {
385         IOR JavaDoc ior = $getIOR();
386         CdrOutputStream output = CdrOutputStream.getInstanceForEncapsulation();
387         output.setGiopVersion(GiopVersion.VERSION_1_0);
388         output.write_Object(this);
389         byte[] bytes = output.getEncapsulation();
390         String JavaDoc hex = Base16Binary.toString(bytes);
391         String JavaDoc iorString = "IOR:" + hex;
392         return iorString;
393     }
394
395     public void $setIORString(String JavaDoc iorString)
396     {
397         $setIOR($getObjectFromIOR(iorString).$getIOR());
398     }
399
400     public static ObjectRef $getObjectFromIOR(String JavaDoc iorString)
401     {
402         String JavaDoc hex = StringUtil.removePrefix(iorString, "IOR:");
403         byte[] bytes = Base16Binary.fromString(hex);
404         CdrInputStream input = CdrInputStream.getInstanceForEncapsulation();
405         input.setGiopVersion(GiopVersion.VERSION_1_0);
406         input.setEncapsulation(bytes);
407         ObjectRef object = (ObjectRef)input.read_Object();
408         return object;
409     }
410
411     public String JavaDoc toString()
412     {
413         return getClass().getName() + ":protocol=" + Protocol.getName($getProtocol())
414             + ":host=" + $getHost() + ":port=" + $getPort()
415             + ":key=" + Base16Binary.toString($getObjectKey());
416     }
417 }
418
Popular Tags