KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > rmi > ir > RepositoryImpl


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.iiop.rmi.ir;
23
24 import org.omg.CORBA.Repository JavaDoc;
25 import org.omg.CORBA.RepositoryPOATie;
26 import org.omg.CORBA.ORB JavaDoc;
27 import org.omg.CORBA.IRObject JavaDoc;
28 import org.omg.CORBA.DefinitionKind JavaDoc;
29 import org.omg.CORBA.RepositoryOperations JavaDoc;
30 import org.omg.CORBA.Contained JavaDoc;
31 import org.omg.CORBA.ContainedHelper;
32 import org.omg.CORBA.TypeCode JavaDoc;
33 import org.omg.CORBA.PrimitiveDef JavaDoc;
34 import org.omg.CORBA.StringDef JavaDoc;
35 import org.omg.CORBA.WstringDef JavaDoc;
36 import org.omg.CORBA.SequenceDef JavaDoc;
37 import org.omg.CORBA.ArrayDef JavaDoc;
38 import org.omg.CORBA.FixedDef JavaDoc;
39 import org.omg.CORBA.BAD_INV_ORDER JavaDoc;
40 import org.omg.PortableServer.POA JavaDoc;
41
42 import java.util.Map JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 import java.io.UnsupportedEncodingException JavaDoc;
47
48 /**
49  * An Interface Repository.
50  *
51  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
52  * @version $Revision: 37459 $
53  */

54 class RepositoryImpl
55    extends ContainerImpl
56    implements RepositoryOperations JavaDoc, LocalContainer
57 {
58    // Constants -----------------------------------------------------
59

60    // Attributes ----------------------------------------------------
61

62    // Static --------------------------------------------------------
63

64    private static final org.jboss.logging.Logger logger =
65                org.jboss.logging.Logger.getLogger(RepositoryImpl.class);
66
67    // Constructors --------------------------------------------------
68

69    public RepositoryImpl(ORB JavaDoc orb, POA JavaDoc poa, String JavaDoc name)
70    {
71       super(DefinitionKind.dk_Repository, null);
72
73       this.orb = orb;
74       this.poa = poa;
75       try {
76          oid = (name).getBytes("UTF-8");
77       } catch (UnsupportedEncodingException JavaDoc ex) {
78          throw new RuntimeException JavaDoc("UTF-8 encoding not supported.");
79       }
80       oidPrefix = name + ":";
81       anonOidPrefix = oidPrefix + "anon";
82
83       repository = this;
84    }
85
86    // Public --------------------------------------------------------
87

88    // LocalIRObject implementation ----------------------------------
89

90    public IRObject JavaDoc getReference()
91    {
92       if (ref == null) {
93          ref = org.omg.CORBA.RepositoryHelper.narrow(
94                               servantToReference(new RepositoryPOATie(this)) );
95       }
96       return ref;
97    }
98
99    public void allDone()
100       throws IRConstructionException
101    {
102       super.allDone();
103
104       // call allDone() for all our sequences
105
Iterator JavaDoc iter = sequenceMap.values().iterator();
106       while (iter.hasNext())
107          ((SequenceDefImpl)iter.next()).allDone();
108    }
109
110    public void shutdown()
111    {
112       // shutdown all anonymous IR objects in this IR
113
for (long i = 1; i < nextPOAId; i++) {
114          try {
115             getPOA().deactivate_object(getAnonymousObjectId(i));
116          } catch (org.omg.CORBA.UserException JavaDoc ex) {
117             logger.warn("Could not deactivate anonymous IR object", ex);
118          }
119       }
120       
121       // shutdown this IR's top-level container
122
super.shutdown();
123    }
124
125    // Repository implementation -------------------------------------
126

127    public Contained JavaDoc lookup_id(java.lang.String JavaDoc search_id)
128    {
129       logger.debug("RepositoryImpl.lookup_id(\"" + search_id + "\") entered.");
130       LocalContained c = _lookup_id(search_id);
131
132       if (c == null)
133          return null;
134
135       return ContainedHelper.narrow(c.getReference());
136    }
137
138    public TypeCode JavaDoc get_canonical_typecode(org.omg.CORBA.TypeCode JavaDoc tc)
139    {
140       logger.debug("RepositoryImpl.get_canonical_typecode() entered.");
141       // TODO
142
return null;
143    }
144
145    public PrimitiveDef JavaDoc get_primitive(org.omg.CORBA.PrimitiveKind JavaDoc kind)
146    {
147       logger.debug("RepositoryImpl.get_primitive() entered.");
148       // TODO
149
return null;
150    }
151
152    public StringDef JavaDoc create_string(int bound)
153    {
154       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
155    }
156
157    public WstringDef JavaDoc create_wstring(int bound)
158    {
159       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
160    }
161
162    public SequenceDef JavaDoc create_sequence(int bound, org.omg.CORBA.IDLType JavaDoc element_type)
163    {
164       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
165    }
166
167    public ArrayDef JavaDoc create_array(int length, org.omg.CORBA.IDLType JavaDoc element_type)
168    {
169       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
170    }
171
172    public FixedDef JavaDoc create_fixed(short digits, short scale)
173    {
174       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
175    }
176
177  
178
179    // Y overrides ---------------------------------------------------
180

181    // Package protected ---------------------------------------------
182

183    /**
184     * The ORB that I use.
185     */

186    ORB JavaDoc orb = null;
187
188    /**
189     * The POA that I use.
190     */

191    POA JavaDoc poa = null;
192
193    /**
194     * The POA object ID of this repository.
195     */

196    private byte[] oid = null;
197
198    /**
199     * Prefix for POA object IDs of IR objects in this repository.
200     */

201    private String JavaDoc oidPrefix = null;
202
203    /**
204     * Prefix for POA object IDs of "anonymous" IR objects in this repository.
205     */

206    private String JavaDoc anonOidPrefix = null;
207
208    /**
209     * Maps typecodes of sequences defined in this IR to the sequences.
210     */

211    Map JavaDoc sequenceMap = new HashMap JavaDoc();
212
213    /**
214     * Maps repository IDs of sequences defined in this IR to the sequences.
215     */

216    Map JavaDoc sequenceIdMap = new HashMap JavaDoc();
217
218
219    LocalContained _lookup_id(java.lang.String JavaDoc search_id)
220    {
221       logger.debug("RepositoryImpl._lookup_id(\"" + search_id +
222                    "\") entered.");
223       // mapping of arrays are special
224
if (search_id.startsWith("RMI:["))
225          return (ValueBoxDefImpl)sequenceIdMap.get(search_id);
226
227       // convert id
228
String JavaDoc name = scopedName(search_id);
229       logger.debug("RepositoryImpl._lookup_id(): scopedName=\"" +
230                    scopedName(search_id) + "\"");
231
232       // look it up if converted id not null
233
//return (name == null) ? null : _lookup(name);
234
LocalContained ret = (name == null) ? null : _lookup(name);
235       logger.debug("RepositoryImpl._lookup_id(): returning " +
236                    ((ret == null) ? "null" : "NOT null") );
237       return ret;
238    }
239
240    SequenceDefImpl getSequenceImpl(TypeCode JavaDoc typeCode)
241    {
242       return (SequenceDefImpl)sequenceMap.get(typeCode);
243    }
244
245    void putSequenceImpl(String JavaDoc id, TypeCode JavaDoc typeCode, SequenceDefImpl sequence,
246                         ValueBoxDefImpl valueBox)
247    {
248       sequenceIdMap.put(id, valueBox);
249       sequenceMap.put(typeCode, sequence);
250    }
251
252    String JavaDoc getObjectIdPrefix()
253    {
254       return oidPrefix;
255    }
256
257    // Protected -----------------------------------------------------
258

259    /**
260     * Return the POA object ID of this IR object.
261     */

262    protected byte[] getObjectId()
263    {
264       return (byte[])oid.clone();
265    }
266
267    /**
268     * Generate the ID of the n-th "anonymous" object created in this IR.
269     */

270    protected byte[] getAnonymousObjectId(long n)
271    {
272       String JavaDoc s = anonOidPrefix + Long.toString(n);
273       try {
274          return s.getBytes("UTF-8");
275       } catch (UnsupportedEncodingException JavaDoc ex) {
276          throw new RuntimeException JavaDoc("UTF-8 encoding not supported.");
277       }
278    }
279
280    /**
281     * The next "anonymous" POA object ID.
282     * While contained IR objects can generate a sensible ID from their
283     * repository ID, non-contained objects use this method to get an
284     * ID that is unique within the IR.
285     */

286    protected byte[] getNextObjectId()
287    {
288       return getAnonymousObjectId(nextPOAId++);
289    }
290
291
292    // Private -------------------------------------------------------
293

294    /**
295     * My CORBA reference.
296     */

297    private Repository JavaDoc ref = null;
298
299    /**
300     * The next "anonymous" POA object ID.
301     */

302    private long nextPOAId = 1;
303
304    /**
305     * Convert a repository ID to an IDL scoped name.
306     * Returns <code>null</code> if the ID cannot be understood.
307     */

308    private String JavaDoc scopedName(String JavaDoc id)
309    {
310       if (id == null)
311          return null;
312
313       if (id.startsWith("IDL:")) {
314          // OMG IDL format
315

316          // Check for base types
317
if ("IDL:omg.org/CORBA/Object:1.0".equals(id) ||
318              "IDL:omg.org/CORBA/ValueBase:1.0".equals(id))
319             return null;
320
321          // Get 2nd component of ID
322
int idx2 = id.indexOf(':', 4); // 2nd colon
323
if (idx2 == -1)
324            return null; // invalid ID, version part missing
325
String JavaDoc base = id.substring(4, id.indexOf(':', 4));
326
327          // Check special prefixes
328
if (base.startsWith("omg.org"))
329            base = "org/omg" + base.substring(7);
330          if (base.startsWith("w3c.org"))
331            base = "org/w3c" + base.substring(7);
332
333          // convert '/' to "::"
334
StringBuffer JavaDoc b = new StringBuffer JavaDoc();
335          for (int i = 0; i < base.length(); ++i) {
336             char c = base.charAt(i);
337
338             if (c != '/')
339                b.append(c);
340             else
341                b.append("::");
342          }
343
344          return b.toString();
345       } else if (id.startsWith("RMI:")) {
346          // RMI hashed format
347

348          // Get 2nd component of ID
349
int idx2 = id.indexOf(':', 4); // 2nd colon
350
if (idx2 == -1)
351            return null; // invalid ID, version part missing
352
String JavaDoc base = id.substring(4, id.indexOf(':', 4));
353
354          // convert '.' to "::"
355
StringBuffer JavaDoc b = new StringBuffer JavaDoc();
356          for (int i = 0; i < base.length(); ++i) {
357             char c = base.charAt(i);
358
359             if (c != '.')
360                b.append(c);
361             else
362                b.append("::");
363          }
364
365          return b.toString();
366       } else
367          return null;
368    }
369
370
371    // Inner classes -------------------------------------------------
372
}
373
Popular Tags