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: DefaultSchemaProvider.java 1393 2010-01-27 18:34:30Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.model.bootstrap; |
34 | |
35 | import java.net.URL; |
36 | import java.util.Enumeration; |
37 | import javax.xml.bind.JAXBContext; |
38 | import javax.xml.bind.JAXBElement; |
39 | import javax.xml.bind.JAXBException; |
40 | import javax.xml.bind.Unmarshaller; |
41 | |
42 | /** |
43 | * Default {@code SchemaProvider} implementation. |
44 | * |
45 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
46 | * @version $Id: DefaultSchemaProvider.java 1393 2010-01-27 18:34:30Z schulte2005 $ |
47 | * @see BootstrapContext#findSchemas() |
48 | */ |
49 | public class DefaultSchemaProvider implements SchemaProvider |
50 | { |
51 | |
52 | /** |
53 | * Classpath location searched for schemas by default. |
54 | * @see #getDefaultSchemaLocation() |
55 | */ |
56 | private static final String DEFAULT_SCHEMA_LOCATION = "META-INF/jomc-schemas.xml"; |
57 | |
58 | /** Default schema location. */ |
59 | private static volatile String defaultSchemaLocation; |
60 | |
61 | /** Schema location of the instance. */ |
62 | private String schemaLocation; |
63 | |
64 | /** Creates a new {@code DefaultSchemaProvider} instance. */ |
65 | public DefaultSchemaProvider() |
66 | { |
67 | super(); |
68 | } |
69 | |
70 | /** |
71 | * Gets the default location searched for schema resources. |
72 | * <p>The default schema location is controlled by system property |
73 | * {@code org.jomc.model.bootstrap.DefaultSchemaProvider.defaultSchemaLocation} holding the location to search for |
74 | * schema resources by default. If that property is not set, the {@code META-INF/jomc-schemas.xml} default is |
75 | * returned.</p> |
76 | * |
77 | * @return The location searched for schema resources by default. |
78 | * |
79 | * @see #setDefaultSchemaLocation(java.lang.String) |
80 | */ |
81 | public static String getDefaultSchemaLocation() |
82 | { |
83 | if ( defaultSchemaLocation == null ) |
84 | { |
85 | defaultSchemaLocation = System.getProperty( |
86 | "org.jomc.model.bootstrap.DefaultSchemaProvider.defaultSchemaLocation", DEFAULT_SCHEMA_LOCATION ); |
87 | |
88 | } |
89 | |
90 | return defaultSchemaLocation; |
91 | } |
92 | |
93 | /** |
94 | * Sets the default location searched for schema resources. |
95 | * |
96 | * @param value The new default location to search for schema resources or {@code null}. |
97 | * |
98 | * @see #getDefaultSchemaLocation() |
99 | */ |
100 | public static void setDefaultSchemaLocation( final String value ) |
101 | { |
102 | defaultSchemaLocation = value; |
103 | } |
104 | |
105 | /** |
106 | * Gets the location searched for schema resources. |
107 | * |
108 | * @return The location searched for schema resources. |
109 | * |
110 | * @see #getDefaultSchemaLocation() |
111 | * @see #setSchemaLocation(java.lang.String) |
112 | */ |
113 | public String getSchemaLocation() |
114 | { |
115 | if ( this.schemaLocation == null ) |
116 | { |
117 | this.schemaLocation = getDefaultSchemaLocation(); |
118 | } |
119 | |
120 | return this.schemaLocation; |
121 | } |
122 | |
123 | /** |
124 | * Sets the location searched for schema resources. |
125 | * |
126 | * @param value The new location to search for schema resources or {@code null}. |
127 | * |
128 | * @see #getSchemaLocation() |
129 | */ |
130 | public void setSchemaLocation( final String value ) |
131 | { |
132 | this.schemaLocation = value; |
133 | } |
134 | |
135 | /** |
136 | * Searches a given context for schemas. |
137 | * |
138 | * @param context The context to search for schemas. |
139 | * @param location The location to search at. |
140 | * |
141 | * @return The schemas found at {@code location} in {@code context} or {@code null} of no schemas are found. |
142 | * |
143 | * @throws NullPointerException if {@code context} or {@code location} is {@code null}. |
144 | * @throws BootstrapException if searching the context fails. |
145 | */ |
146 | public Schemas findSchemas( final BootstrapContext context, final String location ) throws BootstrapException |
147 | { |
148 | if ( context == null ) |
149 | { |
150 | throw new NullPointerException( "context" ); |
151 | } |
152 | if ( location == null ) |
153 | { |
154 | throw new NullPointerException( "location" ); |
155 | } |
156 | |
157 | try |
158 | { |
159 | final Schemas schemas = new Schemas(); |
160 | final JAXBContext ctx = context.createContext(); |
161 | final Unmarshaller u = ctx.createUnmarshaller(); |
162 | final Enumeration<URL> e = context.findResources( location ); |
163 | u.setSchema( context.createSchema() ); |
164 | |
165 | while ( e.hasMoreElements() ) |
166 | { |
167 | final URL url = e.nextElement(); |
168 | Object content = u.unmarshal( url ); |
169 | if ( content instanceof JAXBElement ) |
170 | { |
171 | content = ( (JAXBElement) content ).getValue(); |
172 | } |
173 | |
174 | if ( content instanceof Schema ) |
175 | { |
176 | schemas.getSchema().add( (Schema) content ); |
177 | } |
178 | else if ( content instanceof Schemas ) |
179 | { |
180 | for ( Schema s : ( (Schemas) content ).getSchema() ) |
181 | { |
182 | schemas.getSchema().add( s ); |
183 | } |
184 | } |
185 | } |
186 | |
187 | return schemas.getSchema().isEmpty() ? null : schemas; |
188 | } |
189 | catch ( final JAXBException e ) |
190 | { |
191 | throw new BootstrapException( e ); |
192 | } |
193 | } |
194 | |
195 | /** |
196 | * {@inheritDoc} |
197 | * |
198 | * @see #getSchemaLocation() |
199 | * @see #findSchemas(org.jomc.model.bootstrap.BootstrapContext, java.lang.String) |
200 | */ |
201 | public Schemas findSchemas( final BootstrapContext context ) throws BootstrapException |
202 | { |
203 | if ( context == null ) |
204 | { |
205 | throw new NullPointerException( "context" ); |
206 | } |
207 | |
208 | return this.findSchemas( context, this.getSchemaLocation() ); |
209 | } |
210 | |
211 | } |