1 10 11 package org.mule.registry.impl; 12 13 import org.mule.registry.Assembly; 14 import org.mule.registry.Registry; 15 import org.mule.registry.RegistryDescriptor; 16 import org.mule.registry.RegistryException; 17 import org.mule.registry.Unit; 18 import org.mule.util.FileUtils; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 public abstract class AbstractAssembly extends AbstractEntry implements Assembly 26 { 27 28 protected List units; 29 protected boolean isTransient; 30 protected RegistryDescriptor descriptor; 31 32 protected AbstractAssembly(Registry registry) 33 { 34 super(registry); 35 this.units = new ArrayList (); 36 } 37 38 public Unit getUnit(String name) 39 { 40 for (Iterator it = this.units.iterator(); it.hasNext();) 41 { 42 AbstractUnit u = (AbstractUnit)it.next(); 43 if (u.getName().equals(name)) 44 { 45 return u; 46 } 47 } 48 return null; 49 } 50 51 public void addUnit(Unit unit) 52 { 53 this.units.add(unit); 54 } 55 56 public void removeUnit(Unit unit) 57 { 58 this.units.remove(unit); 59 } 60 61 66 public Unit[] getUnits() 67 { 68 Collection c = this.units; 69 return (Unit[])c.toArray(new Unit[c.size()]); 70 } 71 72 77 protected void checkDescriptor() throws RegistryException 78 { 79 super.checkDescriptor(); 80 if (!getDescriptor().isServiceAssembly()) 82 { 83 throw new RegistryException("service-assembly should be set"); 84 } 85 } 86 87 92 public synchronized String start() throws RegistryException 93 { 94 if (getCurrentState().equals(UNKNOWN)) 95 { 96 throw new RegistryException("Illegal status: " + getCurrentState()); 97 } 98 if (!getCurrentState().equals(RUNNING)) 99 { 100 Unit[] units = getUnits(); 101 for (int i = 0; i < units.length; i++) 102 { 103 units[i].start(); 104 } 105 setCurrentState(RUNNING); 106 } 107 return ""; 109 } 110 111 116 public synchronized String stop() throws RegistryException 117 { 118 if (getCurrentState().equals(UNKNOWN) || getCurrentState().equals(SHUTDOWN)) 119 { 120 throw new RegistryException("Illegal status: " + getCurrentState()); 121 } 122 if (!getCurrentState().equals(STOPPED)) 123 { 124 Unit[] units = getUnits(); 125 for (int i = 0; i < units.length; i++) 126 { 127 units[i].stop(); 128 } 129 setCurrentState(STOPPED); 130 } 131 return ""; 133 } 134 135 140 public synchronized String shutDown() throws RegistryException 141 { 142 if (getCurrentState().equals(UNKNOWN)) 143 { 144 throw new RegistryException("Illegal status: " + getCurrentState()); 145 } 146 if (!getCurrentState().equals(SHUTDOWN)) 147 { 148 stop(); 149 Unit[] units = getUnits(); 150 for (int i = 0; i < units.length; i++) 151 { 152 units[i].shutDown(); 153 } 154 setCurrentState(SHUTDOWN); 155 } 156 return ""; 158 } 159 160 165 public synchronized String undeploy() throws RegistryException 166 { 167 if (!getCurrentState().equals(SHUTDOWN) && !getCurrentState().equals(UNKNOWN)) 168 { 169 throw new RegistryException("Illegal status: " + getCurrentState()); 170 } 171 Unit[] units = getUnits(); 172 for (int i = 0; i < units.length; i++) 173 { 174 units[i].undeploy(); 175 } 177 FileUtils.deleteTree(FileUtils.newFile(getInstallRoot())); 178 getRegistry().removeAssembly(this); 179 setCurrentState(UNKNOWN); 180 return null; 182 } 183 184 189 public boolean isTransient() 190 { 191 return isTransient; 192 } 193 194 public void setTransient(boolean isTransient) 195 { 196 this.isTransient = isTransient; 197 } 198 199 204 public void restoreState() throws RegistryException 205 { 206 Unit[] units = getUnits(); 207 for (int i = 0; i < units.length; i++) 208 { 209 units[i].init(); 210 if (units[i].getStateAtShutdown().equals(Unit.RUNNING)) 211 { 212 units[i].start(); 213 } 214 else if (units[i].getStateAtShutdown().equals(Unit.SHUTDOWN)) 215 { 216 units[i].shutDown(); 217 } 218 } 219 } 220 221 226 public void saveAndShutdown() throws RegistryException 227 { 228 Unit[] units = getUnits(); 229 for (int i = 0; i < units.length; i++) 230 { 231 units[i].setStateAtShutdown(units[i].getCurrentState()); 232 units[i].shutDown(); 233 } 234 } 235 236 public void setDescriptor(RegistryDescriptor descriptor) 237 { 238 this.descriptor = descriptor; 239 } 240 241 } 242 | Popular Tags |