001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * Created on Feb 4, 2004
019: *
020: *
021: * @author
022: */package org.apache.jetspeed.components.jndi;
023:
024: import java.util.Hashtable;
025:
026: import javax.naming.Context;
027: import javax.naming.NamingException;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import tyrex.naming.MemoryContext;
033: import tyrex.tm.RuntimeContext;
034:
035: /**
036: * <p>
037: * TyrexJNDIComponent
038: * </p>
039: * <p>
040: *
041: * </p>
042: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
043: * @version $Id: TyrexJNDIComponent.java 516881 2007-03-11 10:34:21Z ate $
044: *
045: */
046: public class TyrexJNDIComponent implements JNDIComponent {
047:
048: private static final Log log = LogFactory
049: .getLog(TyrexJNDIComponent.class);
050:
051: private MemoryContext rootJNDIContext;
052:
053: /**
054: * @see org.apache.fulcrum.Service#init()
055: */
056: public TyrexJNDIComponent() throws NamingException {
057: Context ctx = null;
058:
059: // Construct a non-shared memory context
060: Hashtable env = new Hashtable();
061: env.put(Context.INITIAL_CONTEXT_FACTORY,
062: "tyrex.naming.MemoryContextFactory");
063: rootJNDIContext = new MemoryContext(null);
064: rootJNDIContext.createSubcontext("jdbc");
065: ctx = rootJNDIContext.createSubcontext("comp");
066: ctx = ctx.createSubcontext("env");
067: ctx = ctx.createSubcontext("jdbc");
068:
069: // Associate the memory context with a new
070: // runtime context and associate the runtime context
071: // with the current thread
072: bindToCurrentThread();
073: log.info("JNDI successfully initiallized");
074:
075: }
076:
077: /**
078: * @see org.apache.jetspeed.cps.jndi.JNDIService#getRootContext()
079: */
080: public Context getRootContext() {
081: return rootJNDIContext;
082: }
083:
084: /**
085: * @see org.apache.jetspeed.cps.jndi.JNDIService#bindToCurrentThread()
086: */
087: public void bindToCurrentThread() throws NamingException {
088: RuntimeContext runCtx = RuntimeContext.newRuntimeContext(
089: rootJNDIContext, null);
090: RuntimeContext.setRuntimeContext(runCtx);
091: }
092:
093: /**
094: *
095: * <p>
096: * bindObject
097: * </p>
098: *
099: * @see org.apache.jetspeed.cps.jndi.JNDIComponent#bindObject(java.lang.String, java.lang.Object)
100: * @param bindToName
101: * @param obj
102: * @throws NamingException
103: */
104: public void bindObject(String bindToName, Object obj)
105: throws NamingException {
106: log.debug("Binding " + obj + " to name " + bindToName);
107: Context ctx = getRootContext();
108: ctx.bind(bindToName, obj);
109: }
110:
111: /**
112: * <p>
113: * unbindFromCurrentThread
114: * </p>
115: *
116: * @see org.apache.jetspeed.components.jndi.JNDIComponent#unbindFromCurrentThread()
117: * @throws NamingException
118: */
119: public void unbindFromCurrentThread() throws NamingException {
120: RuntimeContext.unsetRuntimeContext();
121: RuntimeContext.cleanup(Thread.currentThread());
122: }
123:
124: /* (non-Javadoc)
125: * @see org.apache.jetspeed.components.jndi.JNDIComponent#unbindObject(java.lang.String)
126: */
127: public void unbindObject(String name) throws NamingException {
128: log.debug("Unbinding name " + name);
129: Context ctx = getRootContext();
130: ctx.unbind(name);
131:
132: }
133: }
|