KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > impl > NameLookupImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.lib.impl;
16
17 import java.util.Hashtable JavaDoc;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.HiveMind;
25 import org.apache.hivemind.lib.NameLookup;
26 import org.apache.hivemind.lib.RemoteExceptionCoordinator;
27 import org.apache.hivemind.lib.RemoteExceptionEvent;
28 import org.apache.hivemind.lib.RemoteExceptionListener;
29
30 /**
31  * Standard implementation of the {@link org.apache.hivemind.lib.NameLookup}
32  * service interface.
33  *
34  * @author Howard Lewis Ship
35  */

36 public class NameLookupImpl implements NameLookup, RemoteExceptionListener
37 {
38     private RemoteExceptionCoordinator _coordinator;
39     private Context JavaDoc _initialContext;
40     private String JavaDoc _initialFactory;
41     private String JavaDoc _URLPackages;
42     private String JavaDoc _providerURL;
43
44     public Object JavaDoc lookup(String JavaDoc name, Class JavaDoc expected)
45     {
46         int i = 0;
47
48         while (true)
49         {
50             Context JavaDoc context = null;
51             Object JavaDoc raw = null;
52
53             try
54             {
55                 context = getInitialContext();
56
57                 raw = context.lookup(name);
58             }
59             catch (NamingException JavaDoc ex)
60             {
61                 if (i++ == 0)
62                     _coordinator.fireRemoteExceptionDidOccur(this, ex);
63                 else
64                     throw new ApplicationRuntimeException(
65                         ImplMessages.unableToLookup(name, context),
66                         ex);
67                 continue;
68             }
69
70             if (raw == null)
71                 throw new ApplicationRuntimeException(ImplMessages.noObject(name, expected));
72
73             if (!expected.isAssignableFrom(raw.getClass()))
74                 throw new ApplicationRuntimeException(ImplMessages.wrongType(name, raw, expected));
75
76             return raw;
77         }
78     }
79
80     private Context JavaDoc getInitialContext() throws NamingException JavaDoc
81     {
82         if (_initialContext == null)
83         {
84
85             Hashtable JavaDoc properties = new Hashtable JavaDoc();
86
87             if (!HiveMind.isBlank(_initialFactory))
88                 properties.put(Context.INITIAL_CONTEXT_FACTORY, _initialFactory);
89
90             if (!HiveMind.isBlank(_providerURL))
91                 properties.put(Context.PROVIDER_URL, _providerURL);
92
93             if (!HiveMind.isBlank(_URLPackages))
94                 properties.put(Context.URL_PKG_PREFIXES, _URLPackages);
95
96             _initialContext = constructContext(properties);
97         }
98
99         return _initialContext;
100     }
101
102     /**
103      * Constructs the InitialContext (this is separated out in a standalone
104      * method so that it may be overridden in a testing subclass).
105      */

106     protected Context JavaDoc constructContext(Hashtable JavaDoc properties) throws NamingException JavaDoc
107     {
108         return new InitialContext JavaDoc(properties);
109     }
110
111     /**
112      * Sets the InitialContext to null.
113      */

114     public void remoteExceptionDidOccur(RemoteExceptionEvent event)
115     {
116         _initialContext = null;
117     }
118
119     /**
120      * Sets the initial factory used to create the initial JNDI context.
121      * Equivalent to the system property <code>java.naming.factory.initial</code>.
122      */

123     public void setInitialFactory(String JavaDoc string)
124     {
125         _initialFactory = string;
126     }
127
128     /**
129      * Sets the JNDI provider URL, used to create the initial JNDI context.
130      * Equivalent to the system property <code>java.naming.provider.url</code>.
131      */

132     public void setProviderURL(String JavaDoc string)
133     {
134         _providerURL = string;
135     }
136
137     /**
138      * Sets the URL packages, used to create the initial JNDI context.
139      * Equivalent to the system property
140      * <code>java.naming.factory.url.pkgs</code>
141      */

142
143     public void setURLPackages(String JavaDoc string)
144     {
145         _URLPackages = string;
146     }
147
148     public void setCoordinator(RemoteExceptionCoordinator coordinator)
149     {
150         _coordinator = coordinator;
151     }
152
153 }
154
Popular Tags