1 | /* |
2 | * Copyright (c) 2009 The JOMC Project |
3 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * o Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * |
13 | * o Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in |
15 | * the documentation and/or other materials provided with the |
16 | * distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * |
30 | * $Id: DefaultModelProvider.java 1331 2010-01-18 00:18:28Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.model; |
34 | |
35 | import java.net.URL; |
36 | import java.text.MessageFormat; |
37 | import java.util.Enumeration; |
38 | import java.util.Locale; |
39 | import java.util.ResourceBundle; |
40 | import java.util.logging.Level; |
41 | import javax.xml.bind.JAXBElement; |
42 | import javax.xml.bind.JAXBException; |
43 | import javax.xml.bind.Unmarshaller; |
44 | |
45 | /** |
46 | * Default {@code ModelProvider} implementation. |
47 | * |
48 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
49 | * @version $Id: DefaultModelProvider.java 1331 2010-01-18 00:18:28Z schulte2005 $ |
50 | * @see ModelContext#findModules() |
51 | */ |
52 | public class DefaultModelProvider implements ModelProvider |
53 | { |
54 | |
55 | /** |
56 | * Classpath location searched for modules by default. |
57 | * @see #getDefaultModuleLocation() |
58 | */ |
59 | private static final String DEFAULT_MODULE_LOCATION = "META-INF/jomc.xml"; |
60 | |
61 | /** Default module location. */ |
62 | private static volatile String defaultModuleLocation; |
63 | |
64 | /** Module location of the instance. */ |
65 | private String moduleLocation; |
66 | |
67 | /** Creates a new {@code DefaultModelProvider} instance. */ |
68 | public DefaultModelProvider() |
69 | { |
70 | super(); |
71 | } |
72 | |
73 | /** |
74 | * Gets the default location searched for module resources. |
75 | * <p>The default module location is controlled by system property |
76 | * {@code org.jomc.model.DefaultModelProvider.defaultModuleLocation} holding the location to search for module |
77 | * resources by default. If that property is not set, the {@code META-INF/jomc.xml} default is returned.</p> |
78 | * |
79 | * @return The location searched for module resources by default. |
80 | * |
81 | * @see #setDefaultModuleLocation(java.lang.String) |
82 | */ |
83 | public static String getDefaultModuleLocation() |
84 | { |
85 | if ( defaultModuleLocation == null ) |
86 | { |
87 | defaultModuleLocation = System.getProperty( "org.jomc.model.DefaultModelProvider.defaultModuleLocation", |
88 | DEFAULT_MODULE_LOCATION ); |
89 | |
90 | } |
91 | |
92 | return defaultModuleLocation; |
93 | } |
94 | |
95 | /** |
96 | * Sets the default location searched for module resources. |
97 | * |
98 | * @param value The new default location to search for module resources or {@code null}. |
99 | * |
100 | * @see #getDefaultModuleLocation() |
101 | */ |
102 | public static void setDefaultModuleLocation( final String value ) |
103 | { |
104 | defaultModuleLocation = value; |
105 | } |
106 | |
107 | /** |
108 | * Gets the location searched for module resources. |
109 | * |
110 | * @return The location searched for module resources. |
111 | * |
112 | * @see #getDefaultModuleLocation() |
113 | * @see #setModuleLocation(java.lang.String) |
114 | */ |
115 | public String getModuleLocation() |
116 | { |
117 | if ( this.moduleLocation == null ) |
118 | { |
119 | this.moduleLocation = getDefaultModuleLocation(); |
120 | } |
121 | |
122 | return this.moduleLocation; |
123 | } |
124 | |
125 | /** |
126 | * Sets the location searched for module resources. |
127 | * |
128 | * @param value The new location to search for module resources or {@code null}. |
129 | * |
130 | * @see #getModuleLocation() |
131 | */ |
132 | public void setModuleLocation( final String value ) |
133 | { |
134 | this.moduleLocation = value; |
135 | } |
136 | |
137 | /** |
138 | * Searches a given context for modules. |
139 | * |
140 | * @param context The context to search for modules. |
141 | * @param location The location to search at. |
142 | * |
143 | * @return The modules found at {@code location} in {@code context} or {@code null} of no modules are found. |
144 | * |
145 | * @throws NullPointerException if {@code context} or {@code location} is {@code null}. |
146 | * @throws ModelException if searching the context fails. |
147 | */ |
148 | public Modules findModules( final ModelContext context, final String location ) throws ModelException |
149 | { |
150 | if ( context == null ) |
151 | { |
152 | throw new NullPointerException( "context" ); |
153 | } |
154 | if ( location == null ) |
155 | { |
156 | throw new NullPointerException( "location" ); |
157 | } |
158 | |
159 | try |
160 | { |
161 | final long t0 = System.currentTimeMillis(); |
162 | final Text text = new Text(); |
163 | text.setLanguage( "en" ); |
164 | text.setValue( this.getMessage( "contextModulesInfo", new Object[] |
165 | { |
166 | location |
167 | } ) ); |
168 | |
169 | final Modules modules = new Modules(); |
170 | modules.setDocumentation( new Texts() ); |
171 | modules.getDocumentation().setDefaultLanguage( "en" ); |
172 | modules.getDocumentation().getText().add( text ); |
173 | |
174 | final Unmarshaller u = context.createUnmarshaller(); |
175 | final Enumeration<URL> resources = context.findResources( location ); |
176 | |
177 | int count = 0; |
178 | while ( resources.hasMoreElements() ) |
179 | { |
180 | count++; |
181 | final URL url = resources.nextElement(); |
182 | |
183 | if ( context.isLoggable( Level.FINE ) ) |
184 | { |
185 | context.log( Level.FINE, this.getMessage( "processing", new Object[] |
186 | { |
187 | url.toExternalForm() |
188 | } ), null ); |
189 | |
190 | } |
191 | |
192 | Object content = u.unmarshal( url ); |
193 | if ( content instanceof JAXBElement ) |
194 | { |
195 | content = ( (JAXBElement) content ).getValue(); |
196 | } |
197 | |
198 | if ( content instanceof Module ) |
199 | { |
200 | final Module m = (Module) content; |
201 | if ( context.isLoggable( Level.CONFIG ) ) |
202 | { |
203 | context.log( Level.CONFIG, this.getMessage( "foundModule", new Object[] |
204 | { |
205 | m.getName(), m.getVersion() == null ? "" : m.getVersion() |
206 | } ), null ); |
207 | |
208 | } |
209 | |
210 | modules.getModule().add( m ); |
211 | } |
212 | else if ( context.isLoggable( Level.WARNING ) ) |
213 | { |
214 | context.log( Level.WARNING, this.getMessage( "ignoringDocument", new Object[] |
215 | { |
216 | content == null ? "<>" : content.toString(), url.toExternalForm() |
217 | } ), null ); |
218 | |
219 | } |
220 | } |
221 | |
222 | if ( context.isLoggable( Level.FINE ) ) |
223 | { |
224 | context.log( Level.FINE, this.getMessage( "contextReport", new Object[] |
225 | { |
226 | count, location, Long.valueOf( System.currentTimeMillis() - t0 ) |
227 | } ), null ); |
228 | |
229 | } |
230 | |
231 | return modules.getModule().isEmpty() ? null : modules; |
232 | } |
233 | catch ( final JAXBException e ) |
234 | { |
235 | throw new ModelException( e ); |
236 | } |
237 | } |
238 | |
239 | /** |
240 | * {@inheritDoc} |
241 | * |
242 | * @see #getModuleLocation() |
243 | * @see #findModules(org.jomc.model.ModelContext, java.lang.String) |
244 | */ |
245 | public Modules findModules( final ModelContext context ) throws ModelException |
246 | { |
247 | if ( context == null ) |
248 | { |
249 | throw new NullPointerException( "context" ); |
250 | } |
251 | |
252 | return this.findModules( context, this.getModuleLocation() ); |
253 | } |
254 | |
255 | private String getMessage( final String key, final Object args ) |
256 | { |
257 | return new MessageFormat( |
258 | ResourceBundle.getBundle( DefaultModelProvider.class.getName().replace( '.', '/' ), Locale.getDefault() ). |
259 | getString( key ) ).format( args ); |
260 | |
261 | } |
262 | |
263 | } |