1 44 45 package org.jfree.base; 46 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.lang.reflect.Method ; 50 import java.net.URL ; 51 import java.util.ArrayList ; 52 import java.util.Enumeration ; 53 54 import org.jfree.base.config.HierarchicalConfiguration; 55 import org.jfree.base.config.PropertyFileConfiguration; 56 import org.jfree.base.config.SystemPropertyConfiguration; 57 import org.jfree.base.modules.PackageManager; 58 import org.jfree.base.modules.SubSystem; 59 import org.jfree.util.Configuration; 60 import org.jfree.util.ExtendedConfiguration; 61 import org.jfree.util.ExtendedConfigurationWrapper; 62 import org.jfree.util.Log; 63 import org.jfree.util.ObjectUtilities; 64 65 79 public abstract class AbstractBoot implements SubSystem { 80 81 82 private ExtendedConfigurationWrapper extWrapper; 83 84 85 private PackageManager packageManager; 86 87 88 private Configuration globalConfig; 89 90 91 private boolean bootInProgress; 92 93 94 private boolean bootDone; 95 96 99 protected AbstractBoot() { 100 } 101 102 107 public synchronized PackageManager getPackageManager() { 108 if (this.packageManager == null) { 109 this.packageManager = PackageManager.createInstance(this); 110 } 111 return this.packageManager; 112 } 113 114 119 public synchronized Configuration getGlobalConfig() { 120 if (this.globalConfig == null) { 121 this.globalConfig = loadConfiguration(); 122 } 123 return this.globalConfig; 124 } 125 126 131 public final synchronized boolean isBootInProgress() { 132 return this.bootInProgress; 133 } 134 135 140 public final synchronized boolean isBootDone() { 141 return this.bootDone; 142 } 143 144 149 protected abstract Configuration loadConfiguration(); 150 151 154 public final void start() { 155 156 synchronized (this) { 157 if (isBootDone()) { 158 return; 159 } 160 while (isBootInProgress()) { 161 try { 162 wait(); 163 } 164 catch (InterruptedException e) { 165 } 167 } 168 if (isBootDone()) { 169 return; 170 } 171 this.bootInProgress = true; 172 } 173 174 final BootableProjectInfo info = getProjectInfo(); 176 if (info != null) { 177 final BootableProjectInfo[] childs = info.getDependencies(); 178 for (int i = 0; i < childs.length; i++) { 179 final AbstractBoot boot = loadBooter(childs[i].getBootClass()); 180 if (boot != null) { 181 synchronized(boot) { 183 boot.start(); 184 while (boot.isBootDone() == false) { 185 try { 186 boot.wait(); 187 } 188 catch (InterruptedException e) { 189 } 191 } 192 } 193 } 194 } 195 } 196 197 performBoot(); 198 if (info != null) 199 { 200 Log.info (info.getName() + " " + info.getVersion() + " started."); 201 } 202 else 203 { 204 Log.info (getClass() + " started."); 205 } 206 207 synchronized (this) { 208 this.bootInProgress = false; 209 this.bootDone = true; 210 notifyAll(); 211 } 212 } 213 214 217 protected abstract void performBoot(); 218 219 224 protected abstract BootableProjectInfo getProjectInfo(); 225 226 233 protected AbstractBoot loadBooter(final String classname) { 234 if (classname == null) { 235 return null; 236 } 237 try { 238 final Class c = ObjectUtilities.getClassLoader( 239 getClass()).loadClass(classname); 240 final Method m = c.getMethod("getInstance", (Class []) null); 241 return (AbstractBoot) m.invoke(null, (Object []) null); 242 } 243 catch (Exception e) { 244 Log.info ("Unable to boot dependent class: " + classname); 245 return null; 246 } 247 } 248 249 265 protected Configuration createDefaultHierarchicalConfiguration 266 (final String staticConfig, final String userConfig, 267 final boolean addSysProps) 268 { 269 final HierarchicalConfiguration globalConfig 270 = new HierarchicalConfiguration(); 271 272 if (staticConfig != null) { 273 final PropertyFileConfiguration rootProperty 274 = new PropertyFileConfiguration(); 275 rootProperty.load(staticConfig); 276 globalConfig.insertConfiguration(rootProperty); 277 globalConfig.insertConfiguration( 278 getPackageManager().getPackageConfiguration()); 279 } 280 if (userConfig != null) { 281 String userConfigStripped; 282 if (userConfig.startsWith("/")) { 283 userConfigStripped = userConfig.substring(1); 284 } 285 else { 286 userConfigStripped = userConfig; 287 } 288 try { 289 final Enumeration userConfigs = ObjectUtilities.getClassLoader 290 (getClass()).getResources(userConfigStripped); 291 final ArrayList configs = new ArrayList (); 292 while (userConfigs.hasMoreElements()) { 293 final URL url = (URL ) userConfigs.nextElement(); 294 try { 295 final PropertyFileConfiguration baseProperty = 296 new PropertyFileConfiguration(); 297 final InputStream in = url.openStream(); 298 baseProperty.load(in); 299 in.close(); 300 configs.add(baseProperty); 301 } 302 catch(IOException ioe) { 303 Log.warn ("Failed to load the user configuration at " + url, ioe); 304 } 305 } 306 307 for (int i = configs.size() - 1; i >= 0; i--) { 308 final PropertyFileConfiguration baseProperty = 309 (PropertyFileConfiguration) configs.get(i); 310 globalConfig.insertConfiguration(baseProperty); 311 } 312 } 313 catch (IOException e) { 314 Log.warn ("Failed to lookup the user configurations.", e); 315 } 316 } 317 final SystemPropertyConfiguration systemConfig 318 = new SystemPropertyConfiguration(); 319 globalConfig.insertConfiguration(systemConfig); 320 return globalConfig; 321 } 322 323 328 public synchronized ExtendedConfiguration getExtendedConfig () 329 { 330 if (extWrapper == null) { 331 extWrapper = new ExtendedConfigurationWrapper(getGlobalConfig()); 332 } 333 return extWrapper; 334 } 335 } 336 | Popular Tags |