1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 |
| |
32 |
| |
33 |
| |
34 |
| |
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 |
| |
104 |
| |
105 |
| |
106 |
0
| protected Context constructContext(Hashtable properties) throws NamingException
|
107 |
| { |
108 |
0
| return new InitialContext(properties);
|
109 |
| } |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
0
| public void remoteExceptionDidOccur(RemoteExceptionEvent event)
|
115 |
| { |
116 |
0
| _initialContext = null;
|
117 |
| } |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
3
| public void setInitialFactory(String string)
|
124 |
| { |
125 |
3
| _initialFactory = string;
|
126 |
| } |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
3
| public void setProviderURL(String string)
|
133 |
| { |
134 |
3
| _providerURL = string;
|
135 |
| } |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
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 |
| } |