358
359 if (!Agent.isValidConcurrency(concurrency)) {
360 throw new BadArgs("Bad value for concurrency: " + concurrency);
361 }
362 }
363
364 /**
365 * Create a connection factory based on the values decoded by decodeAllArgs
366 * Normally called from createAgent.
367 * @return a connection factory based on the values decoded by decodeAllArgs
368 * @throws AgentMain.Fault if there is a problem createing the factory
369 */
370 protected ConnectionFactory createConnectionFactory() throws Fault {
371 String s = AgentMain.class.getName();
372 String pkg = s.substring(0, s.lastIndexOf('.'));
373
374 switch (mode) {
375 case ACTIVE:
376 try {
377 Class<?> c = Class.forName(pkg + ".ActiveConnectionFactory");
378 Constructor m = c.getConstructor(new Class[] {String.class, int.class});
379 Object[] args = { activeHost, new Integer(activePort) };
380 return (ConnectionFactory)(m.newInstance(args));
381 }
382 catch (Throwable e) {
383 Throwable t = unwrapInvocationTargetException(e);
384 String[] msgs = {
385 "Error occurred while trying to start an active agent",
386 t.toString(),
387 "Are the java.net classes available?"
388 };
389 throw new Fault(msgs);
390 }
391
392 case PASSIVE:
393 try {
394 Class<?> c = Class.forName(pkg + ".PassiveConnectionFactory");
395 Constructor m = c.getConstructor(new Class[] {int.class, int.class});
396 Object[] args = { new Integer(passivePort), new Integer(concurrency + 1) };
397 return (ConnectionFactory)(m.newInstance(args));
398 }
399 catch (Throwable e) {
400 Throwable t = unwrapInvocationTargetException(e);
401 if (t instanceof IOException)
402 throw new Fault("Cannot create socket on port " + passivePort);
403 else {
404 String[] msgs = {
405 "Error occurred while trying to start a passive agent",
406 t.toString(),
407 "Are the java.net classes available?"
408 };
409 throw new Fault(msgs);
410 }
411 }
412
413 case SERIAL:
414 try {
415 Class<?> c = Class.forName(pkg + ".SerialPortConnectionFactory");
416 Constructor m = c.getConstructor(new Class[] {String.class, String.class, int.class});
417 Object[] args = {serialPort, Agent.productName, new Integer(10*1000)};
418 return (ConnectionFactory)(m.newInstance(args));
419 }
420 catch (InvocationTargetException e) {
421 Throwable t = e.getTargetException();
422 if (t instanceof IllegalArgumentException ||
423 t.getClass().getName().equals("gnu.io.NoSuchPortException")) {
424 throw new Fault(serialPort + " is not a valid port");
425 }
426 else {
427 String[] msgs = {
428 "Error occurred while trying to access the communication ports",
429 t.toString(),
430 "Is the gnu.io extension installed?"
431 };
432 throw new Fault(msgs);
433 }
434 }
435 catch (Throwable e) {
436 String[] msgs = {
443
444 default:
445 throw new Error("unexpected mode");
446 }
447 }
448
449 /**
450 * Create an agent based on the command line options previously decoded.
451 * createConnectionFactory() is used to create the connection factory required
452 * by the agent.
453 * @return an agent based on the values decoded by decodeAllArgs
454 * @throws AgentMain.Fault if there is a problem createing the agent
455 */
456 protected Agent createAgent() throws Fault {
457 ConnectionFactory cf = createConnectionFactory();
458 Agent agent = new Agent(cf, concurrency);
459 agent.setTracing(tracing);
460
461 if (observerClassName != null) {
462 try {
463 Class observerClass = Class.forName(observerClassName);
464 Agent.Observer observer = (Agent.Observer)(observerClass.newInstance());
465 agent.addObserver(observer);
466 }
467 catch (ClassCastException e) {
468 throw new Fault("observer is not of type " +
469 Agent.Observer.class.getName() + ": " + observerClassName);
470 }
471 catch (ClassNotFoundException e) {
472 throw new Fault("cannot find observer class: " + observerClassName);
473 }
474 catch (IllegalAccessException e) {
475 throw new Fault("problem instantiating observer: " + e);
476 }
477 catch (InstantiationException e) {
478 throw new Fault("problem instantiating observer: " + e);
479 }
480 }
481
482 // for now, we only read a map file if one is explicitly specified;
483 // in JDK 1.1, it might be nice to check if <<HOSTNAME>>.jtm exists
604 }
605
606 public void execTest(Agent agent, Connection c, String tag, String className, String[] args) {
607 }
608
609 public void execCommand(Agent agent, Connection c, String tag, String className, String[] args) {
610 }
611
612 public void execMain(Agent agent, Connection c, String tag, String className, String[] args) {
613 }
614
615 public void result(Agent agent, Connection c, Status result) {
616 }
617
618 public void exception(Agent agent, Connection c, Throwable e) {
619 }
620
621 public void completed(Agent agent, Connection c) {
622 }
623
624 private Class connectExceptionClass;
625 private Class unknownHostExceptionClass;
626 }
627 }
|
358
359 if (!Agent.isValidConcurrency(concurrency)) {
360 throw new BadArgs("Bad value for concurrency: " + concurrency);
361 }
362 }
363
364 /**
365 * Create a connection factory based on the values decoded by decodeAllArgs
366 * Normally called from createAgent.
367 * @return a connection factory based on the values decoded by decodeAllArgs
368 * @throws AgentMain.Fault if there is a problem createing the factory
369 */
370 protected ConnectionFactory createConnectionFactory() throws Fault {
371 String s = AgentMain.class.getName();
372 String pkg = s.substring(0, s.lastIndexOf('.'));
373
374 switch (mode) {
375 case ACTIVE:
376 try {
377 Class<?> c = Class.forName(pkg + ".ActiveConnectionFactory");
378 Constructor<?> m = c.getConstructor(new Class<?>[] {String.class, int.class});
379 Object[] args = { activeHost, new Integer(activePort) };
380 return (ConnectionFactory)(m.newInstance(args));
381 }
382 catch (Throwable e) {
383 Throwable t = unwrapInvocationTargetException(e);
384 String[] msgs = {
385 "Error occurred while trying to start an active agent",
386 t.toString(),
387 "Are the java.net classes available?"
388 };
389 throw new Fault(msgs);
390 }
391
392 case PASSIVE:
393 try {
394 Class<?> c = Class.forName(pkg + ".PassiveConnectionFactory");
395 Constructor<?> m = c.getConstructor(new Class<?>[] {int.class, int.class});
396 Object[] args = { new Integer(passivePort), new Integer(concurrency + 1) };
397 return (ConnectionFactory)(m.newInstance(args));
398 }
399 catch (Throwable e) {
400 Throwable t = unwrapInvocationTargetException(e);
401 if (t instanceof IOException)
402 throw new Fault("Cannot create socket on port " + passivePort);
403 else {
404 String[] msgs = {
405 "Error occurred while trying to start a passive agent",
406 t.toString(),
407 "Are the java.net classes available?"
408 };
409 throw new Fault(msgs);
410 }
411 }
412
413 case SERIAL:
414 try {
415 Class<?> c = Class.forName(pkg + ".SerialPortConnectionFactory");
416 Constructor<?> m = c.getConstructor(new Class<?>[] {String.class, String.class, int.class});
417 Object[] args = {serialPort, Agent.productName, new Integer(10*1000)};
418 return (ConnectionFactory)(m.newInstance(args));
419 }
420 catch (InvocationTargetException e) {
421 Throwable t = e.getTargetException();
422 if (t instanceof IllegalArgumentException ||
423 t.getClass().getName().equals("gnu.io.NoSuchPortException")) {
424 throw new Fault(serialPort + " is not a valid port");
425 }
426 else {
427 String[] msgs = {
428 "Error occurred while trying to access the communication ports",
429 t.toString(),
430 "Is the gnu.io extension installed?"
431 };
432 throw new Fault(msgs);
433 }
434 }
435 catch (Throwable e) {
436 String[] msgs = {
443
444 default:
445 throw new Error("unexpected mode");
446 }
447 }
448
449 /**
450 * Create an agent based on the command line options previously decoded.
451 * createConnectionFactory() is used to create the connection factory required
452 * by the agent.
453 * @return an agent based on the values decoded by decodeAllArgs
454 * @throws AgentMain.Fault if there is a problem createing the agent
455 */
456 protected Agent createAgent() throws Fault {
457 ConnectionFactory cf = createConnectionFactory();
458 Agent agent = new Agent(cf, concurrency);
459 agent.setTracing(tracing);
460
461 if (observerClassName != null) {
462 try {
463 Class<?> observerClass = Class.forName(observerClassName);
464 Agent.Observer observer = (Agent.Observer)(observerClass.newInstance());
465 agent.addObserver(observer);
466 }
467 catch (ClassCastException e) {
468 throw new Fault("observer is not of type " +
469 Agent.Observer.class.getName() + ": " + observerClassName);
470 }
471 catch (ClassNotFoundException e) {
472 throw new Fault("cannot find observer class: " + observerClassName);
473 }
474 catch (IllegalAccessException e) {
475 throw new Fault("problem instantiating observer: " + e);
476 }
477 catch (InstantiationException e) {
478 throw new Fault("problem instantiating observer: " + e);
479 }
480 }
481
482 // for now, we only read a map file if one is explicitly specified;
483 // in JDK 1.1, it might be nice to check if <<HOSTNAME>>.jtm exists
604 }
605
606 public void execTest(Agent agent, Connection c, String tag, String className, String[] args) {
607 }
608
609 public void execCommand(Agent agent, Connection c, String tag, String className, String[] args) {
610 }
611
612 public void execMain(Agent agent, Connection c, String tag, String className, String[] args) {
613 }
614
615 public void result(Agent agent, Connection c, Status result) {
616 }
617
618 public void exception(Agent agent, Connection c, Throwable e) {
619 }
620
621 public void completed(Agent agent, Connection c) {
622 }
623
624 private Class<?> connectExceptionClass;
625 private Class<?> unknownHostExceptionClass;
626 }
627 }
|