1 27 package ch.ethz.prose; 28 29 import java.lang.reflect.InvocationTargetException ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Modifier ; 33 import java.util.Arrays ; 34 import java.util.Enumeration ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Set ; 38 import java.util.TreeSet ; 39 import java.util.Vector ; 40 41 70 public 71 class RootComponent implements Component { 72 73 static List startupMethods = null; 74 static List teardownMethods = null; 75 public static boolean reverseOrder = false; 76 77 78 static boolean isStarted = false; 79 83 protected RootComponent( ) 84 { 85 86 } 87 88 89 99 public static synchronized void startup() throws SystemStartupException 100 { 101 if (isStarted) 102 return; 103 isStarted=true; 104 105 startupMethods = new Vector (); 106 teardownMethods = new Vector (); 107 108 initStartupTeardownMethods(); 110 111 doStartup(); 113 } 114 115 122 public synchronized static void teardown() throws SystemTeardownException 123 { 124 if (!isStarted) 125 return; 126 isStarted=false; 127 if (reverseOrder) 128 { 129 synchronized(teardownMethods) 130 { 131 List temp = new Vector (); 132 while (!(teardownMethods.isEmpty())) 133 { 134 temp.add(0, ((Vector )teardownMethods).firstElement()); 135 teardownMethods.remove(0); 136 } 137 teardownMethods = temp; 138 } 139 } 140 141 doTeardown(); 143 } 144 145 146 private static void initStartupTeardownMethods() throws SystemStartupException 147 { 148 Set propNames = new TreeSet (); 149 List clsNames = new Vector (); 150 Enumeration keys = System.getProperties().keys(); 151 String propPrefix = "ch.ethz.prose.ESSystem."; 152 153 while(keys.hasMoreElements()) 155 { 156 String crtPropName = (String )keys.nextElement(); 157 if (crtPropName.startsWith(propPrefix) && 158 crtPropName.length() > propPrefix.length()) 159 propNames.add(crtPropName); 160 } 161 162 Iterator j = propNames.iterator(); 163 while (j.hasNext()) 164 clsNames.add(System.getProperty((String )j.next())); 165 166 Iterator i = clsNames.iterator(); 168 while(i.hasNext()) 169 { 170 String crtClsName = null; 171 Method startupMethod = null; 172 Method teardownMethod = null; 173 try 174 { 175 crtClsName = (String )i.next(); 176 Class sysClass = Class.forName(crtClsName); 177 startupMethod = sysClass.getMethod("startup",new Class []{}); 178 teardownMethod = sysClass.getMethod("teardown",new Class []{}); 179 180 if ( ((startupMethod.getModifiers() & Modifier.STATIC) != Modifier.STATIC) || 181 ((teardownMethod.getModifiers() & Modifier.STATIC) != Modifier.STATIC) || 182 (!Arrays.asList(startupMethod.getExceptionTypes()).contains(SystemStartupException.class)) || 183 (!Arrays.asList(teardownMethod.getExceptionTypes()).contains(SystemTeardownException.class))) 184 throw new SystemStartupException("methods startup/teardown are non-static or don't have" + 185 " compatible throws clauses"); 186 } 187 catch (ClassNotFoundException noSuchClass) 188 { throw new SystemStartupException("Startup class " + crtClsName + " not found"); } 189 catch (NoSuchMethodException noSuchMethod) 190 { throw new SystemStartupException("startup and teardown methods not present in class " + crtClsName); } 191 192 startupMethods.add(startupMethod); 193 teardownMethods.add(teardownMethod); 194 } 195 196 } 198 199 200 private static void doStartup() throws SystemStartupException 201 { 202 Iterator i = null; 203 204 i=startupMethods.iterator(); 205 206 try 207 { 208 while (i.hasNext()) 209 { 210 Method crtMeth = (Method )i.next(); 211 crtMeth.invoke(null,new Object []{}); 212 } 213 } 214 catch (IllegalAccessException notPublic) 215 { 216 notPublic.printStackTrace(); 217 throw new SystemStartupException("Cannot access startup method"); 218 } 219 catch (InvocationTargetException realException) 220 { 221 realException.printStackTrace(); 222 throw (SystemStartupException)realException.getTargetException(); 223 } 224 } 225 226 227 private static void doTeardown() throws SystemTeardownException 228 { 229 Iterator i = null; 230 231 i=teardownMethods.iterator(); 232 233 try 234 { 235 while (i.hasNext()) 236 { 237 Method crtMeth = (Method )i.next(); 238 crtMeth.invoke(null,new Object []{}); 239 } 240 } 241 catch (IllegalAccessException notPublic) 242 { 243 throw new SystemTeardownException("Cannot access teardown method"); 244 } 245 catch (InvocationTargetException realException) 246 { 247 throw (SystemTeardownException)realException.getTargetException(); 248 } 249 } 250 251 } 252 253 254 | Popular Tags |