1 /* 2 * Copyright 2006 Thierry Guerin 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.exolab.castor.xml.wls8; 17 18 import java.lang.reflect.InvocationTargetException; 19 import java.lang.reflect.Method; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 public abstract class WeblogicXercesImplementation { 25 private static final Log LOG = LogFactory.getLog(WeblogicXercesImplementation.class); 26 27 /** 28 * Returns a Method object that reflects the specified public member method of the class 29 * or interface represented by <code>aClass</code>. 30 * <br> If either an SecurityException or NoSuchMethodException 31 * is thrown, calls {@link #handleStaticInitException(Exception)}. 32 * 33 * @param aClass The class. 34 * @param methodName The name of the method. 35 * @param parameterTypes The list of parameters. 36 * @return The Method object that matches the specified name and parameterTypes. 37 * @see Class#getMethod(java.lang.String, java.lang.Class[]) 38 */ 39 protected static Method getMethod(Class aClass, String methodName, Class[] parameterTypes) { 40 Method method = null; 41 try { 42 method = aClass.getMethod(methodName, parameterTypes); 43 } 44 catch (SecurityException e) { 45 handleStaticInitException("Error while trying to get the method " 46 + methodName + " in class " + aClass, e); 47 } 48 catch (NoSuchMethodException e) { 49 handleStaticInitException("Error while trying to get the method " 50 + methodName + " in class " + aClass, e); 51 } 52 return method; 53 } 54 55 /** 56 * Invokes a method. Calls {@link Method#invoke(java.lang.Object, java.lang.Object[])}. If either 57 * an IllegalArgumentException, IllegalAccessException or InvocationTargetException is thrown, 58 * calls {@link #handleMethodInvokeException(Exception)}. 59 * 60 * @param anObject The object instance to invoke the method on. 61 * @param method The method to invoke. 62 * @param params The parameters to pass to the method. 63 * @return If the method completes normally, the value it returns is returned to the caller 64 * of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. 65 * If the underlying method return type is void, the invocation returns null. 66 * @see Method#invoke(java.lang.Object, java.lang.Object[]) 67 */ 68 protected Object invoke(Object anObject, Method method, Object[] params) { 69 try { 70 return method.invoke(anObject, params); 71 } 72 catch (IllegalArgumentException e) { 73 handleMethodInvokeException(e); 74 } 75 catch (IllegalAccessException e) { 76 handleMethodInvokeException(e); 77 } 78 catch (InvocationTargetException e) { 79 handleMethodInvokeException(e); 80 } 81 // this code is never reached in theory but is necessary because handleMethodInvokeException 82 // throws a RuntimeException (unchecked). 83 return null; 84 } 85 86 // exception handling methods 87 public static void handleStaticInitException(Exception e) { 88 handleStaticInitException("Error while intializing class", e); 89 } 90 91 /** 92 * Throws a Runtime exception with <code>e</code>'s {@link Exception#getMessage() message} as its 93 * detail message. Also logs the exception as an error. 94 * Called if an error occurs during the static initialization of WeblogicXercesSerializer 95 * & OutputFormat (these classes use reflection to get the Weblogic classes & methods). 96 * 97 * @param message The Message that will be inserted before <code>e</code>'s 98 * {@link Exception#getMessage() message} in the RuntimeException's detail message. 99 * @param e The exception that will be "wrapped" in a RuntimeException 100 */ 101 public static void handleStaticInitException(String message, Exception e) { 102 LOG.error(message, e); 103 if (e instanceof RuntimeException) // don't wrap the exception 104 throw (RuntimeException) e; 105 throw new RuntimeException(message + ". " + e.getMessage()); // java 1.3, can't wrap using the 1.4 constructor 106 } 107 108 protected static void handleMethodInvokeException(Exception e) { 109 handleMethodInvokeException("Error while trying to invoke a method", e); 110 } 111 112 /** 113 * Throws a Runtime exception with <code>e</code>'s {@link Exception#getMessage() message} as its 114 * detail message. Also logs the exception as an error. 115 * 116 * @param message The Message that will be inserted before <code>e</code>'s 117 * {@link Exception#getMessage() message} in the RuntimeException's detail message. 118 * @param e The exception that will be "wrapped" in a RuntimeException. 119 */ 120 protected static void handleMethodInvokeException(String message, Exception e) { 121 LOG.error(message, e); 122 if (e instanceof RuntimeException) // don't wrap the exception 123 throw (RuntimeException) e; 124 throw new RuntimeException(message + ". " + e.getMessage()); // java 1.3, can't wrap using the 1.4 constructor 125 } 126 }