|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2; |
|
27 |
|
28 import java.io.BufferedReader; |
|
29 import java.io.IOException; |
|
30 import java.io.InputStream; |
|
31 import java.io.InputStreamReader; |
|
32 import java.util.Collection; |
|
33 import java.util.Collections; |
|
34 import java.util.HashMap; |
|
35 import java.util.List; |
|
36 import java.util.Map; |
|
37 import java.util.StringTokenizer; |
|
38 import java.util.logging.Level; |
|
39 |
|
40 import javax.xml.bind.JAXBContext; |
|
41 import javax.xml.bind.JAXBException; |
|
42 |
|
43 import com.sun.istack.internal.FinalArrayList; |
|
44 import com.sun.xml.internal.bind.Util; |
|
45 import com.sun.xml.internal.bind.api.JAXBRIContext; |
|
46 import com.sun.xml.internal.bind.api.TypeReference; |
|
47 import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader; |
|
48 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
|
49 import com.sun.xml.internal.bind.v2.util.TypeCast; |
|
50 |
|
51 /** |
|
52 * This class is responsible for producing RI JAXBContext objects. In |
|
53 * the RI, this is the class that the javax.xml.bind.context.factory |
|
54 * property will point to. |
|
55 * |
|
56 * <p> |
|
57 * Used to create JAXBContext objects for v1.0.1 and forward |
|
58 * |
|
59 * @since 2.0 |
|
60 * @author Kohsuke Kawaguchi |
|
61 */ |
|
62 public class ContextFactory { |
|
63 /** |
|
64 * The API will invoke this method via reflection |
|
65 */ |
|
66 public static JAXBContext createContext( Class[] classes, Map<String,Object> properties ) throws JAXBException { |
|
67 // fool-proof check, and copy the map to make it easier to find unrecognized properties. |
|
68 if(properties==null) |
|
69 properties = Collections.emptyMap(); |
|
70 else |
|
71 properties = new HashMap<String,Object>(properties); |
|
72 |
|
73 String defaultNsUri = getPropertyValue(properties,JAXBRIContext.DEFAULT_NAMESPACE_REMAP,String.class); |
|
74 |
|
75 Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class); |
|
76 if(c14nSupport==null) |
|
77 c14nSupport = false; |
|
78 |
|
79 Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class); |
|
80 if(allNillable==null) |
|
81 allNillable = false; |
|
82 |
|
83 Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class); |
|
84 if(retainPropertyInfo==null) |
|
85 retainPropertyInfo = false; |
|
86 |
|
87 Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class); |
|
88 if(supressAccessorWarnings==null) |
|
89 supressAccessorWarnings = false; |
|
90 |
|
91 Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class); |
|
92 if(improvedXsiTypeHandling == null) |
|
93 improvedXsiTypeHandling = true; |
|
94 |
|
95 Boolean xmlAccessorFactorySupport = getPropertyValue(properties, |
|
96 JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class); |
|
97 if(xmlAccessorFactorySupport==null){ |
|
98 xmlAccessorFactorySupport = false; |
|
99 Util.getClassLogger().log(Level.FINE, "Property " + |
|
100 JAXBRIContext.XMLACCESSORFACTORY_SUPPORT + |
|
101 "is not active. Using JAXB's implementation"); |
|
102 } |
|
103 |
|
104 RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class); |
|
105 |
|
106 Map<Class,Class> subclassReplacements; |
|
107 try { |
|
108 subclassReplacements = TypeCast.checkedCast( |
|
109 getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class); |
|
110 } catch (ClassCastException e) { |
|
111 throw new JAXBException(Messages.INVALID_TYPE_IN_MAP.format(),e); |
|
112 } |
|
113 |
|
114 if(!properties.isEmpty()) { |
|
115 throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next())); |
|
116 } |
|
117 |
|
118 JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
|
119 builder.setClasses(classes); |
|
120 builder.setTypeRefs(Collections.<TypeReference>emptyList()); |
|
121 builder.setSubclassReplacements(subclassReplacements); |
|
122 builder.setDefaultNsUri(defaultNsUri); |
|
123 builder.setC14NSupport(c14nSupport); |
|
124 builder.setAnnotationReader(ar); |
|
125 builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
|
126 builder.setAllNillable(allNillable); |
|
127 builder.setRetainPropertyInfo(retainPropertyInfo); |
|
128 builder.setSupressAccessorWarnings(supressAccessorWarnings); |
|
129 builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
|
130 return builder.build(); |
|
131 } |
|
132 |
|
133 /** |
|
134 * If a key is present in the map, remove the value and return it. |
|
135 */ |
|
136 private static <T> T getPropertyValue(Map<String, Object> properties, String keyName, Class<T> type ) throws JAXBException { |
|
137 Object o = properties.get(keyName); |
|
138 if(o==null) return null; |
|
139 |
|
140 properties.remove(keyName); |
|
141 if(!type.isInstance(o)) |
|
142 throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); |
|
143 else |
|
144 return type.cast(o); |
|
145 } |
|
146 |
|
147 public static JAXBRIContext createContext( Class[] classes, |
|
148 Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
|
149 String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
|
150 boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException { |
|
151 |
|
152 return createContext(classes, typeRefs, subclassReplacements, |
|
153 defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport, |
|
154 allNillable, retainPropertyInfo, false); |
|
155 } |
|
156 |
|
157 public static JAXBRIContext createContext( Class[] classes, |
|
158 Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
|
159 String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
|
160 boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException { |
|
161 |
|
162 JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
|
163 builder.setClasses(classes); |
|
164 builder.setTypeRefs(typeRefs); |
|
165 builder.setSubclassReplacements(subclassReplacements); |
|
166 builder.setDefaultNsUri(defaultNsUri); |
|
167 builder.setC14NSupport(c14nSupport); |
|
168 builder.setAnnotationReader(ar); |
|
169 builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
|
170 builder.setAllNillable(allNillable); |
|
171 builder.setRetainPropertyInfo(retainPropertyInfo); |
|
172 builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
|
173 return builder.build(); |
|
174 } |
|
175 |
|
176 /** |
|
177 * The API will invoke this method via reflection. |
|
178 */ |
|
179 public static JAXBContext createContext( String contextPath, |
|
180 ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException { |
|
181 FinalArrayList<Class> classes = new FinalArrayList<Class>(); |
|
182 StringTokenizer tokens = new StringTokenizer(contextPath,":"); |
|
183 List<Class> indexedClasses; |
|
184 |
|
185 // at least on of these must be true per package |
|
186 boolean foundObjectFactory; |
|
187 boolean foundJaxbIndex; |
|
188 |
|
189 while(tokens.hasMoreTokens()) { |
|
190 foundObjectFactory = foundJaxbIndex = false; |
|
191 String pkg = tokens.nextToken(); |
|
192 |
|
193 // look for ObjectFactory and load it |
|
194 final Class<?> o; |
|
195 try { |
|
196 o = classLoader.loadClass(pkg+".ObjectFactory"); |
|
197 classes.add(o); |
|
198 foundObjectFactory = true; |
|
199 } catch (ClassNotFoundException e) { |
|
200 // not necessarily an error |
|
201 } |
|
202 |
|
203 // look for jaxb.index and load the list of classes |
|
204 try { |
|
205 indexedClasses = loadIndexedClasses(pkg, classLoader); |
|
206 } catch (IOException e) { |
|
207 //TODO: think about this more |
|
208 throw new JAXBException(e); |
|
209 } |
|
210 if(indexedClasses != null) { |
|
211 classes.addAll(indexedClasses); |
|
212 foundJaxbIndex = true; |
|
213 } |
|
214 |
|
215 if( !(foundObjectFactory || foundJaxbIndex) ) { |
|
216 throw new JAXBException( Messages.BROKEN_CONTEXTPATH.format(pkg)); |
|
217 } |
|
218 } |
|
219 |
|
220 |
|
221 return createContext(classes.toArray(new Class[classes.size()]),properties); |
|
222 } |
|
223 |
|
224 /** |
|
225 * Look for jaxb.index file in the specified package and load it's contents |
|
226 * |
|
227 * @param pkg package name to search in |
|
228 * @param classLoader ClassLoader to search in |
|
229 * @return a List of Class objects to load, null if there weren't any |
|
230 * @throws IOException if there is an error reading the index file |
|
231 * @throws JAXBException if there are any errors in the index file |
|
232 */ |
|
233 private static List<Class> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException { |
|
234 final String resource = pkg.replace('.', '/') + "/jaxb.index"; |
|
235 final InputStream resourceAsStream = classLoader.getResourceAsStream(resource); |
|
236 |
|
237 if (resourceAsStream == null) { |
|
238 return null; |
|
239 } |
|
240 |
|
241 BufferedReader in = |
|
242 new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8")); |
|
243 try { |
|
244 FinalArrayList<Class> classes = new FinalArrayList<Class>(); |
|
245 String className = in.readLine(); |
|
246 while (className != null) { |
|
247 className = className.trim(); |
|
248 if (className.startsWith("#") || (className.length() == 0)) { |
|
249 className = in.readLine(); |
|
250 continue; |
|
251 } |
|
252 |
|
253 if (className.endsWith(".class")) { |
|
254 throw new JAXBException(Messages.ILLEGAL_ENTRY.format(className)); |
|
255 } |
|
256 |
|
257 try { |
|
258 classes.add(classLoader.loadClass(pkg + '.' + className)); |
|
259 } catch (ClassNotFoundException e) { |
|
260 throw new JAXBException(Messages.ERROR_LOADING_CLASS.format(className, resource),e); |
|
261 } |
|
262 |
|
263 className = in.readLine(); |
|
264 } |
|
265 return classes; |
|
266 } finally { |
|
267 in.close(); |
|
268 } |
|
269 } |
|
270 |
|
271 public static final String USE_JAXB_PROPERTIES = "_useJAXBProperties"; |
|
272 } |