1 17 18 package org.objectweb.jac.core.dist; 19 20 import java.net.InetAddress ; 21 import java.util.Arrays ; 22 import java.util.Hashtable ; 23 import org.apache.log4j.Logger; 24 25 41 42 abstract public class Distd { 43 static final Logger logger = Logger.getLogger("dist"); 44 45 47 public static long outputCount = 0; 48 50 public static long inputCount = 0; 51 52 58 public static Hashtable stringArrayToHashtable(String [] strs) { 59 60 Hashtable ret = new Hashtable (); 61 62 for ( int i=0 ; i < strs.length ; i++ ) { 63 ret.put( strs[i], "" ); 64 } 65 66 return ret; 67 } 68 69 73 abstract public void init(); 74 75 81 abstract protected RemoteContainer newContainer(String name) 82 throws Exception ; 83 84 92 abstract protected RemoteContainer newContainer(String name, String className) 93 throws Exception ; 94 95 99 abstract public void run(); 100 101 106 public Distd(String [] args) { 107 108 109 Hashtable hArgs = parseArguments(args, flags, options); 110 111 112 113 init(); 114 115 try { 116 117 118 verbose = 119 (hArgs.get("v") == null && hArgs.get("-verbose") == null) ? 120 false : true ; 121 122 Object oNames = hArgs.get("_files"); 123 if (oNames == null) 124 usage(); 125 126 String [] names = (String [])oNames; 127 if (names.length != 1) 128 usage(); 129 String name = names[0]; 130 131 132 133 RemoteContainer container = null; 134 135 if (getClass().getClassLoader() instanceof DistdClassLoader) { 136 logger.debug("DistdClassLoader..."); 137 Object classRepositoryName = hArgs.get("r"); 138 if (classRepositoryName == null) 139 classRepositoryName = hArgs.get("-repository"); 140 if (classRepositoryName == null) 141 classRepositoryName = "s0"; 142 classRepositoryName = getFullHostName((String )classRepositoryName); 143 DistdClassLoader.classRepositoryName = (String )classRepositoryName; 144 referenceContainerName = (String ) classRepositoryName; 145 } else { 146 } 148 149 Object className = hArgs.get("i"); 150 if (className == null) 151 className = hArgs.get("-init"); 152 153 int ok = 20; 156 while (ok>0) { 157 try { 158 if (className == null) { 159 container = newContainer(name); 160 } else { 161 container = newContainer(name, (String )className); 162 } 163 logger.info("--- Distd started."); 164 ok = 0; 165 } catch(Exception e) { 166 ok--; 167 if (ok == 0) { 168 logger.error("ERROR: distd did not start",e); 169 } else { 170 logger.debug("--- Distd starting try, "+ok+" left..."); 171 Thread.sleep(200); 172 } 173 } 174 } 175 176 containers.put(name, container); 177 localContainerName = container.getName(); 178 179 180 run(); 181 182 if (getClass().getClassLoader() instanceof DistdClassLoader) { 183 ((DistdClassLoader)getClass().getClassLoader()) 184 .bootstrapping = false; 185 } 186 187 } catch(Exception e) { 188 logger.error("Distd "+Arrays.asList(args),e); 189 } 190 } 191 192 193 public static String referenceContainerName = null; 194 195 210 public static String getFullHostName(String name) { 211 String fullname = ""; 212 name = name.replace('\\','/'); 213 if (!name.startsWith("//")) { 214 try { 215 fullname = "//"+InetAddress.getLocalHost().getHostName()+"/"+name; 216 } catch(Exception e) { 217 logger.error("getFullHostName "+name,e); 218 } 219 } 220 if (name.startsWith("//localhost/")) { 221 try { 222 fullname = "//" + InetAddress.getLocalHost().getHostName() + 223 "/" + name.substring(12); 224 } catch(Exception e) { 225 logger.error("getFullHostName "+name,e); 226 } 227 } 228 if (fullname.equals("")) 229 fullname = name; 230 return fullname; 231 } 232 233 234 236 237 245 246 protected static String localContainerName = ""; 247 248 249 public static String getLocalContainerName () { 250 return localContainerName; 251 } 252 253 254 protected static final String [] flags = {"v","-verbose"}; 255 256 257 protected static final String [] options = {"i","-init","r","-repository"}; 258 259 260 protected static boolean verbose = false; 261 262 263 264 265 protected static void usage() { 266 System.out.println( 267 "Usage: java org.objectweb.jac.core.dist.Distd [options] name\n" + 268 "\n" + 269 "-v --verbose: display system informations\n" + 270 "-i --init classname: create an instance of classname\n" + 271 "-r --repository sitename: the site where the classes repository is located (the loader will load the classes from this site if possible -- else from the local file system)\n" + 272 "name: the daemon identifier" 273 ); 274 System.exit(1); 275 } 276 277 293 protected static Hashtable parseArguments(String [] args, 294 String [] flags, 295 String [] options) { 296 297 298 299 Hashtable hFlags = stringArrayToHashtable(flags); 300 Hashtable hOptions = stringArrayToHashtable(options); 301 302 303 304 305 Hashtable result = new Hashtable (); 306 307 String flagOrOption; 308 Object flag, option; 309 310 for (int i=0; i<args.length; i++) { 311 if (args[i].startsWith("-")) { 312 flagOrOption = args[i].substring(1); 314 flag = hFlags.get(flagOrOption); 315 option = hOptions.get(flagOrOption); 316 317 if (flag != null) { 318 result.put(flagOrOption, ""); 319 } else if (option != null) { 320 i++; 322 if (i >= args.length) { 323 usage(); 324 } 325 result.put(flagOrOption,args[i]); 326 } else { 327 usage(); 328 } 329 } else { 330 int numberOfFiles = args.length - i; 333 String [] files = new String [numberOfFiles]; 334 System.arraycopy(args, i, files, 0, numberOfFiles); 335 336 result.put("_files", files); 337 338 return result; 339 } 340 } 341 342 return result; 343 } 344 345 346 protected static Hashtable containers = new Hashtable (); 347 348 349 355 public static boolean containsContainer(RemoteContainer container) { 356 return containers.values().contains(container); 357 } 358 } 359 | Popular Tags |