KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > simpl > SimpleClassLoadHelper


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.simpl;
22
23 import org.quartz.spi.ClassLoadHelper;
24
25 import java.lang.reflect.AccessibleObject JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.io.InputStream JavaDoc;
29
30 /**
31  * A <code>ClassLoadHelper</code> that simply calls <code>Class.forName(..)</code>.
32  *
33  * @see org.quartz.spi.ClassLoadHelper
34  * @see org.quartz.simpl.ThreadContextClassLoadHelper
35  * @see org.quartz.simpl.CascadingClassLoadHelper
36  * @see org.quartz.simpl.LoadingLoaderClassLoadHelper
37  *
38  * @author jhouse
39  */

40 public class SimpleClassLoadHelper implements ClassLoadHelper {
41
42     /*
43      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44      *
45      * Interface.
46      *
47      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48      */

49
50     /**
51      * Called to give the ClassLoadHelper a chance to initialize itself,
52      * including the oportunity to "steal" the class loader off of the calling
53      * thread, which is the thread that is initializing Quartz.
54      */

55     public void initialize() {
56     }
57
58     /**
59      * Return the class with the given name.
60      */

61     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
62         return Class.forName(name);
63     }
64
65     /**
66      * Finds a resource with a given name. This method returns null if no
67      * resource with this name is found.
68      * @param name name of the desired resource
69      * @return a java.net.URL object
70      */

71     public URL JavaDoc getResource(String JavaDoc name) {
72         return getClassLoader().getResource(name);
73     }
74
75     /**
76      * Finds a resource with a given name. This method returns null if no
77      * resource with this name is found.
78      * @param name name of the desired resource
79      * @return a java.io.InputStream object
80      */

81     public InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
82         return getClassLoader().getResourceAsStream(name);
83     }
84
85     private ClassLoader JavaDoc getClassLoader() {
86         // To follow the same behavior of Class.forName(...) I had to play
87
// dirty (Supported by Sun, IBM & BEA JVMs)
88
// ToDo - Test it more.
89
try {
90             // Get a reference to this class' class-loader
91
ClassLoader JavaDoc cl = this.getClass().getClassLoader();
92             // Create a method instance represnting the protected
93
// getCallerClassLoader method of class ClassLoader
94
Method JavaDoc mthd = ClassLoader JavaDoc.class.getDeclaredMethod(
95                     "getCallerClassLoader", new Class JavaDoc[0]);
96             // Make the method accessible.
97
AccessibleObject.setAccessible(new AccessibleObject JavaDoc[] {mthd}, true);
98             // Try to get the caller's class-loader
99
return (ClassLoader JavaDoc)mthd.invoke(cl, new Object JavaDoc[0]);
100         } catch (Exception JavaDoc all) {
101             // Use this class' class-loader
102
return this.getClass().getClassLoader();
103         }
104     }
105
106 }
107
Popular Tags