Author: rmannibucau
Date: Mon Jan 7 21:14:55 2013
New Revision: 1430014
URL: http://svn.apache.org/viewvc?rev=1430014&view=rev
Log:
overriding ContextFinder (to avoid osgi dep) + DatatypConverter to force
converter init
Added:
openejb/trunk/javaee-api/src/main/java/javax/xml/bind/
openejb/trunk/javaee-api/src/main/java/javax/xml/bind/ContextFinder.java
openejb/trunk/javaee-api/src/main/java/javax/xml/bind/DatatypeConverter.java
Modified:
openejb/trunk/javaee-api/pom.xml
Modified: openejb/trunk/javaee-api/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/javaee-api/pom.xml?rev=1430014&r1=1430013&r2=1430014&view=diff
==============================================================================
--- openejb/trunk/javaee-api/pom.xml (original)
+++ openejb/trunk/javaee-api/pom.xml Mon Jan 7 21:14:55 2013
@@ -185,6 +185,8 @@
<exclude>META-INF/LICENSE</exclude>
<exclude>META-INF/NOTICE</exclude>
<exclude>javax/ws/rs/core/UriBuilder.class</exclude>
+ <exclude>javax/xml/bind/ContextFinder.class</exclude>
+ <exclude>javax/xml/bind/DatatypeConverter.class</exclude>
</excludes>
</filter>
<filter>
Added: openejb/trunk/javaee-api/src/main/java/javax/xml/bind/ContextFinder.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/javaee-api/src/main/java/javax/xml/bind/ContextFinder.java?rev=1430014&view=auto
==============================================================================
--- openejb/trunk/javaee-api/src/main/java/javax/xml/bind/ContextFinder.java
(added)
+++ openejb/trunk/javaee-api/src/main/java/javax/xml/bind/ContextFinder.java
Mon Jan 7 21:14:55 2013
@@ -0,0 +1,332 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.xml.bind;
+
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.util.*;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+/**
+ * we use it to endorse tomee and we don't want to depend on OSGi as it is
done in geronimo
+ */
+class ContextFinder {
+
+ private static final String PLATFORM_DEFAULT_FACTORY_CLASS =
"com.sun.xml.bind.v2.ContextFactory";
+ private static final String JAXB_CONTEXT_PROPERTY =
JAXBContext.class.getName();
+ private static final String JAXB_CONTEXT_FACTORY =
JAXBContext.JAXB_CONTEXT_FACTORY;
+
+ private static Class<?> osgiLocator;
+ private static Method getServiceClassMethod;
+ private static Method loadClassMethod;
+
+ static {
+ try {
+ osgiLocator =
Thread.currentThread().getContextClassLoader().loadClass("org.apache.geronimo.osgi.locator.ProviderLocator");
+ getServiceClassMethod = osgiLocator.getMethod("getServiceClass",
String.class, Class.class, ClassLoader.class);
+ loadClassMethod = osgiLocator.getMethod("loadClass", String.class,
Class.class, ClassLoader.class);
+ } catch (Exception e) {
+ osgiLocator = null;
+ } catch (NoClassDefFoundError ncdfe) {
+ osgiLocator = null;
+ }
+ }
+
+ public static JAXBContext find(String contextPath, ClassLoader
classLoader, Map properties) throws JAXBException {
+ contextPath = contextPath.trim();
+ if (contextPath.length() == 0 || contextPath.equals(":")) {
+ throw new JAXBException("Invalid contextPath");
+ }
+ String className = null;
+ String[] packages = contextPath.split("[:]");
+ for (String pkg : packages) {
+ String url = pkg.replace('.', '/') + "/jaxb.properties";
+ className = loadClassNameFromProperties(url, classLoader);
+ if (className != null) {
+ break;
+ }
+ }
+ if (className == null) {
+ className = System.getProperty(JAXB_CONTEXT_PROPERTY);
+ }
+ Class spi = null;
+ // if no specifically specified name, check for META-INF/services, and
+ // fall back to the default factory class if that fails
+ if (className == null) {
+ spi = loadSPIClass(JAXBContext.class, classLoader);
+ if (spi == null) {
+ spi = loadSpi(PLATFORM_DEFAULT_FACTORY_CLASS, classLoader);
+ }
+ }
+ else {
+ spi = loadSpi(className, classLoader);
+ }
+ try {
+ Method m = spi.getMethod("createContext", new Class[] {
String.class, ClassLoader.class, Map.class });
+ return (JAXBContext) m.invoke(null, new Object[] { contextPath,
classLoader, properties });
+ } catch (NoSuchMethodException e) {
+ // will try JAXB 1.0 compatible createContext() method
+ } catch (Throwable t) {
+ throw new JAXBException("Unable to create context", t);
+ }
+
+ // try old JAXB 1.0 compatible createContext() method
+ try {
+ Method m = spi.getMethod("createContext", new Class[] {
String.class, ClassLoader.class });
+ return (JAXBContext) m.invoke(null, new Object[] { contextPath,
classLoader });
+ } catch (Throwable t) {
+ throw new JAXBException("Unable to create context", t);
+ }
+ }
+
+
+ public static JAXBContext find(Class[] classes, Map properties) throws
JAXBException {
+ String className = null;
+ for (Class cl : classes) {
+ Package pkg = cl.getPackage();
+ if (pkg != null) {
+ String url = pkg.getName().replace('.', '/') +
"/jaxb.properties";
+ className = loadClassNameFromProperties(url,
cl.getClassLoader());
+ if (className != null) {
+ break;
+ }
+ }
+ }
+ if (className == null) {
+ className = System.getProperty(JAXB_CONTEXT_PROPERTY);
+ }
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+
+ Class spi = null;
+ // if no specifically specified name, check for META-INF/services, and
+ // fall back to the default factory class if that fails
+ if (className == null) {
+ spi = loadSPIClass(JAXBContext.class, classLoader);
+ if (spi == null) {
+ spi = loadSpi(PLATFORM_DEFAULT_FACTORY_CLASS, classLoader);
+ }
+ }
+ else {
+ spi = loadSpi(className, classLoader);
+ }
+ try {
+ Method m = spi.getMethod("createContext", new Class[] {
Class[].class, Map.class });
+ return (JAXBContext) m.invoke(null, new Object[] { classes,
properties });
+ } catch (Throwable t) {
+ throw new JAXBException("Unable to create context", t);
+ }
+ }
+
+ private static String loadClassNameFromProperties(String url, ClassLoader
classLoader) throws JAXBException {
+ try {
+ InputStream is;
+ if (classLoader != null) {
+ is = classLoader.getResourceAsStream(url);
+ } else {
+ is = ClassLoader.getSystemResourceAsStream(url);
+ }
+ if (is != null) {
+ try {
+ Properties props = new Properties();
+ props.load(is);
+ String className = props.getProperty(JAXB_CONTEXT_FACTORY);
+ if (className == null) {
+ throw new JAXBException("jaxb.properties file " + url
+ " should contain a " + JAXB_CONTEXT_FACTORY + " property");
+ }
+ return className.trim();
+ } finally {
+ is.close();
+ }
+ } else {
+ return null;
+ }
+ } catch (IOException e) {
+ throw new JAXBException(e);
+ }
+ }
+
+ private static Class<?> loadSPIClass(Class<?> iface, ClassLoader
classLoader) throws JAXBException {
+ if (osgiLocator != null) {
+ return loadSPIClassFromOSGi(iface, classLoader);
+ }
+
+ try {
+ return locateServiceClass(iface.getName(), ContextFinder.class,
classLoader);
+ } catch (ClassNotFoundException e) {
+ throw new JAXBException("Provider " + iface.getName() + " not
found", e);
+ }
+ }
+
+ static private Class<?> locateServiceClass(String iface, Class<?>
contextClass, ClassLoader loader) throws ClassNotFoundException {
+ String className = locateServiceClassName(iface, contextClass, loader);
+ if (className == null) {
+ return null;
+ }
+
+ // we found a name, try loading the class. This will throw an
exception if there is an error
+ return loadClass(className, contextClass, loader);
+ }
+
+ static private String locateServiceClassName(String iface, Class<?>
contextClass, ClassLoader loader) {
+ // search first with the loader class path
+ String name = locateServiceClassName(iface, loader);
+ if (name != null) {
+ return name;
+ }
+ // then with the context class, if there is one
+ if (contextClass != null) {
+ name = locateServiceClassName(iface,
contextClass.getClassLoader());
+ if (name != null) {
+ return name;
+ }
+ }
+ // not found
+ return null;
+ }
+
+ static private String locateServiceClassName(String iface, ClassLoader
loader) {
+ if (loader != null) {
+ try {
+ // we only look at resources that match the file name, using
the specified loader
+ String service = "META-INF/services/" + iface;
+ Enumeration<URL> providers = loader.getResources(service);
+
+ while (providers.hasMoreElements()) {
+ List<String>providerNames =
parseServiceDefinition(providers.nextElement());
+ // if there is something defined here, return the first
entry
+ if (!providerNames.isEmpty()) {
+ return providerNames.get(0);
+ }
+ }
+ } catch (IOException e) {
+ }
+ }
+ // not found
+ return null;
+ }
+
+ static public Class<?> loadClass(String className, Class<?> contextClass,
ClassLoader loader) throws ClassNotFoundException {
+ if (loader != null) {
+ try {
+ return loader.loadClass(className);
+ } catch (ClassNotFoundException x) {
+ }
+ }
+ if (contextClass != null) {
+ loader = contextClass.getClassLoader();
+ }
+ // try again using the class context loader
+ return Class.forName(className, true, loader);
+ }
+
+ static private Collection<String> locateServiceClassNames(String iface,
Class<?> contextClass, ClassLoader loader) {
+ Set<String> names = new LinkedHashSet<String>();
+
+ locateServiceClassNames(iface, loader, names);
+ if (contextClass != null) {
+ locateServiceClassNames(iface, contextClass.getClassLoader(),
names);
+ }
+
+ return names;
+ }
+
+ static void locateServiceClassNames(String iface, ClassLoader loader, Set
names) {
+ if (loader != null) {
+
+ try {
+ // we only look at resources that match the file name, using
the specified loader
+ String service = "META-INF/services/" + iface;
+ Enumeration<URL> providers = loader.getResources(service);
+
+ while (providers.hasMoreElements()) {
+ List<String>providerNames =
parseServiceDefinition(providers.nextElement());
+ // just add all of these to the list
+ names.addAll(providerNames);
+ }
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ static private List<String> parseServiceDefinition(URL u) {
+ final String url = u.toString();
+ List<String> classes = new ArrayList<String>();
+ // ignore directories
+ if (url.endsWith("/")) {
+ return classes;
+ }
+ // the identifier used for the provider is the last item in the URL.
+ final String providerId = url.substring(url.lastIndexOf("/") + 1);
+ try {
+ BufferedReader br = new BufferedReader(new
InputStreamReader(u.openStream(), "UTF-8"));
+ // the file can be multiple lines long, with comments. A single
file can define multiple providers
+ // for a single key, so we might need to create multiple entries.
If the file does not contain any
+ // definition lines, then as a default, we use the providerId as
an implementation class also.
+ String line = br.readLine();
+ while (line != null) {
+ // we allow comments on these lines, and a line can be all
comment
+ int comment = line.indexOf('#');
+ if (comment != -1) {
+ line = line.substring(0, comment);
+ }
+ line = line.trim();
+ // if there is nothing left on the line after stripping white
space and comments, skip this
+ if (line.length() > 0) {
+ // add this to our list
+ classes.add(line);
+ }
+ // keep reading until the end.
+ line = br.readLine();
+ }
+ br.close();
+ } catch (IOException e) {
+ // ignore errors and handle as default
+ }
+ return classes;
+ }
+
+ private static Class loadSpi(String className, ClassLoader classLoader)
throws JAXBException {
+ if (osgiLocator != null) {
+ return loadSpiFromOSGi(className, classLoader);
+ }
+
+ try {
+ return loadClass(className, ContextFinder.class, classLoader);
+ } catch (ClassNotFoundException e) {
+ throw new JAXBException("Provider " + className + " not found", e);
+ }
+ }
+
+ private static Class<?> loadSPIClassFromOSGi(Class<?> iface, ClassLoader
classLoader) throws JAXBException {
+ try {
+ return (Class<?>) getServiceClassMethod.invoke(null,
iface.getName(), ContextFinder.class,classLoader);
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ private static Class loadSpiFromOSGi(String className, ClassLoader
classLoader) throws JAXBException {
+ try {
+ return (Class<?>) loadClassMethod.invoke(null, className,
ContextFinder.class, classLoader);
+ } catch (Exception e) {
+ throw new JAXBException("Provider " + className + " not found", e);
+ }
+ }
+}
Added:
openejb/trunk/javaee-api/src/main/java/javax/xml/bind/DatatypeConverter.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/javaee-api/src/main/java/javax/xml/bind/DatatypeConverter.java?rev=1430014&view=auto
==============================================================================
---
openejb/trunk/javaee-api/src/main/java/javax/xml/bind/DatatypeConverter.java
(added)
+++
openejb/trunk/javaee-api/src/main/java/javax/xml/bind/DatatypeConverter.java
Mon Jan 7 21:14:55 2013
@@ -0,0 +1,201 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.xml.bind;
+
+import java.util.Calendar;
+import java.math.BigInteger;
+import java.math.BigDecimal;
+
+import javax.xml.namespace.QName;
+import javax.xml.namespace.NamespaceContext;
+
+public final class DatatypeConverter {
+
+ private static DatatypeConverterInterface converter = new
DatatypeConverterImpl();
+
+ private DatatypeConverter() {
+ // no-op
+ }
+
+ public static void setDatatypeConverter(DatatypeConverterInterface
converter) {
+ if (converter == null) {
+ throw new IllegalArgumentException("The DatatypeConverterInterface
parameter must not be null");
+ }
+ if (DatatypeConverter.converter == null) {
+
+ SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(new JAXBPermission("setDatatypeConverter"));
+ }
+
+ DatatypeConverter.converter = new
DatatypeConverterHelper(converter);
+ }
+ }
+
+ public static String parseString(String lexicalXSDString) {
+ return converter.parseString(lexicalXSDString);
+ }
+
+ public static BigInteger parseInteger(String lexicalXSDInteger) {
+ return converter.parseInteger(lexicalXSDInteger);
+ }
+
+ public static int parseInt(String lexicalXSDInt) {
+ return converter.parseInt(lexicalXSDInt);
+ }
+
+ public static long parseLong(String lexicalXSDLong) {
+ return converter.parseLong(lexicalXSDLong);
+ }
+
+ public static short parseShort(String lexicalXSDShort) {
+ return converter.parseShort(lexicalXSDShort);
+ }
+
+ public static BigDecimal parseDecimal(String lexicalXSDDecimal) {
+ return converter.parseDecimal(lexicalXSDDecimal);
+ }
+
+ public static float parseFloat(String lexicalXSDFloat) {
+ return converter.parseFloat(lexicalXSDFloat);
+ }
+
+ public static double parseDouble(String lexicalXSDDouble) {
+ return converter.parseDouble(lexicalXSDDouble);
+ }
+
+ public static boolean parseBoolean(String lexicalXSDBoolean) {
+ return converter.parseBoolean(lexicalXSDBoolean);
+ }
+
+ public static byte parseByte(String lexicalXSDByte) {
+ return converter.parseByte(lexicalXSDByte);
+ }
+
+ public static QName parseQName(String lexicalXSDQName, NamespaceContext
nsc) {
+ return converter.parseQName(lexicalXSDQName, nsc);
+ }
+
+ public static Calendar parseDateTime(String lexicalXSDDateTime) {
+ return converter.parseDateTime(lexicalXSDDateTime);
+ }
+
+ public static byte[] parseBase64Binary(String lexicalXSDBase64Binary) {
+ return converter.parseBase64Binary(lexicalXSDBase64Binary);
+ }
+
+ public static byte[] parseHexBinary(String lexicalXSDHexBinary) {
+ return converter.parseHexBinary(lexicalXSDHexBinary);
+ }
+
+ public static long parseUnsignedInt(String lexicalXSDUnsignedInt) {
+ return converter.parseUnsignedInt(lexicalXSDUnsignedInt);
+ }
+
+ public static int parseUnsignedShort(String lexicalXSDUnsignedShort) {
+ return converter.parseUnsignedShort(lexicalXSDUnsignedShort);
+ }
+
+ public static Calendar parseTime(String lexicalXSDTime) {
+ return converter.parseTime(lexicalXSDTime);
+ }
+
+ public static Calendar parseDate(String lexicalXSDDate) {
+ return converter.parseDate(lexicalXSDDate);
+ }
+
+ public static String parseAnySimpleType(String lexicalXSDAnySimpleType) {
+ return converter.parseAnySimpleType(lexicalXSDAnySimpleType);
+ }
+
+ public static String printString(String val) {
+ return converter.printString(val);
+ }
+
+ public static String printInteger(BigInteger val) {
+ return converter.printInteger(val);
+ }
+
+ public static String printInt(int val) {
+ return converter.printInt(val);
+ }
+
+ public static String printLong(long val) {
+ return converter.printLong(val);
+ }
+
+ public static String printShort(short val) {
+ return converter.printShort(val);
+ }
+
+ public static String printDecimal(BigDecimal val) {
+ return converter.printDecimal(val);
+ }
+
+ public static String printFloat(float val) {
+ return converter.printFloat(val);
+ }
+
+ public static String printDouble(double val) {
+ return converter.printDouble(val);
+ }
+
+ public static String printBoolean(boolean val) {
+ return converter.printBoolean(val);
+ }
+
+ public static String printByte(byte val) {
+ return converter.printByte(val);
+ }
+
+ public static String printQName(QName val, NamespaceContext nsc) {
+ return converter.printQName(val, nsc);
+ }
+
+ public static String printDateTime(Calendar val) {
+ return converter.printDateTime(val);
+ }
+
+ public static String printBase64Binary(byte val[]) {
+ return converter.printBase64Binary(val);
+ }
+
+ public static String printHexBinary(byte val[]) {
+ return converter.printHexBinary(val);
+ }
+
+ public static String printUnsignedInt(long val) {
+ return converter.printUnsignedInt(val);
+ }
+
+ public static String printUnsignedShort(int val) {
+ return converter.printUnsignedShort(val);
+ }
+
+ public static String printTime(Calendar val) {
+ return converter.printTime(val);
+ }
+
+ public static String printDate(Calendar val) {
+ return converter.printDate(val);
+ }
+
+ public static String printAnySimpleType(String val) {
+ return converter.printAnySimpleType(val);
+ }
+
+}