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.jndi;
018
019import javax.naming.NamingException;
020
021import org.springframework.beans.factory.InitializingBean;
022import org.springframework.util.StringUtils;
023
024/**
025 * Convenient superclass for JNDI-based service locators,
026 * providing configurable lookup of a specific JNDI resource.
027 *
028 * <p>Exposes a {@link #setJndiName "jndiName"} property. This may or may not
029 * include the "java:comp/env/" prefix expected by J2EE applications when
030 * accessing a locally mapped (Environmental Naming Context) resource. If it
031 * doesn't, the "java:comp/env/" prefix will be prepended if the "resourceRef"
032 * property is true (the default is <strong>false</strong>) and no other scheme
033 * (e.g. "java:") is given.
034 *
035 * <p>Subclasses may invoke the {@link #lookup()} method whenever it is appropriate.
036 * Some classes might do this on initialization, while others might do it
037 * on demand. The latter strategy is more flexible in that it allows for
038 * initialization of the locator before the JNDI object is available.
039 *
040 * @author Juergen Hoeller
041 * @since 1.1
042 * @see #setJndiName
043 * @see #setJndiTemplate
044 * @see #setJndiEnvironment
045 * @see #setResourceRef
046 * @see #lookup()
047 */
048public abstract class JndiObjectLocator extends JndiLocatorSupport implements InitializingBean {
049
050        private String jndiName;
051
052        private Class<?> expectedType;
053
054
055        /**
056         * Specify the JNDI name to look up. If it doesn't begin with "java:comp/env/"
057         * this prefix is added automatically if "resourceRef" is set to "true".
058         * @param jndiName the JNDI name to look up
059         * @see #setResourceRef
060         */
061        public void setJndiName(String jndiName) {
062                this.jndiName = jndiName;
063        }
064
065        /**
066         * Return the JNDI name to look up.
067         */
068        public String getJndiName() {
069                return this.jndiName;
070        }
071
072        /**
073         * Specify the type that the located JNDI object is supposed
074         * to be assignable to, if any.
075         */
076        public void setExpectedType(Class<?> expectedType) {
077                this.expectedType = expectedType;
078        }
079
080        /**
081         * Return the type that the located JNDI object is supposed
082         * to be assignable to, if any.
083         */
084        public Class<?> getExpectedType() {
085                return this.expectedType;
086        }
087
088        @Override
089        public void afterPropertiesSet() throws IllegalArgumentException, NamingException {
090                if (!StringUtils.hasLength(getJndiName())) {
091                        throw new IllegalArgumentException("Property 'jndiName' is required");
092                }
093        }
094
095
096        /**
097         * Perform the actual JNDI lookup for this locator's target resource.
098         * @return the located target object
099         * @throws NamingException if the JNDI lookup failed or if the
100         * located JNDI object is not assigable to the expected type
101         * @see #setJndiName
102         * @see #setExpectedType
103         * @see #lookup(String, Class)
104         */
105        protected Object lookup() throws NamingException {
106                return lookup(getJndiName(), getExpectedType());
107        }
108
109}