001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: NamingManager.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.naming;
025:
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.transaction.UserTransaction;
030:
031: import org.ow2.easybeans.naming.context.ContextImpl;
032: import org.ow2.util.log.Log;
033: import org.ow2.util.log.LogFactory;
034:
035: /**
036: * Manages the java: context used by components.
037: * @author Florent Benoit
038: */
039: public final class NamingManager {
040:
041: /**
042: * Logger.
043: */
044: private static Log logger = LogFactory.getLog(NamingManager.class);
045:
046: /**
047: * Associate a context to a thread.
048: */
049: private static ThreadLocal<Context> threadContext = new ThreadLocal<Context>();
050:
051: /**
052: * Initial Context.
053: */
054: private InitialContext ictx = null;
055:
056: /**
057: * Static context used by client container. One context for all the JVM.
058: */
059: private static Context clientCtx = null;
060:
061: /**
062: * Singleton management: - the constructor is private. - use static method
063: * getInstance to retrieve/create the instance.
064: */
065: private static NamingManager unique = null;
066:
067: /**
068: * UserTransaction object, to be shared by all components.
069: */
070: private UserTransaction userTransaction = null;
071:
072: /**
073: * Create the naming manager.
074: * @throws NamingException if no initial context is built
075: */
076: private NamingManager() throws NamingException {
077: ictx = new InitialContext();
078: }
079:
080: /**
081: * Return the unique instance of a NamingManager.
082: * @return NamingManager the unique instance.
083: * @throws NamingException if it failed.
084: */
085: public static synchronized NamingManager getInstance()
086: throws NamingException {
087: if (unique == null) {
088: unique = new NamingManager();
089: }
090: return unique;
091: }
092:
093: /**
094: * Get the initialContext used in this jonas server.
095: * @return InitialContext the initial context.
096: */
097: public InitialContext getInitialContext() {
098: return ictx;
099: }
100:
101: /**
102: * Create Context for application and component environments. (formally
103: * known as createComponentContext)
104: * @param namespace namespace to used for the Context
105: * @return a java: context with comp/ subcontext
106: * @throws NamingException if the creation of the java: context failed.
107: */
108: public Context createEnvironmentContext(final String namespace)
109: throws NamingException {
110:
111: // Create a new environment
112: ContextImpl ctx = new ContextImpl(namespace);
113:
114: // Create subContext
115: Context compCtx = ctx.createSubcontext("comp");
116:
117: // Bind java:comp/UserTransaction
118: if (userTransaction == null) {
119: try {
120: userTransaction = (UserTransaction) ictx
121: .lookup("javax.transaction.UserTransaction");
122: } catch (NamingException e) {
123: if (logger.isDebugEnabled()) {
124: logger.debug("Cannot lookup UserTransaction.", e);
125: }
126: }
127: }
128: if (userTransaction != null) {
129: compCtx.rebind("UserTransaction", userTransaction);
130: }
131:
132: //TODO : ORB
133:
134: return ctx;
135: }
136:
137: /**
138: * Get the Context associated with the current thread or to a class loader.
139: * @return Context the component context.
140: * @throws NamingException When operation is not allowed
141: */
142: public Context getComponentContext() throws NamingException {
143:
144: Context ctx = null;
145:
146: // Check if there is a context to the local thread
147: // For ejbs
148: ctx = threadContext.get();
149: if (ctx != null) {
150: return ctx;
151: }
152:
153: // Check static context. use in client. One context per JVM.
154: if (clientCtx != null) {
155: ctx = clientCtx;
156: if (ctx != null) {
157: return ctx;
158: }
159: }
160:
161: // No context found. This is outside of a j2ee component or server
162: // component.
163: if (ctx == null) {
164: throw new NamingException(
165: "No java: context for components running outside EasyBeans.");
166: }
167: return ctx;
168: }
169:
170: /**
171: * Associate this CompNamingContext with the current thread.
172: * This method should be called before the call to the business method.
173: * After, resetComponentContext should be called to reset the context.
174: * @param ctx the context to associate to the current thread.
175: * @return Context the context of the thread
176: */
177: public Context setComponentContext(final Context ctx) {
178: Context ret = threadContext.get();
179: threadContext.set(ctx);
180: return ret;
181: }
182:
183: /**
184: * Set back the context with the given value.
185: * Don't return the previous context, use setComponentContext() method for this.
186: * @param ctx the context to associate to the current thread.
187: */
188: public void resetComponentContext(final Context ctx) {
189: threadContext.set(ctx);
190: }
191:
192: /**
193: * Set the context used by client container (per JVM instead of per thread).
194: * @param ctx the context to set
195: */
196: public void setClientContainerComponentContext(final Context ctx) {
197: clientCtx = ctx;
198: }
199:
200: }
|