001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.system.util;
022    
023    import java.sql.Driver;
024    import java.lang.reflect.Method;
025    import java.lang.reflect.InvocationTargetException;
026    
027    import org.apache.geronimo.gbean.GBeanLifecycle;
028    import org.apache.geronimo.gbean.GBeanInfo;
029    import org.apache.geronimo.gbean.GBeanInfoBuilder;
030    
031    /**
032     * @version $Rev: 533154 $ $Date: 2007-04-27 12:06:53 -0400 (Fri, 27 Apr 2007) $
033     */
034    public class JDBCDriverRegistrationGBean implements GBeanLifecycle {
035    
036        private final Class ddClass;
037        private final Driver instance;
038    
039        public JDBCDriverRegistrationGBean(String className, ClassLoader classLoader) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
040            ddClass = classLoader.loadClass("org.apache.geronimo.jdbc.DelegatingDriver");
041            Method m = ddClass.getDeclaredMethod("registerDriver", new Class[] {Driver.class});
042            Class clazz = classLoader.loadClass(className);
043            instance = (Driver) clazz.newInstance();
044            //This is definitely enough to make the dd register itself with DriverManager
045            m.invoke(null, new Object[] {instance});
046        }
047    
048        public void doStart() throws Exception {
049        }
050    
051        public void doStop() throws Exception {
052            Method m = ddClass.getDeclaredMethod("unregisterDriver", new Class[] {Driver.class});
053            m.invoke(null, new Object[] {instance});
054        }
055    
056        public void doFail() {
057            try {
058                doStop();
059            } catch (Exception e) {
060                //ignore
061            }
062        }
063        public static final GBeanInfo GBEAN_INFO;
064    
065        static {
066            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JDBCDriverRegistrationGBean.class, "GBean");
067            infoBuilder.addAttribute("driverClassName", String.class, true);
068            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
069    
070            infoBuilder.setConstructor(new String[]{"driverClassName", "classLoader"});
071    
072            GBEAN_INFO = infoBuilder.getBeanInfo();
073        }
074    
075        public static GBeanInfo getGBeanInfo() {
076            return GBEAN_INFO;
077        }
078    
079    }