001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.datasource.lookup;
018
019import javax.naming.NamingException;
020import javax.sql.DataSource;
021
022import org.springframework.jndi.JndiLocatorSupport;
023
024/**
025 * JNDI-based {@link DataSourceLookup} implementation.
026 *
027 * <p>For specific JNDI configuration, it is recommended to configure
028 * the "jndiEnvironment"/"jndiTemplate" properties.
029 *
030 * @author Costin Leau
031 * @author Juergen Hoeller
032 * @since 2.0
033 * @see #setJndiEnvironment
034 * @see #setJndiTemplate
035 */
036public class JndiDataSourceLookup extends JndiLocatorSupport implements DataSourceLookup {
037
038        public JndiDataSourceLookup() {
039                setResourceRef(true);
040        }
041
042        @Override
043        public DataSource getDataSource(String dataSourceName) throws DataSourceLookupFailureException {
044                try {
045                        return lookup(dataSourceName, DataSource.class);
046                }
047                catch (NamingException ex) {
048                        throw new DataSourceLookupFailureException(
049                                        "Failed to look up JNDI DataSource with name '" + dataSourceName + "'", ex);
050                }
051        }
052
053}