| 1 | /* |
| 2 | * Copyright (C) Christian Schulte, 2005-206 |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * o Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * o Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 19 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * $JOMC: DefaultModletProvider.java 4841 2014-01-06 01:24:25Z schulte $ |
| 29 | * |
| 30 | */ |
| 31 | package org.jomc.modlet; |
| 32 | |
| 33 | import java.net.URL; |
| 34 | import java.text.MessageFormat; |
| 35 | import java.util.Enumeration; |
| 36 | import java.util.ResourceBundle; |
| 37 | import java.util.logging.Level; |
| 38 | import javax.xml.bind.JAXBContext; |
| 39 | import javax.xml.bind.JAXBElement; |
| 40 | import javax.xml.bind.JAXBException; |
| 41 | import javax.xml.bind.UnmarshalException; |
| 42 | import javax.xml.bind.Unmarshaller; |
| 43 | |
| 44 | /** |
| 45 | * Default {@code ModletProvider} implementation. |
| 46 | * |
| 47 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
| 48 | * @version $JOMC: DefaultModletProvider.java 4841 2014-01-06 01:24:25Z schulte $ |
| 49 | * @see ModelContext#findModlets(org.jomc.modlet.Modlets) |
| 50 | */ |
| 51 | public class DefaultModletProvider implements ModletProvider |
| 52 | { |
| 53 | |
| 54 | /** |
| 55 | * Constant for the name of the model context attribute backing property {@code enabled}. |
| 56 | * @see #findModlets(org.jomc.modlet.ModelContext, org.jomc.modlet.Modlets) |
| 57 | * @see ModelContext#getAttribute(java.lang.String) |
| 58 | * @since 1.2 |
| 59 | */ |
| 60 | public static final String ENABLED_ATTRIBUTE_NAME = "org.jomc.modlet.DefaultModletProvider.enabledAttribute"; |
| 61 | |
| 62 | /** |
| 63 | * Constant for the name of the system property controlling property {@code defaultEnabled}. |
| 64 | * @see #isDefaultEnabled() |
| 65 | * @since 1.2 |
| 66 | */ |
| 67 | private static final String DEFAULT_ENABLED_PROPERTY_NAME = |
| 68 | "org.jomc.modlet.DefaultModletProvider.defaultEnabled"; |
| 69 | |
| 70 | /** |
| 71 | * Default value of the flag indicating the provider is enabled by default. |
| 72 | * @see #isDefaultEnabled() |
| 73 | * @since 1.2 |
| 74 | */ |
| 75 | private static final Boolean DEFAULT_ENABLED = Boolean.TRUE; |
| 76 | |
| 77 | /** Flag indicating the provider is enabled by default. */ |
| 78 | private static volatile Boolean defaultEnabled; |
| 79 | |
| 80 | /** Flag indicating the provider is enabled. */ |
| 81 | private Boolean enabled; |
| 82 | |
| 83 | /** |
| 84 | * Constant for the name of the model context attribute backing property {@code modletLocation}. |
| 85 | * @see #findModlets(org.jomc.modlet.ModelContext, org.jomc.modlet.Modlets) |
| 86 | * @see ModelContext#getAttribute(java.lang.String) |
| 87 | * @since 1.2 |
| 88 | */ |
| 89 | public static final String MODLET_LOCATION_ATTRIBUTE_NAME = |
| 90 | "org.jomc.modlet.DefaultModletProvider.modletLocationAttribute"; |
| 91 | |
| 92 | /** |
| 93 | * Constant for the name of the system property controlling property {@code defaultModletLocation}. |
| 94 | * @see #getDefaultModletLocation() |
| 95 | * @since 1.2 |
| 96 | */ |
| 97 | private static final String DEFAULT_MODLET_LOCATION_PROPERTY_NAME = |
| 98 | "org.jomc.modlet.DefaultModletProvider.defaultModletLocation"; |
| 99 | |
| 100 | /** |
| 101 | * Class path location searched for {@code Modlets} by default. |
| 102 | * @see #getDefaultModletLocation() |
| 103 | */ |
| 104 | private static final String DEFAULT_MODLET_LOCATION = "META-INF/jomc-modlet.xml"; |
| 105 | |
| 106 | /** Default {@code Modlet} location. */ |
| 107 | private static volatile String defaultModletLocation; |
| 108 | |
| 109 | /** Modlet location of the instance. */ |
| 110 | private String modletLocation; |
| 111 | |
| 112 | /** |
| 113 | * Constant for the name of the model context attribute backing property {@code validating}. |
| 114 | * @see #findModlets(org.jomc.modlet.ModelContext, java.lang.String) |
| 115 | * @see ModelContext#getAttribute(java.lang.String) |
| 116 | * @since 1.2 |
| 117 | */ |
| 118 | public static final String VALIDATING_ATTRIBUTE_NAME = |
| 119 | "org.jomc.modlet.DefaultModletProvider.validatingAttribute"; |
| 120 | |
| 121 | /** |
| 122 | * Constant for the name of the system property controlling property {@code defaultValidating}. |
| 123 | * @see #isDefaultValidating() |
| 124 | * @since 1.2 |
| 125 | */ |
| 126 | private static final String DEFAULT_VALIDATING_PROPERTY_NAME = |
| 127 | "org.jomc.modlet.DefaultModletProvider.defaultValidating"; |
| 128 | |
| 129 | /** |
| 130 | * Default value of the flag indicating the provider is validating resources by default. |
| 131 | * @see #isDefaultValidating() |
| 132 | * @since 1.2 |
| 133 | */ |
| 134 | private static final Boolean DEFAULT_VALIDATING = Boolean.TRUE; |
| 135 | |
| 136 | /** |
| 137 | * Flag indicating the provider is validating resources by default. |
| 138 | * @since 1.2 |
| 139 | */ |
| 140 | private static volatile Boolean defaultValidating; |
| 141 | |
| 142 | /** |
| 143 | * Flag indicating the provider is validating resources. |
| 144 | * @since 1.2 |
| 145 | */ |
| 146 | private Boolean validating; |
| 147 | |
| 148 | /** |
| 149 | * Constant for the name of the system property controlling property {@code defaultOrdinal}. |
| 150 | * @see #getDefaultOrdinal() |
| 151 | * @since 1.6 |
| 152 | */ |
| 153 | private static final String DEFAULT_ORDINAL_PROPERTY_NAME = |
| 154 | "org.jomc.modlet.DefaultModletProvider.defaultOrdinal"; |
| 155 | |
| 156 | /** |
| 157 | * Default value of the ordinal number of the provider. |
| 158 | * @see #getDefaultOrdinal() |
| 159 | * @since 1.6 |
| 160 | */ |
| 161 | private static final Integer DEFAULT_ORDINAL = 0; |
| 162 | |
| 163 | /** |
| 164 | * Default ordinal number of the provider. |
| 165 | * @since 1.6 |
| 166 | */ |
| 167 | private static volatile Integer defaultOrdinal; |
| 168 | |
| 169 | /** |
| 170 | * Ordinal number of the provider. |
| 171 | * @since 1.6 |
| 172 | */ |
| 173 | private Integer ordinal; |
| 174 | |
| 175 | /** Creates a new {@code DefaultModletProvider} instance. */ |
| 176 | public DefaultModletProvider() |
| 177 | { |
| 178 | super(); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Gets a flag indicating the provider is enabled by default. |
| 183 | * <p>The default enabled flag is controlled by system property |
| 184 | * {@code org.jomc.modlet.DefaultModletProvider.defaultEnabled} holding a value indicating the provider is |
| 185 | * enabled by default. If that property is not set, the {@code true} default is returned.</p> |
| 186 | * |
| 187 | * @return {@code true}, if the provider is enabled by default; {@code false}, if the provider is disabled by |
| 188 | * default. |
| 189 | * |
| 190 | * @see #isEnabled() |
| 191 | * @see #setDefaultEnabled(java.lang.Boolean) |
| 192 | */ |
| 193 | public static boolean isDefaultEnabled() |
| 194 | { |
| 195 | if ( defaultEnabled == null ) |
| 196 | { |
| 197 | defaultEnabled = Boolean.valueOf( System.getProperty( |
| 198 | DEFAULT_ENABLED_PROPERTY_NAME, Boolean.toString( DEFAULT_ENABLED ) ) ); |
| 199 | |
| 200 | } |
| 201 | |
| 202 | return defaultEnabled; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sets the flag indicating the provider is enabled by default. |
| 207 | * |
| 208 | * @param value The new value of the flag indicating the provider is enabled by default or {@code null}. |
| 209 | * |
| 210 | * @see #isDefaultEnabled() |
| 211 | */ |
| 212 | public static void setDefaultEnabled( final Boolean value ) |
| 213 | { |
| 214 | defaultEnabled = value; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets a flag indicating the provider is enabled. |
| 219 | * |
| 220 | * @return {@code true}, if the provider is enabled; {@code false}, if the provider is disabled. |
| 221 | * |
| 222 | * @see #isDefaultEnabled() |
| 223 | * @see #setEnabled(java.lang.Boolean) |
| 224 | */ |
| 225 | public final boolean isEnabled() |
| 226 | { |
| 227 | if ( this.enabled == null ) |
| 228 | { |
| 229 | this.enabled = isDefaultEnabled(); |
| 230 | } |
| 231 | |
| 232 | return this.enabled; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Sets the flag indicating the provider is enabled. |
| 237 | * |
| 238 | * @param value The new value of the flag indicating the provider is enabled or {@code null}. |
| 239 | * |
| 240 | * @see #isEnabled() |
| 241 | */ |
| 242 | public final void setEnabled( final Boolean value ) |
| 243 | { |
| 244 | this.enabled = value; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Gets the default location searched for {@code Modlet} resources. |
| 249 | * <p>The default {@code Modlet} location is controlled by system property |
| 250 | * {@code org.jomc.modlet.DefaultModletProvider.defaultModletLocation} holding the location to search for |
| 251 | * {@code Modlet} resources by default. If that property is not set, the {@code META-INF/jomc-modlet.xml} default is |
| 252 | * returned.</p> |
| 253 | * |
| 254 | * @return The location searched for {@code Modlet} resources by default. |
| 255 | * |
| 256 | * @see #setDefaultModletLocation(java.lang.String) |
| 257 | */ |
| 258 | public static String getDefaultModletLocation() |
| 259 | { |
| 260 | if ( defaultModletLocation == null ) |
| 261 | { |
| 262 | defaultModletLocation = System.getProperty( |
| 263 | DEFAULT_MODLET_LOCATION_PROPERTY_NAME, DEFAULT_MODLET_LOCATION ); |
| 264 | |
| 265 | } |
| 266 | |
| 267 | return defaultModletLocation; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Sets the default location searched for {@code Modlet} resources. |
| 272 | * |
| 273 | * @param value The new default location to search for {@code Modlet} resources or {@code null}. |
| 274 | * |
| 275 | * @see #getDefaultModletLocation() |
| 276 | */ |
| 277 | public static void setDefaultModletLocation( final String value ) |
| 278 | { |
| 279 | defaultModletLocation = value; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Gets the location searched for {@code Modlet} resources. |
| 284 | * |
| 285 | * @return The location searched for {@code Modlet} resources. |
| 286 | * |
| 287 | * @see #getDefaultModletLocation() |
| 288 | * @see #setModletLocation(java.lang.String) |
| 289 | */ |
| 290 | public final String getModletLocation() |
| 291 | { |
| 292 | if ( this.modletLocation == null ) |
| 293 | { |
| 294 | this.modletLocation = getDefaultModletLocation(); |
| 295 | } |
| 296 | |
| 297 | return this.modletLocation; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Sets the location searched for {@code Modlet} resources. |
| 302 | * |
| 303 | * @param value The new location to search for {@code Modlet} resources or {@code null}. |
| 304 | * |
| 305 | * @see #getModletLocation() |
| 306 | */ |
| 307 | public final void setModletLocation( final String value ) |
| 308 | { |
| 309 | this.modletLocation = value; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Gets a flag indicating the provider is validating resources by default. |
| 314 | * <p>The default validating flag is controlled by system property |
| 315 | * {@code org.jomc.modlet.DefaultModletProvider.defaultValidating} holding a value indicating the provider is |
| 316 | * validating resources by default. If that property is not set, the {@code true} default is returned.</p> |
| 317 | * |
| 318 | * @return {@code true}, if the provider is validating resources by default; {@code false}, if the provider is not |
| 319 | * validating resources by default. |
| 320 | * |
| 321 | * @see #isValidating() |
| 322 | * @see #setDefaultValidating(java.lang.Boolean) |
| 323 | * |
| 324 | * @since 1.2 |
| 325 | */ |
| 326 | public static boolean isDefaultValidating() |
| 327 | { |
| 328 | if ( defaultValidating == null ) |
| 329 | { |
| 330 | defaultValidating = Boolean.valueOf( System.getProperty( |
| 331 | DEFAULT_VALIDATING_PROPERTY_NAME, Boolean.toString( DEFAULT_VALIDATING ) ) ); |
| 332 | |
| 333 | } |
| 334 | |
| 335 | return defaultValidating; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Sets the flag indicating the provider is validating resources by default. |
| 340 | * |
| 341 | * @param value The new value of the flag indicating the provider is validating resources by default or |
| 342 | * {@code null}. |
| 343 | * |
| 344 | * @see #isDefaultValidating() |
| 345 | * |
| 346 | * @since 1.2 |
| 347 | */ |
| 348 | public static void setDefaultValidating( final Boolean value ) |
| 349 | { |
| 350 | defaultValidating = value; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Gets a flag indicating the provider is validating resources. |
| 355 | * |
| 356 | * @return {@code true}, if the provider is validating resources; {@code false}, if the provider is not validating |
| 357 | * resources. |
| 358 | * |
| 359 | * @see #isDefaultValidating() |
| 360 | * @see #setValidating(java.lang.Boolean) |
| 361 | * |
| 362 | * @since 1.2 |
| 363 | */ |
| 364 | public final boolean isValidating() |
| 365 | { |
| 366 | if ( this.validating == null ) |
| 367 | { |
| 368 | this.validating = isDefaultValidating(); |
| 369 | } |
| 370 | |
| 371 | return this.validating; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Sets the flag indicating the provider is validating resources. |
| 376 | * |
| 377 | * @param value The new value of the flag indicating the provider is validating resources or {@code null}. |
| 378 | * |
| 379 | * @see #isValidating() |
| 380 | * |
| 381 | * @since 1.2 |
| 382 | */ |
| 383 | public final void setValidating( final Boolean value ) |
| 384 | { |
| 385 | this.validating = value; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Gets the default ordinal number of the provider. |
| 390 | * <p>The default ordinal number is controlled by system property |
| 391 | * {@code org.jomc.modlet.DefaultModletProvider.defaultOrdinal} holding the default ordinal number of the provider. |
| 392 | * If that property is not set, the {@code 0} default is returned.</p> |
| 393 | * |
| 394 | * @return The default ordinal number of the provider. |
| 395 | * |
| 396 | * @see #setDefaultOrdinal(java.lang.Integer) |
| 397 | * |
| 398 | * @since 1.6 |
| 399 | */ |
| 400 | public static int getDefaultOrdinal() |
| 401 | { |
| 402 | if ( defaultOrdinal == null ) |
| 403 | { |
| 404 | defaultOrdinal = Integer.getInteger( DEFAULT_ORDINAL_PROPERTY_NAME, DEFAULT_ORDINAL ); |
| 405 | } |
| 406 | |
| 407 | return defaultOrdinal; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Sets the default ordinal number of the provider. |
| 412 | * |
| 413 | * @param value The new default ordinal number of the provider or {@code null}. |
| 414 | * |
| 415 | * @see #getDefaultOrdinal() |
| 416 | * |
| 417 | * @since 1.6 |
| 418 | */ |
| 419 | public static void setDefaultOrdinal( final Integer value ) |
| 420 | { |
| 421 | defaultOrdinal = value; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Gets the ordinal number of the provider. |
| 426 | * |
| 427 | * @return The ordinal number of the provider. |
| 428 | * |
| 429 | * @see #getDefaultOrdinal() |
| 430 | * @see #setOrdinal(java.lang.Integer) |
| 431 | * |
| 432 | * @since 1.6 |
| 433 | */ |
| 434 | public final int getOrdinal() |
| 435 | { |
| 436 | if ( this.ordinal == null ) |
| 437 | { |
| 438 | this.ordinal = getDefaultOrdinal(); |
| 439 | } |
| 440 | |
| 441 | return this.ordinal; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Sets the ordinal number of the provider. |
| 446 | * |
| 447 | * @param value The new ordinal number of the provider or {@code null}. |
| 448 | * |
| 449 | * @see #getOrdinal() |
| 450 | * |
| 451 | * @since 1.6 |
| 452 | */ |
| 453 | public final void setOrdinal( final Integer value ) |
| 454 | { |
| 455 | this.ordinal = value; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Searches a given context for {@code Modlets}. |
| 460 | * |
| 461 | * @param context The context to search for {@code Modlets}. |
| 462 | * @param location The location to search at. |
| 463 | * |
| 464 | * @return The {@code Modlets} found at {@code location} in {@code context} or {@code null}, if no {@code Modlets} |
| 465 | * are found. |
| 466 | * |
| 467 | * @throws NullPointerException if {@code context} or {@code location} is {@code null}. |
| 468 | * @throws ModelException if searching the context fails. |
| 469 | * |
| 470 | * @see #isValidating() |
| 471 | * @see #VALIDATING_ATTRIBUTE_NAME |
| 472 | */ |
| 473 | public Modlets findModlets( final ModelContext context, final String location ) throws ModelException |
| 474 | { |
| 475 | if ( context == null ) |
| 476 | { |
| 477 | throw new NullPointerException( "context" ); |
| 478 | } |
| 479 | if ( location == null ) |
| 480 | { |
| 481 | throw new NullPointerException( "location" ); |
| 482 | } |
| 483 | |
| 484 | URL url = null; |
| 485 | |
| 486 | try |
| 487 | { |
| 488 | boolean contextValidating = this.isValidating(); |
| 489 | if ( DEFAULT_VALIDATING == contextValidating |
| 490 | && context.getAttribute( VALIDATING_ATTRIBUTE_NAME ) instanceof Boolean ) |
| 491 | { |
| 492 | contextValidating = (Boolean) context.getAttribute( VALIDATING_ATTRIBUTE_NAME ); |
| 493 | } |
| 494 | |
| 495 | Modlets modlets = null; |
| 496 | final long t0 = System.currentTimeMillis(); |
| 497 | final JAXBContext ctx = context.createContext( ModletObject.MODEL_PUBLIC_ID ); |
| 498 | final Unmarshaller u = ctx.createUnmarshaller(); |
| 499 | final Enumeration<URL> e = context.findResources( location ); |
| 500 | |
| 501 | if ( contextValidating ) |
| 502 | { |
| 503 | u.setSchema( context.createSchema( ModletObject.MODEL_PUBLIC_ID ) ); |
| 504 | } |
| 505 | |
| 506 | while ( e.hasMoreElements() ) |
| 507 | { |
| 508 | url = e.nextElement(); |
| 509 | Object content = u.unmarshal( url ); |
| 510 | if ( content instanceof JAXBElement<?> ) |
| 511 | { |
| 512 | content = ( (JAXBElement<?>) content ).getValue(); |
| 513 | } |
| 514 | |
| 515 | if ( content instanceof Modlet ) |
| 516 | { |
| 517 | if ( modlets == null ) |
| 518 | { |
| 519 | modlets = new Modlets(); |
| 520 | } |
| 521 | |
| 522 | modlets.getModlet().add( (Modlet) content ); |
| 523 | } |
| 524 | else if ( content instanceof Modlets ) |
| 525 | { |
| 526 | if ( modlets == null ) |
| 527 | { |
| 528 | modlets = new Modlets(); |
| 529 | } |
| 530 | |
| 531 | modlets.getModlet().addAll( ( (Modlets) content ).getModlet() ); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if ( context.isLoggable( Level.FINE ) ) |
| 536 | { |
| 537 | context.log( Level.FINE, getMessage( "contextReport", |
| 538 | modlets != null ? modlets.getModlet().size() : 0, |
| 539 | location, System.currentTimeMillis() - t0 ), null ); |
| 540 | |
| 541 | } |
| 542 | |
| 543 | return modlets == null || modlets.getModlet().isEmpty() ? null : modlets; |
| 544 | } |
| 545 | catch ( final UnmarshalException e ) |
| 546 | { |
| 547 | String message = getMessage( e ); |
| 548 | if ( message == null && e.getLinkedException() != null ) |
| 549 | { |
| 550 | message = getMessage( e.getLinkedException() ); |
| 551 | } |
| 552 | |
| 553 | if ( url != null ) |
| 554 | { |
| 555 | message = getMessage( "unmarshalException", url.toExternalForm(), |
| 556 | message != null ? " " + message : "" ); |
| 557 | |
| 558 | } |
| 559 | |
| 560 | throw new ModelException( message, e ); |
| 561 | } |
| 562 | catch ( final JAXBException e ) |
| 563 | { |
| 564 | String message = getMessage( e ); |
| 565 | if ( message == null && e.getLinkedException() != null ) |
| 566 | { |
| 567 | message = getMessage( e.getLinkedException() ); |
| 568 | } |
| 569 | |
| 570 | throw new ModelException( message, e ); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * {@inheritDoc} |
| 576 | * |
| 577 | * @return The {@code Modlets} found in the context or {@code null}, if no {@code Modlets} are found or the provider |
| 578 | * is disabled. |
| 579 | * |
| 580 | * @see #isEnabled() |
| 581 | * @see #getModletLocation() |
| 582 | * @see #findModlets(org.jomc.modlet.ModelContext, java.lang.String) |
| 583 | * @see #ENABLED_ATTRIBUTE_NAME |
| 584 | * @see #MODLET_LOCATION_ATTRIBUTE_NAME |
| 585 | * @deprecated As of JOMC 1.6, this method has been replaced by {@link #findModlets(org.jomc.modlet.ModelContext, org.jomc.modlet.Modlets)}. |
| 586 | * This method will be removed in JOMC 2.0. |
| 587 | */ |
| 588 | @Deprecated |
| 589 | public Modlets findModlets( final ModelContext context ) throws ModelException |
| 590 | { |
| 591 | if ( context == null ) |
| 592 | { |
| 593 | throw new NullPointerException( "context" ); |
| 594 | } |
| 595 | |
| 596 | return this.findModlets( context, new Modlets() ); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * {@inheritDoc} |
| 601 | * |
| 602 | * @return The {@code Modlets} found in the context or {@code null}, if no {@code Modlets} are found or the provider |
| 603 | * is disabled. |
| 604 | * |
| 605 | * @see #isEnabled() |
| 606 | * @see #getModletLocation() |
| 607 | * @see #findModlets(org.jomc.modlet.ModelContext, java.lang.String) |
| 608 | * @see #ENABLED_ATTRIBUTE_NAME |
| 609 | * @see #MODLET_LOCATION_ATTRIBUTE_NAME |
| 610 | * @since 1.6 |
| 611 | */ |
| 612 | public Modlets findModlets( final ModelContext context, final Modlets modlets ) throws ModelException |
| 613 | { |
| 614 | if ( context == null ) |
| 615 | { |
| 616 | throw new NullPointerException( "context" ); |
| 617 | } |
| 618 | if ( modlets == null ) |
| 619 | { |
| 620 | throw new NullPointerException( "context" ); |
| 621 | } |
| 622 | |
| 623 | Modlets provided = null; |
| 624 | |
| 625 | boolean contextEnabled = this.isEnabled(); |
| 626 | if ( DEFAULT_ENABLED == contextEnabled && context.getAttribute( ENABLED_ATTRIBUTE_NAME ) instanceof Boolean ) |
| 627 | { |
| 628 | contextEnabled = (Boolean) context.getAttribute( ENABLED_ATTRIBUTE_NAME ); |
| 629 | } |
| 630 | |
| 631 | String contextModletLocation = this.getModletLocation(); |
| 632 | if ( DEFAULT_MODLET_LOCATION.equals( contextModletLocation ) |
| 633 | && context.getAttribute( MODLET_LOCATION_ATTRIBUTE_NAME ) instanceof String ) |
| 634 | { |
| 635 | contextModletLocation = (String) context.getAttribute( MODLET_LOCATION_ATTRIBUTE_NAME ); |
| 636 | } |
| 637 | |
| 638 | if ( contextEnabled ) |
| 639 | { |
| 640 | final Modlets found = this.findModlets( context, contextModletLocation ); |
| 641 | |
| 642 | if ( found != null ) |
| 643 | { |
| 644 | provided = modlets.clone(); |
| 645 | provided.getModlet().addAll( found.getModlet() ); |
| 646 | } |
| 647 | } |
| 648 | else if ( context.isLoggable( Level.FINER ) ) |
| 649 | { |
| 650 | context.log( Level.FINER, getMessage( "disabled", this.getClass().getSimpleName() ), null ); |
| 651 | } |
| 652 | |
| 653 | return provided; |
| 654 | } |
| 655 | |
| 656 | private static String getMessage( final String key, final Object... arguments ) |
| 657 | { |
| 658 | return MessageFormat.format( ResourceBundle.getBundle( |
| 659 | DefaultModletProvider.class.getName().replace( '.', '/' ) ).getString( key ), arguments ); |
| 660 | |
| 661 | } |
| 662 | |
| 663 | private static String getMessage( final Throwable t ) |
| 664 | { |
| 665 | return t != null |
| 666 | ? t.getMessage() != null && t.getMessage().trim().length() > 0 |
| 667 | ? t.getMessage() |
| 668 | : getMessage( t.getCause() ) |
| 669 | : null; |
| 670 | |
| 671 | } |
| 672 | |
| 673 | } |