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 <cs@schulte.it>, 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 5061 2015-05-31 13:20:40Z 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.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
059// </editor-fold>
060// SECTION-END
061public abstract class ObjectManagerFactory
062{
063    // SECTION-START[ObjectManagerFactory]
064
065    /**
066     * Constant for the name of the class providing the default {@code getObjectManager()} method.
067     */
068    private static final String DEFAULT_FACTORY_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
069
070    /**
071     * Constant for the name of the class providing the default {@code ObjectManager} implementation.
072     */
073    private static final String DEFAULT_IMPLEMENTATION_CLASSNAME = "org.jomc.ri.DefaultObjectManager";
074
075    /**
076     * Constant for the name of the system property holding the {@code getObjectManager()} method's class name.
077     */
078    private static final String SYS_FACTORY_CLASSNAME = "org.jomc.ObjectManagerFactory";
079
080    /**
081     * Constant for the name of the system property holding the {@code ObjectManager} implementation class name.
082     */
083    private static final String SYS_IMPLEMENTATION_CLASSNAME = "org.jomc.ObjectManager";
084
085    /**
086     * Gets the {@code ObjectManager} singleton instance.
087     * <p>
088     * This method is controlled by system property {@code org.jomc.ObjectManagerFactory} providing the name of a
089     * class declaring a <blockquote>{@code public static ObjectManager getObjectManager( ClassLoader )}</blockquote>
090     * method called by this method to get the instance to return.
091     * </p>
092     * <p>
093     * <b>Note:</b><br/>
094     * The {@code newObjectManager} method should be used by {@code getObjectManager} implementors to retrieve a new
095     * {@code ObjectManager} implementation.
096     * </p>
097     *
098     * @param classLoader The class loader to use for getting the singleton instance; {@code null} to use the platform's
099     * 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}