KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > naming > java > javaURLContextFactory


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.naming.java;
31
32 import com.caucho.loader.EnvironmentLocal;
33 import com.caucho.log.Log;
34 import com.caucho.naming.AbstractModel;
35 import com.caucho.naming.ContextImpl;
36 import com.caucho.naming.MemoryModel;
37 import com.caucho.util.L10N;
38
39 import javax.naming.Context JavaDoc;
40 import javax.naming.Name JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42 import javax.naming.spi.ObjectFactory JavaDoc;
43 import java.util.Hashtable JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Create a remote object
49  */

50 public class javaURLContextFactory implements ObjectFactory JavaDoc {
51   private static L10N L = new L10N(javaURLContextFactory.class);
52   private static Logger JavaDoc dbg = Log.open(javaURLContextFactory.class);
53
54   private static EnvironmentLocal<AbstractModel> _javaModel =
55   new EnvironmentLocal<AbstractModel>("caucho.naming.model.java");
56
57   /**
58    * Sets the model for the current class loader.
59    */

60   public static AbstractModel getContextModel()
61   {
62     return _javaModel.get();
63   }
64
65   /**
66    * Sets the model for the current class loader.
67    */

68   public static void setContextModel(AbstractModel model)
69   {
70     _javaModel.set(model);
71   }
72   
73   public Object JavaDoc getObjectInstance(Object JavaDoc obj,
74                                   Name JavaDoc name,
75                                   Context JavaDoc parentContext,
76                                   Hashtable JavaDoc<?,?> env)
77     throws NamingException JavaDoc
78   {
79     AbstractModel model = _javaModel.getLevel();
80
81     if (model == null) {
82       if (dbg.isLoggable(Level.FINER)) {
83     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
84     
85         dbg.finer(L.l("creating JNDI java: model for {0} parent:{1}",
86               loader,
87               (loader != null ? loader.getParent() : null)));
88       }
89         
90       model = _javaModel.get();
91
92       if (model != null)
93         model = model.copy();
94       else
95         model = new MemoryModel();
96
97       if (model.lookup("java:comp") == null)
98         model.createSubcontext("java:comp");
99
100       if (model.lookup("java:") == null)
101         model.createSubcontext("java:");
102
103       // XXX: java: is not a context itself. So you can't do a lookup on
104
// java: and then get a list.
105

106       _javaModel.set(model);
107     }
108
109     Context JavaDoc context = new ContextImpl(model, env);
110
111     if (obj != null)
112       return context.lookup((String JavaDoc) obj);
113     else
114       return context;
115   }
116 }
117
Popular Tags