KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > config > classloader > ConfigurationSource


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17 package org.sape.carbon.services.config.classloader;
18
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLClassLoader JavaDoc;
22
23 import org.sape.carbon.core.exception.InvalidParameterException;
24
25 /**
26  * <p>This class encapsulates the backing data store for classloader
27  * based nodes.</p>
28  *
29  * Copyright 2003 Sapient
30  * @since carbon 2.1
31  * @author Douglas Voet, Nov 10, 2003
32  * @version $Revision: 1.1 $($Author: dvoet $ / $Date: 2003/11/11 21:19:58 $)
33  */

34 public class ConfigurationSource {
35     
36     private URL JavaDoc[] sourceURLs = null;
37     private ClassLoader JavaDoc sourceLoader;
38     
39     /**
40      * Constructs the ConfigurationSource using this class' Classloader
41      * as the backing data store.
42      */

43     public ConfigurationSource() {
44         this.sourceLoader = this.getClass().getClassLoader();
45     }
46     
47     /**
48      * Constructs the ConfigurationSource using a new URLClassloader
49      * as the backing data store.
50      *
51      * @param sourceURLs the URLs that are used to construct the URLClassloader
52      *
53      * @see URLClassLoader
54      */

55     public ConfigurationSource(URL JavaDoc[] sourceURLs) {
56         if (sourceURLs == null) {
57             throw new InvalidParameterException (
58                 this.getClass(),
59                 "sourceURLs cannot be null");
60         }
61         
62         this.sourceURLs = sourceURLs;
63         this.sourceLoader = new URLClassLoader JavaDoc(sourceURLs);
64     }
65     
66     /**
67      * If a new URLClassloader is being used as the backing data store, this
68      * method will dereference the current URLClassloader, allowing it to
69      * be garbage collected, and create a new one. Otherwise this method
70      * does nothing as there is no way to unload this class' classloader.
71      */

72     public void refresh() {
73         if (this.sourceURLs != null) {
74             synchronized (this) {
75                 this.sourceLoader = new URLClassLoader JavaDoc(sourceURLs);
76             }
77         }
78     }
79     
80     /**
81      * Gets the InputStream for the given resource or null if the resouce does
82      * not exists.
83      *
84      * @see ClassLoader#getResourceAsStream(java.lang.String)
85      * @param resourcePath the resource to get
86      * @return the resource's InputStream
87      */

88     public InputStream JavaDoc getResourceAsStream(String JavaDoc resourcePath) {
89         if (this.sourceURLs != null) {
90             // synchronize only if it is possible to refresh
91
synchronized (this) {
92                 return this.sourceLoader.getResourceAsStream(resourcePath);
93             }
94         } else {
95             return this.sourceLoader.getResourceAsStream(resourcePath);
96         }
97     }
98     
99     /**
100      * Gets the URL for the given resource or null if the resouce does
101      * not exists.
102      *
103      * @see ClassLoader#getResource(java.lang.String)
104      * @param resourcePath the resource to get
105      * @return the resource's URL
106      */

107     public URL JavaDoc getResource(String JavaDoc resourcePath) {
108         if (this.sourceURLs != null) {
109             // synchronize only if it is possible to refresh
110
synchronized (this) {
111                 return this.sourceLoader.getResource(resourcePath);
112             }
113         } else {
114             return this.sourceLoader.getResource(resourcePath);
115         }
116     }
117 }
118
Popular Tags