001// SECTION-START[License Header]
002// <editor-fold defaultstate="collapsed" desc=" Generated License ">
003/*
004 *   Java Object Management and Configuration
005 *   Copyright (C) Christian Schulte, 2005-206
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 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
021 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
022 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
023 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
024 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
025 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
029 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 *
031 *   $JOMC: ObjectManagerFactory.java 4795 2013-04-21 09:09:02Z schulte $
032 *
033 */
034// </editor-fold>
035// SECTION-END
036package org.jomc;
037
038// SECTION-START[Documentation]
039// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
040/**
041 * Factory for the {@code ObjectManager} singleton.
042 *
043 * <dl>
044 *   <dt><b>Identifier:</b></dt><dd>org.jomc.ObjectManagerFactory</dd>
045 *   <dt><b>Name:</b></dt><dd>JOMC ⁑ API</dd>
046 *   <dt><b>Abstract:</b></dt><dd>Yes</dd>
047 *   <dt><b>Final:</b></dt><dd>Yes</dd>
048 *   <dt><b>Stateless:</b></dt><dd>No</dd>
049 * </dl>
050 *
051 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
052 * @version 1.0
053 */
054// </editor-fold>
055// SECTION-END
056// SECTION-START[Annotations]
057// <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
058@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
059// </editor-fold>
060// SECTION-END
061public abstract class ObjectManagerFactory
062{
063    // SECTION-START[ObjectManagerFactory]
064
065    /** Constant for the name of the class providing the default {@code getObjectManager()} method. */
066    private static final String DEFAULT_FACTORY_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
067
068    /** Constant for the name of the class providing the default {@code ObjectManager} implementation. */
069    private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
070
071    /** Constant for the name of the system property holding the {@code getObjectManager()} method's class name. */
072    private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory";
073
074    /** Constant for the name of the system property holding the {@code ObjectManager} implementation class name. */
075    private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager";
076
077    /**
078     * Gets the {@code ObjectManager} singleton instance.
079     * <p>This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a
080     * class declaring a <blockquote>{@code public static ObjectManager getObjectManager( ClassLoader )}</blockquote>
081     * method called by this method to get the instance to return.</p>
082     * <p><b>Note</b><br/>
083     * The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new
084     * {@code ObjectManager} implementation.</p>
085     *
086     * @param classLoader The class loader to use for getting the singleton instance; {@code null} to use the platform's
087     * bootstrap class loader.
088     *
089     * @return The {@code ObjectManager} singleton instance.
090     *
091     * @see #newObjectManager(java.lang.ClassLoader)
092     *
093     * @throws ObjectManagementException if getting the singleton instance fails.
094     */
095    public static ObjectManager getObjectManager( final ClassLoader classLoader )
096    {
097        try
098        {
099            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
141               ? t.getMessage() != null && t.getMessage().trim().length() > 0
142                 ? t.getMessage()
143                 : getMessage( t.getCause() )
144               : null;
145
146    }
147
148    // SECTION-END
149    // SECTION-START[Constructors]
150    // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
151    /** Creates a new {@code ObjectManagerFactory} instance. */
152    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
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}