01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.wsdl.codegen.CodeGenConfiguration;
23:
24: import java.lang.reflect.Constructor;
25: import java.lang.reflect.InvocationTargetException;
26: import java.lang.reflect.Method;
27:
28: /**
29: * Code generation data binding extension for JiBX support. JiBX currently requires a predefined
30: * binding definition to be supplied in order to be used with Axis2.
31: */
32: public class JiBXExtension extends AbstractDBProcessingExtension {
33:
34: /** Name of "extra" option used to supply binding definition path. */
35: public static final String BINDING_PATH_OPTION = "bindingfile";
36: private static final String JIBX_MODEL_CLASS = "org.jibx.binding.model.BindingElement";
37: private static final String JIBX_UTILITY_CLASS = "org.apache.axis2.jibx.CodeGenerationUtility";
38: private static final String JIBX_UTILITY_METHOD = "engage";
39:
40: public void engage(CodeGenConfiguration configuration) {
41:
42: // just return if JiBX binding not active
43: if (testFallThrough(configuration.getDatabindingType())) {
44: return;
45: }
46:
47: // check the JiBX binding definition file specified
48: String path = (String) configuration.getProperties().get(
49: BINDING_PATH_OPTION);
50: try {
51:
52: // try dummy load of framework class first to check missing jars
53: try {
54: getClass().getClassLoader().loadClass(JIBX_MODEL_CLASS);
55: } catch (ClassNotFoundException e) {
56: throw new RuntimeException(
57: "JiBX framework jars not in classpath");
58: }
59:
60: // load the actual utility class
61: Class clazz;
62: try {
63: clazz = JiBXExtension.class.getClassLoader().loadClass(
64: JIBX_UTILITY_CLASS);
65: } catch (ClassNotFoundException e) {
66: throw new RuntimeException(
67: "JiBX binding extension not in classpath");
68: }
69:
70: // create an instance of the class
71: Object inst = null;
72: Constructor constructor = clazz
73: .getConstructor(new Class[] { CodeGenConfiguration.class });
74: inst = constructor
75: .newInstance(new Object[] { configuration });
76:
77: // invoke utility class method for actual processing
78: Method method = clazz.getMethod(JIBX_UTILITY_METHOD,
79: new Class[] { String.class });
80: method.invoke(inst, new Object[] { path });
81:
82: } catch (Exception e) {
83: if (e instanceof RuntimeException) {
84: throw (RuntimeException) e;
85: } else if (e instanceof InvocationTargetException) {
86: if (e.getCause() instanceof RuntimeException) {
87: throw (RuntimeException) e.getCause();
88: } else {
89: throw new RuntimeException(e);
90: }
91: } else {
92: throw new RuntimeException(e);
93: }
94: }
95:
96: }
97: }
|