1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.axis2.wsdl.codegen.extension;
21
22 import org.apache.axis2.description.AxisService;
23 import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
24 import org.apache.axis2.wsdl.databinding.TypeMapper;
25 import org.apache.axis2.wsdl.i18n.CodegenMessages;
26 import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
33 import java.io.InputStream;
34 import java.lang.reflect.Method;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38
39 public class JAXBRIExtension extends AbstractDBProcessingExtension {
40
41 public static final String SCHEMA_FOLDER = "schemas";
42
43 public static String MAPPINGS = "mappings";
44 public static String MAPPING = "mapping";
45 public static String MESSAGE = "message";
46 public static String JAVA_NAME = "javaclass";
47
48 public static final String MAPPING_FOLDER = "Mapping";
49 public static final String MAPPER_FILE_NAME = "mapper";
50 public static final String SCHEMA_PATH = "/org/apache/axis2/wsdl/codegen/schema/";
51
52 public static final String JAXB_RI_API_CLASS = "javax.xml.bind.JAXBContext";
53 public static final String JAXB_RI_IMPL_CLASS = "com.sun.xml.bind.Util";
54 public static final String JAXB_RI_XJC_CLASS = "com.sun.tools.xjc.api.XJC";
55
56 public static final String JAXB_RI_UTILITY_CLASS =
57 "org.apache.axis2.jaxbri.CodeGenerationUtility";
58
59 public static final String JAXB_RI_PROCESS_METHOD = "processSchemas";
60
61
62 public void engage(CodeGenConfiguration configuration) {
63
64 //test the databinding type. If not just fall through
65 if (testFallThrough(configuration.getDatabindingType())) {
66 return;
67 }
68
69 try {
70
71 // try dummy load of framework classes first to check missing jars
72 try {
73 ClassLoader cl = getClass().getClassLoader();
74 cl.loadClass(JAXB_RI_API_CLASS);
75 cl.loadClass(JAXB_RI_IMPL_CLASS);
76 cl.loadClass(JAXB_RI_XJC_CLASS);
77 } catch (ClassNotFoundException e) {
78 throw new RuntimeException("JAX-B RI JARs not on classpath");
79 }
80
81 // load the actual utility class
82 Class clazz = null;
83 try {
84 clazz = JAXBRIExtension.class.getClassLoader().loadClass(JAXB_RI_UTILITY_CLASS);
85 } catch (ClassNotFoundException e) {
86 throw new RuntimeException("JAX-B RI binding extension not in classpath");
87 }
88
89 // invoke utility class method for actual processing
90 Method method = clazz.getMethod(JAXB_RI_PROCESS_METHOD,
91 new Class[] { List.class, Element[].class,
92 CodeGenConfiguration.class });
93 List schemas = new ArrayList();
94 List axisServices = configuration.getAxisServices();
95 AxisService axisService = null;
96 for (Iterator iter = axisServices.iterator(); iter.hasNext();) {
97 axisService = (AxisService)iter.next();
98 schemas.addAll(axisService.getSchema());
99 }
100 Element[] additionalSchemas = loadAdditionalSchemas();
101 TypeMapper mapper = (TypeMapper)method.invoke(null,
102 new Object[] { schemas, additionalSchemas,
103 configuration });
104
105 // set the type mapper to the config
106 configuration.setTypeMapper(mapper);
107
108 } catch (Exception e) {
109 if (e instanceof RuntimeException) {
110 throw (RuntimeException)e;
111 } else {
112 throw new RuntimeException(e);
113 }
114 }
115
116 }
117
118
119 /**
120 * Loading the external schemas.
121 *
122 * @return element array consisting of the the DOM element objects that represent schemas
123 */
124 private Element[] loadAdditionalSchemas() {
125 //load additional schemas
126 String[] schemaNames = ConfigPropertyFileLoader.getThirdPartySchemaNames();
127 Element[] schemaElements;
128
129 try {
130 ArrayList additionalSchemaElements = new ArrayList();
131 DocumentBuilder documentBuilder = getNamespaceAwareDocumentBuilder();
132 for (int i = 0; i < schemaNames.length; i++) {
133 //the location for the third party schema;s is hardcoded
134 if (!"".equals(schemaNames[i].trim())) {
135 InputStream schemaStream =
136 this.getClass().getResourceAsStream(SCHEMA_PATH + schemaNames[i]);
137 Document doc = documentBuilder.parse(schemaStream);
138 additionalSchemaElements.add(doc.getDocumentElement());
139 }
140 }
141
142 //Create the Schema element array
143 schemaElements = new Element[additionalSchemaElements.size()];
144 for (int i = 0; i < additionalSchemaElements.size(); i++) {
145 schemaElements[i] = (Element)additionalSchemaElements.get(i);
146
147 }
148 } catch (Exception e) {
149 throw new RuntimeException(
150 CodegenMessages.getMessage("extension.additionalSchemaFailure"), e);
151 }
152
153 return schemaElements;
154 }
155
156 private DocumentBuilder getNamespaceAwareDocumentBuilder() throws ParserConfigurationException {
157 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
158 documentBuilderFactory.setNamespaceAware(true);
159 return documentBuilderFactory.newDocumentBuilder();
160 }
161 }
162