1 10 11 package org.mule.registry.impl; 12 13 import org.mule.registry.ClassLoaderFactory; 14 import org.mule.registry.ComponentType; 15 import org.mule.registry.Library; 16 import org.mule.registry.Registry; 17 import org.mule.registry.RegistryComponent; 18 import org.mule.registry.RegistryDescriptor; 19 import org.mule.registry.RegistryException; 20 import org.mule.registry.Unit; 21 22 import javax.management.ObjectName ; 23 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 36 public abstract class AbstractRegistryComponent extends AbstractEntry implements RegistryComponent 37 { 38 39 protected ComponentType type; 40 protected String name; 41 protected transient ObjectName objectName; 42 protected List units; 43 protected List libraries; 44 protected String workspaceRoot; 45 protected List classPathElements; 46 protected String componentClassName; 47 protected boolean isClassLoaderParentFirst; 48 protected boolean isTransient; 49 protected Object component; 50 protected RegistryDescriptor descriptor; 51 52 protected AbstractRegistryComponent(String name, ComponentType type, Registry registry) 53 { 54 super(registry); 55 this.type = type; 56 this.name = name; 57 this.units = new ArrayList (); 58 this.libraries = new ArrayList (); 59 } 60 61 public ComponentType getType() 62 { 63 return type; 64 } 65 66 protected void readObject(java.io.ObjectInputStream in) throws IOException , ClassNotFoundException 67 { 68 super.readObject(in); 69 in.defaultReadObject(); 70 } 71 72 77 public Library[] getLibraries() 78 { 79 Collection c = new ArrayList (); 80 for (Iterator it = this.libraries.iterator(); it.hasNext();) 81 { 82 String ref = (String )it.next(); 83 Library library = getRegistry().getLibrary(ref); 84 c.add(library); 85 } 86 return (Library[])c.toArray(new Library[c.size()]); 87 } 88 89 94 public Unit[] getUnits() 95 { 96 Collection c = new ArrayList (); 97 for (Iterator it = this.units.iterator(); it.hasNext();) 98 { 99 String ref = (String )it.next(); 100 String [] refs = ref.split("/"); 101 if (refs.length != 2) 102 { 103 throw new IllegalStateException ("Malformed unit ref"); 104 } 105 Unit unit = getRegistry().getAssembly(refs[0]).getUnit(refs[1]); 106 c.add(unit); 107 } 108 return (Unit[])c.toArray(new Unit[c.size()]); 109 } 110 111 public void addUnit(Unit unit) 112 { 113 this.units.add(unit.getAssembly().getName() + "/" + unit.getName()); 114 } 115 116 public void removeUnit(Unit unit) 117 { 118 this.units.remove(unit.getAssembly().getName() + "/" + unit.getName()); 119 } 120 121 126 public ObjectName getObjectName() 127 { 128 return this.objectName; 129 } 130 131 136 protected void checkDescriptor() throws RegistryException 137 { 138 super.checkDescriptor(); 139 if (!getDescriptor().isComponent()) 141 { 142 throw new RegistryException("component should be set"); 143 } 144 } 145 146 protected void createComponent() throws RegistryException 147 { 148 try 149 { 150 ClassLoader loader = ClassLoaderFactory.getInstance().createComponentClassLoader(this); 151 Class cl = Class.forName(this.componentClassName, true, loader); 152 this.component = cl.newInstance(); 153 } 154 catch (Exception e) 155 { 156 throw new RegistryException(e); 157 } 158 } 159 160 165 public synchronized void install() throws RegistryException 166 { 167 if (!getCurrentState().equals(UNKNOWN)) 168 { 169 throw new RegistryException("Illegal status: " + getCurrentState()); 170 } 171 if (isTransient) 172 { 173 return; 174 } 175 176 try 177 { 178 doInstall(); 179 } 180 catch (Exception e) 181 { 182 throw new RegistryException(e); 183 } 184 185 createComponent(); 187 try 188 { 189 objectName = initComponent(); 190 } 191 catch (Exception e) 192 { 193 throw new RegistryException(e); 194 } 195 } 196 197 protected abstract void doInstall() throws Exception ; 198 199 204 public final synchronized void restoreState() throws RegistryException 205 { 206 if (!getCurrentState().equals(UNKNOWN) && !getCurrentState().equals(INITIALIZED)) 207 { 208 throw new RegistryException("Illegal status: " + getCurrentState()); 209 } 210 try 211 { 212 if (!isTransient) 213 { 214 createComponent(); 215 initComponent(); 216 } 217 if (getStateAtShutdown().equals(RUNNING)) 218 { 219 start(); 220 } 221 doRestoreState(); 222 } 223 catch (Exception e) 224 { 225 throw new RegistryException(e); 226 } 227 } 228 229 protected abstract void doRestoreState() throws Exception ; 230 231 236 public synchronized void saveAndShutdown() throws RegistryException 237 { 238 setStateAtShutdown(getCurrentState()); 239 Unit[] units = getUnits(); 240 for (int i = 0; i < units.length; i++) 241 { 242 units[i].setStateAtShutdown(units[i].getCurrentState()); 243 } 244 shutDown(); 245 } 246 247 252 public final synchronized void start() throws RegistryException 253 { 254 if (getCurrentState().equals(UNKNOWN)) 255 { 256 throw new RegistryException("Illegal status: " + getCurrentState()); 257 } 258 if (getCurrentState().equals(RUNNING)) 259 { 260 return; 261 } 262 try 263 { 264 doStart(); 265 } 266 catch (Exception e) 267 { 268 throw new RegistryException(e); 269 } 270 setCurrentState(RUNNING); 271 } 272 273 protected abstract void doStart() throws Exception ; 274 275 280 public final synchronized void stop() throws RegistryException 281 { 282 if (getCurrentState().equals(UNKNOWN) || getCurrentState().equals(SHUTDOWN)) 283 { 284 throw new RegistryException("Illegal status: " + getCurrentState()); 285 } 286 if (getCurrentState().equals(STOPPED)) 287 { 288 return; 289 } 290 try 291 { 292 doStop(); 293 } 294 catch (Exception e) 295 { 296 throw new RegistryException(e); 297 } 298 setCurrentState(STOPPED); 299 } 300 301 protected abstract void doStop() throws Exception ; 302 303 308 public final synchronized void shutDown() throws RegistryException 309 { 310 if (getCurrentState().equals(UNKNOWN)) 311 { 312 throw new RegistryException("Illegal status: " + getCurrentState()); 313 } 314 if (getCurrentState().equals(SHUTDOWN)) 315 { 316 return; 317 } 318 stop(); 319 try 321 { 322 doShutDown(); 323 } 324 catch (Exception e) 325 { 326 throw new RegistryException(e); 327 } 328 setCurrentState(SHUTDOWN); 329 } 330 331 protected abstract void doShutDown() throws Exception ; 332 333 338 public synchronized void uninstall() throws RegistryException 339 { 340 if (!getCurrentState().equals(SHUTDOWN) && !getCurrentState().equals(UNKNOWN)) 341 { 342 throw new RegistryException("Illegal status: " + getCurrentState()); 343 } 344 if (this.units.size() > 0) 345 { 346 throw new RegistryException("Component has service units deployed"); 347 } 348 Library[] libraries = getLibraries(); 349 for (int i = 0; i < libraries.length; i++) 350 { 351 libraries[i].removeComponent(this); 352 } 353 registry.getManagementContext().deleteDir(getInstallRoot()); 355 registry.getManagementContext().deleteDir(getWorkspaceRoot()); 356 getRegistry().removeComponent(this); 358 setCurrentState(UNKNOWN); 359 } 360 361 366 public String getWorkspaceRoot() 367 { 368 return this.workspaceRoot; 369 } 370 371 376 public void setWorkspaceRoot(String workspaceRoot) 377 { 378 this.workspaceRoot = workspaceRoot; 379 } 380 381 386 public List getClassPathElements() 387 { 388 return this.classPathElements; 389 } 390 391 396 public void setClassPathElements(List classPathElements) 397 { 398 this.classPathElements = classPathElements; 399 } 400 401 406 public boolean isClassLoaderParentFirst() 407 { 408 return this.isClassLoaderParentFirst; 409 } 410 411 public void setComponent(Object component) 412 { 413 this.component = component; 414 } 415 416 public boolean isTransient() 417 { 418 return isTransient; 419 } 420 421 public void setTransient(boolean isTransient) 422 { 423 this.isTransient = isTransient; 424 } 425 426 431 public Object getComponent() 432 { 433 return component; 434 } 435 436 441 public RegistryDescriptor getDescriptor() throws RegistryException 442 { 443 return descriptor; 444 } 445 446 public void setDescriptor(RegistryDescriptor descriptor) throws RegistryException 447 { 448 this.descriptor = descriptor; 449 } 450 } 451 | Popular Tags |