001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.geronimo.webservices.saaj;
018    
019    import java.lang.reflect.InvocationTargetException;
020    import java.lang.reflect.Method;
021    
022    import javax.xml.soap.MessageFactory;
023    import javax.xml.soap.SAAJMetaFactory;
024    import javax.xml.soap.SOAPException;
025    import javax.xml.soap.SOAPFactory;
026    
027    public class GeronimoMetaFactory extends SAAJMetaFactory {
028    
029        protected MessageFactory newMessageFactory(String arg0) throws SOAPException {
030            return (MessageFactory)callFactoryMethod("newMessageFactory", arg0);
031        }
032    
033        protected SOAPFactory newSOAPFactory(String arg0) throws SOAPException {
034            return (SOAPFactory)callFactoryMethod("newSOAPFactory", arg0);
035        }
036    
037        private Object callFactoryMethod(String methodName, String arg) throws SOAPException {
038            SAAJMetaFactory factory = 
039                (SAAJMetaFactory) SAAJFactoryFinder.find("javax.xml.soap.MetaFactory");
040    
041            try {
042                Method method = 
043                    factory.getClass().getDeclaredMethod(methodName, new Class[] { String.class });
044                boolean accessibility = method.isAccessible();
045                try {
046                    method.setAccessible(true);
047                    Object result = method.invoke(factory, new Object[] { arg });                
048                    return result;
049                } catch (InvocationTargetException e) {
050                    if (e.getTargetException() instanceof SOAPException) {
051                        throw (SOAPException) e.getTargetException();
052                    } else {
053                        throw new SOAPException("Error calling factory method: " + methodName, e);
054                    }
055                } catch (IllegalArgumentException e) {
056                    throw new SOAPException("Error calling factory method: " + methodName, e);
057                } catch (IllegalAccessException e) {
058                    throw new SOAPException("Error calling factory method: " + methodName, e);
059                } finally {
060                    method.setAccessible(accessibility);
061                }
062            } catch (NoSuchMethodException e) {
063                throw new SOAPException("Factory method not found: " + methodName, e);
064            }
065        }
066    
067    }