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, 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 4588 2012-06-03 06:01:30Z schulte2005 $
32   *
33   */
34  // </editor-fold>
35  // SECTION-END
36  package org.jomc;
37  
38  // SECTION-START[Documentation]
39  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
40  /**
41   * Factory for the {@code ObjectManager} singleton.
42   *
43   * <dl>
44   *   <dt><b>Identifier:</b></dt><dd>org.jomc.ObjectManagerFactory</dd>
45   *   <dt><b>Name:</b></dt><dd>JOMC API</dd>
46   *   <dt><b>Abstract:</b></dt><dd>Yes</dd>
47   *   <dt><b>Final:</b></dt><dd>Yes</dd>
48   *   <dt><b>Stateless:</b></dt><dd>No</dd>
49   * </dl>
50   *
51   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
52   * @version 1.0
53   */
54  // </editor-fold>
55  // SECTION-END
56  // SECTION-START[Annotations]
57  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
58  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" )
59  // </editor-fold>
60  // SECTION-END
61  public abstract class ObjectManagerFactory
62  {
63      // SECTION-START[ObjectManagerFactory]
64  
65      /** Constant for the name of the class providing the default {@code getObjectManager()} method. */
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      private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
70  
71      /** Constant for the name of the system property holding the {@code getObjectManager()} method's class name. */
72      private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory";
73  
74      /** Constant for the name of the system property holding the {@code ObjectManager} implementation class name. */
75      private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager";
76  
77      /**
78       * Gets the {@code ObjectManager} singleton instance.
79       * <p>This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a
80       * class declaring a <blockquote>{@code public static ObjectManager getObjectManager( ClassLoader )}</blockquote>
81       * method called by this method to get the instance to return.</p>
82       * <p><b>Note</b><br/>
83       * The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new
84       * {@code ObjectManager} implementation.</p>
85       *
86       * @param classLoader The class loader to use for getting the singleton instance; {@code null} to use the platform's
87       * bootstrap class loader.
88       *
89       * @return The {@code ObjectManager} singleton instance.
90       *
91       * @see #newObjectManager(java.lang.ClassLoader)
92       *
93       * @throws ObjectManagementException if getting the singleton instance fails.
94       */
95      public static ObjectManager getObjectManager( final ClassLoader classLoader )
96      {
97          try
98          {
99              return (ObjectManager) Class.forName( System.getProperty(
100                 SYS_FACTORY_CLASSNAME, DEFAULT_FACTORY_CLASSNAME ), false, classLoader ).
101                 getMethod( "getObjectManager", ClassLoader.class ).invoke( null, classLoader );
102 
103         }
104         catch ( final Exception e )
105         {
106             throw new ObjectManagementException( getMessage( e ), e );
107         }
108     }
109 
110     /**
111      * Creates a new {@code ObjectManager} instance.
112      * <p>The object manager implementation returned by this method is controlled by system property
113      * {@code org.jomc.ObjectManager} providing the name of the {@code ObjectManager} implementation class to return
114      * a new instance of.</p>
115      *
116      * @param classLoader The class loader to use for creating the instance; {@code null} to use the platform's
117      * bootstrap class loader.
118      *
119      * @return A new {@code ObjectManager} instance.
120      *
121      * @throws ObjectManagementException if creating a new {@code ObjectManager} instance fails.
122      */
123     public static ObjectManager newObjectManager( final ClassLoader classLoader )
124     {
125         try
126         {
127             return Class.forName( System.getProperty(
128                 SYS_IMPLEMENTATION_CLASSNAME, DEFAULT_IMPLEMENTATION_CLASSNAME ), false, classLoader ).
129                 asSubclass( ObjectManager.class ).newInstance();
130 
131         }
132         catch ( final Exception e )
133         {
134             throw new ObjectManagementException( getMessage( e ), e );
135         }
136     }
137 
138     private static String getMessage( final Throwable t )
139     {
140         return t != null ? t.getMessage() != null ? t.getMessage() : getMessage( t.getCause() ) : null;
141     }
142 
143     // SECTION-END
144     // SECTION-START[Constructors]
145     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
146     /** Creates a new {@code ObjectManagerFactory} instance. */
147     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" )
148     public ObjectManagerFactory()
149     {
150         // SECTION-START[Default Constructor]
151         super();
152         // SECTION-END
153     }
154     // </editor-fold>
155     // SECTION-END
156     // SECTION-START[Dependencies]
157     // SECTION-END
158     // SECTION-START[Properties]
159     // SECTION-END
160     // SECTION-START[Messages]
161     // SECTION-END
162 }