View Javadoc
1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    * Java Object Management and Configuration
5    * Copyright (C) Christian Schulte <cs@schulte.it>, 2005-206
6    * All rights reserved.
7    *
8    * Redistribution and use in source and binary forms, with or without
9    * modification, are permitted provided that the following conditions
10   * are met:
11   *
12   *   o Redistributions of source code must retain the above copyright
13   *     notice, this list of conditions and the following disclaimer.
14   *
15   *   o Redistributions in binary form must reproduce the above copyright
16   *     notice, this list of conditions and the following disclaimer in
17   *     the documentation and/or other materials provided with the
18   *     distribution.
19   *
20   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * $JOMC: DefaultLocator.java 5061 2015-05-31 13:20:40Z schulte $
32   *
33   */
34  // </editor-fold>
35  // SECTION-END
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  // SECTION-START[Documentation]
48  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
49  /**
50   * Default {@code Locator} implementation.
51   *
52   * <dl>
53   *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.DefaultLocator</dd>
54   *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ DefaultLocator</dd>
55   *   <dt><b>Abstract:</b></dt><dd>No</dd>
56   *   <dt><b>Final:</b></dt><dd>No</dd>
57   *   <dt><b>Stateless:</b></dt><dd>No</dd>
58   * </dl>
59   *
60   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
61   * @version 1.0
62   */
63  // </editor-fold>
64  // SECTION-END
65  // SECTION-START[Annotations]
66  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
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  // </editor-fold>
69  // SECTION-END
70  public class DefaultLocator implements Locator
71  {
72      // SECTION-START[DefaultLocator]
73  
74      /**
75       * Constant for the {@code 'jndi'} URI scheme.
76       */
77      private static final String JNDI_URI_SCHEME = "jndi";
78  
79      /**
80       * Constant for the {@code 'jndi+rmi'} URI scheme.
81       */
82      private static final String JNDI_RMI_URI_SCHEME = "jndi+rmi";
83  
84      /**
85       * URI schemes supported by this {@code Locator} implementation.
86       */
87      private static final String[] SUPPORTED_URI_SCHEMES =
88      {
89          JNDI_URI_SCHEME, JNDI_RMI_URI_SCHEME
90      };
91  
92      /**
93       * The JNDI context of the instance.
94       */
95      private Context jndiContext;
96  
97      /**
98       * Gets a flag indicating support for a given location URI.
99       *
100      * @param location The location URI to test support for.
101      *
102      * @return {@code true}, if {@code location} is supported by this implementation; {@code false}, else.
103      *
104      * @throws NullPointerException if {@code location} is {@code null}.
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      * Gets the JNDI context of the instance.
126      *
127      * @return The JNDI context of the instance.
128      *
129      * @throws NamingException if getting the context fails.
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      * Gets the JNDI name for a given location.
143      *
144      * @param location The location to get a JNDI name for.
145      *
146      * @return The JNDI name for {@code location}.
147      *
148      * @throws NullPointerException if {@code location} is {@code null}.
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             // JDK: As of JDK 6, "new IOException( message, cause )".
207             throw (IOException) new IOException( getMessage( e ) ).initCause( e );
208         }
209         catch ( final ClassCastException e )
210         {
211             // JDK: As of JDK 6, "new IOException( message, cause )".
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     // SECTION-END
230     // SECTION-START[Constructors]
231     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
232     /** Creates a new {@code DefaultLocator} instance. */
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         // SECTION-START[Default Constructor]
237         super();
238         // SECTION-END
239     }
240     // </editor-fold>
241     // SECTION-END
242     // SECTION-START[Dependencies]
243     // SECTION-END
244     // SECTION-START[Properties]
245     // SECTION-END
246     // SECTION-START[Messages]
247     // <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
248     /**
249      * Gets the text of the {@code <Illegal Object Message>} message.
250      * <p><dl>
251      *   <dt><b>Languages:</b></dt>
252      *     <dd>English (default)</dd>
253      *     <dd>Deutsch</dd>
254      *   <dt><b>Final:</b></dt><dd>No</dd>
255      * </dl></p>
256      * @param locale The locale of the message to return.
257      * @param objectInfo Format argument.
258      * @param classInfo Format argument.
259      * @return The text of the {@code <Illegal Object Message>} message for {@code locale}.
260      * @throws org.jomc.ObjectManagementException if getting the message instance fails.
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      * Gets the text of the {@code <Unsupported URI Scheme Message>} message.
321      * <p><dl>
322      *   <dt><b>Languages:</b></dt>
323      *     <dd>English (default)</dd>
324      *     <dd>Deutsch</dd>
325      *   <dt><b>Final:</b></dt><dd>No</dd>
326      * </dl></p>
327      * @param locale The locale of the message to return.
328      * @param schemeInfo Format argument.
329      * @return The text of the {@code <Unsupported URI Scheme Message>} message for {@code locale}.
330      * @throws org.jomc.ObjectManagementException if getting the message instance fails.
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     // </editor-fold>
390     // SECTION-END
391 
392 }