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