001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.naming;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.config.types.InitParam;
033: import com.caucho.util.L10N;
034:
035: import javax.annotation.PostConstruct;
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038: import javax.naming.NamingException;
039: import javax.naming.spi.InitialContextFactory;
040: import java.util.Hashtable;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: /**
045: * An object proxy for a foreign JNDI factory.
046: */
047: public class LinkProxy implements ObjectProxy, java.io.Serializable {
048: private static Logger log = Logger.getLogger(LinkProxy.class
049: .getName());
050: private static L10N L = new L10N(LinkProxy.class);
051:
052: // The foreign factory
053: protected InitialContextFactory _factory;
054: // The foreign factory
055: protected Class _factoryClass;
056: // Properties for the object
057: protected Hashtable<String, String> _props = new Hashtable<String, String>();
058: // The jndi-link path
059: protected String _jndiName;
060: // The jndi-link path
061: protected String _jndiLocalName;
062: // The foreign name
063: protected String _foreignName;
064:
065: /**
066: * Creates a new LinkProxy.
067: */
068: public LinkProxy() throws NamingException {
069: }
070:
071: /**
072: * Creates a new LinkProxy.
073: *
074: * @param factory the foreign factory
075: * @param props the properties for the object
076: * @param name the jndi-link path in the foreign namespace
077: */
078: public LinkProxy(InitialContextFactory factory,
079: Hashtable<String, String> props, String name)
080: throws NamingException {
081: if (factory == null)
082: throw new NullPointerException();
083:
084: _factory = factory;
085: _props = props;
086: _foreignName = name;
087: }
088:
089: /**
090: * Creates a new LinkProxy.
091: *
092: * @param name the jndi-link path in the foreign namespace
093: */
094: public LinkProxy(String name) throws NamingException {
095: _foreignName = name;
096: }
097:
098: /**
099: * Sets the jndi name.
100: */
101: public void setJndiName(String name) {
102: _jndiName = name;
103: }
104:
105: /**
106: * Sets the jndi name.
107: */
108: public void setJndiLocalName(String name) {
109: _jndiLocalName = name;
110: }
111:
112: /**
113: * @deprecated Use {@link #setJndiName}
114: */
115: public void setName(String name) {
116: setJndiName(name);
117: }
118:
119: /**
120: * Sets the factory
121: */
122: public void setFactory(Class factoryClass) {
123: _factoryClass = factoryClass;
124: }
125:
126: /**
127: * @deprecated Use {@link #setFactory}
128: */
129: public void setJndiFactory(Class factoryClass) {
130: setFactory(factoryClass);
131: }
132:
133: /**
134: * Sets the foreign-name
135: */
136: public void setForeignName(String name) {
137: _foreignName = name;
138: }
139:
140: /**
141: * Adds init param.
142: */
143: public void addInitParam(InitParam initParam) {
144: if (_props == null)
145: _props = new Hashtable<String, String>();
146:
147: _props.putAll(initParam.getParameters());
148: }
149:
150: /**
151: * Creates the object from the proxy.
152: *
153: * @param env the calling environment
154: *
155: * @return the object named by the proxy.
156: */
157: public Object createObject(Hashtable env) throws NamingException {
158: Context context;
159: Hashtable<String, String> mergeEnv;
160:
161: mergeEnv = new Hashtable<String, String>();
162:
163: if (env != null && env.size() > 0)
164: mergeEnv.putAll(env);
165: if (_props != null && _props.size() > 0)
166: mergeEnv.putAll(_props);
167:
168: context = new InitialContext(mergeEnv);
169:
170: if (_foreignName != null) {
171: String foreignName;
172:
173: if (_factoryClass != null)
174: foreignName = _foreignName;
175: else
176: foreignName = Jndi.getFullName(_foreignName);
177:
178: try {
179: Object value = context.lookup(foreignName);
180:
181: return value;
182: } catch (RuntimeException e) {
183: throw e;
184: } catch (NamingException e) {
185: throw e;
186: }
187: } else
188: return context;
189: }
190:
191: /**
192: * Initialize the resource.
193: */
194: @PostConstruct
195: public void init() throws Exception {
196: if (_jndiName == null && _jndiLocalName == null)
197: throw new ConfigException(
198: L
199: .l("<jndi-link> configuration needs a <jndi-name>. The <jndi-name> is the JNDI name where the context will be linked."));
200:
201: Class factoryClass = _factoryClass;
202:
203: if (factoryClass != null) {
204: if (_props == null)
205: _props = new Hashtable<String, String>();
206:
207: _props.put("java.naming.factory.initial", factoryClass
208: .getName());
209: }
210:
211: if (log.isLoggable(Level.CONFIG)) {
212: if (_foreignName != null)
213: log.config("jndi-link[jndi-name=" + _jndiName
214: + ", foreign-name=" + _foreignName
215: + "] configured");
216: else if (_factoryClass != null)
217: log.config("jndi-link[jndi-name=" + _jndiName
218: + ", factory=" + _factoryClass.getName()
219: + "] configured");
220: }
221:
222: if (_foreignName != null
223: && Jndi.getFullName(_jndiName).equals(
224: Jndi.getFullName(_foreignName)))
225: return;
226:
227: // server/155a - not a short link since it needs to be able to bind
228: // the jndi root
229: if (_jndiLocalName != null)
230: Jndi.rebindDeep(_jndiLocalName, this );
231: else
232: Jndi.rebindDeepShort(_jndiName, this );
233: }
234:
235: public String toString() {
236: if (_factoryClass != null)
237: return "LinkProxy[name=" + _jndiName + ",factory="
238: + _factoryClass.getName() + "]";
239: else
240: return "LinkProxy[name=" + _jndiName + ",foreign="
241: + _foreignName + "]";
242: }
243: }
|