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
36 package org.jomc.ri;
37
38 import java.io.IOException;
39 import java.net.URI;
40 import java.util.Locale;
41 import javax.naming.Context;
42 import javax.naming.InitialContext;
43 import javax.naming.NamingException;
44 import javax.rmi.PortableRemoteObject;
45 import org.jomc.spi.Locator;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
68
69
70 public class DefaultLocator implements Locator
71 {
72
73
74
75
76
77 private static final String JNDI_URI_SCHEME = "jndi";
78
79
80
81
82 private static final String JNDI_RMI_URI_SCHEME = "jndi+rmi";
83
84
85
86
87 private static final String[] SUPPORTED_URI_SCHEMES =
88 {
89 JNDI_URI_SCHEME, JNDI_RMI_URI_SCHEME
90 };
91
92
93
94
95 private Context jndiContext;
96
97
98
99
100
101
102
103
104
105
106 public boolean isLocationSupported( final URI location )
107 {
108 if ( location == null )
109 {
110 throw new NullPointerException( "location" );
111 }
112
113 for ( int i = SUPPORTED_URI_SCHEMES.length - 1; i >= 0; i-- )
114 {
115 if ( SUPPORTED_URI_SCHEMES[i].equals( location.getScheme() ) )
116 {
117 return true;
118 }
119 }
120
121 return false;
122 }
123
124
125
126
127
128
129
130
131 public Context getJndiContext() throws NamingException
132 {
133 if ( this.jndiContext == null )
134 {
135 this.jndiContext = new InitialContext();
136 }
137
138 return this.jndiContext;
139 }
140
141
142
143
144
145
146
147
148
149
150 public String getJndiName( final URI location )
151 {
152 if ( location == null )
153 {
154 throw new NullPointerException( "location" );
155 }
156
157 String name = location.getSchemeSpecificPart();
158 if ( name == null || name.replace( '/', ' ' ).trim().length() == 0 )
159 {
160 name = "";
161 }
162 if ( location.getFragment() != null )
163 {
164 name += '#' + location.getFragment();
165 }
166
167 return name;
168 }
169
170 public <T> T getObject( final Class<T> specification, final URI location ) throws IOException
171 {
172 if ( specification == null )
173 {
174 throw new NullPointerException( "specification" );
175 }
176 if ( location == null )
177 {
178 throw new NullPointerException( "location" );
179 }
180
181 T object = null;
182
183 try
184 {
185 final String scheme = location.getScheme();
186 if ( !this.isLocationSupported( location ) )
187 {
188 throw new IOException( getUnsupportedUriSchemeMessage( Locale.getDefault(), location.getScheme() ) );
189 }
190
191 final Object jndiObject = this.getJndiContext().lookup( this.getJndiName( location ) );
192
193 if ( JNDI_URI_SCHEME.equals( scheme ) )
194 {
195 object = (T) jndiObject;
196 }
197 else if ( JNDI_RMI_URI_SCHEME.equals( scheme ) )
198 {
199 object = (T) PortableRemoteObject.narrow( jndiObject, specification );
200 }
201
202 return object;
203 }
204 catch ( final NamingException e )
205 {
206
207 throw (IOException) new IOException( getMessage( e ) ).initCause( e );
208 }
209 catch ( final ClassCastException e )
210 {
211
212 throw (IOException) new IOException( getIllegalObjectMessage(
213 Locale.getDefault(), object != null ? object.toString() : null,
214 specification.getName() ) ).initCause( e );
215
216 }
217 }
218
219 private static String getMessage( final Throwable t )
220 {
221 return t != null
222 ? t.getMessage() != null && t.getMessage().trim().length() > 0
223 ? t.getMessage()
224 : getMessage( t.getCause() )
225 : null;
226
227 }
228
229
230
231
232
233 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
234 public DefaultLocator()
235 {
236
237 super();
238
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
263 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
264 private static String getIllegalObjectMessage( final java.util.Locale locale, final java.lang.String objectInfo, final java.lang.String classInfo )
265 {
266 java.io.BufferedReader reader = null;
267 boolean suppressExceptionOnClose = true;
268
269 try
270 {
271 final String message = java.text.MessageFormat.format( java.util.ResourceBundle.getBundle( "org.jomc.ri.DefaultLocator", locale ).getString( "Illegal Object Message" ), objectInfo, classInfo, (Object) null );
272 final java.lang.StringBuilder builder = new java.lang.StringBuilder( message.length() );
273 reader = new java.io.BufferedReader( new java.io.StringReader( message ) );
274 final String lineSeparator = System.getProperty( "line.separator", "\n" );
275
276 String line;
277 while ( ( line = reader.readLine() ) != null )
278 {
279 builder.append( lineSeparator ).append( line );
280 }
281
282 suppressExceptionOnClose = false;
283 return builder.length() > 0 ? builder.substring( lineSeparator.length() ) : "";
284 }
285 catch( final java.lang.ClassCastException e )
286 {
287 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
288 }
289 catch( final java.lang.IllegalArgumentException e )
290 {
291 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
292 }
293 catch( final java.util.MissingResourceException e )
294 {
295 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
296 }
297 catch( final java.io.IOException e )
298 {
299 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
300 }
301 finally
302 {
303 try
304 {
305 if( reader != null )
306 {
307 reader.close();
308 }
309 }
310 catch( final java.io.IOException e )
311 {
312 if( !suppressExceptionOnClose )
313 {
314 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
315 }
316 }
317 }
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
333 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
334 private static String getUnsupportedUriSchemeMessage( final java.util.Locale locale, final java.lang.String schemeInfo )
335 {
336 java.io.BufferedReader reader = null;
337 boolean suppressExceptionOnClose = true;
338
339 try
340 {
341 final String message = java.text.MessageFormat.format( java.util.ResourceBundle.getBundle( "org.jomc.ri.DefaultLocator", locale ).getString( "Unsupported URI Scheme Message" ), schemeInfo, (Object) null );
342 final java.lang.StringBuilder builder = new java.lang.StringBuilder( message.length() );
343 reader = new java.io.BufferedReader( new java.io.StringReader( message ) );
344 final String lineSeparator = System.getProperty( "line.separator", "\n" );
345
346 String line;
347 while ( ( line = reader.readLine() ) != null )
348 {
349 builder.append( lineSeparator ).append( line );
350 }
351
352 suppressExceptionOnClose = false;
353 return builder.length() > 0 ? builder.substring( lineSeparator.length() ) : "";
354 }
355 catch( final java.lang.ClassCastException e )
356 {
357 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
358 }
359 catch( final java.lang.IllegalArgumentException e )
360 {
361 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
362 }
363 catch( final java.util.MissingResourceException e )
364 {
365 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
366 }
367 catch( final java.io.IOException e )
368 {
369 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
370 }
371 finally
372 {
373 try
374 {
375 if( reader != null )
376 {
377 reader.close();
378 }
379 }
380 catch( final java.io.IOException e )
381 {
382 if( !suppressExceptionOnClose )
383 {
384 throw new org.jomc.ObjectManagementException( e.getMessage(), e );
385 }
386 }
387 }
388 }
389
390
391
392 }