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 javax.xml.registry.ConnectionFactory;
020    import javax.xml.registry.JAXRException;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.geronimo.gbean.GBeanInfo;
025    import org.apache.geronimo.gbean.GBeanInfoBuilder;
026    import org.apache.geronimo.naming.ResourceSource;
027    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
028    
029    /**
030     * Simple GBean to provide access to a JAXR ConnectionFactory
031     *
032     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
033     */
034    public class JAXRGBean implements ResourceSource {
035    
036        private final Log log = LogFactory.getLog(JAXRGBean.class);
037        private final ClassLoader cl;
038        private final String connectionFactoryClass;
039    
040        public JAXRGBean(String connectionFactoryClass, ClassLoader cl) {
041            this.cl = cl;
042            this.connectionFactoryClass = connectionFactoryClass;
043        }
044    
045        /**
046         * Returns a fresh, new implementation of javax.xml.registry.ConnectionFactory
047         *
048         * @return ConnectionFactory
049         */
050        public Object $getResource() {
051            if (connectionFactoryClass != null) {
052                System.setProperty("javax.xml.registry.ConnectionFactoryClass", connectionFactoryClass);
053                //TODO consider whether we should bypass the code below and just construct it ourselves, like it does.
054            }
055            Thread currentThread = Thread.currentThread();
056            ClassLoader oldCl = currentThread.getContextClassLoader();
057            currentThread.setContextClassLoader(cl);
058            try {
059                return ConnectionFactory.newInstance();
060            } catch (JAXRException e) {
061                log.error("Error creating ConnectionFactory", e);
062            } finally {
063                currentThread.setContextClassLoader(oldCl);
064            }
065    
066            return null;
067        }
068    
069    
070        public static final GBeanInfo GBEAN_INFO;
071    
072        static {
073            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(JAXRGBean.class,
074                    NameFactory.JAXR_CONNECTION_FACTORY);
075    
076            infoFactory.addAttribute("connectionFactoryClass", String.class, true, true);
077            infoFactory.addAttribute("classLoader", ClassLoader.class, false);
078    
079            infoFactory.addOperation("$getResource");
080    
081            infoFactory.setConstructor(new String[] {"connectionFactoryClass", "classLoader"});
082    
083            GBEAN_INFO = infoFactory.getBeanInfo();
084        }
085    
086        public static GBeanInfo getGBeanInfo() {
087            return GBEAN_INFO;
088        }
089    
090    
091    }