001 // SECTION-START[License Header]
002 // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003 /*
004 * Copyright (c) 2009 The JOMC Project
005 * Copyright (c) 2005 Christian Schulte <cs@jomc.org>
006 * All rights reserved.
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions
010 * are met:
011 *
012 * o Redistributions of source code must retain the above copyright
013 * notice, this list of conditions and the following disclaimer.
014 *
015 * o Redistributions in binary form must reproduce the above copyright
016 * notice, this list of conditions and the following disclaimer in
017 * the documentation and/or other materials provided with the
018 * distribution.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 *
032 * $Id: ObjectManagerFactory.java 968 2009-11-18 06:08:28Z schulte2005 $
033 *
034 */
035 // </editor-fold>
036 // SECTION-END
037 package org.jomc;
038
039 import java.lang.reflect.InvocationTargetException;
040 import java.lang.reflect.Method;
041
042 // SECTION-START[Documentation]
043 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
044 /**
045 * Factory for the {@code ObjectManager} singleton.
046 *
047 * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0
048 * @version $Id: ObjectManagerFactory.java 968 2009-11-18 06:08:28Z schulte2005 $
049 */
050 // </editor-fold>
051 // SECTION-END
052 // SECTION-START[Annotations]
053 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
054 @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
055 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-8/jomc-tools" )
056 // </editor-fold>
057 // SECTION-END
058 public class ObjectManagerFactory
059 {
060 // SECTION-START[ObjectManagerFactory]
061
062 /** Constant for the name of the class providing the default {@code getObjectManager()} method. */
063 private static final String DEFAULT_FACTORY_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
064
065 /** Constant for the name of the class providing the default {@code ObjectManager} implementation. */
066 private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
067
068 /** Constant for the name of the system property holding the {@code getObjectManager()} method's class name. */
069 private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory";
070
071 /** Constant for the name of the system property holding the {@code ObjectManager} implementation class name. */
072 private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager";
073
074 /**
075 * Gets the {@code ObjectManager} singleton instance.
076 * <p>This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a
077 * class declaring a <blockquote>{@code public static ObjectManager getObjectManager( ClassLoader )}</blockquote>
078 * method called by this method to get the instance to return.</p>
079 * <p><b>Note</b><br/>
080 * The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new
081 * {@code ObjectManager} implementation.</p>
082 *
083 * @param classLoader The class loader to use for getting the singleton instance; {@code null} to use the platform's
084 * bootstrap class loader.
085 *
086 * @return The {@code ObjectManager} singleton instance.
087 *
088 * @see #newObjectManager(java.lang.ClassLoader)
089 *
090 * @throws ObjectManagementException if getting the singleton instance fails.
091 */
092 public static ObjectManager getObjectManager( final ClassLoader classLoader )
093 {
094 final String factory = System.getProperty( SYS_FACTORY_CLASSNAME, DEFAULT_FACTORY_CLASSNAME );
095
096 try
097 {
098 final Class factoryClass = Class.forName( factory, true, classLoader );
099 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-8/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 }