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