KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import org.quartz.spi.ClassLoadHelper;
29
30 /**
31  * A <code>ClassLoadHelper</code> uses all of the <code>ClassLoadHelper</code>
32  * types that are found in this package in its attempts to load a class, when
33  * one scheme is found to work, it is promoted to the scheme that will be used
34  * first the next time a class is loaded (in order to improve perfomance).
35  *
36  * <p>
37  * This approach is used because of the wide variance in class loader behavior
38  * between the various environments in which Quartz runs (e.g. disparate
39  * application servers, stand-alone, mobile devices, etc.). Because of this
40  * disparity, Quartz ran into difficulty with a one class-load style fits-all
41  * design. Thus, this class loader finds the approach that works, then
42  * 'remembers' it.
43  * </p>
44  *
45  * @see org.quartz.spi.ClassLoadHelper
46  * @see org.quartz.simpl.LoadingLoaderClassLoadHelper
47  * @see org.quartz.simpl.SimpleClassLoadHelper
48  * @see org.quartz.simpl.ThreadContextClassLoadHelper
49  * @see org.quartz.simpl.InitThreadContextClassLoadHelper
50  *
51  * @author jhouse
52  */

53 public class CascadingClassLoadHelper implements ClassLoadHelper {
54
55     
56     /*
57      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58      *
59      * Data members.
60      *
61      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62      */

63
64     private LinkedList JavaDoc loadHelpers;
65
66     private ClassLoadHelper bestCandidate;
67
68     /*
69      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70      *
71      * Interface.
72      *
73      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74      */

75
76     /**
77      * Called to give the ClassLoadHelper a chance to initialize itself,
78      * including the oportunity to "steal" the class loader off of the calling
79      * thread, which is the thread that is initializing Quartz.
80      */

81     public void initialize() {
82         loadHelpers = new LinkedList JavaDoc();
83
84         loadHelpers.add(new LoadingLoaderClassLoadHelper());
85         loadHelpers.add(new SimpleClassLoadHelper());
86         loadHelpers.add(new ThreadContextClassLoadHelper());
87         loadHelpers.add(new InitThreadContextClassLoadHelper());
88         
89         Iterator JavaDoc iter = loadHelpers.iterator();
90         while (iter.hasNext()) {
91             ClassLoadHelper loadHelper = (ClassLoadHelper) iter.next();
92             loadHelper.initialize();
93         }
94     }
95
96     /**
97      * Return the class with the given name.
98      */

99     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
100
101         if (bestCandidate != null) {
102             try {
103                 return bestCandidate.loadClass(name);
104             } catch (Exception JavaDoc e) {
105                 bestCandidate = null;
106             }
107         }
108
109         ClassNotFoundException JavaDoc cnfe = null;
110         Class JavaDoc clazz = null;
111         ClassLoadHelper loadHelper = null;
112
113         Iterator JavaDoc iter = loadHelpers.iterator();
114         while (iter.hasNext()) {
115             loadHelper = (ClassLoadHelper) iter.next();
116
117             try {
118                 clazz = loadHelper.loadClass(name);
119                 break;
120             } catch (ClassNotFoundException JavaDoc e) {
121                 cnfe = e;
122             }
123         }
124
125         if (clazz == null) {
126             throw cnfe;
127         }
128
129         bestCandidate = loadHelper;
130
131         return clazz;
132     }
133
134     /**
135      * Finds a resource with a given name. This method returns null if no
136      * resource with this name is found.
137      * @param name name of the desired resource
138      * @return a java.net.URL object
139      */

140     public URL JavaDoc getResource(String JavaDoc name) {
141
142         if (bestCandidate != null) {
143             try {
144                 return bestCandidate.getResource(name);
145             } catch (Exception JavaDoc e) {
146                 bestCandidate = null;
147             }
148         }
149
150         URL JavaDoc result = null;
151         ClassLoadHelper loadHelper = null;
152
153         Iterator JavaDoc iter = loadHelpers.iterator();
154         while (iter.hasNext()) {
155             loadHelper = (ClassLoadHelper) iter.next();
156
157             result = loadHelper.getResource(name);
158             if (result != null) {
159                 break;
160             }
161         }
162
163         bestCandidate = loadHelper;
164         return result;
165
166     }
167
168     /**
169      * Finds a resource with a given name. This method returns null if no
170      * resource with this name is found.
171      * @param name name of the desired resource
172      * @return a java.io.InputStream object
173      */

174     public InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
175
176         if (bestCandidate != null) {
177             try {
178                 return bestCandidate.getResourceAsStream(name);
179             } catch (Exception JavaDoc e) {
180                 bestCandidate = null;
181             }
182         }
183
184         InputStream JavaDoc result = null;
185         ClassLoadHelper loadHelper = null;
186
187         Iterator JavaDoc iter = loadHelpers.iterator();
188         while (iter.hasNext()) {
189             loadHelper = (ClassLoadHelper) iter.next();
190
191             result = loadHelper.getResourceAsStream(name);
192             if (result != null) {
193                 break;
194             }
195         }
196
197         bestCandidate = loadHelper;
198         return result;
199
200     }
201
202 }
203
Popular Tags