| 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, 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 4795 2013-04-21 09:09:02Z 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.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" ) |
| 68 | // </editor-fold> |
| 69 | // SECTION-END |
| 70 | public class DefaultLocator implements Locator |
| 71 | { |
| 72 | // SECTION-START[DefaultLocator] |
| 73 | |
| 74 | /** Constant for the {@code 'jndi'} URI scheme. */ |
| 75 | private static final String JNDI_URI_SCHEME = "jndi"; |
| 76 | |
| 77 | /** Constant for the {@code 'jndi+rmi'} URI scheme. */ |
| 78 | private static final String JNDI_RMI_URI_SCHEME = "jndi+rmi"; |
| 79 | |
| 80 | /** URI schemes supported by this {@code Locator} implementation. */ |
| 81 | private static final String[] SUPPORTED_URI_SCHEMES = |
| 82 | { |
| 83 | JNDI_URI_SCHEME, JNDI_RMI_URI_SCHEME |
| 84 | }; |
| 85 | |
| 86 | /** The JNDI context of the instance. */ |
| 87 | private Context jndiContext; |
| 88 | |
| 89 | /** |
| 90 | * Gets a flag indicating support for a given location URI. |
| 91 | * |
| 92 | * @param location The location URI to test support for. |
| 93 | * |
| 94 | * @return {@code true}, if {@code location} is supported by this implementation; {@code false}, else. |
| 95 | * |
| 96 | * @throws NullPointerException if {@code location} is {@code null}. |
| 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 | * Gets the JNDI context of the instance. |
| 118 | * |
| 119 | * @return The JNDI context of the instance. |
| 120 | * |
| 121 | * @throws NamingException if getting the context fails. |
| 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 | * Gets the JNDI name for a given location. |
| 135 | * |
| 136 | * @param location The location to get a JNDI name for. |
| 137 | * |
| 138 | * @return The JNDI name for {@code location}. |
| 139 | * |
| 140 | * @throws NullPointerException if {@code location} is {@code null}. |
| 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 | // JDK: As of JDK 6, "new IOException( message, cause )". |
| 199 | throw (IOException) new IOException( getMessage( e ) ).initCause( e ); |
| 200 | } |
| 201 | catch ( final ClassCastException e ) |
| 202 | { |
| 203 | // JDK: As of JDK 6, "new IOException( message, cause )". |
| 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 |
| 214 | ? t.getMessage() != null && t.getMessage().trim().length() > 0 |
| 215 | ? t.getMessage() |
| 216 | : getMessage( t.getCause() ) |
| 217 | : null; |
| 218 | |
| 219 | } |
| 220 | |
| 221 | // SECTION-END |
| 222 | // SECTION-START[Constructors] |
| 223 | // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> |
| 224 | /** Creates a new {@code DefaultLocator} instance. */ |
| 225 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" ) |
| 226 | public DefaultLocator() |
| 227 | { |
| 228 | // SECTION-START[Default Constructor] |
| 229 | super(); |
| 230 | // SECTION-END |
| 231 | } |
| 232 | // </editor-fold> |
| 233 | // SECTION-END |
| 234 | // SECTION-START[Dependencies] |
| 235 | // SECTION-END |
| 236 | // SECTION-START[Properties] |
| 237 | // SECTION-END |
| 238 | // SECTION-START[Messages] |
| 239 | // <editor-fold defaultstate="collapsed" desc=" Generated Messages "> |
| 240 | /** |
| 241 | * Gets the text of the {@code <Illegal Object Message>} message. |
| 242 | * <p><dl> |
| 243 | * <dt><b>Languages:</b></dt> |
| 244 | * <dd>English (default)</dd> |
| 245 | * <dd>Deutsch</dd> |
| 246 | * <dt><b>Final:</b></dt><dd>No</dd> |
| 247 | * </dl></p> |
| 248 | * @param locale The locale of the message to return. |
| 249 | * @param objectInfo Format argument. |
| 250 | * @param classInfo Format argument. |
| 251 | * @return The text of the {@code <Illegal Object Message>} message for {@code locale}. |
| 252 | * @throws org.jomc.ObjectManagementException if getting the message instance fails. |
| 253 | */ |
| 254 | @SuppressWarnings("unused") |
| 255 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" ) |
| 256 | private static String getIllegalObjectMessage( final java.util.Locale locale, final java.lang.String objectInfo, final java.lang.String classInfo ) |
| 257 | { |
| 258 | java.io.BufferedReader reader = null; |
| 259 | boolean suppressExceptionOnClose = true; |
| 260 | |
| 261 | try |
| 262 | { |
| 263 | final String message = java.text.MessageFormat.format( java.util.ResourceBundle.getBundle( "org.jomc.ri.DefaultLocator", locale ).getString( "Illegal Object Message" ), objectInfo, classInfo, (Object) null ); |
| 264 | final java.lang.StringBuilder builder = new java.lang.StringBuilder( message.length() ); |
| 265 | reader = new java.io.BufferedReader( new java.io.StringReader( message ) ); |
| 266 | final String lineSeparator = System.getProperty( "line.separator", "\n" ); |
| 267 | |
| 268 | String line; |
| 269 | while ( ( line = reader.readLine() ) != null ) |
| 270 | { |
| 271 | builder.append( lineSeparator ).append( line ); |
| 272 | } |
| 273 | |
| 274 | suppressExceptionOnClose = false; |
| 275 | return builder.length() > 0 ? builder.substring( lineSeparator.length() ) : ""; |
| 276 | } |
| 277 | catch( final java.lang.ClassCastException e ) |
| 278 | { |
| 279 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 280 | } |
| 281 | catch( final java.lang.IllegalArgumentException e ) |
| 282 | { |
| 283 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 284 | } |
| 285 | catch( final java.util.MissingResourceException e ) |
| 286 | { |
| 287 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 288 | } |
| 289 | catch( final java.io.IOException e ) |
| 290 | { |
| 291 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 292 | } |
| 293 | finally |
| 294 | { |
| 295 | try |
| 296 | { |
| 297 | if( reader != null ) |
| 298 | { |
| 299 | reader.close(); |
| 300 | } |
| 301 | } |
| 302 | catch( final java.io.IOException e ) |
| 303 | { |
| 304 | if( !suppressExceptionOnClose ) |
| 305 | { |
| 306 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | /** |
| 312 | * Gets the text of the {@code <Unsupported URI Scheme Message>} message. |
| 313 | * <p><dl> |
| 314 | * <dt><b>Languages:</b></dt> |
| 315 | * <dd>English (default)</dd> |
| 316 | * <dd>Deutsch</dd> |
| 317 | * <dt><b>Final:</b></dt><dd>No</dd> |
| 318 | * </dl></p> |
| 319 | * @param locale The locale of the message to return. |
| 320 | * @param schemeInfo Format argument. |
| 321 | * @return The text of the {@code <Unsupported URI Scheme Message>} message for {@code locale}. |
| 322 | * @throws org.jomc.ObjectManagementException if getting the message instance fails. |
| 323 | */ |
| 324 | @SuppressWarnings("unused") |
| 325 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" ) |
| 326 | private static String getUnsupportedUriSchemeMessage( final java.util.Locale locale, final java.lang.String schemeInfo ) |
| 327 | { |
| 328 | java.io.BufferedReader reader = null; |
| 329 | boolean suppressExceptionOnClose = true; |
| 330 | |
| 331 | try |
| 332 | { |
| 333 | final String message = java.text.MessageFormat.format( java.util.ResourceBundle.getBundle( "org.jomc.ri.DefaultLocator", locale ).getString( "Unsupported URI Scheme Message" ), schemeInfo, (Object) null ); |
| 334 | final java.lang.StringBuilder builder = new java.lang.StringBuilder( message.length() ); |
| 335 | reader = new java.io.BufferedReader( new java.io.StringReader( message ) ); |
| 336 | final String lineSeparator = System.getProperty( "line.separator", "\n" ); |
| 337 | |
| 338 | String line; |
| 339 | while ( ( line = reader.readLine() ) != null ) |
| 340 | { |
| 341 | builder.append( lineSeparator ).append( line ); |
| 342 | } |
| 343 | |
| 344 | suppressExceptionOnClose = false; |
| 345 | return builder.length() > 0 ? builder.substring( lineSeparator.length() ) : ""; |
| 346 | } |
| 347 | catch( final java.lang.ClassCastException e ) |
| 348 | { |
| 349 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 350 | } |
| 351 | catch( final java.lang.IllegalArgumentException e ) |
| 352 | { |
| 353 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 354 | } |
| 355 | catch( final java.util.MissingResourceException e ) |
| 356 | { |
| 357 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 358 | } |
| 359 | catch( final java.io.IOException e ) |
| 360 | { |
| 361 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 362 | } |
| 363 | finally |
| 364 | { |
| 365 | try |
| 366 | { |
| 367 | if( reader != null ) |
| 368 | { |
| 369 | reader.close(); |
| 370 | } |
| 371 | } |
| 372 | catch( final java.io.IOException e ) |
| 373 | { |
| 374 | if( !suppressExceptionOnClose ) |
| 375 | { |
| 376 | throw new org.jomc.ObjectManagementException( e.getMessage(), e ); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | // </editor-fold> |
| 382 | // SECTION-END |
| 383 | } |