KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > ref > RefUtil


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.ref;
6
7 import java.util.Map JavaDoc;
8 import java.io.UnsupportedEncodingException JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.commons.collections.LRUMap;
13
14 import org.omg.CORBA.Object JavaDoc;
15 import org.omg.CORBA.Any JavaDoc;
16 import org.omg.CORBA.ORB JavaDoc;
17 import org.omg.CORBA.ObjectHelper JavaDoc;
18 import org.omg.IOP.IOR JavaDoc;
19 import org.omg.IOP.IORHelper JavaDoc;
20 import org.omg.IOP.TaggedProfile JavaDoc;
21 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc;
22 import org.omg.IOP.TaggedComponent JavaDoc;
23 import org.omg.IOP.CodecFactory JavaDoc;
24 import org.omg.IOP.CodecFactoryHelper JavaDoc;
25 import org.omg.IOP.Encoding JavaDoc;
26 import org.omg.IOP.ENCODING_CDR_ENCAPS JavaDoc;
27 import org.omg.IOP.Codec JavaDoc;
28 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding JavaDoc;
29 import org.omg.IIOP.ProfileBody_1_1Helper;
30 import org.omg.IIOP.ProfileBody_1_1;
31
32 /**
33  * This class provides operations to manipulate CORBA and Jackass references.
34  * @author Guido Urdaneta
35  */

36 public final class RefUtil
37 {
38     /** TAG to be used for the Jackass Component ID Tagged Component in Corba References */
39     public static final int JACKASS_COMPONENT_ID_TAG = 100;
40     /** Encodging to be used for the Jackass Component ID Tagged Component Data */
41     public static final String JavaDoc JACKASS_COMPONENT_ID_ENCODING = "UTF-8";
42     /** CORBA CODEC used to analize references*/
43     public static final Encoding JavaDoc DEF_ENCODING = new Encoding JavaDoc(ENCODING_CDR_ENCAPS.value, (byte) 1, (byte) 0);
44
45     private static final Log LOG = LogFactory.getLog(RefUtil.class);
46     private static final String JavaDoc ERR_INIT = "Error initializing";
47     //private static final String ERR_RIP = "Error ripping reference";
48
private static final String JavaDoc ERR_NULL_ARGS = "Illegal null arguments";
49     private static final String JavaDoc ERR_ENCONDING = "Unsupported enconding: " + JACKASS_COMPONENT_ID_ENCODING;
50     private static final String JavaDoc ERR_CORBA_ENCODING = "Error using CORBA enconding";
51     private static final String JavaDoc ERR_NOT_1_1_REFERENCE = "It is required an IIOP 1.1 or greater reference";
52
53     private ORB JavaDoc orb;
54     private CodecFactory JavaDoc codecFactory;
55     private Codec JavaDoc codec;
56     private Map JavaDoc cache;
57
58     /**
59      * Creates a RefUtil object that uses the specified ORB to code/decode the references.
60      * @param nOrb the ORB
61      */

62     public RefUtil(ORB JavaDoc nOrb)
63     {
64         try
65         {
66             orb = nOrb;
67             codecFactory = CodecFactoryHelper.narrow(orb.resolve_initial_references("CodecFactory"));
68             codec = codecFactory.create_codec(DEF_ENCODING);
69             cache = new LRUMap(100);
70         }
71         catch (Exception JavaDoc e)
72         {
73             if (LOG.isErrorEnabled()) LOG.error(ERR_INIT, e);
74             throw new IllegalStateException JavaDoc(ERR_INIT + e);
75         }
76     }
77
78     /**
79      * Determins if a CORBA refence is a Jackass reference.
80      * @param ref the reference to analyze
81      * @return true if ref is a Jackass reference, false otherwise
82      */

83     public boolean isJackassComponent(Object JavaDoc ref)
84     {
85         CacheEntry entry = updateCache(ref);
86         return entry.isJackassComponent;
87     }
88
89     /**
90      * Returns the component ID of a specified Jackass reference
91      * @param ref the reference
92      * @return if ref is a Jackass reference, its Component ID, null otherwise.
93      */

94     public String JavaDoc getComponentID(Object JavaDoc ref)
95     {
96         CacheEntry entry = updateCache(ref);
97         return entry.componentID;
98     }
99
100     /**
101      * Creates a Jackass reference appending a Component ID TaggedComponent value to
102      * a specified reference
103      * @param objectRef an initial, valid CORBA (non-Jackass) reference
104      * @param componentID the Component ID value to set the CID TaggedComponent to
105      * @return the newly created Jackass reference
106      */

107     public Object JavaDoc createJackassComponentReference(Object JavaDoc objectRef, String JavaDoc componentID)
108     {
109         byte[] cid = null;
110         try
111         {
112             cid = componentID.getBytes(JACKASS_COMPONENT_ID_ENCODING);
113         }
114         catch (UnsupportedEncodingException JavaDoc e)
115         {
116             if (LOG.isErrorEnabled()) LOG.error(ERR_ENCONDING, e);
117             throw new IllegalStateException JavaDoc(ERR_ENCONDING);
118         }
119
120         IORData iorData = rip(objectRef);
121         if (!iorData.isIIOP_1_1())
122         {
123             throw new IllegalArgumentException JavaDoc(ERR_NOT_1_1_REFERENCE);
124         }
125
126         final int TC_LEN = iorData.profileBody.components.length;
127         TaggedComponent JavaDoc[] tcs = new TaggedComponent JavaDoc[TC_LEN + 1];
128         System.arraycopy(iorData.profileBody.components, 0, tcs, 0, TC_LEN);
129         tcs[TC_LEN] = new TaggedComponent JavaDoc(JACKASS_COMPONENT_ID_TAG, cid);
130         iorData.profileBody.components = tcs;
131
132         Any JavaDoc anyProfBody = orb.create_any();
133         ProfileBody_1_1Helper.insert(anyProfBody, iorData.profileBody);
134
135         try
136         {
137             iorData.ior.profiles[iorData.inetProfileIndex].profile_data = codec.encode_value(anyProfBody);
138         }
139         catch (InvalidTypeForEncoding JavaDoc e)
140         {
141             if (LOG.isErrorEnabled()) LOG.error(ERR_CORBA_ENCODING, e);
142             throw new IllegalStateException JavaDoc(ERR_CORBA_ENCODING);
143         }
144
145         Any JavaDoc iorAny = orb.create_any();
146         IORHelper.insert(iorAny, iorData.ior);
147         return ObjectHelper.extract(iorAny);
148     }
149
150     /**
151      * Updates the internal cache with information about the
152      * specified reference.
153      * @param ref the reference.
154      * @return the CacheEntry placed in the cache.
155      */

156     private CacheEntry updateCache(Object JavaDoc ref)
157     {
158         CacheEntry ret = (CacheEntry) cache.get(ref);
159         if (ret != null)
160         {
161             return ret;
162         }
163
164         IORData iorData = rip(ref);
165         if (iorData.isIIOP_1_1())
166         {
167             TaggedComponent JavaDoc[] components = iorData.profileBody.components;
168             for (int j = 0; j < components.length; j++)
169             {
170                 if (components[j].tag == JACKASS_COMPONENT_ID_TAG)
171                 {
172                     try
173                     {
174                         ret = new CacheEntry(true, new String JavaDoc(components[j].component_data,
175                                                               JACKASS_COMPONENT_ID_ENCODING));
176                     }
177                     catch (java.io.UnsupportedEncodingException JavaDoc e)
178                     {
179                         if (LOG.isErrorEnabled()) LOG.error(ERR_ENCONDING, e);
180                         throw new IllegalStateException JavaDoc(ERR_ENCONDING);
181                     }
182                     cache.put(ref, ret);
183                     return ret;
184                 }
185             }
186         }
187
188         ret = new CacheEntry(false, null);
189         cache.put(ref, ret);
190         return ret;
191     }
192
193     /**
194      * Returns information about a CORBA reference.
195      * @param ref the CORBA reference to rip.
196      * @return the reference data.
197      */

198     IORData rip(Object JavaDoc ref)
199     {
200         if (ref == null)
201         {
202             throw new NullPointerException JavaDoc(ERR_NULL_ARGS);
203         }
204         Any JavaDoc any = orb.create_any();
205         any.insert_Object(ref);
206
207         IOR JavaDoc ior = IORHelper.extract(any);
208         for (int i = 0; i < ior.profiles.length; i++)
209         {
210             TaggedProfile JavaDoc profile = ior.profiles[i];
211             if (profile.tag == TAG_INTERNET_IOP.value)
212             {
213                 try
214                 {
215                     Any JavaDoc anyProfBody = codec.decode_value(profile.profile_data, ProfileBody_1_1Helper.type());
216                     ProfileBody_1_1 profBody = ProfileBody_1_1Helper.extract(anyProfBody);
217                     if (LOG.isDebugEnabled()) LOG.debug("Successfully decoded reference as IIOP 1.1");
218                     return new IORData(ior, profBody, i);
219                 }
220                 catch (Exception JavaDoc e)
221                 {
222                     if (LOG.isInfoEnabled()) LOG.info("Could not decode reference as IIOP 1.1 or greater");
223                 }
224             }
225         }
226
227         return new IORData(ior);
228     }
229
230     /**
231      * Objects of this class are put as values in
232      * the internal cache with information about
233      * references.
234      * The information in a CacheEntry stores 
235      * if a reference is a Jackass reference, and if
236      * so, also stores the component ID.
237      */

238     private static final class CacheEntry
239     {
240         private final boolean isJackassComponent;
241         private final String JavaDoc componentID;
242
243         /**
244          * Constructor.
245          * @param isJackass true if it is a jackass component; otherwise false.
246          * @param cid component identifier.
247          */

248         CacheEntry(boolean isJackass, String JavaDoc cid)
249         {
250             this.isJackassComponent = isJackass;
251             this.componentID = cid;
252         }
253     }
254
255 }
256
Popular Tags