|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
NameLookupImpl.java | 64.3% | 87.5% | 75% | 79.6% |
|
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;
|
|
18 |
|
|
19 |
import javax.naming.Context;
|
|
20 |
import javax.naming.InitialContext;
|
|
21 |
import javax.naming.NamingException;
|
|
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 _initialContext;
|
|
40 |
private String _initialFactory;
|
|
41 |
private String _URLPackages;
|
|
42 |
private String _providerURL;
|
|
43 |
|
|
44 | 4 |
public Object lookup(String name, Class expected)
|
45 |
{ |
|
46 | 4 |
int i = 0;
|
47 |
|
|
48 | 4 |
while (true) |
49 |
{ |
|
50 | 5 |
Context context = null;
|
51 | 5 |
Object raw = null;
|
52 |
|
|
53 | 5 |
try
|
54 |
{ |
|
55 | 5 |
context = getInitialContext(); |
56 |
|
|
57 | 5 |
raw = context.lookup(name); |
58 |
} |
|
59 |
catch (NamingException ex)
|
|
60 |
{ |
|
61 | 2 |
if (i++ == 0)
|
62 | 1 |
_coordinator.fireRemoteExceptionDidOccur(this, ex);
|
63 |
else
|
|
64 | 1 |
throw new ApplicationRuntimeException( |
65 |
ImplMessages.unableToLookup(name, context), |
|
66 |
ex); |
|
67 | 1 |
continue;
|
68 |
} |
|
69 |
|
|
70 | 3 |
if (raw == null) |
71 | 0 |
throw new ApplicationRuntimeException(ImplMessages.noObject(name, expected)); |
72 |
|
|
73 | 3 |
if (!expected.isAssignableFrom(raw.getClass()))
|
74 | 0 |
throw new ApplicationRuntimeException(ImplMessages.wrongType(name, raw, expected)); |
75 |
|
|
76 | 3 |
return raw;
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 | 5 |
private Context getInitialContext() throws NamingException |
81 |
{ |
|
82 | 5 |
if (_initialContext == null) |
83 |
{ |
|
84 |
|
|
85 | 3 |
Hashtable properties = new Hashtable();
|
86 |
|
|
87 | 3 |
if (!HiveMind.isBlank(_initialFactory))
|
88 | 3 |
properties.put(Context.INITIAL_CONTEXT_FACTORY, _initialFactory); |
89 |
|
|
90 | 3 |
if (!HiveMind.isBlank(_providerURL))
|
91 | 3 |
properties.put(Context.PROVIDER_URL, _providerURL); |
92 |
|
|
93 | 3 |
if (!HiveMind.isBlank(_URLPackages))
|
94 | 3 |
properties.put(Context.URL_PKG_PREFIXES, _URLPackages); |
95 |
|
|
96 | 3 |
_initialContext = constructContext(properties); |
97 |
} |
|
98 |
|
|
99 | 5 |
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 | 0 |
protected Context constructContext(Hashtable properties) throws NamingException |
107 |
{ |
|
108 | 0 |
return new InitialContext(properties); |
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Sets the InitialContext to null.
|
|
113 |
*/
|
|
114 | 0 |
public void remoteExceptionDidOccur(RemoteExceptionEvent event) |
115 |
{ |
|
116 | 0 |
_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 | 3 |
public void setInitialFactory(String string) |
124 |
{ |
|
125 | 3 |
_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 | 3 |
public void setProviderURL(String string) |
133 |
{ |
|
134 | 3 |
_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 | 3 |
public void setURLPackages(String string) |
144 |
{ |
|
145 | 3 |
_URLPackages = string; |
146 |
} |
|
147 |
|
|
148 | 3 |
public void setCoordinator(RemoteExceptionCoordinator coordinator) |
149 |
{ |
|
150 | 3 |
_coordinator = coordinator; |
151 |
} |
|
152 |
|
|
153 |
} |
|
154 |
|
|