KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > naming > NamingContext


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.naming;
20
21 import gcc.properties.*;
22 import gcc.adapter.Adapter;
23
24 import java.util.*;
25 import javax.naming.*;
26
27 public class NamingContext
28 {
29     public static final NamingContext getInstance(Class baseClass)
30     {
31         NamingContext context;
32         synchronized (_contextMap)
33         {
34             context = (NamingContext)_contextMap.get(baseClass);
35             if (context == null)
36             {
37                 context = new NamingContext();
38                 _contextMap.put(baseClass, context);
39                 context.init(baseClass);
40             }
41         }
42         return context;
43     }
44
45     private static ThreadLocal _current = new ThreadLocal();
46     private static HashMap _contextMap = new HashMap();
47     private static boolean _quiet = false; // TODO: Configure
48
private static boolean _verbose = true; // TODO: Configure
49
private String _logContext;
50     private HashMap _map = new HashMap();
51
52     public static final NamingContext getCurrent()
53     {
54         return (NamingContext)_current.get();
55     }
56
57     public static final NamingContext push(NamingContext that)
58     {
59         NamingContext restore = getCurrent();
60         _current.set(that);
61         return restore;
62     }
63
64     public static void pop(NamingContext restore)
65     {
66         _current.set(restore);
67     }
68
69     public HashMap getMap()
70     {
71         return _map;
72     }
73
74     public Object lookup(String name, String prefix) throws NamingException
75     {
76         if (prefix != null)
77         {
78             name += prefix + "/" + name;
79         }
80
81         // Note: this part of the method is performance critical. Please
82
// refrain from using string concatenation, synchronization and
83
// other slow calls here. All possible initialization should
84
// be performed in 'init' so as to permit this method to be as
85
// fast as possible (i.e. a simple unsynchronized HashMap lookup).
86

87         Object value = _map.get(name);
88
89         if (value == null)
90         {
91             value = dynamicLookup(name);
92             if (value != null)
93             {
94                 _map.put(name, value); // TODO: allow refresh.
95
}
96         }
97
98         if (value == null)
99         {
100             NameNotFoundException notFound = new NameNotFoundException(name.length() == 0 ? formatEmptyName() : name);
101             if (! _quiet)
102             {
103                 NameServiceLog.getInstance().warnNameNotFound(_logContext, notFound);
104             }
105             throw notFound;
106         }
107         else
108         {
109             return value;
110         }
111     }
112
113     public Object lookupReturnNullIfNotFound(String name, String prefix)
114     {
115         if (prefix != null)
116         {
117             name += prefix + "/" + name;
118         }
119         return _map.get(name);
120     }
121
122     protected void init(Class baseClass)
123     {
124         // TODO: Nothing really to do as this would init all the env-prop res-ref ... from a component
125
// this logic isn't required for the CORBA container.
126
}
127
128     protected void bindAdapter(Adapter adp)
129     {
130         _map.put( adp.getBindName(), adp );
131     }
132
133     protected boolean adapterExists(String name)
134     {
135         System.out.println( "TODO: NamingComponent.componentExists(): name = " + name );
136
137         //String propsFileName = SystemProperties.getRepository() + "/Component/" + name.replace('.', '/') + ".properties";
138
//return new java.io.File(propsFileName).exists();
139

140         return false;
141     }
142
143     protected Object dynamicLookup(String name)
144     {
145         return null;
146     }
147
148     /*
149     protected List getComponentsForInterface(String interfaceName)
150     {
151         return null;
152     }
153     */

154
155     /*
156     protected String resolveComponent(String name, String pattern)
157     {
158         return "";
159     }
160     */

161
162     /*
163     protected void copyObjectsWithRemoteInterface(final HashMap intoMap)
164     {
165     }
166     */

167
168     protected String formatEmptyName()
169     {
170         return "formatEmptyName:";
171     }
172
173 }
174
Popular Tags