KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > SotoApplicationFactory


1 package org.sapia.soto;
2
3 import org.sapia.soto.config.Application;
4 import org.sapia.soto.config.Choose;
5 import org.sapia.soto.config.If;
6 import org.sapia.soto.config.Include;
7 import org.sapia.soto.config.ServiceRef;
8 import org.sapia.soto.config.ServiceTag;
9 import org.sapia.soto.util.CompositeObjectFactoryEx;
10 import org.sapia.soto.util.Def;
11 import org.sapia.soto.util.Namespace;
12
13 import org.sapia.util.text.SystemContext;
14 import org.sapia.util.text.TemplateContextIF;
15 import org.sapia.util.xml.confix.ConfigurationException;
16 import org.sapia.util.xml.confix.CreationStatus;
17 import org.sapia.util.xml.confix.ObjectCreationException;
18 import org.sapia.util.xml.confix.ReflectionFactory;
19
20 import java.util.List JavaDoc;
21
22
23 /**
24  * This factory creates the objects that belong to the "soto" namespace.
25  *
26  * @author Yanick Duchesne
27  * <dl>
28  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
29  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
30  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
31  * </dl>
32  */

33 public class SotoApplicationFactory extends CompositeObjectFactoryEx {
34   public static final String JavaDoc APP = "app";
35   public static final String JavaDoc DEFS = "defs";
36   public static final String JavaDoc NAMESPACE = "namespace";
37   public static final String JavaDoc SERVICE = "service";
38   public static final String JavaDoc SERVICE_REF = "serviceRef";
39   public static final String JavaDoc INCLUDE = "include";
40   public static final String JavaDoc IF = "if";
41   public static final String JavaDoc CHOOSE = "choose";
42   public static final String JavaDoc DEBUG = "debug";
43     public static final String JavaDoc ENV = "env";
44   private SotoContainer _container;
45   private TemplateContextIF _ctx;
46   private ReflectionFactory _fac = new ReflectionFactory(new String JavaDoc[0]);
47
48   /**
49    * Constructor for SotoApplicationFactory.
50    */

51   public SotoApplicationFactory(SotoContainer container) {
52     _container = container;
53   }
54
55   /**
56    * Sets this instance's template context - holding name/value
57    * binding.
58    *
59    * @param ctx a <code>TemplateContextIF</code>.
60    */

61   void setContext(TemplateContextIF ctx) {
62     _ctx = ctx;
63   }
64
65   /**
66    * Adds the given namespace to this instance.
67    *
68    * @param defs a <code>Namespace</code>.
69    */

70   public void addNamespace(Namespace defs) throws ConfigurationException {
71     registerDefs(defs);
72   }
73
74   public TemplateContextIF getTemplateContext() {
75     if (_ctx == null) {
76       return new SystemContext();
77     }
78
79     return _ctx;
80   }
81
82   /**
83    * @see org.sapia.util.xml.confix.CompositeObjectFactory#newObjectFor(java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
84    */

85   public CreationStatus newObjectFor(String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2,
86     Object JavaDoc arg3) throws ObjectCreationException {
87     return setUp(doNewObjectFor(arg0, arg1, arg2, arg3));
88   }
89
90   /**
91    * @see org.sapia.soto.util.CompositeObjectFactoryEx#registerDefs(org.sapia.soto.util.Namespace)
92    */

93   public void registerDefs(Namespace defs) throws ConfigurationException {
94     super.registerDefs(defs);
95     Debug.debug("===> Definitions for: " + defs.getPrefix());
96
97     List JavaDoc defList = defs.getDefs();
98     Def def;
99
100     for (int i = 0; i < defList.size(); i++) {
101       def = (Def) defList.get(i);
102       Debug.debug(" name: " + def.getName() + ", " + def.getClazz());
103     }
104   }
105
106   protected CreationStatus doNewObjectFor(String JavaDoc prefix, String JavaDoc uri,
107     String JavaDoc name, Object JavaDoc parent) throws ObjectCreationException {
108     CreationStatus toReturn;
109
110     if ((prefix != null) && (prefix.length() > 0)) {
111       if (prefix.equals("soto")) {
112         if (name.equals(SERVICE)) {
113           toReturn = CreationStatus.create(new ServiceTag(_container,
114                 _container.getServiceList()));
115         } else if (name.equals(SERVICE_REF)) {
116           toReturn = CreationStatus.create(new ServiceRef(_container));
117         } else if (name.equals(NAMESPACE)) {
118           toReturn = CreationStatus.create(new Namespace(this));
119         } else if (name.equals(APP) || name.equals(DEFS)) {
120           toReturn = CreationStatus.create(new Application(_container, this));
121         } else if (name.equals(INCLUDE)) {
122           toReturn = CreationStatus.create(new Include(_container, this));
123         } else if (name.equals(IF)) {
124           toReturn = CreationStatus.create(new If(_ctx, this));
125         } else if (name.equals(CHOOSE)) {
126           toReturn = CreationStatus.create(new Choose(_ctx, this));
127         } else if (name.equals(DEBUG)) {
128           toReturn = CreationStatus.create(new org.sapia.soto.config.Debug());
129                 } else if (name.equals(ENV)) {
130                     toReturn = CreationStatus.create(_container.toEnv());
131         } else {
132           toReturn = super.newObjectFor(prefix, uri, name, parent);
133         }
134       } else {
135         toReturn = super.newObjectFor(prefix, uri, name, parent);
136       }
137     } else {
138       toReturn = _fac.newObjectFor(prefix, uri, name, parent);
139     }
140
141     return toReturn;
142   }
143
144   private CreationStatus setUp(CreationStatus instance) {
145     if (instance.getCreated() instanceof EnvAware) {
146       ((EnvAware) instance.getCreated()).setEnv(_container.toEnv());
147     }
148
149     return instance;
150   }
151 }
152
Popular Tags