001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.xml.bind;
031:
032: import org.w3c.dom.Node;
033:
034: import java.io.IOException;
035: import java.lang.reflect.Constructor;
036: import java.lang.reflect.InvocationTargetException;
037: import java.lang.reflect.Method;
038: import java.util.Map;
039: import java.util.logging.Logger;
040:
041: public abstract class JAXBContext {
042: private static final Logger log = Logger
043: .getLogger(JAXBContext.class.getName());
044:
045: protected JAXBContext() {
046: }
047:
048: /** subclasses must override */
049: public Binder<Node> createBinder() {
050: return null;
051: }
052:
053: /** subclasses must override */
054: public <T> Binder<T> createBinder(Class<T> domType) {
055: return null;
056:
057: }
058:
059: /** subclasses must override */
060: public JAXBIntrospector createJAXBIntrospector() {
061: return null;
062: }
063:
064: public abstract Marshaller createMarshaller() throws JAXBException;
065:
066: public abstract Unmarshaller createUnmarshaller()
067: throws JAXBException;
068:
069: public abstract Validator createValidator() throws JAXBException;
070:
071: /** subclasses must override */
072: public void generateSchema(SchemaOutputResolver outputResolver)
073: throws IOException {
074: }
075:
076: public static JAXBContext newInstance(Class... classesToBeBound)
077: throws JAXBException {
078: return newInstance(classesToBeBound, null);
079: }
080:
081: public static JAXBContext newInstance(Class[] classesToBeBound,
082: Map<String, ?> properties) throws JAXBException {
083: try {
084: ClassLoader classLoader = Thread.currentThread()
085: .getContextClassLoader();
086:
087: FactoryLoader factoryLoader = FactoryLoader
088: .getFactoryLoader("javax.xml.bind.JAXBContext");
089:
090: Class cl = factoryLoader.getFactoryClass(classLoader);
091:
092: if (cl != null) {
093: Method m = cl.getMethod("createContext", Class[].class,
094: Map.class);
095: return (JAXBContext) m.invoke(null, classesToBeBound,
096: properties);
097: }
098:
099: cl = Class.forName("com.caucho.jaxb.JAXBContextImpl");
100: Constructor con = cl.getConstructor(new Class[] {
101: Class[].class, Map.class });
102: return (JAXBContext) con.newInstance(new Object[] {
103: classesToBeBound, properties });
104: } catch (InvocationTargetException e) {
105: if (e.getCause() instanceof JAXBException)
106: throw (JAXBException) e.getCause();
107: else
108: throw new JAXBException(e);
109: } catch (Exception e) {
110: throw new JAXBException(e);
111: }
112: }
113:
114: public static JAXBContext newInstance(String contextPath)
115: throws JAXBException {
116: return newInstance(contextPath, Thread.currentThread()
117: .getContextClassLoader(), null);
118: }
119:
120: public static JAXBContext newInstance(String contextPath,
121: ClassLoader classLoader) throws JAXBException {
122: return newInstance(contextPath, classLoader, null);
123: }
124:
125: public static JAXBContext newInstance(String contextPath,
126: ClassLoader classLoader, Map<String, ?> properties)
127: throws JAXBException {
128: try {
129: FactoryLoader factoryLoader = FactoryLoader
130: .getFactoryLoader("javax.xml.bind.JAXBContext");
131:
132: Class cl = factoryLoader.getFactoryClass(classLoader,
133: contextPath);
134:
135: if (cl != null) {
136: try {
137: Method m = cl.getMethod("createContext",
138: String.class, ClassLoader.class, Map.class);
139: return (JAXBContext) m.invoke(null, contextPath,
140: classLoader, properties);
141: } catch (NoSuchMethodException e) {
142: }
143:
144: Method m = cl.getMethod("createContext", String.class,
145: ClassLoader.class);
146: return (JAXBContext) m.invoke(null, contextPath,
147: classLoader);
148: }
149:
150: cl = Class.forName("com.caucho.jaxb.JAXBContextImpl");
151: Constructor con = cl.getConstructor(new Class[] {
152: String.class, ClassLoader.class, Map.class });
153:
154: return (JAXBContext) con.newInstance(new Object[] {
155: contextPath, classLoader, properties });
156: } catch (Exception e) {
157: throw new JAXBException(e);
158: }
159: }
160: }
|