KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > naming > LinkProxy


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.naming;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.config.types.InitParam;
33 import com.caucho.util.L10N;
34
35 import javax.annotation.PostConstruct;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import javax.naming.spi.InitialContextFactory JavaDoc;
40 import java.util.Hashtable JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * An object proxy for a foreign JNDI factory.
46  */

47 public class LinkProxy implements ObjectProxy, java.io.Serializable JavaDoc {
48   private static Logger JavaDoc log
49     = Logger.getLogger(LinkProxy.class.getName());
50   private static L10N L = new L10N(LinkProxy.class);
51
52   // The foreign factory
53
protected InitialContextFactory JavaDoc _factory;
54   // The foreign factory
55
protected Class JavaDoc _factoryClass;
56   // Properties for the object
57
protected Hashtable JavaDoc<String JavaDoc,String JavaDoc> _props;
58   // The jndi-link path
59
protected String JavaDoc _name;
60   // The foreign name
61
protected String JavaDoc _foreignName;
62
63   /**
64    * Creates a new LinkProxy.
65    */

66   public LinkProxy()
67     throws NamingException JavaDoc
68   {
69   }
70
71   /**
72    * Creates a new LinkProxy.
73    *
74    * @param factory the foreign factory
75    * @param props the properties for the object
76    * @param name the jndi-link path in the foreign namespace
77    */

78   public LinkProxy(InitialContextFactory JavaDoc factory,
79                    Hashtable JavaDoc<String JavaDoc,String JavaDoc> props,
80                    String JavaDoc name)
81     throws NamingException JavaDoc
82   {
83     if (factory == null)
84       throw new NullPointerException JavaDoc();
85     
86     _factory = factory;
87     _props = props;
88     _foreignName = name;
89   }
90
91   /**
92    * Creates a new LinkProxy.
93    *
94    * @param name the jndi-link path in the foreign namespace
95    */

96   public LinkProxy(String JavaDoc name)
97     throws NamingException JavaDoc
98   {
99     _foreignName = name;
100   }
101
102   /**
103    * Sets the jndi name.
104    */

105   public void setJndiName(String JavaDoc name)
106   {
107     _name = name;
108   }
109
110   /**
111    * @deprecated Use {@link #setJndiName}
112    */

113   public void setName(String JavaDoc name)
114   {
115     setJndiName(name);
116   }
117
118   /**
119    * Sets the factory
120    */

121   public void setFactory(Class JavaDoc factoryClass)
122   {
123     _factoryClass = factoryClass;
124   }
125
126   /**
127    * @deprecated Use {@link #setFactory}
128    */

129   public void setJndiFactory(Class JavaDoc factoryClass)
130   {
131     setFactory(factoryClass);
132   }
133
134   /**
135    * Sets the foreign-name
136    */

137   public void setForeignName(String JavaDoc name)
138   {
139     _foreignName = name;
140   }
141
142   /**
143    * Adds init param.
144    */

145   public void addInitParam(InitParam initParam)
146   {
147     if (_props == null)
148       _props = new Hashtable JavaDoc<String JavaDoc,String JavaDoc>();
149     
150     _props.putAll(initParam.getParameters());
151   }
152
153   /**
154    * Creates the object from the proxy.
155    *
156    * @param env the calling environment
157    *
158    * @return the object named by the proxy.
159    */

160   public Object JavaDoc createObject(Hashtable JavaDoc env)
161     throws NamingException JavaDoc
162   {
163     Context JavaDoc context;
164     Hashtable JavaDoc<String JavaDoc,String JavaDoc> mergeEnv;
165     
166     if (env == null || env.size() == 0)
167       mergeEnv = _props;
168     else if (_props == null || _props.size() == 0)
169       mergeEnv = env;
170     else {
171       mergeEnv = new Hashtable JavaDoc<String JavaDoc,String JavaDoc>();
172       mergeEnv.putAll(_props);
173       mergeEnv.putAll(env);
174     }
175
176     if (_factory != null) {
177       context = _factory.getInitialContext(mergeEnv);
178     }
179     else {
180       context = new InitialContext JavaDoc(mergeEnv);
181       _foreignName = Jndi.getFullName(_foreignName);
182     }
183
184     if (_foreignName != null) {
185       try {
186
187         return context.lookup(_foreignName);
188
189       } catch (RuntimeException JavaDoc e) {
190
191         if (log.isLoggable(Level.FINE))
192           log.log(Level.FINE, e.toString(), e);
193
194         throw e;
195
196       } catch (NamingException JavaDoc e) {
197
198         if (log.isLoggable(Level.FINER))
199           log.log(Level.FINER, e.toString(), e);
200
201         throw e;
202       }
203     }
204     else
205       return context;
206   }
207
208   /**
209    * Initialize the resource.
210    */

211   @PostConstruct
212   public void init()
213     throws Exception JavaDoc
214   {
215     if (_name == null)
216       throw new ConfigException(L.l("<jndi-link> configuration needs a <jndi-name>. The <jndi-name> is the JNDI name where the context will be linked."));
217     
218     Class JavaDoc factoryClass = _factoryClass;
219
220     if (factoryClass != null)
221       _factory = (InitialContextFactory JavaDoc) factoryClass.newInstance();
222
223     if (log.isLoggable(Level.CONFIG)) {
224       if (_foreignName != null)
225     log.config("jndi-link[jndi-name=" + _name
226            + ", foreign-name=" + _foreignName + "] configured");
227       else if (_factoryClass != null)
228     log.config("jndi-link[jndi-name=" + _name
229            + ", factory=" + _factoryClass.getName() + "] configured");
230     }
231
232     if (_foreignName != null
233     && Jndi.getFullName(_name).equals(Jndi.getFullName(_foreignName)))
234       return;
235
236     // server/155a - not a short link since it needs to be able to bind
237
// the jndi root
238
Jndi.bindDeep(_name, this);
239   }
240
241   public String JavaDoc toString()
242   {
243     if (_factoryClass != null)
244       return "LinkProxy[name=" + _name + ",factory=" + _factoryClass.getName() + "]";
245     else
246       return "LinkProxy[name=" + _name + ",foreign=" + _foreignName + "]";
247   }
248 }
249
Popular Tags