KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jndi > JndiObjectTargetSource


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jndi;
18
19 import javax.naming.NamingException JavaDoc;
20
21 import org.springframework.aop.TargetSource;
22
23 /**
24  * AOP {@link org.springframework.aop.TargetSource} that provides
25  * configurable JNDI lookups for <code>getTarget()</code> calls.
26  *
27  * <p>Can be used as alternative to {@link JndiObjectFactoryBean}, to allow for
28  * relocating a JNDI object lazily or for each operation (see "lookupOnStartup"
29  * and "cache" properties). This is particularly useful during development, as it
30  * allows for hot restarting of the JNDI server (for example, a remote JMS server).
31  *
32  * <p>Example:
33  *
34  * <pre>
35  * &lt;bean id="queueConnectionFactoryTarget" class="org.springframework.jndi.JndiObjectTargetSource"&gt;
36  * &lt;property name="jndiName" value="JmsQueueConnectionFactory"/&gt;
37  * &lt;property name="lookupOnStartup" value="false"/&gt;
38  * &lt;/bean&gt;
39  *
40  * &lt;bean id="queueConnectionFactory" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;
41  * &lt;property name="proxyInterfaces" value="javax.jms.QueueConnectionFactory"/&gt;
42  * &lt;property name="targetSource" ref="queueConnectionFactoryTarget"/&gt;
43  * &lt;/bean&gt;</pre>
44  *
45  * A <code>createQueueConnection</code> call on the "queueConnectionFactory" proxy will
46  * cause a lazy JNDI lookup for "JmsQueueConnectionFactory" and a subsequent delegating
47  * call to the retrieved QueueConnectionFactory's <code>createQueueConnection</code>.
48  *
49  * <p><b>Alternatively, use a {@link JndiObjectFactoryBean} with a "proxyInterface".</b>
50  * "lookupOnStartup" and "cache" can then be specified on the JndiObjectFactoryBean,
51  * creating a JndiObjectTargetSource underneath (instead of defining separate
52  * ProxyFactoryBean and JndiObjectTargetSource beans).
53  *
54  * @author Juergen Hoeller
55  * @since 1.1
56  * @see #setLookupOnStartup
57  * @see #setCache
58  * @see org.springframework.aop.framework.ProxyFactoryBean#setTargetSource
59  * @see JndiObjectFactoryBean#setProxyInterface
60  */

61 public class JndiObjectTargetSource extends JndiObjectLocator implements TargetSource {
62
63     private boolean lookupOnStartup = true;
64
65     private boolean cache = true;
66
67     private Object JavaDoc cachedObject;
68
69     private Class JavaDoc targetClass;
70
71
72     /**
73      * Set whether to look up the JNDI object on startup. Default is "true".
74      * <p>Can be turned off to allow for late availability of the JNDI object.
75      * In this case, the JNDI object will be fetched on first access.
76      * @see #setCache
77      */

78     public void setLookupOnStartup(boolean lookupOnStartup) {
79         this.lookupOnStartup = lookupOnStartup;
80     }
81
82     /**
83      * Set whether to cache the JNDI object once it has been located.
84      * Default is "true".
85      * <p>Can be turned off to allow for hot redeployment of JNDI objects.
86      * In this case, the JNDI object will be fetched for each invocation.
87      * @see #setLookupOnStartup
88      */

89     public void setCache(boolean cache) {
90         this.cache = cache;
91     }
92
93     public void afterPropertiesSet() throws NamingException JavaDoc {
94         super.afterPropertiesSet();
95         if (this.lookupOnStartup) {
96             Object JavaDoc object = lookup();
97             if (this.cache) {
98                 this.cachedObject = object;
99             }
100             else {
101                 this.targetClass = object.getClass();
102             }
103         }
104     }
105
106
107     public Class JavaDoc getTargetClass() {
108         return (this.cachedObject != null ? this.cachedObject.getClass() : this.targetClass);
109     }
110
111     public boolean isStatic() {
112         return (this.cachedObject != null);
113     }
114
115     public Object JavaDoc getTarget() {
116         try {
117             if (this.lookupOnStartup || !this.cache) {
118                 return (this.cachedObject != null ? this.cachedObject : lookup());
119             }
120             else {
121                 synchronized (this) {
122                     if (this.cachedObject == null) {
123                         this.cachedObject = lookup();
124                     }
125                     return this.cachedObject;
126                 }
127             }
128         }
129         catch (NamingException JavaDoc ex) {
130             throw new JndiLookupFailureException("JndiObjectTargetSource failed to obtain new target object", ex);
131         }
132     }
133
134     public void releaseTarget(Object JavaDoc target) {
135     }
136
137 }
138
Popular Tags