KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > ir > RepositoryID


1 package org.jacorb.ir;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.StringTokenizer JavaDoc;
24 import org.omg.CORBA.portable.BoxedValueHelper JavaDoc;
25 import org.omg.CORBA.INTF_REPOS JavaDoc;
26
27 /**
28  * This class builds CORBA repository IDs from Java classes
29  * or class names, or builds Java class names from repository
30  * IDs
31  */

32 public class RepositoryID
33 {
34     /**
35      * Returns the fully qualified name of the Java class to which
36      * the given Repository ID is mapped.
37      */

38     public static String JavaDoc className (String JavaDoc repId,
39                                     ClassLoader JavaDoc loader)
40     {
41         return className (repId, null, loader);
42     }
43
44     /**
45      * Returns the fully qualified name of the Java class to which
46      * the given Repository ID is mapped, with a given suffix appended
47      * to the class name. For example, the string "Helper" can be used
48      * as the suffix to find the helper class for a given Repository ID.
49      */

50     public static String JavaDoc className (String JavaDoc repId,
51                                     String JavaDoc suffix,
52                                     ClassLoader JavaDoc loader)
53     {
54         if (repId.startsWith ("RMI:"))
55         {
56             return repId.substring (4, repId.indexOf (':', 4))
57                    + ( suffix != null ? suffix : "" );
58         }
59         else if (repId.startsWith ("IDL:"))
60         {
61             String JavaDoc id = repId.substring (4, repId.lastIndexOf(':'))
62                         + ( suffix != null ? suffix : "" );
63             if (id.equals ("omg.org/CORBA/WStringValue"))
64                 return "java.lang.String";
65             else
66             {
67                 int firstSlash = id.indexOf ("/");
68                 String JavaDoc prefix = id.substring (0, firstSlash);
69
70                 if (prefix.equals ("omg.org"))
71                     return ir2scopes ("org.omg",
72                                       id.substring (firstSlash + 1),
73                                       loader);
74                 else if (prefix.indexOf ('.') != -1)
75                     return ir2scopes (reversePrefix (prefix),
76                                       id.substring (firstSlash + 1),
77                                       loader);
78                 else
79                     return ir2scopes ("", id, loader);
80             }
81         }
82         else
83         {
84            throw new INTF_REPOS JavaDoc ("Unrecognized RepositoryID: " + repId);
85         }
86     }
87
88     private static final String JavaDoc reversePrefix (String JavaDoc prefix)
89     {
90         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc (prefix, ".");
91         String JavaDoc result = tok.nextToken();
92
93         while (tok.hasMoreTokens())
94         {
95             result = tok.nextToken() + '.' + result;
96         }
97         return result;
98     }
99
100     /**
101      * FIXME: This method needs documentation.
102      * What does this algorithm do, and why is it necessary? AS.
103      */

104     private static String JavaDoc ir2scopes (String JavaDoc prefix,
105                                      String JavaDoc s,
106                                      ClassLoader JavaDoc loader)
107     {
108         if( s.indexOf("/") < 0)
109             return s;
110         java.util.StringTokenizer JavaDoc strtok =
111             new java.util.StringTokenizer JavaDoc( s, "/" );
112
113         int count = strtok.countTokens();
114         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
115         sb.append(prefix);
116
117         for( int i = 0; strtok.hasMoreTokens(); i++ )
118         {
119             String JavaDoc sc = strtok.nextToken();
120             Class JavaDoc c = null;
121             if( sb.toString().length() > 0 )
122                 c = loadClass (sb.toString() + "." + sc, loader);
123             else
124                 c = loadClass (sc, loader);
125             if (c == null)
126                 if( sb.toString().length() > 0 )
127                     sb.append( "." + sc );
128                 else
129                     sb.append( sc );
130             else
131                 if( i < count-1)
132                     sb.append( "." + sc + "Package");
133                 else
134                     sb.append( "." + sc );
135         }
136
137         return sb.toString();
138     }
139
140     public static String JavaDoc repId (Class JavaDoc c)
141     {
142         if (org.omg.CORBA.portable.IDLEntity JavaDoc.class.isAssignableFrom (c))
143         {
144             String JavaDoc className = c.getName();
145             String JavaDoc head = "";
146             String JavaDoc body = "";
147
148             // add "IDL:" and ":1.0"
149
// and swap "org.omg" if necessary
150

151             if( className.startsWith("org.omg") ||
152                 className.startsWith("org/omg") )
153             {
154                 if( className.length() > 7 )
155                     body = className.substring(7);
156                 return "IDL:omg.org/" + scopesToIR(body) + ":1.0";
157             }
158             else
159                 return "IDL:" + scopesToIR(className) + ":1.0" ;
160         }
161     else
162             return org.jacorb.util.ValueHandler.getRMIRepositoryID (c);
163     }
164
165
166     private static String JavaDoc scopesToIR( String JavaDoc s )
167     {
168         if( s.indexOf(".") < 0)
169             return s;
170         java.util.StringTokenizer JavaDoc strtok =
171             new java.util.StringTokenizer JavaDoc( s, "." );
172
173         String JavaDoc scopes[] = new String JavaDoc[strtok.countTokens()];
174
175         for( int i = 0; strtok.hasMoreTokens(); i++ )
176         {
177             String JavaDoc sc = strtok.nextToken();
178             if( sc.endsWith("Package"))
179                 scopes[i] = sc.substring(0,sc.indexOf("Package"));
180             else
181                 scopes[i] = sc;
182         }
183
184         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
185         if( scopes.length > 1 )
186         {
187             for( int i = 0; i < scopes.length-1; i++)
188                 sb.append( scopes[i] + "/" );
189         }
190
191         sb.append( scopes[scopes.length-1] );
192         return sb.toString();
193     }
194
195     /**
196      * Converts a class name to a Repository ID.
197      * @param classname the class name to convert
198      * @param resolveClass indicates whether the method should try to
199      * resolve and load the class. If true and the class could
200      * not be loaded, an IllegalArgumentException will be thrown
201      */

202     public static String JavaDoc toRepositoryID ( String JavaDoc className,
203                                           boolean resolveClass,
204                                           ClassLoader JavaDoc loader )
205     {
206         if( className.equals("") ||
207             className.startsWith("IDL:") ||
208             className.startsWith ("RMI:"))
209             return className;
210         else
211         {
212             if( resolveClass )
213             {
214
215                 Class JavaDoc c = loadClass(className, loader);
216                 if (c == null)
217                     throw new IllegalArgumentException JavaDoc("cannot find class: " + className);
218                 else
219                     return repId (c);
220             }
221             return "IDL:" + className + ":1.0";
222         }
223     }
224
225     public static String JavaDoc toRepositoryID( String JavaDoc className, ClassLoader JavaDoc loader )
226     {
227         return toRepositoryID( className, true, loader );
228     }
229
230     /**
231      * Loads class `name' using an appropriate class loader.
232      * Returns the corresponding class object, or null if the class loader
233      * cannot find a class by that name.
234      */

235     private static Class JavaDoc loadClass (String JavaDoc name, ClassLoader JavaDoc loader)
236     {
237         try
238         {
239             if (loader != null)
240                 return loader.loadClass (name);
241             else
242                 return org.jacorb.util.ObjectUtil.classForName(name);
243         }
244         catch (ClassNotFoundException JavaDoc e)
245         {
246             return null;
247         }
248     }
249
250     /**
251      * Creates a BoxedValueHelper instance for a given repository ID.
252      * @param repId the repository ID of the boxed value type
253      * @return a newly created BoxedValueHelper, or null if no
254      * BoxedValueHelper class can be found for that ID
255      * @throws RuntimeException if creation of the Helper instance fails
256      */

257     public static BoxedValueHelper JavaDoc createBoxedValueHelper(String JavaDoc repId,
258                                                           ClassLoader JavaDoc loader)
259     {
260         String JavaDoc className = className(repId, "Helper", loader);
261         Class JavaDoc c = loadClass(className, loader);
262         if (c != null)
263             try
264             {
265                 return (BoxedValueHelper JavaDoc)c.newInstance();
266             }
267             catch (Exception JavaDoc e)
268             {
269                 throw new RuntimeException JavaDoc(e.toString());
270             }
271         else
272             return null;
273     }
274 }
275
Popular Tags