ObjectManagerFactory.java

  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: ObjectManagerFactory.java 5061 2015-05-31 13:20:40Z schulte $
  32.  *
  33.  */
  34. // </editor-fold>
  35. // SECTION-END
  36. package org.jomc;

  37. // SECTION-START[Documentation]
  38. // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
  39. /**
  40.  * Factory for the {@code ObjectManager} singleton.
  41.  *
  42.  * <dl>
  43.  *   <dt><b>Identifier:</b></dt><dd>org.jomc.ObjectManagerFactory</dd>
  44.  *   <dt><b>Name:</b></dt><dd>JOMC ⁑ API</dd>
  45.  *   <dt><b>Abstract:</b></dt><dd>Yes</dd>
  46.  *   <dt><b>Final:</b></dt><dd>Yes</dd>
  47.  *   <dt><b>Stateless:</b></dt><dd>No</dd>
  48.  * </dl>
  49.  *
  50.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
  51.  * @version 1.0
  52.  */
  53. // </editor-fold>
  54. // SECTION-END
  55. // SECTION-START[Annotations]
  56. // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
  57. @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
  58. // </editor-fold>
  59. // SECTION-END
  60. public abstract class ObjectManagerFactory
  61. {
  62.     // SECTION-START[ObjectManagerFactory]

  63.     /**
  64.      * Constant for the name of the class providing the default {@code getObjectManager()} method.
  65.      */
  66.     private static final String DEFAULT_FACTORY_CLASSNAME = "org.jomc.ri.DefaultObjectManager";

  67.     /**
  68.      * Constant for the name of the class providing the default {@code ObjectManager} implementation.
  69.      */
  70.     private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager";

  71.     /**
  72.      * Constant for the name of the system property holding the {@code getObjectManager()} method's class name.
  73.      */
  74.     private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory";

  75.     /**
  76.      * Constant for the name of the system property holding the {@code ObjectManager} implementation class name.
  77.      */
  78.     private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager";

  79.     /**
  80.      * Gets the {@code ObjectManager} singleton instance.
  81.      * <p>
  82.      * This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a
  83.      * class declaring a <blockquote>{@code public static ObjectManager getObjectManager( ClassLoader )}</blockquote>
  84.      * method called by this method to get the instance to return.
  85.      * </p>
  86.      * <p>
  87.      * <b>Note:</b><br/>
  88.      * The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new
  89.      * {@code ObjectManager} implementation.
  90.      * </p>
  91.      *
  92.      * @param classLoader The class loader to use for getting the singleton instance; {@code null} to use the platform's
  93.      * bootstrap class loader.
  94.      *
  95.      * @return The {@code ObjectManager} singleton instance.
  96.      *
  97.      * @see #newObjectManager(java.lang.ClassLoader)
  98.      *
  99.      * @throws ObjectManagementException if getting the singleton instance fails.
  100.      */
  101.     public static ObjectManager getObjectManager( final ClassLoader classLoader )
  102.     {
  103.         try
  104.         {
  105.             return (ObjectManager) Class.forName( System.getProperty(
  106.                 SYS_FACTORY_CLASSNAME, DEFAULT_FACTORY_CLASSNAME ), false, classLoader ).
  107.                 getMethod( "getObjectManager", ClassLoader.class ).invoke( null, classLoader );

  108.         }
  109.         catch ( final Exception e )
  110.         {
  111.             throw new ObjectManagementException( getMessage( e ), e );
  112.         }
  113.     }

  114.     /**
  115.      * Creates a new {@code ObjectManager} instance.
  116.      * <p>
  117.      * The object manager implementation returned by this method is controlled by system property
  118.      * {@code org.jomc.ObjectManager} providing the name of the {@code ObjectManager} implementation class to return
  119.      * a new instance of.
  120.      * </p>
  121.      *
  122.      * @param classLoader The class loader to use for creating the instance; {@code null} to use the platform's
  123.      * bootstrap class loader.
  124.      *
  125.      * @return A new {@code ObjectManager} instance.
  126.      *
  127.      * @throws ObjectManagementException if creating a new {@code ObjectManager} instance fails.
  128.      */
  129.     public static ObjectManager newObjectManager( final ClassLoader classLoader )
  130.     {
  131.         try
  132.         {
  133.             return Class.forName( System.getProperty(
  134.                 SYS_IMPLEMENTATION_CLASSNAME, DEFAULT_IMPLEMENTATION_CLASSNAME ), false, classLoader ).
  135.                 asSubclass( ObjectManager.class ).newInstance();

  136.         }
  137.         catch ( final Exception e )
  138.         {
  139.             throw new ObjectManagementException( getMessage( e ), e );
  140.         }
  141.     }

  142.     private static String getMessage( final Throwable t )
  143.     {
  144.         return t != null
  145.                    ? t.getMessage() != null && t.getMessage().trim().length() > 0
  146.                          ? t.getMessage()
  147.                          : getMessage( t.getCause() )
  148.                    : null;

  149.     }

  150.     // SECTION-END
  151.     // SECTION-START[Constructors]
  152.     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
  153.     /** Creates a new {@code ObjectManagerFactory} instance. */
  154.     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
  155.     public ObjectManagerFactory()
  156.     {
  157.         // SECTION-START[Default Constructor]
  158.         super();
  159.         // SECTION-END
  160.     }
  161.     // </editor-fold>
  162.     // SECTION-END
  163.     // SECTION-START[Dependencies]
  164.     // SECTION-END
  165.     // SECTION-START[Properties]
  166.     // SECTION-END
  167.     // SECTION-START[Messages]
  168.     // SECTION-END

  169. }