KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > ObjectRef


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop;
20
21 import gcc.*;
22 import gcc.rmi.iiop.client.*;
23 import gcc.org.omg.IOP.*;
24 import gcc.org.omg.IIOP.*;
25 import gcc.security.*;
26 import gcc.util.*;
27 import java.util.*;
28
29 public class ObjectRef implements org.omg.CORBA.Object
30 {
31     // -----------------------------------------------------------------------
32
// public data
33
// -----------------------------------------------------------------------
34

35     public static final int IIOP_VERSION_1_0 = 0;
36     public static final int IIOP_VERSION_1_1 = 1;
37     public static final int IIOP_VERSION_1_2 = 2;
38
39     // -----------------------------------------------------------------------
40
// private data
41
// -----------------------------------------------------------------------
42

43     private static FutureObject _defaultNamingContext = new FutureObject()
44     {
45         public Object evaluate()
46         {
47             Hashtable env = new Hashtable();
48             return ClientNamingContext.getInstance(env);
49         }
50     };
51
52     private static Version VERSION_1_1 = new Version((byte)1, (byte)1);
53
54     private static Version VERSION_1_2 = new Version((byte)1, (byte)2);
55
56     private static TaggedComponent[] NO_PROFILE_COMPONENTS = {};
57
58     private int _iiopVersion = IIOP_VERSION_1_2;
59
60     private gcc.org.omg.IOP.IOR _ior;
61
62     private ClientNamingContext _namingContext;
63
64     private String _repositoryID; // CORBA repository ID e.g. "RMI:xyz:0000000000000000"
65

66     private int _protocol;
67
68     private String _endpoint;
69
70     private String _host;
71
72     private int _port = -1;
73
74     public byte[] _objectKey;
75
76     public byte[] _objectState;
77
78     public long _objectVersion;
79
80     // public methods
81

82     public ObjectRef()
83     {
84     }
85
86     public Connection $connect()
87     {
88         Connection conn = $getNamingContext().getConnectionPool().get(_protocol, $getEndpoint(), this);
89         conn.beforeInvoke();
90         return conn;
91     }
92
93     public int $getIiopVersion()
94     {
95         return _iiopVersion;
96     }
97
98     public void $setIiopVersion(int version)
99     {
100         _iiopVersion = version;
101     }
102
103     public String $getID()
104     {
105         if (_repositoryID == null)
106         {
107             return "";
108         }
109         else
110         {
111             return _repositoryID;
112         }
113     }
114
115     public void $setID(String id)
116     {
117         _repositoryID = id;
118     }
119
120     public gcc.org.omg.IOP.IOR $getIOR()
121     {
122         if (_ior == null)
123         {
124             ProfileBody_1_1 profileBody = new ProfileBody_1_1();
125             profileBody.iiop_version = _iiopVersion == IIOP_VERSION_1_1
126                 ? VERSION_1_1 : VERSION_1_2;
127             if (_host == null || _host.length() == 0)
128             {
129                 profileBody.host = ThreadContext.getDefaultRmiHost();
130             }
131             else
132             {
133                 profileBody.host = _host;
134             }
135             if (_port == -1)
136             {
137                 profileBody.port = (short)ThreadContext.getDefaultRmiPort();
138             }
139             else
140             {
141                 profileBody.port = (short)_port;
142             }
143             profileBody.object_key = _objectKey;
144             // TODO: if protocol using SSL, set port to 0 and set components
145
profileBody.components = NO_PROFILE_COMPONENTS;
146
147             TaggedProfile profile = new TaggedProfile();
148             profile.tag = TAG_INTERNET_IOP.value;
149             CdrOutputStream output = CdrOutputStream.getInstanceForEncapsulation();
150             ProfileBody_1_1Helper.write(output, profileBody);
151             profile.profile_data = output.getEncapsulation();
152
153             IOR ior = new IOR();
154             ior.type_id = $getID();
155             ior.profiles = new TaggedProfile[] { profile };
156             return ior;
157         }
158         return _ior;
159     }
160
161     public void $setIOR(gcc.org.omg.IOP.IOR ior)
162     {
163         _ior = ior;
164         _endpoint = null;
165         _objectKey = null;
166         $getObjectKey(); // set _protocol, _host, _port, _objectKey
167
}
168
169     public ClientNamingContext $getNamingContext()
170     {
171         if (_namingContext == null)
172         {
173             _namingContext = (ClientNamingContext)_defaultNamingContext.getValue();
174         }
175         return _namingContext;
176     }
177
178     public void $setNamingContext(ClientNamingContext namingContext)
179     {
180         _namingContext = namingContext;
181     }
182
183     public int $getProtocol()
184     {
185         if (_objectKey == null)
186         {
187             $getObjectKey(); // to set _protocol
188
}
189         return _protocol;
190     }
191
192     public void $setProtocol(int protocol)
193     {
194         _protocol = protocol;
195         _ior = null;
196     }
197
198     public String $getHost()
199     {
200         if (_objectKey == null)
201         {
202             $getObjectKey(); // to set _host
203
}
204         return _host;
205     }
206
207     public void $setHost(String host)
208     {
209         _host = host;
210         _endpoint = null;
211         _ior = null;
212     }
213
214     public int $getPort()
215     {
216         if (_objectKey == null)
217         {
218             $getObjectKey(); // to set _port
219
}
220         return _port;
221     }
222
223     public void $setPort(int port)
224     {
225         _port = port;
226         _endpoint = null;
227         _ior = null;
228     }
229
230     public String $getEndpoint()
231     {
232         if (_endpoint == null)
233         {
234             _endpoint = _host + ":" + _port;
235         }
236         return _endpoint;
237     }
238
239     public byte[] $getObjectKey()
240     {
241         if (_objectKey == null)
242         {
243             if (_ior == null)
244             {
245                 throw new IllegalStateException("$getObjectKey: _ior == null && _objectKey = null");
246             }
247             TaggedProfile profile = _ior.profiles[0];
248             if (profile.tag == TAG_INTERNET_IOP.value
249                 && _ior.profiles.length == 1)
250             {
251                 ProfileBody_1_1 profileBody;
252                 CdrInputStream input = CdrInputStream.getInstanceForEncapsulation();
253                 input.setEncapsulation(profile.profile_data);
254                 profileBody = ProfileBody_1_1Helper.read(input);
255                 input.recycle();
256
257                 _protocol = Protocol.IIOP; // TODO: IIOP/SSL etc.
258
_iiopVersion = profileBody.iiop_version.minor;
259                 _host = profileBody.host;
260                 _port = UnsignedShort.intValue(profileBody.port);
261                 _objectKey = profileBody.object_key;
262             }
263             else
264             {
265                 throw new SystemException("TODO");
266             }
267         }
268         return _objectKey;
269     }
270
271     public String $getObjectKeyString()
272     {
273         return UTF8.toString($getObjectKey());
274     }
275
276     public void $setObjectKey(byte[] objectKey)
277     {
278         _objectKey = objectKey;
279         _ior = null;
280     }
281
282     public void $setObjectKey(String objectKey)
283     {
284         $setObjectKey(UTF8.fromString(objectKey));
285     }
286
287     public void $setObjectKey(String prefix, byte[] suffixBytes)
288     {
289         byte[] prefixBytes = UTF8.fromString(prefix);
290         int p = prefixBytes.length;
291         int s = suffixBytes.length;
292         byte[] objectKey = new byte[p + 1 + s];
293         System.arraycopy(prefixBytes, 0, objectKey, 0, p);
294         objectKey[p] = (byte)':';
295         System.arraycopy(suffixBytes, 0, objectKey, p + 1, s);
296         $setObjectKey(objectKey);
297     }
298
299     public void $setObjectKey(Class compClass)
300     {
301         SimpleSubject subject = SimpleSubject.getCurrent();
302         if (subject != null
303             && (subject.getFlags() & SimpleSubject.FLAG_SESSION_MANAGER) != 0)
304         {
305             // Initialize for simple IDL interoperability.
306
ObjectKey objectKey = new ObjectKey();
307             objectKey.component = compClass.getName();
308             objectKey.username = subject.getUsername();
309             objectKey.password = subject.getPassword();
310             byte[] key = objectKey.encode();
311             key[0] = 'I';
312             _iiopVersion = IIOP_VERSION_1_1;
313             $setObjectKey(key);
314         }
315         else
316         {
317             // Initialize for RMI-IIOP.
318
$setObjectKey(compClass.getName());
319         }
320     }
321
322     public String[] _ids()
323     {
324         String[] ids =
325         {
326             "" //_type
327
}
328         ;
329         return ids;
330     }
331
332     public Object $getRequestKey()
333     {
334         //
335
//if (_automaticFailover)
336
//{
337
// return $getNamingContext().getRequestKey();
338
//}
339
//else
340
//{
341
return null;
342         //}
343
}
344
345     public boolean _is_a( String id )
346     {
347         return false;
348     }
349
350     // -----------------------------------------------------------------------
351
// unimplemented public methods (only required for full CORBA ORB)
352
// -----------------------------------------------------------------------
353

354     public org.omg.CORBA.Request _create_request(org.omg.CORBA.Context p1, String p2, org.omg.CORBA.NVList p3, org.omg.CORBA.NamedValue p4)
355     {
356         throw new org.omg.CORBA.BAD_OPERATION("_create_request");
357     }
358
359     public org.omg.CORBA.Request _create_request(org.omg.CORBA.Context p1, String p2, org.omg.CORBA.NVList p3, org.omg.CORBA.NamedValue p4, org.omg.CORBA.ExceptionList p5, org.omg.CORBA.ContextList p6)
360     {
361         throw new org.omg.CORBA.BAD_OPERATION("_create_request");
362     }
363
364     public org.omg.CORBA.Object _duplicate()
365     {
366         throw new org.omg.CORBA.BAD_OPERATION("_duplicate");
367     }
368
369     public org.omg.CORBA.DomainManager[] _get_domain_managers()
370     {
371         throw new org.omg.CORBA.BAD_OPERATION("_get_domain_manager");
372     }
373
374     public org.omg.CORBA.Object _get_interface_def()
375     {
376         throw new org.omg.CORBA.BAD_OPERATION("_get_interface_def");
377     }
378
379     public org.omg.CORBA.Policy _get_policy(int p1)
380     {
381         throw new org.omg.CORBA.BAD_OPERATION("_get_policy");
382     }
383
384     public int _hash(int p1)
385     {
386         throw new org.omg.CORBA.BAD_OPERATION("_hash");
387     }
388
389     public boolean _is_equivalent(org.omg.CORBA.Object p1)
390     {
391         throw new org.omg.CORBA.BAD_OPERATION("_is_equivalent");
392     }
393
394     public boolean _non_existent()
395     {
396         throw new org.omg.CORBA.BAD_OPERATION("_non_existent");
397     }
398
399     public void _release()
400     {
401         throw new org.omg.CORBA.BAD_OPERATION("_release");
402     }
403
404     public org.omg.CORBA.Request _request(String p1)
405     {
406         throw new org.omg.CORBA.BAD_OPERATION("_request");
407     }
408
409     public org.omg.CORBA.Object _set_policy_override(org.omg.CORBA.Policy[] p1, org.omg.CORBA.SetOverrideType p2)
410     {
411         throw new org.omg.CORBA.BAD_OPERATION("_set_policy_override");
412     }
413 }
414
Popular Tags