KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > etf > ProfileBase


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-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  */

21 package org.jacorb.orb.etf;
22
23 import org.apache.avalon.framework.configuration.*;
24
25 import org.jacorb.orb.CDROutputStream;
26 import org.jacorb.orb.CDRInputStream;
27 import org.jacorb.orb.TaggedComponentList;
28
29 import org.omg.ETF.*;
30 import org.omg.IOP.*;
31
32 /**
33  * @author Andre Spiegel
34  * @version $Id: ProfileBase.java,v 1.1 2004/08/25 09:31:41 simon.mcqueen Exp $
35  */

36 public abstract class ProfileBase
37     extends _ProfileLocalBase
38     implements Cloneable JavaDoc, Configurable
39 {
40     protected org.omg.GIOP.Version version = null;
41     protected byte[] objectKey = null;
42     protected TaggedComponentList components = null;
43
44     protected org.jacorb.config.Configuration configuration;
45     protected String JavaDoc corbalocStr = null;
46
47     public ProfileBase()
48     {
49     }
50     
51     /**
52     * ETF defined operation to set the object key on this profile.
53     */

54     public void set_object_key(byte[] key)
55     {
56         this.objectKey = key;
57     }
58
59     /**
60     * ETF defined operation to get the object key from this profile.
61     */

62     public byte[] get_object_key()
63     {
64         return objectKey;
65     }
66     
67     /**
68     * ETF defined read-only accessor for the GIOP version.
69     */

70     public org.omg.GIOP.Version version()
71     {
72         return version;
73     }
74     
75     /**
76     * ETF defined read-only accessor for the GIOP tag.
77     */

78     public abstract int tag();
79     
80     /**
81     * ETF defined function to marshal the appropriate information for this
82     * transport into the tagged profile. ORBs will typically need
83     * to call the IOR interception points before calling marshal().
84     * <p>
85     * This particular implementation *should* work for any IOP
86     * type protocol that encodes its profile_data as a CDR encapsulated
87     * octet array as long as you have correctly implemented
88     * the {@link #encapsulation()}, {@link #writeAddressProfile()}, and
89     * {@link #readAddressProfile()} methods. But, feel free to override
90     * it for the purpose of optimisation or whatever. It should however,
91     * remain consistent with your implementation
92     * of the above mentioned methods.
93     */

94     public void marshal (TaggedProfileHolder tagged_profile,
95                          TaggedComponentSeqHolder components)
96     {
97         if (encapsulation() != 0)
98         {
99             // You're going to have to define your own marshal operation
100
// for littleEndian profiles.
101
// The CDROutputStream only does big endian currently.
102
throw new Error JavaDoc("We can only marshal big endian stylee profiles !!");
103         }
104         
105         // Start a CDR encapsulation for the profile_data
106
CDROutputStream profileDataStream = new CDROutputStream();
107         profileDataStream.beginEncapsulatedArray();
108         
109         // Write the opaque AddressProfile bytes for this profile...
110
writeAddressProfile(profileDataStream);
111         
112         // ... then the object key
113
profileDataStream.write_long(objectKey.length);
114         profileDataStream.write_octet_array(objectKey,0,objectKey.length);
115         
116         switch( version.minor )
117         {
118             case 0 :
119                 // For GIOP 1.0 there were no tagged components
120
break;
121             default :
122                 // Assume minor != 0 means 1.1 onwards and encode the TaggedComponents
123
if (components == null)
124                 {
125                     components = new TaggedComponentSeqHolder (new TaggedComponent[0]);
126                 }
127                 // Write the length of the TaggedProfile sequence.
128
profileDataStream.write_long(this.components.size() + components.value.length);
129                 
130                 // Write the TaggedProfiles (ours first, then the ORB's)
131
for (int i = 0; i < this.components.asArray().length; i++)
132                 {
133                     TaggedComponentHelper.write(profileDataStream, this.components.asArray()[i]);
134                 }
135                 for (int i = 0; i < components.value.length; i++)
136                 {
137                     TaggedComponentHelper.write(profileDataStream, components.value[i]);
138                 }
139         }
140         
141         // Populate the TaggedProfile for return.
142
tagged_profile.value = new TaggedProfile
143         (
144             this.tag(),
145             profileDataStream.getBufferCopy()
146         );
147     }
148     
149     /**
150     * Method to mirror the marshal method.
151     */

152     public void demarshal(TaggedProfileHolder tagged_profile,
153                           TaggedComponentSeqHolder components)
154     {
155         if (tagged_profile.value.tag != this.tag())
156         {
157             throw new org.omg.CORBA.BAD_PARAM JavaDoc
158                 ("Wrong tag for Transport, tag: "
159                  + tagged_profile.value.tag);
160         }
161         initFromProfileData(tagged_profile.value.profile_data);
162         components.value = getComponents().asArray();
163     }
164     
165     /**
166     * Indicates the encapsulation that will be used by this profile
167     * when encoding it's AddressProfile bytes, and which should subsequently
168     * be used when marshalling all the rest of the TaggedProfile.profile_data.
169     * Using the default CDROutputStream for a transport profile encapsulation
170     * this should always be 0.
171     */

172     public short encapsulation()
173     {
174         return 0; // i.e. Big endian TAG_INTERNET_IOP style
175
}
176     
177     /**
178     * Write the AddressProfile to the supplied stream.
179     * Implementors can assume an encapsulation is already open.
180     */

181     public abstract void writeAddressProfile(CDROutputStream stream);
182     
183     /**
184     * Read the ETF::AddressProfile from the supplied stream.
185     */

186     public abstract void readAddressProfile(CDRInputStream stream);
187     
188     /**
189     * Accessor for the TaggedComponents of the Profile.
190     */

191     public TaggedComponentList getComponents()
192     {
193         return components;
194     }
195
196     public Object JavaDoc getComponent(int tag, Class JavaDoc helper)
197     {
198         return components.getComponent(tag, helper);
199     }
200
201     public void addComponent(int tag, Object JavaDoc data, Class JavaDoc helper)
202     {
203         components.addComponent(tag, data, helper);
204     }
205
206     public void addComponent(int tag, byte[] data)
207     {
208         components.addComponent(tag, data);
209     }
210
211     public TaggedProfile asTaggedProfile()
212     {
213         TaggedProfileHolder result = new TaggedProfileHolder();
214         this.marshal(result, null);
215         return result.value;
216     }
217
218     /**
219      * This function shall return an equivalent, deep-copy of the profile
220      * on the free store.
221      */

222     public Profile copy()
223     {
224         try
225         {
226             return (Profile)this.clone();
227         }
228         catch (CloneNotSupportedException JavaDoc e)
229         {
230             throw new RuntimeException JavaDoc("error cloning profile: " + e);
231         }
232     }
233     
234     /**
235     * Used from the byte[] constructor and the demarshal method. Relies
236     * on subclasses having satisfactorily implemented the
237     * {@link #readAddressProfile()} method.
238     */

239     protected void initFromProfileData(byte[] data)
240     {
241         CDRInputStream in = new CDRInputStream(null, data);
242         in.openEncapsulatedArray();
243
244         readAddressProfile(in);
245         
246         int length = in.read_ulong();
247         
248         objectKey = new byte[length];
249         in.read_octet_array(objectKey, 0, length);
250         
251         components = (version != null && version.minor > 0) ? new TaggedComponentList(in)
252                                                             : new TaggedComponentList();
253     }
254 }
255
Popular Tags