View Javadoc

1   /*
2    * Copyright 2012 Oracle 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.groovy.context;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  import COM.FutureTense.Interfaces.ICS;
21  
22  import com.fatwire.gst.foundation.controller.action.Factory;
23  import com.fatwire.gst.foundation.controller.action.support.BaseFactory;
24  
25  /**
26   * 
27   * Factory that dynamically loads other classes that provide producer methods.
28   * 
29   * @author Dolf Dijkstra
30   * @since September 23, 2012
31   * 
32   */
33  public class GroovyFactory extends BaseFactory {
34  
35      private final ClassLoader classLoader;
36      private Class<?> generalFactoryClass;
37  
38      public GroovyFactory(ICS ics, ClassLoader gcl, Factory... roots) {
39          super(ics, roots);
40          this.classLoader = gcl;
41          try {
42              this.generalFactoryClass = classLoader.loadClass("gsf.ObjectFactory");
43          } catch (ClassNotFoundException e) {
44              // ignore
45              this.generalFactoryClass = null;
46          }
47  
48      }
49  
50      @Override
51      protected Class<?>[] factoryClasses(ICS ics) {
52          String site = ics.GetVar("site");
53          Class<?> siteClass = null;
54  
55          if (StringUtils.isNotBlank(site)) {
56              try {
57                  siteClass = classLoader.loadClass("gsf." + site.toLowerCase() + ".ObjectFactory");
58              } catch (ClassNotFoundException e) {
59                  // ignore
60              }
61          }
62          if (siteClass == null) {
63              if (generalFactoryClass == null) {
64                  return new Class[0];
65              } else {
66                  return new Class[] { generalFactoryClass };
67              }
68          } else {
69              if (generalFactoryClass == null) {
70                  return new Class[] { siteClass };
71              } else {
72                  return new Class[] { siteClass, generalFactoryClass };
73              }
74  
75          }
76      }
77  }