View Javadoc

1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.controller.action.support;
17  
18  import java.lang.reflect.Constructor;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.commons.logging.Log;
22  
23  import COM.FutureTense.Interfaces.ICS;
24  
25  import com.fatwire.gst.foundation.controller.action.Factory;
26  import com.fatwire.gst.foundation.controller.action.FactoryProducer;
27  import com.fatwire.gst.foundation.facade.logging.LogUtil;
28  
29  /**
30   * Producer for a {@link Factory} that makes use of reflection to construct the
31   * Factory.
32   * 
33   * @author Dolf.Dijkstra
34   * 
35   */
36  public class ReflectionFactoryProducer implements FactoryProducer {
37      protected static final Log LOG = LogUtil.getLog(ReflectionFactoryProducer.class);
38      private Constructor<Factory> constructor;
39  
40      public Factory getFactory(final ICS ics) {
41          Factory factory = null;
42          try {
43              factory = getInjectionFactory(ics);
44          } catch (final Exception e) {
45              LOG.warn(e);
46          }
47          if (factory == null) {
48              factory = new IcsBackedObjectFactoryTemplate(ics);
49          }
50          return factory;
51      }
52  
53      public final Factory getInjectionFactory(final ICS ics) throws Exception {
54          Factory factory = null;
55          if (constructor != null) {
56              factory = constructor.newInstance(ics);
57          }
58          return factory;
59      }
60  
61      @SuppressWarnings("unchecked")
62      private void findConstructor(final String factoryClassname) throws ClassNotFoundException, NoSuchMethodException,
63              SecurityException {
64          if (StringUtils.isNotBlank(factoryClassname)) {
65              final Class<Factory> c = (Class<Factory>) Class.forName(factoryClassname);
66              constructor = c.getConstructor(ICS.class);
67          } else {
68              throw new IllegalArgumentException("factoryClassname cannot be blank.");
69          }
70      }
71  
72      /**
73       * @param factoryClassname the factoryClassname to set
74       */
75      public void setFactoryClassname(final String factoryClassname) {
76          try {
77              findConstructor(factoryClassname);
78          } catch (final RuntimeException e) {
79              throw e;
80          } catch (final Exception e) {
81              throw new IllegalArgumentException("factoryClassname: " + factoryClassname + " is illegal. "
82                      + e.getMessage(), e);
83          }
84      }
85  }