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.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: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 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    }