001    // SECTION-START[License Header]
002    /*
003     *   Copyright (c) 2009 The JOMC Project
004     *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
005     *   All rights reserved.
006     *
007     *   Redistribution and use in source and binary forms, with or without
008     *   modification, are permitted provided that the following conditions
009     *   are met:
010     *
011     *     o Redistributions of source code must retain the above copyright
012     *       notice, this list of conditions and the following disclaimer.
013     *
014     *     o Redistributions in binary form must reproduce the above copyright
015     *       notice, this list of conditions and the following disclaimer in
016     *       the documentation and/or other materials provided with the
017     *       distribution.
018     *
019     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
020     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
022     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
023     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
024     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
025     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
026     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
027     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
028     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030     *
031     *   $Id: ObjectManagerFactory.java 540 2009-09-21 18:50:19Z schulte2005 $
032     *
033     */
034    // SECTION-END
035    package org.jomc;
036    
037    import java.lang.reflect.Method;
038    
039    // SECTION-START[Documentation]
040    /**
041     * Factory for the {@code ObjectManager} singleton.
042     *
043     * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0
044     * @version $Id: ObjectManagerFactory.java 540 2009-09-21 18:50:19Z schulte2005 $
045     */
046    // SECTION-END
047    // SECTION-START[Annotations]
048    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
049                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-1/jomc-tools" )
050    // SECTION-END
051    public class ObjectManagerFactory
052    {
053        // SECTION-START[ObjectManagerFactory]
054    
055        /** Constant for the name of the class providing the default {@code getObjectManager()} method. */
056        private static final String DEFAULT_FACTORY_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
057    
058        /** Constant for the name of the class providing the default {@code ObjectManager} implementation. */
059        private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
060    
061        /** Constant for the name of the system property holding the {@code getObjectManager()} method's class name. */
062        private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory";
063    
064        /** Constant for the name of the system property holding the {@code ObjectManager} implementation class name. */
065        private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager";
066    
067        /**
068         * Gets the {@code ObjectManager} singleton instance.
069         * <p>This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a
070         * class declaring a <blockquote>{@code public static ObjectManager getObjectManager()}</blockquote> method called
071         * by this method to get the instance to return.</p>
072         * <p>The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new
073         * {@code ObjectManager} implementation.</p>
074         *
075         * @return The {@code ObjectManager} singleton instance.
076         *
077         * @see ObjectManagerFactory#newObjectManager()
078         *
079         * @throws ObjectManagementException if getting the singleton instance fails.
080         */
081        public static ObjectManager getObjectManager()
082        {
083            final String factory = System.getProperty( SYS_FACTORY_CLASSNAME, DEFAULT_FACTORY_CLASSNAME );
084    
085            try
086            {
087                final Class factoryClass = Class.forName( factory );
088                final Method factoryMethod = factoryClass.getMethod( "getObjectManager", (Class[]) null );
089                return (ObjectManager) factoryMethod.invoke( null, (Object[]) null );
090            }
091            catch ( final Exception e )
092            {
093                throw new ObjectManagementException( e.getMessage(), e );
094            }
095        }
096    
097        /**
098         * Creates a new {@code ObjectManager} instance.
099         * <p>The object manager implementation returned by this method is controlled by system property
100         * {@code org.jomc.ObjectManager} providing the name of the {@code ObjectManager} implementation to return.</p>
101         *
102         * @return A new {@code ObjectManager} instance.
103         *
104         * @throws ObjectManagementException if creating a new {@code ObjectManager} instance fails.
105         */
106        public static ObjectManager newObjectManager()
107        {
108            final String impl = System.getProperty( SYS_IMPLEMENTATION_CLASSNAME, DEFAULT_IMPLEMENTATION_CLASSNAME );
109    
110            try
111            {
112                return (ObjectManager) Class.forName( impl ).newInstance();
113            }
114            catch ( final Exception e )
115            {
116                throw new ObjectManagementException( e.getMessage(), e );
117            }
118        }
119    
120        // SECTION-END
121        // SECTION-START[Constructors]
122    
123        /** Creates a new {@code ObjectManagerFactory} instance. */
124        @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
125                                     comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-1/jomc-tools" )
126        public ObjectManagerFactory()
127        {
128            // SECTION-START[Default Constructor]
129            super();
130            // SECTION-END
131        }
132        // SECTION-END
133        // SECTION-START[Dependencies]
134        // SECTION-END
135        // SECTION-START[Properties]
136        // SECTION-END
137        // SECTION-START[Messages]
138        // SECTION-END
139    }