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.jdbc;
022    
023    import java.sql.Driver;
024    import java.sql.Connection;
025    import java.sql.SQLException;
026    import java.sql.DriverPropertyInfo;
027    import java.sql.DriverManager;
028    import java.util.Properties;
029    
030    import javax.sql.DataSource;
031    import javax.naming.Context;
032    import javax.naming.InitialContext;
033    import javax.naming.NamingException;
034    import javax.naming.NameNotFoundException;
035    
036    /**
037     * @version $Rev: 533154 $ $Date: 2007-04-27 12:06:53 -0400 (Fri, 27 Apr 2007) $
038     */
039    public class DataSourceDriver implements Driver {
040    
041        private static final String BASE = "jdbc:geronimo:datasource:";
042    
043        static {
044            try {
045                DriverManager.registerDriver(new DataSourceDriver());
046            } catch (SQLException e) {
047                throw new RuntimeException(e);
048            }
049        }
050    
051        public Connection connect(String url, Properties info) throws SQLException {
052            if (url == null || !url.startsWith(BASE)) {
053                return null;
054            }
055            String name = url.substring(BASE.length());
056            DataSource ds;
057            try {
058                Context ctx = new InitialContext();
059                try {
060                    ds = (DataSource) ctx.lookup("java:comp/env/" + name);
061                } catch (NameNotFoundException e) {
062                    ds = (DataSource) ctx.lookup(name);
063                }
064            } catch (NamingException e) {
065                throw (SQLException)new SQLException("Could not look up datasource").initCause(e);
066            }
067            return ds.getConnection();
068        }
069    
070        public boolean acceptsURL(String url) throws SQLException {
071            return url != null && url.startsWith(BASE);
072        }
073    
074        public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
075            return new DriverPropertyInfo[0];
076        }
077    
078        public int getMajorVersion() {
079            return 1;
080        }
081    
082        public int getMinorVersion() {
083            return 0;
084        }
085    
086        public boolean jdbcCompliant() {
087            //lie shamelessly
088            return true;
089        }
090    }