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.loader.EnvironmentLocal;
032: import com.caucho.util.L10N;
033:
034: import javax.naming.Context;
035: import javax.naming.NamingException;
036: import javax.naming.spi.InitialContextFactory;
037: import java.util.Hashtable;
038: import java.util.logging.Logger;
039:
040: /**
041: * Returns the JNDI context for the current classloader. Since each
042: * thread will normally have its own current class loader, the
043: * actual JNDI context is stored in the contextClassLoader.
044: */
045: public class InitialContextFactoryImpl implements InitialContextFactory {
046: private static Logger log = Logger
047: .getLogger(InitialContextFactoryImpl.class.getName());
048: private static L10N L = new L10N(InitialContextFactoryImpl.class);
049:
050: private static EnvironmentLocal<AbstractModel> _rootModel = new EnvironmentLocal<AbstractModel>();
051:
052: /**
053: * Constructor with an initial root.
054: */
055: public InitialContextFactoryImpl() {
056: }
057:
058: /**
059: * Sets the model for the current class loader.
060: */
061: public static AbstractModel getContextModel() {
062: return _rootModel.get();
063: }
064:
065: /**
066: * Sets the model for the current class loader.
067: */
068: public static void setContextModel(AbstractModel model) {
069: _rootModel.set(model);
070: }
071:
072: public static AbstractModel createRoot() {
073: synchronized (_rootModel) {
074: AbstractModel model = _rootModel.getLevel();
075:
076: if (model == null) {
077: EnvironmentModelRoot root = EnvironmentModelRoot
078: .create();
079:
080: model = root.get("");
081:
082: _rootModel.set(model);
083:
084: try {
085: AbstractModel javaComp = model
086: .createSubcontext("java:comp");
087: AbstractModel java = model
088: .createSubcontext("java:");
089: // server/158i
090: java.bind("comp", javaComp);
091: } catch (NamingException e) {
092: throw new RuntimeException(e);
093: }
094: }
095:
096: return model;
097: }
098: }
099:
100: /**
101: * Returns the initial context for the current thread.
102: */
103: public Context getInitialContext(Hashtable<?, ?> env)
104: throws NamingException {
105: AbstractModel model = createRoot();
106:
107: return new ContextImpl(model, env);
108: }
109: }
|