1 3 6 23 24 package edu.neu.ccs.jmk; 25 26 import java.io.PrintWriter ; 27 28 37 final class ClassOperator 38 implements Operator 39 { 40 private final String name = "forname"; 41 42 46 public String getName() { 47 return name; 48 } 49 50 57 public void exec(String [] args, PrintWriter out) 58 throws CommandFailedException 59 { 60 String msg = null; try { 62 if (args.length > 0) { 63 Class cls = Class.forName(args[0]); 64 Operator operator = (Operator)cls.newInstance(); 65 String [] params = new String [args.length - 1]; 66 for (int i = 0; i < params.length; i++) 67 params[i] = args[i + 1]; 68 operator.exec(params, out); 69 } 70 else 71 msg = "Missing class name"; 72 } 73 catch (CommandFailedException ex) { throw ex; 75 } 76 catch (ClassNotFoundException ex) { msg = args[0] + " class not found"; 78 } 79 catch (ClassCastException ex) { msg = args[0] + " does not implement Operator"; 81 } 82 catch (IllegalAccessException ex) { 83 msg = "Cannot create an instance of " + args[0] + 84 " due to access restrictions"; 85 } 86 catch (InstantiationException ex) { 87 msg = "Cannot create an instance of " + args[0]; 88 } 89 if (msg != null) 90 throw new CommandFailedException(msg); 91 } 92 93 98 public static void main(String [] args) { 99 PrintWriter out = new PrintWriter (System.out, true); 100 try { 101 new ClassOperator().exec(args, out); 102 } 103 catch (Throwable t) { 104 System.err.println("Internal error: " + t.getMessage()); 105 t.printStackTrace(); 106 } 107 out.println("Class command completed"); 108 } 109 } 110 | Popular Tags |