001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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.jaxr;
018    
019    import org.apache.geronimo.gbean.GBeanInfo;
020    import org.apache.geronimo.gbean.GBeanInfoBuilder;
021    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
022    import org.apache.commons.logging.LogFactory;
023    import org.apache.commons.logging.Log;
024    
025    import javax.xml.registry.ConnectionFactory;
026    import javax.xml.registry.JAXRException;
027    
028    /**
029     * Simple GBean to provide access to a JAXR ConnectionFactory
030     *
031     * @version $Rev: 398050 $ $Date: 2006-04-28 18:10:56 -0700 (Fri, 28 Apr 2006) $
032     */
033    public class JAXRGBean {
034    
035        private final Log log = LogFactory.getLog(JAXRGBean.class);
036        private final ClassLoader cl;
037        private final String connectionFactoryClass;
038    
039        public JAXRGBean(String connectionFactoryClass, ClassLoader cl) {
040            this.cl = cl;
041            this.connectionFactoryClass = connectionFactoryClass;
042        }
043    
044        /**
045         * Returns a fresh, new implementation of javax.xml.registry.ConnectionFactory
046         *
047         * @return ConnectionFactory
048         */
049        public Object $getResource() {
050            if (connectionFactoryClass != null) {
051                System.setProperty("javax.xml.registry.ConnectionFactoryClass", connectionFactoryClass);
052                //TODO consider whether we should bypass the code below and just construct it ourselves, like it does.
053            }
054            Thread currentThread = Thread.currentThread();
055            ClassLoader oldCl = currentThread.getContextClassLoader();
056            currentThread.setContextClassLoader(cl);
057            try {
058                return ConnectionFactory.newInstance();
059            } catch (JAXRException e) {
060                log.error("Error creating ConnectionFactory", e);
061            } finally {
062                currentThread.setContextClassLoader(oldCl);
063            }
064    
065            return null;
066        }
067    
068    
069        public static final GBeanInfo GBEAN_INFO;
070    
071        static {
072            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(JAXRGBean.class,
073                    NameFactory.JAXR_CONNECTION_FACTORY);
074    
075            infoFactory.addAttribute("connectionFactoryClass", String.class, true, true);
076            infoFactory.addAttribute("classLoader", ClassLoader.class, false);
077    
078            infoFactory.addOperation("$getResource");
079    
080            infoFactory.setConstructor(new String[] {"connectionFactoryClass", "classLoader"});
081    
082            GBEAN_INFO = infoFactory.getBeanInfo();
083        }
084    
085        public static GBeanInfo getGBeanInfo() {
086            return GBEAN_INFO;
087        }
088    
089    
090    }