KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > util > InternetProfile


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.util;
26
27 public final class InternetProfile {
28     private static final int DEFAULT_IIOP_PORT = 2089;
29     public byte major, minor;
30     public String JavaDoc host;
31     public int port;
32     public byte object_key[];
33     public org.omg.IOP.TaggedComponent JavaDoc components[];
34
35     private boolean Check() {
36         if (host == null || object_key == null) {
37             return false;
38         }
39
40         if (components != null) {
41             for (int i = 0; i < components.length; i++) {
42                 if (components[i].component_data == null) {
43                     return false;
44                 }
45             }
46         }
47
48         return true;
49     }
50
51     void Read_ior(byte profile_data[]) {
52         ModestInputStream in = new ModestInputStream(profile_data);
53         major = in.ReadOctet();
54         minor = in.ReadOctet();
55         host = in.ReadString();
56         port = in.ReadUShort();
57         object_key = in.ReadOctetArray();
58
59         if (major >= 1 && minor >= 1) {
60             int ncomponents = in.ReadULong();
61             components = new org.omg.IOP.TaggedComponent JavaDoc[ncomponents];
62
63             for (int i = 0; i < ncomponents; i++) {
64                 org.omg.IOP.TaggedComponent JavaDoc component = new org.omg.IOP.TaggedComponent JavaDoc();
65                 component.tag = in.ReadULong();
66                 component.component_data = in.ReadOctetArray();
67                 components[i] = component;
68             }
69         }
70     }
71
72     void Read_corbaloc(String JavaDoc address) {
73         // check protocol id
74
int index;
75
76         if (address.startsWith(":")) {
77             index = 1;
78         } else if (address.length() >= 5 && address.substring(0, 5).compareToIgnoreCase("iiop:") == 0) {
79             index = 5;
80         } else {
81             throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad address: " + address + " (<iiop_id> not found)");
82         // read version number
83
}
84         int ver = address.indexOf('@', index);
85
86         if (ver == - 1) {
87             major = 1;
88             minor = 0;
89         } else {
90             int dot = address.indexOf('.');
91
92             if (dot == - 1 || dot > ver) {
93                 throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad version number: \"" + address.substring(index, ver) + '"');
94             }
95
96             try {
97                 major = (byte)Integer.parseInt(address.substring(index, dot));
98                 minor = (byte)Integer.parseInt(address.substring(dot + 1, ver));
99             } catch (NumberFormatException JavaDoc exc) {
100                 throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad version number: \"" + address.substring(index, ver) + '"');
101             }
102
103             index = ver + 1;
104         }
105         // read address
106
int colon = address.indexOf(':', index);
107
108         if (colon == - 1) {
109             host = address.substring(index);
110             port = DEFAULT_IIOP_PORT;
111         } else {
112             host = address.substring(index, colon);
113
114             try {
115                 port = Integer.parseInt(address.substring(colon + 1));
116             } catch (NumberFormatException JavaDoc exc) {
117                 throw new org.omg.CORBA.INV_OBJREF JavaDoc("Bad port number: \"" + address.substring(colon + 1) + '"');
118             }
119         }
120
121         if (host.length() == 0) {
122             throw new org.omg.CORBA.INV_OBJREF JavaDoc("No host/IP address found");
123         }
124
125         components = null;
126     }
127
128     byte[] Write_ior(boolean little_endian) {
129         if (!Check()) {
130             throw new org.omg.CORBA.INV_OBJREF JavaDoc();
131         }
132         ModestOutputStream out = new ModestOutputStream(little_endian);
133
134         out.WriteOctet(major);
135         out.WriteOctet(minor);
136         out.WriteString(host);
137         out.WriteUShort((short)port);
138         out.WriteOctetArray(object_key);
139
140         if (major >= 1 && minor >= 1) {
141             if (components == null) {
142                 out.WriteULong(0);
143             } else {
144                 out.WriteULong(components.length);
145
146                 for (int i = 0; i < components.length; i++) {
147                     out.WriteULong(components[i].tag);
148                     out.WriteOctetArray(components[i].component_data);
149                 }
150             }
151         }
152
153         return out.GetBytes();
154     }
155
156     void Write_corbaloc(StringBuffer JavaDoc sb) {
157         if (!Check()) {
158             throw new org.omg.CORBA.INV_OBJREF JavaDoc();
159         }
160
161         sb.append(':');
162
163         if (major != 1 || minor != 0) {
164             sb.append(Integer.toString(major) + '.' + minor + '@');
165         }
166
167         sb.append(host);
168
169         if (port != DEFAULT_IIOP_PORT) {
170             sb.append(":" + port);
171         }
172     }
173
174     public InternetProfile() {
175     }
176
177     public InternetProfile(InternetProfile profile) {
178
179         Set(profile);
180     }
181
182     public void Set(InternetProfile profile) {
183         major = profile.major;
184         minor = profile.minor;
185         host = profile.host;
186         port = profile.port;
187
188         if (profile.object_key != null) {
189             object_key = new byte[profile.object_key.length];
190             System.arraycopy(profile.object_key, 0, object_key, 0, object_key.length);
191         } else {
192             object_key = null;
193         }
194
195         if (profile.components != null) {
196             components = new org.omg.IOP.TaggedComponent JavaDoc[profile.components.length];
197             System.arraycopy(profile.components, 0, components, 0, components.length);
198         } else {
199             components = null;
200         }
201     }
202 }
203
Popular Tags