KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > iiop > IIOPProfile


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20 package org.jacorb.orb.iiop;
21
22 import java.util.*;
23
24 import org.apache.avalon.framework.configuration.*;
25 import org.apache.avalon.framework.logger.Logger;
26
27 import org.jacorb.orb.CDRInputStream;
28 import org.jacorb.orb.CDROutputStream;
29 import org.jacorb.orb.IIOPAddress;
30 import org.jacorb.orb.TaggedComponentList;
31
32 import org.omg.ETF.*;
33 import org.omg.IOP.*;
34 import org.omg.SSLIOP.*;
35 import org.omg.CSIIOP.*;
36
37 /**
38  * @author Andre Spiegel
39  * @version $Id: IIOPProfile.java,v 1.14 2004/10/21 14:50:43 francisco Exp $
40  */

41 public class IIOPProfile
42     extends org.jacorb.orb.etf.ProfileBase
43 {
44     private IIOPAddress primaryAddress = null;
45
46     private boolean dnsEnabled = false;
47     private Logger logger;
48     
49     public IIOPProfile()
50     {
51         super();
52     }
53     
54     public IIOPProfile(byte[] data)
55     {
56         initFromProfileData(data);
57     }
58
59     public IIOPProfile(IIOPAddress address, byte[] objectKey)
60     {
61         this.version = new org.omg.GIOP.Version((byte)1,(byte)2);
62         this.primaryAddress = address;
63         this.objectKey = objectKey;
64         this.components = new TaggedComponentList();
65     }
66
67     public IIOPProfile(IIOPAddress address, byte[] objectKey, int minor)
68     {
69         this.version = new org.omg.GIOP.Version((byte)1,(byte)minor);
70         this.primaryAddress = address;
71         this.objectKey = objectKey;
72         this.components = new TaggedComponentList();
73     }
74
75     /**
76      * Constructs an IIOPProfile from a corbaloc URL. Only to be used
77      * from the corbaloc parser.
78      */

79
80     public IIOPProfile(String JavaDoc corbaloc)
81     {
82         this.version = null;
83         this.primaryAddress = null;
84         this.objectKey = null;
85         this.components = null;
86         this.corbalocStr = corbaloc;
87     }
88
89     public void configure(Configuration configuration)
90         throws ConfigurationException
91     {
92         this.configuration = (org.jacorb.config.Configuration)configuration;
93         logger = this.configuration.getNamedLogger("jacorb.iiop.profile");
94         dnsEnabled = configuration.getAttribute("jacorb.dns.enable","off").equals("on");
95         if (this.primaryAddress != null)
96             this.primaryAddress.configure(configuration);
97
98         if (this.corbalocStr != null)
99         {
100             try
101             {
102                 this.decode_corbaloc(this.corbalocStr);
103             }
104             catch(Exception JavaDoc e)
105             {
106                 e.printStackTrace(); // debug
107
}
108         }
109     }
110
111
112
113     private void decode_corbaloc(String JavaDoc addr)
114     {
115         String JavaDoc host = "127.0.0.1"; //default to localhost
116
short port = 2809; // default IIOP port
117

118         int major = 1;
119         int minor = 2; // should this be 0? should it be configurable?
120

121         String JavaDoc errorstr =
122             "Illegal IIOP protocol format in object address format: " + addr;
123         int sep = addr.indexOf(':');
124         String JavaDoc protocol_identifier = "";
125         if( sep != 0)
126             protocol_identifier = addr.substring( 0,sep);
127         if( sep + 1 == addr.length())
128             throw new IllegalArgumentException JavaDoc(errorstr);
129         addr = addr.substring(sep + 1);
130
131         // decode optional version number
132
sep = addr.indexOf( '@' );
133         if( sep > -1)
134         {
135             String JavaDoc ver_str = addr.substring(0,sep);
136             addr = addr.substring(sep+1);
137             sep = ver_str.indexOf('.');
138             if( sep != -1 )
139             {
140                 try
141                 {
142                     major = Integer.parseInt(ver_str.substring(0,sep));
143                     minor = Integer.parseInt(ver_str.substring(sep+1));
144                 }
145                 catch( NumberFormatException JavaDoc nfe )
146                 {
147                     throw new IllegalArgumentException JavaDoc(errorstr);
148                 }
149             }
150         }
151         version = new org.omg.GIOP.Version((byte)major,(byte)minor);
152
153         sep = addr.indexOf(':');
154         if( sep != -1 )
155         {
156             try
157             {
158                 port =(short)Integer.parseInt(addr.substring(sep+1));
159                 host = addr.substring(0, sep);
160             }
161             catch( NumberFormatException JavaDoc ill )
162             {
163                 throw new IllegalArgumentException JavaDoc(errorstr);
164             }
165         }
166         primaryAddress = new IIOPAddress(host,port);
167         try
168         {
169             primaryAddress.configure(configuration);
170         }
171         catch( ConfigurationException ce)
172         {
173             if (logger.isWarnEnabled())
174                 logger.warn("ConfigurationException", ce );
175         }
176         decode_extensions(protocol_identifier.toLowerCase());
177     }
178
179     private void decode_extensions(String JavaDoc ident)
180     {
181         this.components = new TaggedComponentList();
182         if (ident.equals("ssliop"))
183         {
184             SSL ssl = new SSL();
185             ssl.port = (short)primaryAddress.getPort();
186             String JavaDoc propname =
187                 "jacorb.security.ssl.corbaloc_ssliop.supported_options";
188             ssl.target_supports = get_ssl_options(propname);
189             propname =
190                 "jacorb.security.ssl.corbaloc_ssliop.required_options";
191             ssl.target_requires = get_ssl_options(propname);
192
193             //create the tagged component containing the ssl struct
194
CDROutputStream out = new CDROutputStream();
195             out.beginEncapsulatedArray();
196             SSLHelper.write( out, ssl );
197
198             components.addComponent
199                 (new TaggedComponent( TAG_SSL_SEC_TRANS.value,
200                                       out.getBufferCopy() )
201                  );
202         }
203     }
204
205     private short get_ssl_options(String JavaDoc propname)
206     {
207         //For the time being, we only use EstablishTrustInTarget,
208
//because we don't handle any of the other options anyway.
209
// So this makes a reasonable default.
210

211         short value =
212             (short)configuration.getAttributeAsInteger(propname,EstablishTrustInTarget.value);
213         return value;
214     }
215
216     /**
217      * This function marshals the appropriate information for this
218      * transport into the tagged profile. ORBs will typically need
219      * to call the IOR interception points before calling marshal().
220      */

221 /* public void marshal(TaggedProfileHolder tagged_profile,
222                          TaggedComponentSeqHolder components)
223     {
224         TaggedComponent[] allComponents = null;
225         CDROutputStream profileDataStream = null;
226
227         if (components == null)
228         {
229             components = new TaggedComponentSeqHolder(new TaggedComponent[0]);
230         }
231
232         switch( version.minor )
233         {
234             case 2 :
235             {
236                 //same as IIOP 1.1
237             }
238             case 1:
239             {
240                 // create IIOP 1.1 profile
241
242                 // concatenate the two component lists
243
244                 allComponents = new TaggedComponent[ this.components.size()
245                                                      + components.value.length ];
246                 System.arraycopy( this.components.asArray(), 0,
247                                   allComponents, 0, this.components.size() );
248                 System.arraycopy( components.value, 0,
249                                   allComponents, this.components.size(),
250                                   components.value.length );
251
252                 ProfileBody_1_1 pb1 = new ProfileBody_1_1
253                 (
254                     new org.omg.IIOP.Version( version.major, version.minor ),
255                     dnsEnabled ? primaryAddress.getHostname() : primaryAddress.getIP(),
256                     (short)primaryAddress.getPort(),
257                     objectKey,
258                     allComponents
259                 );
260
261                 // serialize the profile id 1, leave idx 0 for v.1.0 profile
262                 profileDataStream = new CDROutputStream();
263                 profileDataStream.beginEncapsulatedArray();
264                 ProfileBody_1_1Helper.write( profileDataStream, pb1 );
265
266                 tagged_profile.value = new TaggedProfile
267                 (
268                     TAG_INTERNET_IOP.value,
269                     profileDataStream.getBufferCopy()
270                 );
271                 break;
272             }
273             case 0:
274             {
275                 // create IIOP 1.0 profile
276                 ProfileBody_1_0 pb0 = new ProfileBody_1_0
277                 (
278                     new org.omg.IIOP.Version( version.major, version.minor ),
279                     dnsEnabled ? primaryAddress.getHostname() : primaryAddress.getIP(),
280                     (short)primaryAddress.getPort(),
281                     objectKey
282                 );
283
284                 profileDataStream = new CDROutputStream();
285                 profileDataStream.beginEncapsulatedArray();
286                 ProfileBody_1_0Helper.write( profileDataStream, pb0 );
287
288                 tagged_profile.value = new TaggedProfile
289                 (
290                     TAG_INTERNET_IOP.value,
291                     profileDataStream.getBufferCopy()
292                 );
293             }
294         }
295     }*/

296     
297     /**
298     * Writes the bytes that would make up the ETF::AddressProfile bytes (new spec)
299     * to a stream.
300     * <p>
301     * Writes GIOP version, host string, and port.
302     */

303     public void writeAddressProfile(CDROutputStream addressProfileStream)
304     {
305         org.omg.GIOP.VersionHelper.write( addressProfileStream, version);
306         addressProfileStream.write_string(dnsEnabled
307                                           ? primaryAddress.getHostname()
308                                           : primaryAddress.getIP());
309         addressProfileStream.write_ushort( (short) primaryAddress.getPort());
310     }
311     
312     /**
313     * Reads the bytes that make up the ETF::AddressProfile bytes (new spec)
314     * from a stream.
315     * <p>
316     * Writes GIOP version, host string, and port.
317     */

318     public void readAddressProfile(CDRInputStream addressProfileStream)
319     {
320         this.version = org.omg.GIOP.VersionHelper.read(addressProfileStream);
321         this.primaryAddress = IIOPAddress.read(addressProfileStream);
322         if (configuration != null)
323         {
324             try
325             {
326                 primaryAddress.configure(configuration);
327             }
328             catch( ConfigurationException ce)
329             {
330                 if (logger.isWarnEnabled())
331                     logger.warn("ConfigurationException", ce );
332             }
333         }
334     }
335
336     /**
337      * To improve the management of a large set of profile instances,
338      * the author may provide a hash function using the data in a Profile
339      * instance. The Profile shall always implement this function and either
340      * return a hash number, or 0 (zero) if no hashing is supported.
341      */

342     public int hash()
343     {
344         return hashCode();
345     }
346
347     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
348     {
349         IIOPProfile result = (IIOPProfile)super.clone(); // bitwise copy
350

351         result.version = new org.omg.GIOP.Version(this.version.major,
352                                                    this.version.minor);
353
354         // No need to make a deep copy of the primaryAddress, because
355
// the address can safely be shared between this IIOPProfile
356
// and the clone. This way, both will profit from any subsequent
357
// DNS resolution of that address.
358

359         if (this.objectKey != null)
360         {
361             result.objectKey = new byte [this.objectKey.length];
362             System.arraycopy(this.objectKey, 0, result.objectKey, 0,
363                               this.objectKey.length);
364         }
365
366         if (this.components != null)
367         {
368             result.components = (TaggedComponentList)this.components.clone();
369         }
370
371         return result;
372     }
373
374     /**
375      * This function shall determine if the passed profile, prof, is a match
376      * to this profile. The specifics of the match are left to the details
377      * of the underlying transport, however profiles shall be considered a
378      * match, if they would create connections that share the same attributes
379      * relevant to the transport setup. Among others, this could include
380      * address information (eg. host address) and transport layer
381      * characteristics (eg. encryption levels). If a match is found, it
382      * shall return true, or false otherwise.
383      */

384     public boolean is_match(Profile prof)
385     {
386         if (prof instanceof IIOPProfile)
387         {
388             IIOPProfile other = (IIOPProfile)prof;
389             return this.primaryAddress.equals(other.primaryAddress)
390                && this.getAlternateAddresses().equals(other.getAlternateAddresses())
391                && this.getSSLPort() == other.getSSLPort();
392         }
393         else
394             return false;
395     }
396
397     public int tag()
398     {
399         return TAG_INTERNET_IOP.value;
400     }
401
402     public IIOPAddress getAddress()
403     {
404         return primaryAddress;
405     }
406
407     /**
408      * Replaces the host in this profile's primary address with newHost
409      * (if it is not null), and the port with newPort (if it is not -1).
410      */

411     public void patchPrimaryAddress(String JavaDoc newHost, int newPort)
412     {
413         if (newHost != null)
414         {
415             primaryAddress = new IIOPAddress
416                 (
417                  newHost,
418                  (newPort != -1) ? newPort
419                  : primaryAddress.getPort()
420                  );
421
422         }
423         else if(newPort != -1)
424         {
425             primaryAddress = new IIOPAddress(primaryAddress.getIP(),
426                                              newPort);
427         }
428         try
429         {
430             primaryAddress.configure(configuration);
431         }
432         catch( ConfigurationException ce)
433         {
434             if (logger.isWarnEnabled())
435                 logger.warn("ConfigurationException", ce );
436         }
437     }
438
439     public List getAlternateAddresses()
440     {
441         return components.getComponents(TAG_ALTERNATE_IIOP_ADDRESS.value,
442                                         IIOPAddress.class);
443     }
444
445     public SSL getSSL()
446     {
447         return (SSL)components.getComponent( TAG_SSL_SEC_TRANS.value,
448                                              SSLHelper.class );
449     }
450
451     /**
452      * If there is a component tagged with TAG_CSI_SEC_MECH_LIST,
453      * get the SSL port from this component. Return the SSL port in the
454      * TAG_TLS_SEC_TRANS component encapsulated into the transport_mech
455      * field of the first CompoundSecMech of the CSI_SEC_MECH_LIST.
456      * Return -1 if there is no component tagged with TAG_CSI_SEC_MECH_LIST
457      * or if this component specifies no SSL port.
458      */

459     public int getTLSPortFromCSIComponent()
460     {
461         CompoundSecMechList csmList =
462             (CompoundSecMechList)components.getComponent(
463                                             TAG_CSI_SEC_MECH_LIST.value,
464                                             CompoundSecMechListHelper.class);
465         if (csmList != null && csmList.mechanism_list.length > 0)
466         {
467             byte[] tlsSecTransData =
468                 csmList.mechanism_list[0].transport_mech.component_data;
469             CDRInputStream in =
470                 new CDRInputStream((org.omg.CORBA.ORB JavaDoc)null, tlsSecTransData);
471             try
472             {
473                 in.openEncapsulatedArray();
474                 TLS_SEC_TRANS tls = TLS_SEC_TRANSHelper.read(in);
475                 if (tls.addresses.length > 0)
476                 {
477                     int ssl_port = tls.addresses[0].port;
478                     if (ssl_port != 0)
479                     {
480                         if (ssl_port < 0)
481                             ssl_port += 65536;
482                         return ssl_port;
483                     }
484                 }
485             }
486             catch ( Exception JavaDoc ex )
487             {
488             }
489         }
490         return -1;
491
492     }
493
494     /**
495      * Returns the port on which SSL is available according to this profile,
496      * or -1 if SSL is not supported.
497      */

498     public int getSSLPort()
499     {
500         SSL ssl = getSSL();
501         if (ssl == null)
502             return getTLSPortFromCSIComponent();
503         else
504         {
505             int port = ssl.port;
506             if (port < 0) port += 65536;
507             return port;
508         }
509     }
510
511     /**
512      * Returns a copy of this profile that is compatible with GIOP 1.0.
513      */

514     public IIOPProfile to_GIOP_1_0()
515     {
516         IIOPProfile result = new IIOPProfile(this.primaryAddress,
517                                               this.objectKey);
518         result.version.minor = 0;
519         return result;
520     }
521
522     public boolean equals(Object JavaDoc other)
523     {
524         if (other instanceof org.omg.ETF.Profile)
525             return this.is_match((org.omg.ETF.Profile)other);
526         else
527             return false;
528     }
529
530     public int hashCode()
531     {
532         return primaryAddress.hashCode();
533     }
534
535     public String JavaDoc toString()
536     {
537         return primaryAddress.toString();
538     }
539
540 }
541
Popular Tags