1 | // SECTION-START[License Header] |
2 | // <editor-fold defaultstate="collapsed" desc=" Generated License "> |
3 | /* |
4 | * Copyright (c) 2010 The JOMC Project |
5 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
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 BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
27 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
30 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | * |
32 | * $Id: ObjectManagerFactory.java 1306 2010-01-16 14:28:11Z schulte2005 $ |
33 | * |
34 | */ |
35 | // </editor-fold> |
36 | // SECTION-END |
37 | package org.jomc; |
38 | |
39 | import java.lang.reflect.InvocationTargetException; |
40 | import java.lang.reflect.Method; |
41 | |
42 | // SECTION-START[Documentation] |
43 | // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> |
44 | /** |
45 | * Factory for the {@code ObjectManager} singleton. |
46 | * |
47 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0 |
48 | * @version $Id: ObjectManagerFactory.java 1306 2010-01-16 14:28:11Z schulte2005 $ |
49 | */ |
50 | // </editor-fold> |
51 | // SECTION-END |
52 | // SECTION-START[Annotations] |
53 | // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> |
54 | @javax.annotation.Generated( value = "org.jomc.tools.JavaSources", |
55 | comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" ) |
56 | // </editor-fold> |
57 | // SECTION-END |
58 | public class ObjectManagerFactory |
59 | { |
60 | // SECTION-START[ObjectManagerFactory] |
61 | |
62 | /** Constant for the name of the class providing the default {@code getObjectManager()} method. */ |
63 | private static final String DEFAULT_FACTORY_CLASSNAME = "org.jomc.ri.DefaultObjectManager"; |
64 | |
65 | /** Constant for the name of the class providing the default {@code ObjectManager} implementation. */ |
66 | private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager"; |
67 | |
68 | /** Constant for the name of the system property holding the {@code getObjectManager()} method's class name. */ |
69 | private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory"; |
70 | |
71 | /** Constant for the name of the system property holding the {@code ObjectManager} implementation class name. */ |
72 | private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager"; |
73 | |
74 | /** |
75 | * Gets the {@code ObjectManager} singleton instance. |
76 | * <p>This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a |
77 | * class declaring a <blockquote>{@code public static ObjectManager getObjectManager( ClassLoader )}</blockquote> |
78 | * method called by this method to get the instance to return.</p> |
79 | * <p><b>Note</b><br/> |
80 | * The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new |
81 | * {@code ObjectManager} implementation.</p> |
82 | * |
83 | * @param classLoader The class loader to use for getting the singleton instance; {@code null} to use the platform's |
84 | * bootstrap class loader. |
85 | * |
86 | * @return The {@code ObjectManager} singleton instance. |
87 | * |
88 | * @see #newObjectManager(java.lang.ClassLoader) |
89 | * |
90 | * @throws ObjectManagementException if getting the singleton instance fails. |
91 | */ |
92 | public static ObjectManager getObjectManager( final ClassLoader classLoader ) |
93 | { |
94 | final String factory = System.getProperty( SYS_FACTORY_CLASSNAME, DEFAULT_FACTORY_CLASSNAME ); |
95 | |
96 | try |
97 | { |
98 | final Class factoryClass = Class.forName( factory, true, classLoader ); |
99 | final Method factoryMethod = factoryClass.getMethod( "getObjectManager", ClassLoader.class ); |
100 | return (ObjectManager) factoryMethod.invoke( null, classLoader ); |
101 | } |
102 | catch ( final InvocationTargetException e ) |
103 | { |
104 | if ( e.getTargetException() != null ) |
105 | { |
106 | throw new ObjectManagementException( e.getTargetException().getMessage(), e.getTargetException() ); |
107 | } |
108 | else |
109 | { |
110 | throw new ObjectManagementException( e.getMessage(), e ); |
111 | } |
112 | } |
113 | catch ( final Exception e ) |
114 | { |
115 | throw new ObjectManagementException( e.getMessage(), e ); |
116 | } |
117 | } |
118 | |
119 | /** |
120 | * Creates a new {@code ObjectManager} instance. |
121 | * <p>The object manager implementation returned by this method is controlled by system property |
122 | * {@code org.jomc.ObjectManager} providing the name of the {@code ObjectManager} implementation class to return |
123 | * a new instance of.</p> |
124 | * |
125 | * @param classLoader The class loader to use for creating the instance; {@code null} to use the platform's |
126 | * bootstrap class loader. |
127 | * |
128 | * @return A new {@code ObjectManager} instance. |
129 | * |
130 | * @throws ObjectManagementException if creating a new {@code ObjectManager} instance fails. |
131 | */ |
132 | public static ObjectManager newObjectManager( final ClassLoader classLoader ) |
133 | { |
134 | final String impl = System.getProperty( SYS_IMPLEMENTATION_CLASSNAME, DEFAULT_IMPLEMENTATION_CLASSNAME ); |
135 | |
136 | try |
137 | { |
138 | return (ObjectManager) Class.forName( impl, true, classLoader ).newInstance(); |
139 | } |
140 | catch ( final Exception e ) |
141 | { |
142 | throw new ObjectManagementException( e.getMessage(), e ); |
143 | } |
144 | } |
145 | |
146 | // SECTION-END |
147 | // SECTION-START[Constructors] |
148 | // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> |
149 | |
150 | /** Creates a new {@code ObjectManagerFactory} instance. */ |
151 | @javax.annotation.Generated( value = "org.jomc.tools.JavaSources", |
152 | comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" ) |
153 | public ObjectManagerFactory() |
154 | { |
155 | // SECTION-START[Default Constructor] |
156 | super(); |
157 | // SECTION-END |
158 | } |
159 | // </editor-fold> |
160 | // SECTION-END |
161 | // SECTION-START[Dependencies] |
162 | // SECTION-END |
163 | // SECTION-START[Properties] |
164 | // SECTION-END |
165 | // SECTION-START[Messages] |
166 | // SECTION-END |
167 | } |