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.
66       * (which could be related to the parameters, etc)
67       */
68      public static <T> T quietNewInstance(String className, Object... params) {
69          try {
70              return Classes.<T>newInstance(className, params);
71          } catch (ClassNotFoundException e) {
72              log.warn("Couldn't find class with name {}", className);
73              return null;
74          } catch (MgnlInstantiationException e) {
75              log.warn("Couldn't instantiate {}: {}", className, e.getMessage());
76              return null;
77          }
78      }
79  
80      public static ClassFactory getClassFactory() {
81          return cfp.current();
82      }
83  
84      // this field should be final but isn't, for tests' sake
85      private static ClassFactoryProvider cfp = new ClassFactoryProvider(new DefaultClassFactory());
86  
87      /**
88       * ClassFactoryProvider is used to hide the "swapability" of ClassFactory.
89       * This current implementation checks the current instance versus the configured property
90       * on every call.
91       * TODO : This could possibly be optimized or "blocked" as soon as we can now this property has indeed been configured.
92       * I don't think the above would be interesting until we can do some refactoring of the ConfigLoader, PropertiesInitializer and SystemProperty classes.
93       */
94      protected static class ClassFactoryProvider {
95          private ClassFactory initial;
96          private ClassFactory current;
97          private boolean swapping;
98  
99          public ClassFactoryProvider(ClassFactory initial) {
100             this.initial = initial;
101             this.current = initial;
102         }
103 
104         public ClassFactory current() {
105             check();
106             return current;
107         }
108 
109         private void check() {
110             final String configuredCFClassName = getCurrentlyConfiguredClassName();
111             final String currentCFClassName = current.getClass().getName();
112             if (StringUtils.isNotEmpty(configuredCFClassName) && !currentCFClassName.equals(configuredCFClassName) && !swapping) {
113                 // we need to swap this instance, but other calls should not try to do it until we're done
114                 swapping = true;
115 
116                 // whichever ClassFactory is registered will be instantiated with the initial ClassFactory.
117                 try {
118                     final Class<ClassFactory> c = initial.forName(configuredCFClassName);
119                     current = initial.newInstance(c);
120                 } catch (ClassNotFoundException e) {
121                     log.error("Could not find {}, will keep on using {} for now", configuredCFClassName, current.getClass().getSimpleName());
122                 }
123                 swapping = false;
124             }
125         }
126 
127         protected String getCurrentlyConfiguredClassName() {
128             return SystemProperty.getProperty(ClassFactory.class.getName());
129         }
130     }
131 
132 }