1 43 44 package org.jfree.base.modules; 45 46 import org.jfree.util.Log; 47 48 54 public class PackageState 55 { 56 57 public static final int STATE_NEW = 0; 58 59 60 public static final int STATE_CONFIGURED = 1; 61 62 63 public static final int STATE_INITIALIZED = 2; 64 65 66 public static final int STATE_ERROR = -2; 67 68 69 private final Module module; 70 71 private int state; 72 73 79 public PackageState(final Module module) 80 { 81 this (module, STATE_NEW); 82 } 83 84 91 public PackageState(final Module module, final int state) 92 { 93 if (module == null) 94 { 95 throw new NullPointerException ("Module must not be null."); 96 } 97 if (state != STATE_CONFIGURED && state != STATE_ERROR 98 && state != STATE_INITIALIZED && state != STATE_NEW) 99 { 100 throw new IllegalArgumentException ("State is not valid"); 101 } 102 this.module = module; 103 this.state = state; 104 } 105 106 114 public boolean configure(final SubSystem subSystem) 115 { 116 if (this.state == STATE_NEW) 117 { 118 try 119 { 120 this.module.configure(subSystem); 121 this.state = STATE_CONFIGURED; 122 return true; 123 } 124 catch (NoClassDefFoundError noClassDef) 125 { 126 Log.warn (new Log.SimpleMessage("Unable to load module classes for ", 127 this.module.getName(), ":", noClassDef.getMessage())); 128 this.state = STATE_ERROR; 129 } 130 catch (Exception e) 131 { 132 if (Log.isDebugEnabled()) 133 { 134 Log.warn("Unable to configure the module " + this.module.getName(), e); 136 } 137 else if (Log.isWarningEnabled()) 138 { 139 Log.warn("Unable to configure the module " + this.module.getName()); 140 } 141 this.state = STATE_ERROR; 142 } 143 } 144 return false; 145 } 146 147 152 public Module getModule() 153 { 154 return this.module; 155 } 156 157 163 public int getState() 164 { 165 return this.state; 166 } 167 168 178 public boolean initialize(final SubSystem subSystem) 179 { 180 if (this.state == STATE_CONFIGURED) 181 { 182 try 183 { 184 this.module.initialize(subSystem); 185 this.state = STATE_INITIALIZED; 186 return true; 187 } 188 catch (NoClassDefFoundError noClassDef) 189 { 190 Log.warn (new Log.SimpleMessage("Unable to load module classes for ", 191 this.module.getName(), ":", noClassDef.getMessage())); 192 this.state = STATE_ERROR; 193 } 194 catch (ModuleInitializeException me) 195 { 196 if (Log.isDebugEnabled()) 197 { 198 Log.warn("Unable to initialize the module " + this.module.getName(), me); 200 } 201 else if (Log.isWarningEnabled()) 202 { 203 Log.warn("Unable to initialize the module " + this.module.getName()); 204 } 205 this.state = STATE_ERROR; 206 } 207 catch (Exception e) 208 { 209 if (Log.isDebugEnabled()) 210 { 211 Log.warn("Unable to initialize the module " + this.module.getName(), e); 213 } 214 else if (Log.isWarningEnabled()) 215 { 216 Log.warn("Unable to initialize the module " + this.module.getName()); 217 } 218 this.state = STATE_ERROR; 219 } 220 } 221 return false; 222 } 223 224 232 public boolean equals(final Object o) 233 { 234 if (this == o) 235 { 236 return true; 237 } 238 if (!(o instanceof PackageState)) 239 { 240 return false; 241 } 242 243 final PackageState packageState = (PackageState) o; 244 245 if (!this.module.getModuleClass().equals(packageState.module.getModuleClass())) 246 { 247 return false; 248 } 249 250 return true; 251 } 252 253 259 public int hashCode() 260 { 261 return this.module.hashCode(); 262 } 263 } 264 | Popular Tags |