KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > rmi > RmiIdlUtil


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;
23
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.Member JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Modifier JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * This class contains a bunch of methods taken from
33  * org.jboss.verifier.strategy.AbstractVerifier. There they are declared
34  * as instance methods. I copied them to this class and made them static here
35  * in order to call them as utility methods, without having to create a
36  * verifier instance,
37  *
38  * @author <a HREF="mailto:juha.lindfors@jboss.org">Juha Lindfors</a>
39  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
40  * @author Vinay Menon (menonv@cpw.co.uk)
41  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
42  * @version $Revision: 42302 $
43  */

44 public class RmiIdlUtil {
45
46    public static boolean hasLegalRMIIIOPArguments(Method JavaDoc method) {
47       
48       Class JavaDoc[] params = method.getParameterTypes();
49       
50       for (int i = 0; i < params.length; ++i)
51          if (!isRMIIIOPType(params[i]))
52             return false;
53       
54       return true;
55    }
56    
57    public static boolean hasLegalRMIIIOPReturnType(Method JavaDoc method) {
58       return isRMIIIOPType(method.getReturnType());
59    }
60    
61    public static boolean hasLegalRMIIIOPExceptionTypes(Method JavaDoc method) {
62       
63       /*
64        * All checked exception classes used in method declarations
65        * (other than java.rmi.RemoteException) MUST be conforming
66        * RMI/IDL exception types.
67        *
68        * Spec 28.2.3 (4)
69        */

70       Iterator JavaDoc it = Arrays.asList(method.getExceptionTypes()).iterator();
71       
72       while (it.hasNext()) {
73          Class JavaDoc exception = (Class JavaDoc)it.next();
74          
75          if (!isRMIIDLExceptionType(exception))
76             return false;
77       }
78       
79       return true;
80    }
81    
82    /*
83     * checks if the method's throws clause includes java.rmi.RemoteException
84     * or a superclass of it.
85     */

86    public static boolean throwsRemoteException(Method JavaDoc method) {
87       
88       Class JavaDoc[] exception = method.getExceptionTypes();
89       
90       for (int i = 0; i < exception.length; ++i)
91          if (exception[i].isAssignableFrom(java.rmi.RemoteException JavaDoc.class))
92             return true;
93       
94       return false;
95    }
96    
97    /*
98     * checks if a class's member (method, constructor or field) has a 'static'
99     * modifier.
100     */

101    public static boolean isStatic(Member JavaDoc member) {
102       return (Modifier.isStatic(member.getModifiers()));
103    }
104    
105    /*
106     * checks if the given class is declared as static (inner classes only)
107     */

108    public static boolean isStatic(Class JavaDoc c) {
109       return (Modifier.isStatic(c.getModifiers()));
110    }
111    
112    /*
113     * checks if a class's member (method, constructor or field) has a 'final'
114     * modifier.
115     */

116    public static boolean isFinal(Member JavaDoc member) {
117       return (Modifier.isFinal(member.getModifiers()));
118    }
119    
120    /*
121     * checks if the given class is declared as final
122     */

123    public static boolean isFinal(Class JavaDoc c) {
124       return (Modifier.isFinal(c.getModifiers()));
125    }
126    
127    /*
128     * checks if a class's memeber (method, constructor or field) has a 'public'
129     * modifier.
130     */

131    public static boolean isPublic(Member JavaDoc member) {
132       return (Modifier.isPublic(member.getModifiers()));
133    }
134    
135    /*
136     * checks if the given class is declared as public
137     */

138    public static boolean isPublic(Class JavaDoc c) {
139       return (Modifier.isPublic(c.getModifiers()));
140    }
141    
142    /**
143     * Checks whether all the fields in the class are declared as public.
144     */

145    public static boolean isAllFieldsPublic(Class JavaDoc c) {
146       try {
147          Field JavaDoc list[] = c.getFields();
148          for(int i=0; i<list.length; i++)
149             if(!Modifier.isPublic(list[i].getModifiers()))
150                return false;
151       } catch(Exception JavaDoc e) {
152          return false;
153       }
154       return true;
155    }
156    
157    /*
158     * checks if the given class is declared as abstract
159     */

160    public static boolean isAbstract(Class JavaDoc c) {
161       return (Modifier.isAbstract(c.getModifiers()));
162    }
163    
164    public static boolean isRMIIIOPType(Class JavaDoc type) {
165       
166       /*
167        * Java Language to IDL Mapping
168        * ftp://ftp.omg.org/pub/docs/ptc/99-03-09.pdf
169        *
170        * A conforming RMI/IDL type is a Java type whose values
171        * may be transmitted across an RMI/IDL remote interface at
172        * run-time. A Java data type is a conforming RMI/IDL type
173        * if it is:
174        *
175        * - one of the Java primitive types (see Primitive Types on page 28-2).
176        * - a conforming remote interface (as defined in RMI/IDL Remote Interfaces on page 28-2).
177        * - a conforming value type (as defined in RMI/IDL Value Types on page 28-4).
178        * - an array of conforming RMI/IDL types (see RMI/IDL Arrays on page 28-5).
179        * - a conforming exception type (see RMI/IDL Exception Types on page 28-5).
180        * - a conforming CORBA object reference type (see CORBA Object Reference Types on page 28-6).
181        * - a conforming IDL entity type see IDL Entity Types on page 28-6).
182        */

183       
184       /*
185        * Primitive types.
186        *
187        * Spec 28.2.2
188        */

189         if (type.isPrimitive())
190            return true;
191         
192         /*
193          * Conforming array.
194          *
195          * Spec 28.2.5
196          */

197         if (type.isArray())
198            return isRMIIIOPType(type.getComponentType());
199         
200         /*
201          * Conforming CORBA reference type
202          *
203          * Spec 28.2.7
204          */

205         if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(type))
206            return true;
207         
208         /*
209          * Conforming IDL Entity type
210          *
211          * Spec 28.2.8
212          */

213         if (org.omg.CORBA.portable.IDLEntity JavaDoc.class.isAssignableFrom(type))
214            return true;
215         
216         /*
217          * Conforming remote interface.
218          *
219          * Spec 28.2.3
220          */

221         if (isRMIIDLRemoteInterface(type))
222            return true;
223         
224         /*
225          * Conforming exception.
226          *
227          * Spec 28.2.6
228          */

229         if (isRMIIDLExceptionType(type))
230            return true;
231         
232         /*
233          * Conforming value type.
234          *
235          * Spec 28.2.4
236          */

237         if (isRMIIDLValueType(type))
238            return true;
239         
240         return false;
241    }
242    
243    public static boolean isRMIIDLRemoteInterface(Class JavaDoc type) {
244       
245       /*
246        * If does not implement java.rmi.Remote, cannot be valid RMI-IDL
247        * remote interface.
248        */

249       if (!java.rmi.Remote JavaDoc.class.isAssignableFrom(type))
250          return false;
251       
252       Iterator JavaDoc methodIterator = Arrays.asList(type.getMethods()).iterator();
253       
254       while (methodIterator.hasNext()) {
255          Method JavaDoc m = (Method JavaDoc)methodIterator.next();
256          
257          /*
258           * All methods in the interface MUST throw
259           * java.rmi.RemoteException or a superclass of it.
260           *
261           * Spec 28.2.3 (2)
262           */

263          if (!throwsRemoteException(m)) {
264             return false;
265          }
266          
267          /*
268           * All checked exception classes used in method declarations
269           * (other than java.rmi.RemoteException) MUST be conforming
270           * RMI/IDL exception types.
271           *
272           * Spec 28.2.3 (4)
273           */

274          Iterator JavaDoc it = Arrays.asList(m.getExceptionTypes()).iterator();
275          
276          while (it.hasNext()) {
277             Class JavaDoc exception = (Class JavaDoc)it.next();
278             
279             if (!isRMIIDLExceptionType(exception))
280                return false;
281          }
282       }
283       
284       /*
285        * The constant values defined in the interface MUST be
286        * compile-time types of RMI/IDL primitive types or String.
287        *
288        * Spec 28.2.3 (6)
289        */

290       Iterator JavaDoc fieldIterator = Arrays.asList(type.getFields()).iterator();
291       
292       while (fieldIterator.hasNext()) {
293          
294          Field JavaDoc f = (Field JavaDoc)fieldIterator.next();
295          
296          if (f.getType().isPrimitive())
297             continue;
298          
299          if (f.getType().equals(java.lang.String JavaDoc.class))
300             continue;
301          
302          return false;
303       }
304       
305       return true;
306    }
307    
308    public static boolean isAbstractInterface(Class JavaDoc type) {
309       
310       /*
311        * It must be a Java interface.
312        */

313       if (!type.isInterface())
314          return false;
315
316       /*
317        * It must not be the interface of a CORBA object.
318        */

319       if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(type))
320          return false;
321       
322       /*
323        * It must not extend java.rmi.Remote directly or indirectly.
324        */

325       if (java.rmi.Remote JavaDoc.class.isAssignableFrom(type))
326          return false;
327       
328       Iterator JavaDoc methodIterator = Arrays.asList(type.getMethods()).iterator();
329       
330       while (methodIterator.hasNext()) {
331          Method JavaDoc m = (Method JavaDoc)methodIterator.next();
332          
333          /*
334           * All methods MUST throw java.rmi.RemoteException
335           * or a superclass of it.
336           */

337          if (!throwsRemoteException(m)) {
338             return false;
339          }
340          
341       }
342       
343       return true;
344    }
345    
346    public static boolean isRMIIDLExceptionType(Class JavaDoc type) {
347       
348       /*
349        * A conforming RMI/IDL Exception class MUST be a checked
350        * exception class and MUST be a valid RMI/IDL value type.
351        *
352        * Spec 28.2.6
353        */

354       if (!Throwable JavaDoc.class.isAssignableFrom(type))
355          return false;
356       
357       if (Error JavaDoc.class.isAssignableFrom(type))
358          return false;
359       
360       if (RuntimeException JavaDoc.class.isAssignableFrom(type))
361          return false;
362       
363       if (!isRMIIDLValueType(type))
364          return false;
365       
366       return true;
367    }
368    
369    public static boolean isRMIIDLValueType(Class JavaDoc type) {
370       
371       /*
372        * A value type MUST NOT either directly or indirectly implement the
373        * java.rmi.Remote interface.
374        *
375        * Spec 28.2.4 (4)
376        */

377       if (java.rmi.Remote JavaDoc.class.isAssignableFrom(type))
378          return false;
379       
380       
381       /*
382        * It cannot be a CORBA object.
383        */

384       if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(type))
385          return false;
386       
387       /*
388        * If class is a non-static inner class then its containing class must
389        * also be a conforming RMI/IDL value type.
390        *
391        * Spec 2.8.4 (3)
392        */

393       if (type.getDeclaringClass() != null && isStatic(type))
394          if (!isRMIIDLValueType(type.getDeclaringClass()))
395             return false;
396       
397       return true;
398    }
399
400    public static boolean isAbstractValueType(Class JavaDoc type) {
401       
402       if (!type.isInterface())
403          return false;
404
405       if (org.omg.CORBA.Object JavaDoc.class.isAssignableFrom(type))
406          return false;
407       
408       boolean cannotBeRemote = false;
409       boolean cannotBeAbstractInterface = false;
410
411       if (java.rmi.Remote JavaDoc.class.isAssignableFrom(type)) {
412          cannotBeAbstractInterface = true;
413       }
414       else {
415          cannotBeRemote = true;
416       }
417
418       Iterator JavaDoc methodIterator = Arrays.asList(type.getMethods()).iterator();
419
420       while (methodIterator.hasNext()) {
421          Method JavaDoc m = (Method JavaDoc)methodIterator.next();
422          
423          if (!throwsRemoteException(m)) {
424             cannotBeAbstractInterface = true;
425             cannotBeRemote = true;
426             break;
427          }
428
429          Iterator JavaDoc it = Arrays.asList(m.getExceptionTypes()).iterator();
430          
431          while (it.hasNext()) {
432             Class JavaDoc exception = (Class JavaDoc)it.next();
433             
434             if (!isRMIIDLExceptionType(exception)) {
435                cannotBeRemote = true;
436                break;
437             }
438          }
439       }
440       
441       if (!cannotBeRemote) {
442          Iterator JavaDoc fieldIterator = Arrays.asList(type.getFields()).iterator();
443       
444          while (fieldIterator.hasNext()) {
445             Field JavaDoc f = (Field JavaDoc)fieldIterator.next();
446          
447             if (f.getType().isPrimitive())
448                continue;
449             if (f.getType().equals(java.lang.String JavaDoc.class))
450                 continue;
451             cannotBeRemote = true;
452             break;
453          }
454       }
455       return cannotBeRemote && cannotBeAbstractInterface;
456    }
457     
458    public static void rethrowIfCorbaSystemException(Exception JavaDoc e)
459    {
460       if (e instanceof java.rmi.MarshalException JavaDoc)
461          throw new org.omg.CORBA.MARSHAL JavaDoc(e.toString());
462       else if (e instanceof java.rmi.NoSuchObjectException JavaDoc)
463          throw new org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc(e.toString());
464       else if (e instanceof java.rmi.AccessException JavaDoc)
465          throw new org.omg.CORBA.NO_PERMISSION JavaDoc(e.toString());
466       else if (e instanceof javax.transaction.TransactionRequiredException JavaDoc)
467          throw new org.omg.CORBA.TRANSACTION_REQUIRED JavaDoc(e.toString());
468       else if (e instanceof javax.transaction.TransactionRolledbackException JavaDoc)
469          throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc(e.toString());
470       else if (e instanceof javax.transaction.InvalidTransactionException JavaDoc)
471          throw new org.omg.CORBA.INVALID_TRANSACTION JavaDoc(e.toString());
472    }
473 }
474
Popular Tags