View Javadoc

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j.util;
    9
    10  /***
    11   * <p>
    12   * <code>SimpleSingleton</code> is an implementation of the SingletonStrategy
    13   * interface used to provide common factory access for the same object instance.
    14   * This implementation will create a new instance from the class specified and
    15   * will not create a new one unless it is reset.
    16   * </p>
    17   *
    18   * @author <a href="mailto:ddlucas@users.sourceforge.net">David Lucas </a>
    19   * @version $Revision: 1.3 $
    20   */
    21
    22  public class SimpleSingleton implements SingletonStrategy {
    23      private String singletonClassName = null;
    24
    25      private Object singletonInstance = null;
    26
    27      public SimpleSingleton() {
    28      }
    29
    30      public Object instance() {
    31          return singletonInstance;
    32      }
    33
    34      public void reset() {
    35          if (singletonClassName != null) {
    36              Class clazz = null;
    37              try {
    38                  clazz = Thread.currentThread().getContextClassLoader().loadClass(
    39                          singletonClassName);
    40                  singletonInstance = clazz.newInstance();
    41              } catch (Exception ignore) {
    42                  try {
    43                      clazz = Class.forName(singletonClassName);
    44                      singletonInstance = clazz.newInstance();
    45                  } catch (Exception ignore2) {
    46                  }
    47              }
    48
    49          }
    50      }
    51
    52      public void setSingletonClassName(String singletonClassName) {
    53          this.singletonClassName = singletonClassName;
    54          reset();
    55      }
    56
    57  }
    58
    59  /*
    60   * Redistribution and use of this software and associated documentation
    61   * ("Software"), with or without modification, are permitted provided that the
    62   * following conditions are met:
    63   *
    64   * 1. Redistributions of source code must retain copyright statements and
    65   * notices. Redistributions must also contain a copy of this document.
    66   *
    67   * 2. Redistributions in binary form must reproduce the above copyright notice,
    68   * this list of conditions and the following disclaimer in the documentation
    69   * and/or other materials provided with the distribution.
    70   *
    71   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    72   * from this Software without prior written permission of MetaStuff, Ltd. For
    73   * written permission, please contact dom4j-info@metastuff.com.
    74   *
    75   * 4. Products derived from this Software may not be called "DOM4J" nor may
    76   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    77   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    78   *
    79   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    80   *
    81   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    82   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    83   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    84   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    85   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    86   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    87   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    88   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    89   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    90   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    91   * POSSIBILITY OF SUCH DAMAGE.
    92   *
    93   * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    94   */