KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > ThreadLocalENCFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.Name JavaDoc;
28 import javax.naming.spi.ObjectFactory JavaDoc;
29 import org.jnp.interfaces.NamingContext;
30 import org.jnp.server.NamingServer;
31
32 /**
33  * Implementation of "java:comp" namespace factory. The context is associated
34  * with the thread class loader.
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 37459 $
38  */

39 public class ThreadLocalENCFactory
40         implements ObjectFactory JavaDoc
41 {
42    private static ThreadLocal JavaDoc enc = new ThreadLocal JavaDoc();
43    private static ThreadLocal JavaDoc stack = new ThreadLocal JavaDoc();
44
45    public static Context JavaDoc create(Context JavaDoc parent) throws Exception JavaDoc
46    {
47       NamingServer srv = new NamingServer();
48       return new NamingContext(parent.getEnvironment(), null, srv);
49    }
50
51    public static void push(Context JavaDoc ctx)
52    {
53       if (enc.get() == null)
54       {
55          enc.set(ctx);
56          return;
57       }
58       LinkedList JavaDoc currentStack = (LinkedList JavaDoc) stack.get();
59       if (currentStack == null)
60       {
61          currentStack = new LinkedList JavaDoc();
62          stack.set(currentStack);
63       }
64       currentStack.addLast(enc.get());
65       enc.set(ctx);
66    }
67
68    public static void pop()
69    {
70       LinkedList JavaDoc currentStack = (LinkedList JavaDoc) stack.get();
71       if (currentStack == null)
72       {
73          enc.set(null);
74          return;
75       }
76       if (currentStack.size() == 0)
77       {
78          enc.set(null);
79          return;
80       }
81       Object JavaDoc previous = currentStack.removeLast();
82       enc.set(previous);
83    }
84    // Constructors --------------------------------------------------
85

86    // Public --------------------------------------------------------
87

88    // ObjectFactory implementation ----------------------------------
89
public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
90                                    Hashtable JavaDoc environment)
91            throws Exception JavaDoc
92    {
93       return enc.get();
94    }
95
96 }
97
Popular Tags