View Javadoc

1   /**
2    * This file Copyright (c) 2010-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.objectfactory;
35  
36  import info.magnolia.cms.core.SystemProperty;
37  import org.apache.commons.lang.StringUtils;
38  
39  /**
40   * Entry point to the currently configured ClassFactory.
41   *
42   * @see info.magnolia.objectfactory.ClassFactory
43   *
44   * @author gjoseph
45   * @version $Revision: $ ($Author: $)
46   */
47  public class Classes {
48      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Classes.class);
49  
50      /**
51       * Convenience/shortcut method for instantiating new classes.
52       * @see info.magnolia.objectfactory.ClassFactory
53       * @see #getClassFactory()
54       * @throws ClassNotFoundException
55       * @throws MgnlInstantiationException
56       */
57      public static <T> T newInstance(String className, Object... params) throws ClassNotFoundException {
58          final ClassFactory cf = getClassFactory();
59          final Class<T> cl = cf.forName(className);
60          return cf.newInstance(cl, params);
61      }
62  
63      /**
64       * Convenience/shortcut for {@link #newInstance(String, Object...)}, returning null both in case
65       * of a ClassNotFoundException or if the class could not be instantiated (which could be related to the parameters, etc)
66       */
67      public static <T> T quietNewInstance(String className, Object... params) {
68          try {
69              return Classes.<T>newInstance(className, params);
70          } catch (ClassNotFoundException e) {
71              log.warn("Couldn't find class with name {}", className);
72              return null;
73          } catch (MgnlInstantiationException e) {
74              log.warn("Couldn't instantiate {}: {}", className, e.getMessage());
75              return null;
76          }
77      }
78  
79      public static ClassFactory getClassFactory() {
80          return cfp.current();
81      }
82  
83      // this field should be final but isn't, for tests' sake
84      private static ClassFactoryProvider cfp = new ClassFactoryProvider(new DefaultClassFactory());
85  
86      /**
87       * ClassFactoryProvider is used to hide the "swapability" of ClassFactory.
88       * This current implementation checks the current instance versus the configured property
89       * on every call.
90       * TODO : This could possibly be optimized or "blocked" as soon as we can now this property has indeed been configured.
91       * I don't think the above would be interesting until we can do some refactoring of the ConfigLoader, PropertiesInitializer and SystemProperty classes.
92       */
93      protected static class ClassFactoryProvider {
94          private ClassFactory initial;
95          private ClassFactory current;
96          private boolean swapping;
97  
98          public ClassFactoryProvider(ClassFactory initial) {
99              this.initial = initial;
100             this.current = initial;
101         }
102 
103         public ClassFactory current() {
104             check();
105             return current;
106         }
107 
108         private void check() {
109             final String configuredCFClassName = getCurrentlyConfiguredClassName();
110             final String currentCFClassName = current.getClass().getName();
111             if (StringUtils.isNotEmpty(configuredCFClassName) && !currentCFClassName.equals(configuredCFClassName) && !swapping) {
112                 // we need to swap this instance, but other calls should not try to do it until we're done
113                 swapping = true;
114 
115                 // whichever ClassFactory is registered will be instantiated with the initial ClassFactory.
116                 try {
117                     final Class<ClassFactory> c = initial.forName(configuredCFClassName);
118                     current = initial.newInstance(c);
119                 } catch (ClassNotFoundException e) {
120                     log.error("Could not find {}, will keep on using {} for now", configuredCFClassName, current.getClass().getSimpleName());
121                 }
122                 swapping = false;
123             }
124         }
125 
126         protected String getCurrentlyConfiguredClassName() {
127             return SystemProperty.getProperty(ClassFactory.class.getName());
128         }
129     }
130 
131 }