KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > classloader > SarLoaderResolver


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.classloader;
88
89 import java.io.File JavaDoc;
90 import java.net.URL JavaDoc;
91 import java.security.Policy JavaDoc;
92 import java.util.Arrays JavaDoc;
93 import java.util.Set JavaDoc;
94 import org.codehaus.loom.classman.builder.SimpleLoaderResolver;
95 import org.codehaus.loom.components.extensions.pkgmgr.OptionalPackage;
96 import org.codehaus.loom.components.extensions.pkgmgr.PackageManager;
97 import org.codehaus.loom.components.util.ResourceUtil;
98 import org.codehaus.loom.extension.Extension;
99 import org.codehaus.spice.salt.i18n.ResourceManager;
100 import org.codehaus.spice.salt.i18n.Resources;
101 import org.codehaus.dna.LogEnabled;
102 import org.codehaus.dna.Logger;
103 import org.codehaus.dna.impl.ContainerUtil;
104
105 /**
106  * a LoaderResolver that knows about container environment, and the way it is
107  * split across multiple directories.
108  *
109  * @author Peter Donald
110  * @version $Revision: 1.4 $ $Date: 2004/08/17 23:14:32 $
111  */

112 class SarLoaderResolver
113     extends SimpleLoaderResolver
114     implements LogEnabled
115 {
116     private final static Resources REZ =
117         ResourceManager.getPackageResources( SarLoaderResolver.class );
118
119     /** The PackageManager to use to resolve Extensions. */
120     private PackageManager m_manager;
121
122     /** Logger to use when reporting information */
123     private Logger m_logger;
124
125     /** The policy object to use when creating ClassLoaders */
126     private Policy JavaDoc m_policy;
127
128     /** Base work directory for application. */
129     private File JavaDoc m_workDirectory;
130
131     /**
132      * Create a resolver for a jar. The resolver merges both the work and base
133      * directory hierarchies.
134      *
135      * @param manager the PackageManager
136      * @param policy the policy to use when creating classloaders
137      * @param baseDirectory the base directory
138      * @param workDirectory the base work directory
139      */

140     SarLoaderResolver( final PackageManager manager,
141                        final Policy JavaDoc policy,
142                        final File JavaDoc baseDirectory,
143                        final File JavaDoc workDirectory )
144     {
145         super( baseDirectory );
146         if( null == manager )
147         {
148             throw new NullPointerException JavaDoc( "manager" );
149         }
150         if( null == policy )
151         {
152             throw new NullPointerException JavaDoc( "policy" );
153         }
154         if( null == baseDirectory )
155         {
156             throw new NullPointerException JavaDoc( "baseDirectory" );
157         }
158         if( null == workDirectory )
159         {
160             throw new NullPointerException JavaDoc( "workDirectory" );
161         }
162
163         m_manager = manager;
164         m_policy = policy;
165         m_workDirectory = workDirectory;
166     }
167
168     /**
169      * Aquire an Avalon Logger.
170      *
171      * @param logger the avalon logger
172      */

173     public void enableLogging( final Logger logger )
174     {
175         m_logger = logger;
176     }
177
178     public URL JavaDoc resolveExtension( final Extension extension )
179         throws Exception JavaDoc
180     {
181         if( null == m_manager )
182         {
183             final String JavaDoc message =
184                 REZ.getString( "missing-packagemanager" );
185             throw new IllegalStateException JavaDoc( message );
186         }
187         final OptionalPackage optionalPackage =
188             m_manager.getOptionalPackage( extension );
189         return optionalPackage.getFile().toURL();
190     }
191
192     /**
193      * Resolve a location to either the work or home hierarchys.
194      *
195      * @param location the location
196      * @return the URL representing location
197      * @throws Exception if unable to resolve location
198      */

199     public URL JavaDoc resolveURL( final String JavaDoc location )
200         throws Exception JavaDoc
201     {
202         final File JavaDoc file =
203             ResourceUtil.getFileForResource( location,
204                                              getBaseDirectory(),
205                                              m_workDirectory );
206         return file.toURL();
207     }
208
209     /**
210      * Resolve a fileset. Make sure it is resolved against both the work and the
211      * base directories of application.
212      *
213      * @param baseDirectory the base directory of fileset
214      * @param includes the fileset includes
215      * @param excludes the ant style excludes
216      * @return the URLs that are in fileset
217      * @throws Exception if unable to resolve fileset
218      */

219     public URL JavaDoc[] resolveFileSet( final String JavaDoc baseDirectory,
220                                  final String JavaDoc[] includes,
221                                  final String JavaDoc[] excludes )
222         throws Exception JavaDoc
223     {
224         final URL JavaDoc[] baseURLs =
225             resolveFileSet( getBaseDirectory(),
226                             baseDirectory,
227                             includes,
228                             excludes );
229         final URL JavaDoc[] workURLs =
230             resolveFileSet( m_workDirectory,
231                             baseDirectory,
232                             includes,
233                             excludes );
234         final URL JavaDoc[] urls = new URL JavaDoc[ baseURLs.length + workURLs.length ];
235         System.arraycopy( baseURLs, 0, urls, 0, baseURLs.length );
236         System.arraycopy( workURLs,
237                           0,
238                           urls,
239                           baseURLs.length,
240                           workURLs.length );
241         return urls;
242     }
243
244     /**
245      * Create a ClassLoader that obeys policy in environment.xml.
246      *
247      * @param parent the parent classloader
248      * @param urls the set of URLs for classloader
249      * @return the new classloader
250      * @throws Exception if unable to create classloader
251      */

252     public ClassLoader JavaDoc createClassLoader( final ClassLoader JavaDoc parent,
253                                           final URL JavaDoc[] urls )
254         throws Exception JavaDoc
255     {
256         final URL JavaDoc[] classpath = determineCompleteClasspath( urls );
257         if( m_logger.isDebugEnabled() )
258         {
259             final String JavaDoc message =
260                 REZ.format( "resolver.loader-urls.notice",
261                             Arrays.asList( classpath ) );
262             m_logger.debug( message );
263         }
264         final PolicyClassLoader loader =
265             new PolicyClassLoader( classpath, parent, m_policy );
266         ContainerUtil.enableLogging( loader, m_logger );
267         return loader;
268     }
269
270     /**
271      * Route Logging to Avalons Logger.
272      *
273      * @param message the debug message
274      */

275     protected void debug( final String JavaDoc message )
276     {
277         m_logger.debug( message );
278     }
279
280     /**
281      * Defer to Avalons Logger to see if debug is enabled.
282      *
283      * @return true if debug is enabled
284      */

285     protected boolean isDebugEnabled()
286     {
287         return m_logger.isDebugEnabled();
288     }
289
290     /**
291      * Route Logging to Logger facade.
292      *
293      * @param message the warn message
294      */

295     protected void warn( final String JavaDoc message )
296     {
297         m_logger.warn( message );
298     }
299
300     /**
301      * Route Logging to Logger facade.
302      *
303      * @param message the warn message
304      */

305     protected void warn( final String JavaDoc message,
306                          final Throwable JavaDoc t )
307     {
308         m_logger.warn( message, t );
309     }
310
311     protected void scanDependencies( final Extension[] required,
312                                      final Extension[] available,
313                                      final Set JavaDoc dependencies,
314                                      final Set JavaDoc unsatisfied )
315     {
316         m_manager.scanDependencies( required,
317                                     available,
318                                     dependencies,
319                                     unsatisfied );
320     }
321 }
322
Popular Tags