View Javadoc
1 /* 2 * Engine.java 3 * Created on July 19, 2003 4 * 5 * The Blues Framework - A lightweight application framework 6 * Copyright (C) 2003 Lonnie Pryor 7 * http://blues.lonniepryor.com 8 * 9 * This library is free software; you can redistribute it and/or modify it under the 10 * terms of the GNU Lesser General Public License as published by the Free Software 11 * Foundation; either version 2.1 of the License, or (at your option) any later 12 * version. 13 * 14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 16 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License along 19 * with this library; if not, write to: 20 * 21 * The Free Software Foundation, Inc. 22 * 59 Temple Place, Suite 330 23 * Boston, MA 02111-1307 USA 24 * 25 */ 26 package com.lonniepryor.blues; 27 28 import java.util.Collections; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 import com.lonniepryor.blues.cfg.ApplicationCfg; 33 import com.lonniepryor.blues.sys.Application; 34 import com.lonniepryor.blues.sys.Registry; 35 import com.lonniepryor.blues.xml.XmlParser; 36 37 /*** 38 * Creates, links, and publishes the contents of a single Blues application. 39 * 40 * @author Lonnie Pryor 41 * @version $Revision: 1.1 $ 42 */ 43 public final class Engine { 44 /*** The singletons registered in this engine's application. */ 45 private final Map singletons; 46 /*** The services registered in this engine's application. */ 47 private final Map services; 48 /*** The dispatchers registered in this engine's application. */ 49 private final Map dispatchers; 50 /*** The instances of Lifecycle registered in this engine's application. */ 51 private final Lifecycle[] lifecycles; 52 /*** True if this engine has been disposed. */ 53 private boolean disposed = true; 54 55 /*** 56 * Creates a new empty Engine object. 57 */ 58 public Engine () { 59 singletons = Collections.EMPTY_MAP; 60 services = Collections.EMPTY_MAP; 61 dispatchers = Collections.EMPTY_MAP; 62 lifecycles = new Lifecycle[] { }; 63 } 64 65 /*** 66 * Creates a new Engine object. 67 * 68 * @param classLoader The class loader to load the application on. 69 * @param configPath The path to a XML application config file. 70 */ 71 public Engine (ClassLoader classLoader, String configPath) { 72 this(classLoader, new XmlParser(classLoader).parseApplication(configPath)); 73 } 74 75 /*** 76 * Creates a new Engine object. 77 * 78 * @param classLoader The class loader to load the application on. 79 * @param configuration The application configuration to use. 80 */ 81 public Engine (ClassLoader classLoader, ApplicationCfg configuration) { 82 Registry reg = new Application(classLoader, configuration) 83 .getAllRegisteredObjects(); 84 this.singletons = Collections.unmodifiableMap(new HashMap(reg.getSingletons())); 85 this.services = Collections.unmodifiableMap(new HashMap(reg.getServices())); 86 this.dispatchers = Collections.unmodifiableMap( 87 new HashMap(reg.getDispatchers())); 88 this.lifecycles = (Lifecycle[])reg.getLifecycles().toArray( 89 new Lifecycle[reg.getLifecycles().size()]); 90 if (lifecycles.length > 0) { 91 Session session = new Session(this); 92 try { 93 initalize(); 94 } finally { 95 session.dispose(); 96 } 97 } 98 disposed = false; 99 } 100 101 /*** 102 * Disposes this engine and all of it's contents. 103 */ 104 public void dispose () { 105 if (disposed) 106 return; 107 try { 108 if (lifecycles.length > 0) { 109 Session session = new Session(this); 110 try { 111 dispose(lifecycles.length); 112 } finally { 113 session.dispose(); 114 } 115 } 116 } finally { 117 disposed = true; 118 } 119 } 120 121 /*** 122 * Returns the requested singleton instance or null if one is not found. 123 * 124 * @param singletonType The type of the singleton to return. 125 * 126 * @return The requested singleton instance or null if one is not found. 127 */ 128 Object getSingletonInstance (Class singletonType) { 129 return disposed ? null : singletons.get(singletonType); 130 } 131 132 /*** 133 * Returns the requested service instance or null if one is not found. 134 * 135 * @param serviceName The name of the service to return. 136 * 137 * @return The requested service instance or null if one is not found. 138 */ 139 Object getServiceInstance (String serviceName) { 140 return disposed ? null : services.get(serviceName); 141 } 142 143 /*** 144 * Returns the requested dispatcher instance or null if one is not found. 145 * 146 * @param dispatcherType The type of dispatcher to return. 147 * 148 * @return The requested dispatcher instance or null if one is not found. 149 */ 150 Object getDispatcherInstance (Class dispatcherType) { 151 return disposed ? null : dispatchers.get(dispatcherType); 152 } 153 154 /*** 155 * Invokes initalize() on every Lifecycle instance in the application, disposing 156 * those it's created if one fails. 157 */ 158 private void initalize () { 159 int current = 0; 160 try { 161 for (; current < lifecycles.length; ++current) 162 lifecycles[current].initalize(); 163 } catch (RuntimeException re) { 164 dispose(current); 165 throw re; 166 } catch (Error e) { 167 dispose(current); 168 throw e; 169 } 170 } 171 172 /*** 173 * Invokes dispose() on the Lifecycle instance at the specified index in the 174 * application, and every preceeding instance. 175 * 176 * @param fromIndex One past thes last index of the Lifecycle instance to dispose. 177 */ 178 private void dispose (int fromIndex) { 179 if (fromIndex == 0) 180 return; 181 try { 182 lifecycles[fromIndex - 1].dispose(); 183 } finally { 184 dispose(fromIndex - 1); 185 } 186 } 187 }

This page was automatically generated by Maven