001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.wsdl.codegen.extension;
021:
022: import org.apache.axis2.description.AxisService;
023: import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
024: import org.apache.axis2.wsdl.databinding.TypeMapper;
025: import org.apache.axis2.wsdl.i18n.CodegenMessages;
026: import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029:
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.parsers.ParserConfigurationException;
033: import java.io.InputStream;
034: import java.lang.reflect.Method;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: public class JAXBRIExtension extends AbstractDBProcessingExtension {
040:
041: public static final String SCHEMA_FOLDER = "schemas";
042:
043: public static String MAPPINGS = "mappings";
044: public static String MAPPING = "mapping";
045: public static String MESSAGE = "message";
046: public static String JAVA_NAME = "javaclass";
047:
048: public static final String MAPPING_FOLDER = "Mapping";
049: public static final String MAPPER_FILE_NAME = "mapper";
050: public static final String SCHEMA_PATH = "/org/apache/axis2/wsdl/codegen/schema/";
051:
052: public static final String JAXB_RI_API_CLASS = "javax.xml.bind.JAXBContext";
053: public static final String JAXB_RI_IMPL_CLASS = "com.sun.xml.bind.Util";
054: public static final String JAXB_RI_XJC_CLASS = "com.sun.tools.xjc.api.XJC";
055:
056: public static final String JAXB_RI_UTILITY_CLASS = "org.apache.axis2.jaxbri.CodeGenerationUtility";
057:
058: public static final String JAXB_RI_PROCESS_METHOD = "processSchemas";
059:
060: public void engage(CodeGenConfiguration configuration) {
061:
062: //test the databinding type. If not just fall through
063: if (testFallThrough(configuration.getDatabindingType())) {
064: return;
065: }
066:
067: try {
068:
069: // try dummy load of framework classes first to check missing jars
070: try {
071: ClassLoader cl = getClass().getClassLoader();
072: cl.loadClass(JAXB_RI_API_CLASS);
073: cl.loadClass(JAXB_RI_IMPL_CLASS);
074: cl.loadClass(JAXB_RI_XJC_CLASS);
075: } catch (ClassNotFoundException e) {
076: throw new RuntimeException(
077: "JAX-B RI JARs not on classpath");
078: }
079:
080: // load the actual utility class
081: Class clazz = null;
082: try {
083: clazz = JAXBRIExtension.class.getClassLoader()
084: .loadClass(JAXB_RI_UTILITY_CLASS);
085: } catch (ClassNotFoundException e) {
086: throw new RuntimeException(
087: "JAX-B RI binding extension not in classpath");
088: }
089:
090: // invoke utility class method for actual processing
091: Method method = clazz.getMethod(JAXB_RI_PROCESS_METHOD,
092: new Class[] { List.class, Element[].class,
093: CodeGenConfiguration.class });
094: List schemas = new ArrayList();
095: List axisServices = configuration.getAxisServices();
096: AxisService axisService = null;
097: for (Iterator iter = axisServices.iterator(); iter
098: .hasNext();) {
099: axisService = (AxisService) iter.next();
100: schemas.addAll(axisService.getSchema());
101: }
102: Element[] additionalSchemas = loadAdditionalSchemas();
103: TypeMapper mapper = (TypeMapper) method.invoke(null,
104: new Object[] { schemas, additionalSchemas,
105: configuration });
106:
107: // set the type mapper to the config
108: configuration.setTypeMapper(mapper);
109:
110: } catch (Exception e) {
111: if (e instanceof RuntimeException) {
112: throw (RuntimeException) e;
113: } else {
114: throw new RuntimeException(e);
115: }
116: }
117:
118: }
119:
120: /**
121: * Loading the external schemas.
122: *
123: * @return element array consisting of the the DOM element objects that represent schemas
124: */
125: private Element[] loadAdditionalSchemas() {
126: //load additional schemas
127: String[] schemaNames = ConfigPropertyFileLoader
128: .getThirdPartySchemaNames();
129: Element[] schemaElements;
130:
131: try {
132: ArrayList additionalSchemaElements = new ArrayList();
133: DocumentBuilder documentBuilder = getNamespaceAwareDocumentBuilder();
134: for (int i = 0; i < schemaNames.length; i++) {
135: //the location for the third party schema;s is hardcoded
136: if (!"".equals(schemaNames[i].trim())) {
137: InputStream schemaStream = this .getClass()
138: .getResourceAsStream(
139: SCHEMA_PATH + schemaNames[i]);
140: Document doc = documentBuilder.parse(schemaStream);
141: additionalSchemaElements.add(doc
142: .getDocumentElement());
143: }
144: }
145:
146: //Create the Schema element array
147: schemaElements = new Element[additionalSchemaElements
148: .size()];
149: for (int i = 0; i < additionalSchemaElements.size(); i++) {
150: schemaElements[i] = (Element) additionalSchemaElements
151: .get(i);
152:
153: }
154: } catch (Exception e) {
155: throw new RuntimeException(CodegenMessages
156: .getMessage("extension.additionalSchemaFailure"), e);
157: }
158:
159: return schemaElements;
160: }
161:
162: private DocumentBuilder getNamespaceAwareDocumentBuilder()
163: throws ParserConfigurationException {
164: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
165: .newInstance();
166: documentBuilderFactory.setNamespaceAware(true);
167: return documentBuilderFactory.newDocumentBuilder();
168: }
169: }
|