1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.client.api;
36
37 import java.io.InputStream;
38 import java.util.Properties;
39
40 import javax.security.auth.login.LoginContext;
41
42 import org.w3.x2005.x08.addressing.EndpointReferenceType;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class ClientLocator<T>
68 {
69
70
71
72 private static final String IMPLEMENTATION_CONFIG_FILENAME = "/META-INF"
73 + ClientFactory.DEFAULT_CONFIGURATION_FILE;
74
75
76
77
78 private static final String APP_CONFIG_FILENAME = ClientFactory.DEFAULT_CONFIGURATION_FILE;
79
80
81
82
83 private final String configurationKey;
84
85 private final String implementationClassFromConfig;
86
87
88
89
90
91
92
93 protected ClientLocator( Class<T> clazz )
94 {
95 configurationKey = clazz.getName();
96 implementationClassFromConfig = readClassFromConfig();
97 }
98
99 private synchronized ClientFactory<T> newClientFactoryInstance()
100 {
101 return newClientFactoryInstance( null );
102 }
103
104 private synchronized ClientFactory<T> newClientFactoryInstance( String alternativeConfiguration )
105 {
106
107
108
109 String clazzName =
110 System.getProperties().getProperty( configurationKey, implementationClassFromConfig );
111
112 if ( alternativeConfiguration != null )
113 {
114 clazzName = readClassFromConfigFile( alternativeConfiguration, clazzName );
115 }
116
117 if ( ( clazzName == null ) || "".equals( clazzName ) )
118 {
119 throw new RuntimeException( "failed to lookup client factory implementation for interface"
120 + configurationKey );
121 }
122
123
124
125
126 try
127 {
128 @SuppressWarnings( "unchecked" )
129 Class<ClientFactory<T>> clazz =
130 (Class<ClientFactory<T>>) Class.forName( clazzName, true,
131 ClientLocator.class.getClassLoader() );
132
133 ClientFactory<T> instance = clazz.newInstance();
134 return instance;
135 }
136 catch ( Exception e )
137 {
138 throw new RuntimeException( "failed to instantiate client factory implementation for interface "
139 + configurationKey );
140 }
141 catch ( Error e )
142 {
143 System.err.println( "failed to instantiate client factory implementation for interface "
144 + configurationKey );
145 System.err.println( e.getMessage() );
146 throw e;
147 }
148 }
149
150 private String readClassFromConfig()
151 {
152
153
154
155 String clientImplConfig = readClassFromConfigFile( IMPLEMENTATION_CONFIG_FILENAME, null );
156
157
158
159
160 String appConfig = readClassFromConfigFile( APP_CONFIG_FILENAME, clientImplConfig );
161 return appConfig;
162 }
163
164
165
166
167
168
169
170 private String readClassFromConfigFile( String fileName, String defaultValue )
171 {
172 Properties properties = new Properties();
173 try
174 {
175 InputStream inputStream = ClientLocator.class.getResourceAsStream( fileName );
176 properties.load( inputStream );
177 }
178 catch ( Exception e )
179 {
180
181 }
182
183 String fromConfig = properties.getProperty( configurationKey, defaultValue );
184
185 return fromConfig;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199 public T newInstance( EndpointReferenceType epr ) throws Exception
200 {
201 return newClientFactoryInstance().newInstance( epr );
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 public T newInstance( EndpointReferenceType epr, LoginContext context ) throws Exception
219 {
220 return newClientFactoryInstance().newInstance( epr, context );
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 protected T newInstance( EndpointReferenceType epr, String altConfig ) throws Exception
238 {
239 return newClientFactoryInstance( altConfig ).newInstance( epr );
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 protected T newInstance( EndpointReferenceType epr, LoginContext context, String altConfig )
260 throws Exception
261 {
262 return newClientFactoryInstance( altConfig ).newInstance( epr, context );
263 }
264 }