1 package org.sapia.soto; 2 3 import org.sapia.soto.config.Include; 4 import org.sapia.soto.config.ServiceTag; 5 import org.sapia.soto.util.ClasspathResourceHandler; 6 import org.sapia.soto.util.FileResourceHandler; 7 import org.sapia.soto.util.Resource; 8 import org.sapia.soto.util.ResourceHandler; 9 import org.sapia.soto.util.ResourceHandlerChain; 10 import org.sapia.soto.util.UrlResourceHandler; 11 import org.sapia.soto.util.Utils; 12 13 import org.sapia.util.text.MapContext; 14 import org.sapia.util.text.SystemContext; 15 import org.sapia.util.text.TemplateContextIF; 16 import org.sapia.util.xml.confix.Dom4jProcessor; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 22 import java.net.URL ; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 30 40 public class SotoContainer implements Consts { 41 42 50 public static final String SOTO_ENV_KEY = "SOTO_ENV"; 51 52 private ResourceHandlerChain _resourceHandlers = new ResourceHandlerChain(); 53 private Map _services = new HashMap (); 54 private List _toStart = new ArrayList (); 55 private SotoApplicationFactory _fac; 56 private boolean _started; 57 private boolean _resourceLoaded; 58 private Env _env; 59 60 63 public SotoContainer() { 64 _fac = new SotoApplicationFactory(this); 65 _resourceHandlers.append(new FileResourceHandler()); 66 _resourceHandlers.append(new ClasspathResourceHandler()); 67 _resourceHandlers.append(new UrlResourceHandler()); 68 _env = new EnvImpl(this); 69 } 70 71 76 List getServiceList() { 77 return _toStart; 78 } 79 80 88 public void bind(ServiceMetaData meta) throws DuplicateException, Exception { 89 if (meta.getServiceID() == null) { 90 throw new IllegalArgumentException ("No identifier provided for service: " + 91 meta.getService().getClass().getName()); 92 } 93 94 if (_services.get(meta.getServiceID()) != null) { 95 throw new DuplicateException("A service already exists for ID: " + 96 meta.getServiceID()); 97 } 98 99 _services.put(meta.getServiceID(), meta); 100 } 101 102 108 public Object lookup(String id) throws NotFoundException { 109 ServiceMetaData service = (ServiceMetaData) _services.get(id); 111 112 if (service == null) { 113 throw new NotFoundException(id); 114 } 115 116 return service.getService(); 117 } 118 119 127 public SotoContainer load(InputStream is, Map vars) throws Exception { 128 if (vars == null) { 129 vars = new HashMap (); 130 } 131 132 TemplateContextIF ctx = new MapContext(vars, new SystemContext(), true); 133 134 if (_fac == null) { 135 _fac = new SotoApplicationFactory(this); 136 } 137 138 _fac.setContext(ctx); 139 140 Dom4jProcessor proc = new Dom4jProcessor(_fac); 141 142 if (!_resourceLoaded) { 143 InputStream resources = getResourceAsStream("soto_defs.xml"); 144 145 if (resources == null) { 146 } else { 147 proc.process(resources); 148 } 149 150 _resourceLoaded = true; 151 } 152 153 proc.process(Utils.replaceVars(ctx, is, "null")); 154 155 return this; 156 } 157 158 164 public SotoContainer load(InputStream is) throws Exception { 165 return load(is, null); 166 } 167 168 177 public SotoContainer load(String resourceName, Map vars) 178 throws Exception { 179 return include("resource:/" + resourceName, vars); 180 } 181 182 189 public SotoContainer load(String resourceName) throws Exception { 190 return load(resourceName, null); 191 } 192 193 201 public SotoContainer load(File f, Map map) throws Exception { 202 String path = f.getAbsolutePath().trim(); 203 204 if (path.charAt(0) == '/') { 205 return include("file:" + f.getAbsolutePath(), map); 206 } else { 207 return include("file:/" + f.getAbsolutePath(), map); 208 } 209 } 210 211 219 public SotoContainer load(URL url, Map map) throws Exception { 220 return include(url.toExternalForm(), map); 221 } 222 223 229 public SotoContainer load(File f) throws Exception { 230 return load(f, null); 231 } 232 233 238 public void start() throws Exception { 239 ServiceTag svc; 240 ServiceMetaData meta; 241 List services = new ArrayList (_toStart.size()); 242 243 for (int i = 0; i < _toStart.size(); i++) { 244 svc = (ServiceTag) _toStart.get(i); 245 services.add(meta = svc.getServiceMetaData()); 246 meta.start(); 247 } 248 249 _started = true; 250 _toStart.clear(); 251 _toStart = services; 252 } 253 254 260 public void dispose() { 261 for (int i = _toStart.size() - 1; i >= 0; i--) { 262 try{ 263 ((ServiceMetaData) _toStart.get(i)).dispose(); 264 }catch(ClassCastException e){ 265 if(_toStart.get(i) instanceof ServiceTag){ 268 _toStart.remove(i--); 269 } 270 } 271 } 272 } 273 274 281 public ResourceHandlerChain getResourceHandlers(){ 282 return _resourceHandlers; 283 } 284 285 290 public ResourceHandler getResourceHandlerFor(String uri) { 291 ResourceHandler handler = _resourceHandlers.select(uri); 292 293 if (handler == null) { 294 throw new IllegalArgumentException ("No resource handler defined for: " + 295 uri); 296 } 297 298 return handler; 299 } 300 301 305 public SotoApplicationFactory getApplicationFactory() { 306 return _fac; 307 } 308 309 private void checkStarted() { 310 if (!_started) { 311 throw new IllegalStateException ( 312 "Soto container must be started prior to operation being performed"); 313 } 314 } 315 316 private SotoContainer include(String uri, Map vars) throws Exception { 317 TemplateContextIF ctx; 318 319 if (vars == null) { 320 vars = new HashMap (); 321 } 322 323 ctx = new MapContext(vars, new SystemContext(), true); 324 325 if (_fac == null) { 326 _fac = new SotoApplicationFactory(this); 327 } 328 329 _fac.setContext(ctx); 330 331 Dom4jProcessor proc = new Dom4jProcessor(_fac); 332 333 if (!_resourceLoaded) { 334 InputStream resources = getResourceAsStream("soto_defs.xml"); 335 336 if (resources == null) { 337 } else { 338 proc.process(resources); 339 } 340 341 _resourceLoaded = true; 342 } 343 344 Include include = new Include(this, _fac); 345 include.setUri(uri); 346 include.onCreate(); 347 348 return this; 349 } 350 351 private InputStream getResourceAsStream(String resourceName) { 352 InputStream resource = getClass().getResourceAsStream(resourceName); 353 354 if (resource == null) { 355 resource = Thread.currentThread().getContextClassLoader() 356 .getResourceAsStream(resourceName); 357 } 358 359 if (resource == null) { 360 resource = ClassLoader.getSystemResourceAsStream(resourceName); 361 } 362 363 return resource; 364 } 365 366 369 public Env toEnv() { 370 return _env; 371 } 372 373 public static final class EnvImpl implements Env { 375 SotoContainer _cont; 376 377 EnvImpl(SotoContainer cont) { 378 _cont = cont; 379 } 380 381 384 public ResourceHandler getResourceHandlerFor(String protocol) { 385 return _cont.getResourceHandlerFor(protocol); 386 } 387 388 391 public Object lookup(String serviceId) throws NotFoundException { 392 return _cont.lookup(serviceId); 393 } 394 395 398 public ResourceHandlerChain getResourceHandlers() { 399 return _cont.getResourceHandlers(); 400 } 401 402 405 public InputStream resolveStream(String path) throws IOException { 406 return _cont.getResourceHandlerFor(path).getResource(path); 407 } 408 409 412 public Resource resolveResource(String path) throws IOException { 413 return _cont.getResourceHandlerFor(path).getResourceObject(path); 414 } 415 416 public SotoContainer getContainer() { 417 return _cont; 418 } 419 } 420 } 421 | Popular Tags |