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 ; 21 22 23 33 public class SotoApplicationFactory extends CompositeObjectFactoryEx { 34 public static final String APP = "app"; 35 public static final String DEFS = "defs"; 36 public static final String NAMESPACE = "namespace"; 37 public static final String SERVICE = "service"; 38 public static final String SERVICE_REF = "serviceRef"; 39 public static final String INCLUDE = "include"; 40 public static final String IF = "if"; 41 public static final String CHOOSE = "choose"; 42 public static final String DEBUG = "debug"; 43 public static final String ENV = "env"; 44 private SotoContainer _container; 45 private TemplateContextIF _ctx; 46 private ReflectionFactory _fac = new ReflectionFactory(new String [0]); 47 48 51 public SotoApplicationFactory(SotoContainer container) { 52 _container = container; 53 } 54 55 61 void setContext(TemplateContextIF ctx) { 62 _ctx = ctx; 63 } 64 65 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 85 public CreationStatus newObjectFor(String arg0, String arg1, String arg2, 86 Object arg3) throws ObjectCreationException { 87 return setUp(doNewObjectFor(arg0, arg1, arg2, arg3)); 88 } 89 90 93 public void registerDefs(Namespace defs) throws ConfigurationException { 94 super.registerDefs(defs); 95 Debug.debug("===> Definitions for: " + defs.getPrefix()); 96 97 List 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 prefix, String uri, 107 String name, Object 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 |