|
|||||||||||||||||||
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 | 58.3% | 86.7% | 75% | 78% |
|
1 |
// Copyright 2004 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 InitialContext _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 |
Hashtable properties = new Hashtable();
|
83 |
|
|
84 | 5 |
if (!HiveMind.isBlank(_initialFactory))
|
85 | 5 |
properties.put(Context.INITIAL_CONTEXT_FACTORY, _initialFactory); |
86 |
|
|
87 | 5 |
if (!HiveMind.isBlank(_providerURL))
|
88 | 5 |
properties.put(Context.PROVIDER_URL, _providerURL); |
89 |
|
|
90 | 5 |
if (!HiveMind.isBlank(_URLPackages))
|
91 | 5 |
properties.put(Context.URL_PKG_PREFIXES, _URLPackages); |
92 |
|
|
93 | 5 |
return constructContext(properties);
|
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Constructs the InitialContext (this is separated out in a standalone
|
|
98 |
* method so that it may be overridden in a testing subclass).
|
|
99 |
*/
|
|
100 | 0 |
protected Context constructContext(Hashtable properties) throws NamingException |
101 |
{ |
|
102 | 0 |
return new InitialContext(properties); |
103 |
} |
|
104 |
|
|
105 |
/**
|
|
106 |
* Sets the InitialContext to null.
|
|
107 |
*/
|
|
108 | 0 |
public void remoteExceptionDidOccur(RemoteExceptionEvent event) |
109 |
{ |
|
110 | 0 |
_initialContext = null;
|
111 |
} |
|
112 |
|
|
113 |
/**
|
|
114 |
* Sets the initial factory used to create the initial JNDI context.
|
|
115 |
* Equivalent to the system property <code>java.naming.factory.initial</code>.
|
|
116 |
*/
|
|
117 | 3 |
public void setInitialFactory(String string) |
118 |
{ |
|
119 | 3 |
_initialFactory = string; |
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* Sets the JNDI provider URL, used to create the initial JNDI context.
|
|
124 |
* Equivalent to the system property <code>java.naming.provider.url</code>.
|
|
125 |
*/
|
|
126 | 3 |
public void setProviderURL(String string) |
127 |
{ |
|
128 | 3 |
_providerURL = string; |
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Sets the URL packages, used to create the initial JNDI context.
|
|
133 |
* Equivalent to the system property
|
|
134 |
* <code>java.naming.factory.url.pkgs</code>
|
|
135 |
*/
|
|
136 |
|
|
137 | 3 |
public void setURLPackages(String string) |
138 |
{ |
|
139 | 3 |
_URLPackages = string; |
140 |
} |
|
141 |
|
|
142 | 3 |
public void setCoordinator(RemoteExceptionCoordinator coordinator) |
143 |
{ |
|
144 | 3 |
_coordinator = coordinator; |
145 |
} |
|
146 |
|
|
147 |
} |
|
148 |
|
|