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
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:cs@schulte.it">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.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
59 // </editor-fold>
60 // SECTION-END
61 public abstract class ObjectManagerFactory
62 {
63 // SECTION-START[ObjectManagerFactory]
64
65 /**
66 * Constant for the name of the class providing the default {@code getObjectManager()} method.
67 */
68 private static final String DEFAULT_FACTORY_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
69
70 /**
71 * Constant for the name of the class providing the default {@code ObjectManager} implementation.
72 */
73 private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
74
75 /**
76 * Constant for the name of the system property holding the {@code getObjectManager()} method's class name.
77 */
78 private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory";
79
80 /**
81 * Constant for the name of the system property holding the {@code ObjectManager} implementation class name.
82 */
83 private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager";
84
85 /**
86 * Gets the {@code ObjectManager} singleton instance.
87 * <p>
88 * This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a
89 * class declaring a <blockquote>{@code public static ObjectManager getObjectManager( ClassLoader )}</blockquote>
90 * method called by this method to get the instance to return.
91 * </p>
92 * <p>
93 * <b>Note:</b><br/>
94 * The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new
95 * {@code ObjectManager} implementation.
96 * </p>
97 *
98 * @param classLoader The class loader to use for getting the singleton instance; {@code null} to use the platform's
99 * bootstrap class loader.
100 *
101 * @return The {@code ObjectManager} singleton instance.
102 *
103 * @see #newObjectManager(java.lang.ClassLoader)
104 *
105 * @throws ObjectManagementException if getting the singleton instance fails.
106 */
107 public static ObjectManager getObjectManager( final ClassLoader classLoader )
108 {
109 try
110 {
111 return (ObjectManager) Class.forName( System.getProperty(
112 SYS_FACTORY_CLASSNAME, DEFAULT_FACTORY_CLASSNAME ), false, classLoader ).
113 getMethod( "getObjectManager", ClassLoader.class ).invoke( null, classLoader );
114
115 }
116 catch ( final Exception e )
117 {
118 throw new ObjectManagementException( getMessage( e ), e );
119 }
120 }
121
122 /**
123 * Creates a new {@code ObjectManager} instance.
124 * <p>
125 * The object manager implementation returned by this method is controlled by system property
126 * {@code org.jomc.ObjectManager} providing the name of the {@code ObjectManager} implementation class to return
127 * a new instance of.
128 * </p>
129 *
130 * @param classLoader The class loader to use for creating the instance; {@code null} to use the platform's
131 * bootstrap class loader.
132 *
133 * @return A new {@code ObjectManager} instance.
134 *
135 * @throws ObjectManagementException if creating a new {@code ObjectManager} instance fails.
136 */
137 public static ObjectManager newObjectManager( final ClassLoader classLoader )
138 {
139 try
140 {
141 return Class.forName( System.getProperty(
142 SYS_IMPLEMENTATION_CLASSNAME, DEFAULT_IMPLEMENTATION_CLASSNAME ), false, classLoader ).
143 asSubclass( ObjectManager.class ).newInstance();
144
145 }
146 catch ( final Exception e )
147 {
148 throw new ObjectManagementException( getMessage( e ), e );
149 }
150 }
151
152 private static String getMessage( final Throwable t )
153 {
154 return t != null
155 ? t.getMessage() != null && t.getMessage().trim().length() > 0
156 ? t.getMessage()
157 : getMessage( t.getCause() )
158 : null;
159
160 }
161
162 // SECTION-END
163 // SECTION-START[Constructors]
164 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
165 /** Creates a new {@code ObjectManagerFactory} instance. */
166 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
167 public ObjectManagerFactory()
168 {
169 // SECTION-START[Default Constructor]
170 super();
171 // SECTION-END
172 }
173 // </editor-fold>
174 // SECTION-END
175 // SECTION-START[Dependencies]
176 // SECTION-END
177 // SECTION-START[Properties]
178 // SECTION-END
179 // SECTION-START[Messages]
180 // SECTION-END
181
182 }