View Javadoc

1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements.  See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache License, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  /***
18   * Created on Feb 4, 2004
19   *
20   * 
21   * @author
22   */
23  package org.apache.jetspeed.components.jndi;
24  
25  import java.util.Hashtable;
26  
27  import javax.naming.Context;
28  import javax.naming.NamingException;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  import tyrex.naming.MemoryContext;
34  import tyrex.tm.RuntimeContext;
35  
36  /***
37   * <p>
38   * TyrexJNDIComponent
39   * </p>
40   * <p>
41   * 
42   * </p>
43   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
44   * @version $Id: TyrexJNDIComponent.java 516881 2007-03-11 10:34:21Z ate $
45   *
46   */
47  public class TyrexJNDIComponent implements JNDIComponent
48  {
49  
50      private static final Log log = LogFactory.getLog(TyrexJNDIComponent.class);
51  
52      private MemoryContext rootJNDIContext;
53  
54      /***
55       * @see org.apache.fulcrum.Service#init()
56       */
57      public TyrexJNDIComponent() throws NamingException
58      {
59          Context ctx = null;
60  
61          // Construct a non-shared memory context
62          Hashtable env = new Hashtable();
63          env.put(Context.INITIAL_CONTEXT_FACTORY, "tyrex.naming.MemoryContextFactory");
64          rootJNDIContext = new MemoryContext(null);
65          rootJNDIContext.createSubcontext("jdbc");
66          ctx = rootJNDIContext.createSubcontext("comp");
67          ctx = ctx.createSubcontext("env");
68          ctx = ctx.createSubcontext("jdbc");
69  
70          //	Associate the memory context with a new
71          //	runtime context and associate the runtime context
72          //	with the current thread
73          bindToCurrentThread();
74          log.info("JNDI successfully initiallized");
75  
76      }
77  	
78  
79      /***
80       * @see org.apache.jetspeed.cps.jndi.JNDIService#getRootContext()
81       */
82      public Context getRootContext()
83      {
84          return rootJNDIContext;
85      }
86  
87      /***
88       * @see org.apache.jetspeed.cps.jndi.JNDIService#bindToCurrentThread()
89       */
90      public void bindToCurrentThread() throws NamingException
91      {
92          RuntimeContext runCtx = RuntimeContext.newRuntimeContext(rootJNDIContext, null);
93          RuntimeContext.setRuntimeContext(runCtx);
94      }
95      
96      /***
97       *  
98       * <p>
99       * bindObject
100      * </p>
101      * 
102      * @see org.apache.jetspeed.cps.jndi.JNDIComponent#bindObject(java.lang.String, java.lang.Object)
103      * @param bindToName
104      * @param obj
105      * @throws NamingException
106      */
107 	public void bindObject(String bindToName, Object obj) throws NamingException
108 	{
109 	    log.debug("Binding "+obj+" to name "+bindToName);
110 		Context ctx = getRootContext();
111 		ctx.bind(bindToName, obj);		
112 	}
113 
114     /*** 
115      * <p>
116      * unbindFromCurrentThread
117      * </p>
118      * 
119      * @see org.apache.jetspeed.components.jndi.JNDIComponent#unbindFromCurrentThread()
120      * @throws NamingException
121      */
122     public void unbindFromCurrentThread() throws NamingException
123     {
124 		RuntimeContext.unsetRuntimeContext();
125 		RuntimeContext.cleanup(Thread.currentThread());		
126     }
127 
128     /* (non-Javadoc)
129      * @see org.apache.jetspeed.components.jndi.JNDIComponent#unbindObject(java.lang.String)
130      */
131     public void unbindObject( String name ) throws NamingException
132     {
133         log.debug("Unbinding name "+name);
134 		Context ctx = getRootContext();
135 		ctx.unbind(name);		
136 
137     }
138 }