|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultSemanticsRepository.java | 50% | 69.2% | 77.8% | 65.1% |
|
1 | package org.drools.smf; | |
2 | ||
3 | /* | |
4 | * $Id: DefaultSemanticsRepository.java,v 1.8.2.2 2005/04/07 17:32:15 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved. | |
7 | * | |
8 | * Redistribution and use of this software and associated documentation | |
9 | * ("Software"), with or without modification, are permitted provided that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. (http://werken.com/) | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
30 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
39 | * POSSIBILITY OF SUCH DAMAGE. | |
40 | * | |
41 | */ | |
42 | ||
43 | import java.io.BufferedReader; | |
44 | import java.io.IOException; | |
45 | import java.io.InputStream; | |
46 | import java.io.InputStreamReader; | |
47 | import java.net.URL; | |
48 | import java.util.Enumeration; | |
49 | import java.util.HashSet; | |
50 | import java.util.Set; | |
51 | ||
52 | /** | |
53 | * Default <code>SemanticsRepository</code> which uses discovery to autoload | |
54 | * semantic modules. | |
55 | * | |
56 | * <p> | |
57 | * Any <b>semantic module </b> that conforms to the SMF contract will be | |
58 | * discovered and autoloaded upon first access of the | |
59 | * <code>DefaultSemanticsRepository</code>. | |
60 | * </p> | |
61 | * | |
62 | * <p> | |
63 | * To be discovered, the module should be in a jar with a module descriptor | |
64 | * located at <code>/META-INF/drools-semantics.properties</code>. | |
65 | * </p> | |
66 | * | |
67 | * @author <a href="mailto:bob@werken.com">bob mcwhirter </a> | |
68 | * | |
69 | * @version $Id: DefaultSemanticsRepository.java,v 1.4 2004/06/22 17:17:27 bob | |
70 | * Exp $ | |
71 | */ | |
72 | public final class DefaultSemanticsRepository | |
73 | implements | |
74 | SemanticsRepository | |
75 | { | |
76 | // ---------------------------------------------------------------------- | |
77 | // Class members | |
78 | // ---------------------------------------------------------------------- | |
79 | ||
80 | /** Singleton instance, lazily initialized. */ | |
81 | private static SemanticsRepository INSTANCE; | |
82 | ||
83 | private ClassLoader classLoader; | |
84 | ||
85 | // ---------------------------------------------------------------------- | |
86 | // Class methods | |
87 | // ---------------------------------------------------------------------- | |
88 | ||
89 | /** | |
90 | * Retrieve the singleton instance. | |
91 | * | |
92 | * @return The singleton instance. | |
93 | * | |
94 | * @throws Exception | |
95 | * If an error occurs while performing discovery and loading of | |
96 | * the semantic modules. | |
97 | */ | |
98 | 72 | public static synchronized SemanticsRepository getInstance() throws IOException, |
99 | SemanticsReaderException | |
100 | { | |
101 | 72 | if ( INSTANCE == null ) |
102 | { | |
103 | 6 | INSTANCE = new DefaultSemanticsRepository( ); |
104 | } | |
105 | ||
106 | 72 | return INSTANCE; |
107 | } | |
108 | ||
109 | // ---------------------------------------------------------------------- | |
110 | // Instance members | |
111 | // ---------------------------------------------------------------------- | |
112 | ||
113 | private Set loadedSemantics; | |
114 | ||
115 | /** Module repository. */ | |
116 | private SimpleSemanticsRepository repository; | |
117 | ||
118 | // ---------------------------------------------------------------------- | |
119 | // Constructors | |
120 | // ---------------------------------------------------------------------- | |
121 | ||
122 | /** | |
123 | * Construct. | |
124 | * | |
125 | * @throws Exception | |
126 | * If an error occurs while performing discovery and loading of | |
127 | * the semantic modules. | |
128 | */ | |
129 | 6 | private DefaultSemanticsRepository() throws IOException, |
130 | SemanticsReaderException | |
131 | { | |
132 | 6 | this.loadedSemantics = new HashSet( ); |
133 | 6 | this.repository = new SimpleSemanticsRepository( ); |
134 | 6 | init( ); |
135 | } | |
136 | ||
137 | // ---------------------------------------------------------------------- | |
138 | // Instance methods | |
139 | // ---------------------------------------------------------------------- | |
140 | ||
141 | /** | |
142 | * Initialize and perform discovery. | |
143 | * | |
144 | * @throws Exception | |
145 | * If an error occurs while performing discovery and loading of | |
146 | * the semantic modules. | |
147 | */ | |
148 | 6 | protected void init() throws IOException, |
149 | SemanticsReaderException | |
150 | { | |
151 | 6 | String droolsConfigProp = System.getProperty( "drools.conf" ); |
152 | ||
153 | 6 | if ( droolsConfigProp != null ) |
154 | { | |
155 | 0 | loadConfig( droolsConfigProp ); |
156 | } | |
157 | ||
158 | 6 | ClassLoader cl = Thread.currentThread( ).getContextClassLoader( ); |
159 | ||
160 | 6 | if ( cl == null ) |
161 | { | |
162 | 0 | cl = getClass( ).getClassLoader( ); |
163 | } | |
164 | ||
165 | 6 | Enumeration configUrls = cl.getResources( "META-INF/drools.conf" ); |
166 | ||
167 | 6 | if ( !configUrls.hasMoreElements( ) ) |
168 | { | |
169 | 0 | cl = getClass( ).getClassLoader( ); |
170 | 0 | configUrls = cl.getResources( "META-INF/drools.conf" ); |
171 | } | |
172 | ||
173 | 6 | if ( !configUrls.hasMoreElements( ) ) |
174 | { | |
175 | 0 | cl = ClassLoader.getSystemClassLoader( ); |
176 | 0 | configUrls = cl.getResources( "META-INF/drools.conf" ); |
177 | } | |
178 | ||
179 | 6 | this.classLoader = cl; |
180 | 6 | while ( configUrls.hasMoreElements( ) ) |
181 | { | |
182 | 12 | URL configUrl = (URL) configUrls.nextElement( ); |
183 | 12 | loadConfig( configUrl ); |
184 | } | |
185 | ||
186 | } | |
187 | ||
188 | 0 | protected void loadConfig(String path) throws IOException, |
189 | SemanticsReaderException | |
190 | { | |
191 | 0 | URL url = this.classLoader.getResource( path ); |
192 | ||
193 | 0 | if ( url == null ) |
194 | { | |
195 | 0 | System.err.println( "INVALID PATH: [" + path + "]" ); |
196 | 0 | return; |
197 | } | |
198 | ||
199 | 0 | loadConfig( url ); |
200 | } | |
201 | ||
202 | 12 | protected void loadConfig(URL url) throws IOException, |
203 | SemanticsReaderException | |
204 | { | |
205 | 12 | InputStream config = url.openStream( ); |
206 | ||
207 | 12 | BufferedReader in = new BufferedReader( new InputStreamReader( config ) ); |
208 | ||
209 | 12 | try |
210 | { | |
211 | 12 | String line; |
212 | ||
213 | ? | while ( (line = in.readLine( )) != null ) |
214 | { | |
215 | 12 | line = line.trim( ); |
216 | ||
217 | 12 | if ( line.equals( "" ) || line.startsWith( "#" ) ) |
218 | { | |
219 | 0 | continue; |
220 | } | |
221 | ||
222 | 12 | loadSemantics( line ); |
223 | } | |
224 | } | |
225 | finally | |
226 | { | |
227 | 12 | in.close( ); |
228 | } | |
229 | } | |
230 | ||
231 | 12 | protected void loadSemantics(String semanticsName) throws IOException, |
232 | SemanticsReaderException | |
233 | { | |
234 | 12 | if ( this.loadedSemantics.contains( semanticsName ) ) |
235 | { | |
236 | 0 | return; |
237 | } | |
238 | ||
239 | 12 | this.loadedSemantics.add( semanticsName ); |
240 | ||
241 | 12 | String semanticsFile = "META-INF/" + semanticsName + ".conf"; |
242 | ||
243 | 12 | URL descriptor = this.classLoader.getResource( semanticsFile ); |
244 | ||
245 | 12 | if ( descriptor == null ) |
246 | { | |
247 | 0 | System.err.println( "cannot load " + semanticsFile ); |
248 | 0 | return; |
249 | } | |
250 | ||
251 | 12 | SemanticsReader semanticsReader = new SemanticsReader( ); |
252 | ||
253 | 12 | SemanticModule module = semanticsReader.read( descriptor, |
254 | this.classLoader ); | |
255 | ||
256 | 12 | this.repository.registerSemanticModule( module ); |
257 | } | |
258 | ||
259 | /** | |
260 | * @see SemanticsRepository | |
261 | */ | |
262 | 1817 | public SemanticModule lookupSemanticModule(String uri) throws NoSuchSemanticModuleException |
263 | { | |
264 | 1817 | return this.repository.lookupSemanticModule( uri ); |
265 | } | |
266 | ||
267 | /** | |
268 | * @see SemanticsRepository | |
269 | */ | |
270 | 0 | public SemanticModule[] getSemanticModules() |
271 | { | |
272 | 0 | return this.repository.getSemanticModules( ); |
273 | } | |
274 | ||
275 | 66 | public ClassLoader getSemanticModuleClassLoader() |
276 | { | |
277 | 66 | return this.classLoader; |
278 | } | |
279 | } |
|