1 /* ServiceContext.java --
2    Copyright (C) 2005 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package gnu.CORBA.GIOP;
40 
41 import gnu.CORBA.CDR.AbstractCdrInput;
42 import gnu.CORBA.CDR.AbstractCdrOutput;
43 
44 import org.omg.CORBA.BAD_INV_ORDER;
45 import org.omg.CORBA.BAD_PARAM;
46 import org.omg.CORBA.CompletionStatus;
47 import org.omg.CORBA.portable.IDLEntity;
48 
49 /**
50  * Contains the ORB service data being passed.
51  *
52  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
53  */
54 public class ServiceContext
55   implements IDLEntity
56 {
57   /**
58    * Use serialVersionUID for interoperability.
59    */
60   private static final long serialVersionUID = 1;
61 
62   /* Standard values for the context_id. */
63   public static final int TransactionService = 0;
64 
65   /**
66    * Defines code sets, used to encode wide and narrow characters. Required for
67    * messages with data structures, involving wide characters.
68    */
69   public static final int CodeSets = 1;
70 
71   public static final int ChainBypassCheck = 2;
72 
73   public static final int ChainBypassInfo = 3;
74 
75   public static final int LogicalThreadId = 4;
76 
77   public static final int BI_DIR_IIOP = 5;
78 
79   public static final int SendingContextRunTime = 6;
80 
81   public static final int INVOCATION_POLICIES = 7;
82 
83   public static final int FORWARDED_IDENTITY = 8;
84 
85   /**
86    * Contains exception details if exception being transferred is other than
87    * System or User exception. javax.rmi uses this context to transfer arbitrary
88    * java exceptions as CORBA value types.
89    */
90   public static final int UnknownExceptionInfo = 9;
91 
92   public static final int RTCorbaPriority = 10;
93 
94   public static final int RTCorbaPriorityRange = 11;
95 
96   public static final int FT_GROUP_VERSION = 12;
97 
98   public static final int FT_REQUEST = 13;
99 
100   public static final int ExceptionDetailMessage = 14;
101 
102   public static final int SecurityAttributeService = 15;
103 
104   public static final int ActivityService = 16;
105 
106   /**
107    * The context id (for instance, 0x1 for code sets context). At the moment of
108    * writing, the OMG defines 16 standard values and provides rules to register
109    * the vendor specific context ids. The range 0-4095 is reserved for the
110    * future standard OMG contexts.
111    */
112   public int context_id;
113 
114   /**
115    * The context_data.
116    */
117   public byte[] context_data;
118 
119   /**
120    * Crete unitialised instance.
121    */
ServiceContext()122   public ServiceContext()
123   {
124   }
125 
126   /**
127    * Create from omg context.
128    */
ServiceContext(org.omg.IOP.ServiceContext from)129   public ServiceContext(org.omg.IOP.ServiceContext from)
130   {
131     context_id = from.context_id;
132     context_data = from.context_data;
133   }
134 
135   /**
136    * Read the context values from the stream.
137    *
138    * @param istream a stream to read from.
139    */
read(AbstractCdrInput istream)140   public static ServiceContext read(AbstractCdrInput istream)
141   {
142     int id = istream.read_ulong();
143 
144     switch (id)
145       {
146         case CodeSetServiceContext.ID:
147 
148           CodeSetServiceContext codeset = new CodeSetServiceContext();
149           codeset.readContext(istream);
150           return codeset;
151 
152         default:
153 
154           ServiceContext ctx = new ServiceContext();
155           ctx.context_id = id;
156           ctx.context_data = istream.read_sequence();
157           return ctx;
158       }
159   }
160 
161   /**
162    * Read a sequence of contexts from the input stream.
163    */
readSequence(AbstractCdrInput istream)164   public static ServiceContext[] readSequence(AbstractCdrInput istream)
165   {
166     int size = istream.read_long();
167     ServiceContext[] value = new gnu.CORBA.GIOP.ServiceContext[size];
168     for (int i = 0; i < value.length; i++)
169       value[i] = read(istream);
170     return value;
171   }
172 
173   /**
174    * Write the context values into the stream.
175    *
176    * @param ostream a stream to write the data to.
177    */
write(AbstractCdrOutput ostream)178   public void write(AbstractCdrOutput ostream)
179   {
180     ostream.write_ulong(context_id);
181     ostream.write_sequence(context_data);
182   }
183 
184   /**
185    * Write the sequence of contexts into the input stream.
186    */
writeSequence(AbstractCdrOutput ostream, ServiceContext[] value)187   public static void writeSequence(AbstractCdrOutput ostream, ServiceContext[] value)
188   {
189     ostream.write_long(value.length);
190     for (int i = 0; i < value.length; i++)
191       value[i].write(ostream);
192   }
193 
194   /**
195    * Add context to the given array of contexts.
196    */
add(org.omg.IOP.ServiceContext[] cx, org.omg.IOP.ServiceContext service_context, boolean replace)197   public static void add(org.omg.IOP.ServiceContext[] cx,
198     org.omg.IOP.ServiceContext service_context, boolean replace)
199   {
200     int exists = -1;
201 
202     for (int i = 0; i < cx.length; i++)
203       if (cx[i].context_id == service_context.context_id)
204         exists = i;
205 
206     if (exists < 0)
207       {
208         // Add context.
209         org.omg.IOP.ServiceContext[] n = new org.omg.IOP.ServiceContext[cx.length + 1];
210         for (int i = 0; i < cx.length; i++)
211           n[i] = cx[i];
212         n[cx.length] = service_context;
213       }
214     else
215       {
216         // Replace context.
217         if (!replace)
218           throw new BAD_INV_ORDER("Repetetive setting of the context "
219             + service_context.context_id, 15, CompletionStatus.COMPLETED_NO);
220         else
221           cx[exists] = service_context;
222       }
223   }
224 
225   /**
226    * Add context to the given array of contexts.
227    */
add(ServiceContext[] cx, org.omg.IOP.ServiceContext service_context, boolean replace)228   public static ServiceContext[] add(ServiceContext[] cx,
229     org.omg.IOP.ServiceContext service_context, boolean replace)
230   {
231     int exists = -1;
232 
233     for (int i = 0; i < cx.length; i++)
234       if (cx[i].context_id == service_context.context_id)
235         exists = i;
236 
237     if (exists < 0)
238       {
239         // Add context.
240         ServiceContext[] n = new ServiceContext[cx.length + 1];
241         for (int i = 0; i < cx.length; i++)
242           n[i] = cx[i];
243         n[cx.length] = new ServiceContext(service_context);
244         return n;
245       }
246     else
247       {
248         // Replace context.
249         if (!replace)
250           throw new BAD_INV_ORDER("Repetetive setting of the context "
251             + service_context.context_id, 15, CompletionStatus.COMPLETED_NO);
252         else
253           cx[exists] = new ServiceContext(service_context);
254         return cx;
255       }
256   }
257 
258   /**
259    * Find context with the given name in the context array.
260    */
findContext(int ctx_name, org.omg.IOP.ServiceContext[] cx)261   public static org.omg.IOP.ServiceContext findContext(int ctx_name,
262     org.omg.IOP.ServiceContext[] cx)
263   {
264     for (int i = 0; i < cx.length; i++)
265       if (cx[i].context_id == ctx_name)
266         return cx[i];
267     throw new BAD_PARAM("No context with id " + ctx_name);
268   }
269 
270   /**
271    * Find context with the given name in the context array, converting into
272    * org.omg.IOP.ServiceContext.
273    */
findContext(int ctx_name, ServiceContext[] cx)274   public static org.omg.IOP.ServiceContext findContext(int ctx_name,
275     ServiceContext[] cx)
276   {
277     for (int i = 0; i < cx.length; i++)
278       if (cx[i].context_id == ctx_name)
279         return new org.omg.IOP.ServiceContext(ctx_name, cx[i].context_data);
280     throw new BAD_PARAM("No context with id " + ctx_name);
281   }
282 
283   /**
284    * Find context with the given name in the context array without conversions.
285    */
find(int ctx_name, ServiceContext[] cx)286   public static ServiceContext find(int ctx_name, ServiceContext[] cx)
287   {
288     for (int i = 0; i < cx.length; i++)
289       if (cx[i].context_id == ctx_name)
290         return cx[i];
291     return null;
292   }
293 
294   /**
295    * Return a string representation.
296    */
toString()297   public String toString()
298   {
299     return "ctx " + context_id + ", size " + context_data.length;
300   }
301 }
302