View Javadoc

1   /**
2    * This file Copyright (c) 2010-2011 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  import java.lang.reflect.Modifier;
40  
41  /**
42   * Entry point to the currently configured ClassFactory, as well as some additional utility methods for manipulating Class objects.
43   *
44   * @see info.magnolia.objectfactory.ClassFactory
45   *
46   * @author gjoseph
47   * @version $Revision: $ ($Author: $)
48   */
49  public class Classes {
50      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Classes.class);
51  
52      public static boolean isConcrete(Class<?> clazz) {
53          if (clazz == null) {
54              // null class is not concrete .... ever
55              return false;
56          }
57          return !Modifier.isAbstract(clazz.getModifiers());
58      }
59  
60      /**
61       * Convenience/shortcut method for instantiating new classes.
62       * @see info.magnolia.objectfactory.ClassFactory
63       * @see #getClassFactory()
64       * @throws ClassNotFoundException
65       * @throws MgnlInstantiationException
66       */
67      public static <T> T newInstance(String className, Object... params) throws ClassNotFoundException {
68          final ClassFactory cf = getClassFactory();
69          final Class<T> cl = cf.forName(className);
70          return cf.newInstance(cl, params);
71      }
72  
73      /**
74       * Convenience/shortcut for {@link #newInstance(String, Object...)}, returning null both in case
75       * of a ClassNotFoundException or if the class could not be instantiated.
76       * (which could be related to the parameters, etc)
77       */
78      public static <T> T quietNewInstance(String className, Object... params) {
79          try {
80              return Classes.<T>newInstance(className, params);
81          } catch (ClassNotFoundException e) {
82              log.warn("Couldn't find class with name {}", className);
83              return null;
84          } catch (MgnlInstantiationException e) {
85              log.warn("Couldn't instantiate {}: {}", className, e.getMessage());
86              return null;
87          }
88      }
89  
90      public static ClassFactory getClassFactory() {
91          return cfp.current();
92      }
93  
94      // this field should be final but isn't, for tests' sake
95      private static ClassFactoryProvider cfp = new ClassFactoryProvider(new DefaultClassFactory());
96  
97      /**
98       * ClassFactoryProvider is used to hide the "swapability" of ClassFactory.
99       * This current implementation checks the current instance versus the configured property
100      * on every call.
101      * TODO : This could possibly be optimized or "blocked" as soon as we can now this property has indeed been configured.
102      * I don't think the above would be interesting until we can do some refactoring of the ConfigLoader, PropertiesInitializer and SystemProperty classes.
103      */
104     protected static class ClassFactoryProvider {
105         private ClassFactory initial;
106         private ClassFactory current;
107         private boolean swapping;
108 
109         public ClassFactoryProvider(ClassFactory initial) {
110             this.initial = initial;
111             this.current = initial;
112         }
113 
114         public ClassFactory current() {
115             check();
116             return current;
117         }
118 
119         private void check() {
120             final String configuredCFClassName = getCurrentlyConfiguredClassName();
121             final String currentCFClassName = current.getClass().getName();
122             if (StringUtils.isNotEmpty(configuredCFClassName) && !currentCFClassName.equals(configuredCFClassName) && !swapping) {
123                 // we need to swap this instance, but other calls should not try to do it until we're done
124                 swapping = true;
125 
126                 // whichever ClassFactory is registered will be instantiated with the initial ClassFactory.
127                 try {
128                     final Class<ClassFactory> c = initial.forName(configuredCFClassName);
129                     current = initial.newInstance(c);
130                 } catch (ClassNotFoundException e) {
131                     log.error("Could not find {}, will keep on using {} for now", configuredCFClassName, current.getClass().getSimpleName());
132                 }
133                 swapping = false;
134             }
135         }
136 
137         protected String getCurrentlyConfiguredClassName() {
138             return SystemProperty.getProperty(ClassFactory.class.getName());
139         }
140     }
141 
142 }