KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > util > JDKBridge


1 /*
2  * @(#)JDKBridge.java 1.100 04/03/01
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.util;
17
18 import java.rmi.Remote JavaDoc;
19 import java.rmi.NoSuchObjectException JavaDoc;
20 import java.rmi.server.RMIClassLoader JavaDoc;
21 import java.rmi.server.UnicastRemoteObject JavaDoc;
22 import org.omg.CORBA.BAD_PARAM JavaDoc;
23 import org.omg.CORBA.CompletionStatus JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedAction JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import com.sun.corba.se.impl.orbutil.GetPropertyAction;
31
32 /**
33  * Utility methods for doing various method calls which are used
34  * by multiple classes
35  */

36 public class JDKBridge {
37  
38     /**
39      * Get local codebase System property (java.rmi.server.codebase).
40      * May be null or a space separated array of URLS.
41      */

42     public static String JavaDoc getLocalCodebase () {
43         return localCodebase;
44     }
45   
46     /**
47      * Return true if the system property "java.rmi.server.useCodebaseOnly"
48      * is set, false otherwise.
49      */

50     public static boolean useCodebaseOnly () {
51         return useCodebaseOnly;
52     }
53     
54     /**
55      * Returns a class instance for the specified class.
56      * @param className the name of the class
57      * @param remoteCodebase a space-separated array of urls at which
58      * the class might be found. May be null.
59      * @param loader a ClassLoader who may be used to
60      * load the class if all other methods fail.
61      * @return the <code>Class</code> object representing the loaded class.
62      * @exception throws ClassNotFoundException if class cannot be loaded.
63      */

64     public static Class JavaDoc loadClass (String JavaDoc className,
65                                    String JavaDoc remoteCodebase,
66                                    ClassLoader JavaDoc loader)
67     throws ClassNotFoundException JavaDoc {
68         
69         if (loader == null) {
70             return loadClassM(className,remoteCodebase,useCodebaseOnly);
71         } else {
72             try {
73                 return loadClassM(className,remoteCodebase,useCodebaseOnly);
74             } catch (ClassNotFoundException JavaDoc e) {
75                 return loader.loadClass(className);
76             }
77         }
78     }
79     
80     /**
81      * Returns a class instance for the specified class.
82      * @param className the name of the class
83      * @param remoteCodebase a space-separated array of urls at which
84      * the class might be found. May be null.
85      * @return the <code>Class</code> object representing the loaded class.
86      * @exception throws ClassNotFoundException if class cannot be loaded.
87      */

88     public static Class JavaDoc loadClass (String JavaDoc className,
89                                    String JavaDoc remoteCodebase)
90     throws ClassNotFoundException JavaDoc {
91         return loadClass(className,remoteCodebase,null);
92     }
93     
94     /**
95      * Returns a class instance for the specified class.
96      * @param className the name of the class
97      * @return the <code>Class</code> object representing the loaded class.
98      * @exception throws ClassNotFoundException if class cannot be loaded.
99      */

100     public static Class JavaDoc loadClass (String JavaDoc className)
101     throws ClassNotFoundException JavaDoc {
102         return loadClass(className,null,null);
103     }
104
105     private static final String JavaDoc LOCAL_CODEBASE_KEY = "java.rmi.server.codebase";
106     private static final String JavaDoc USE_CODEBASE_ONLY_KEY = "java.rmi.server.useCodebaseOnly";
107     private static String JavaDoc localCodebase = null;
108     private static boolean useCodebaseOnly;
109
110     static {
111         setCodebaseProperties();
112     }
113  
114     public static final void main (String JavaDoc[] args) {
115         System.out.println("1.2 VM");
116         
117     /*
118          // If on 1.2, use a policy with all permissions.
119          System.setSecurityManager (new javax.rmi.download.SecurityManager());
120          String targetClass = "[[Lrmic.Typedef;";
121          System.out.println("localCodebase = "+localCodebase);
122          System.out.println("Trying to load "+targetClass);
123          try {
124          Class clz = loadClass(targetClass,null,localCodebase);
125          System.out.println("Loaded: "+clz);
126          } catch (ClassNotFoundException e) {
127          System.out.println("Caught "+e);
128          }
129     */

130     }
131  
132     /**
133      * Set the codebase and useCodebaseOnly properties. This is public
134      * only for test code.
135      */

136     public static synchronized void setCodebaseProperties () {
137         String JavaDoc prop = (String JavaDoc)AccessController.doPrivileged(
138             new GetPropertyAction(LOCAL_CODEBASE_KEY)
139         );
140         if (prop != null && prop.trim().length() > 0) {
141             localCodebase = prop;
142         }
143
144         prop = (String JavaDoc)AccessController.doPrivileged(
145             new GetPropertyAction(USE_CODEBASE_ONLY_KEY)
146         );
147         if (prop != null && prop.trim().length() > 0) {
148             useCodebaseOnly = Boolean.valueOf(prop).booleanValue();
149         }
150     }
151
152     /**
153      * Set the default code base. This method is here only
154      * for test code.
155      */

156     public static synchronized void setLocalCodebase(String JavaDoc codebase) {
157         localCodebase = codebase;
158     }
159  
160     private static Class JavaDoc loadClassM (String JavaDoc className,
161                             String JavaDoc remoteCodebase,
162                             boolean useCodebaseOnly)
163         throws ClassNotFoundException JavaDoc {
164
165         try {
166             return JDKClassLoader.loadClass(null,className);
167         } catch (ClassNotFoundException JavaDoc e) {}
168         try {
169             if (!useCodebaseOnly && remoteCodebase != null) {
170                 return RMIClassLoader.loadClass(remoteCodebase,
171                                                 className);
172             } else {
173                 return RMIClassLoader.loadClass(className);
174             }
175         } catch (MalformedURLException JavaDoc e) {
176             className = className + ": " + e.toString();
177         }
178
179         throw new ClassNotFoundException JavaDoc(className);
180     }
181 }
182
183
Popular Tags