KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > ClassOperator


1 // $Id: ClassOperator.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // Applies an operator loaded from the first argument to the remander of
4
// the operands.
5

6 /*
7  * Copyright 1997 by John D. Ramsdell
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */

23
24 package edu.neu.ccs.jmk;
25
26 import java.io.PrintWriter JavaDoc;
27
28 /**
29  * This operator is used to implement class commands.
30  * It invokes an arbitrary operation which is specified in a makefile.
31  * The first argument is treated as the name of a class and loaded.
32  * An instance of the class is created which must implement Operator.
33  * The instance is then applied to the remaining operands.
34  * @version November 1997
35  * @author John D. Ramsdell
36  */

37 final class ClassOperator
38 implements Operator
39 {
40   private final String JavaDoc name = "forname";
41
42   /**
43    * Get the name of this operator.
44    * @return the name of this operator
45    */

46   public String JavaDoc getName() {
47     return name;
48   }
49
50   /**
51    * Applies the operation specified by the first argument
52    * to the remaining arguments.
53    * @param args operands from the command
54    * @param out place to write messages
55    * @exception CommandFailedException if operation failed
56    */

57   public void exec(String JavaDoc[] args, PrintWriter JavaDoc out)
58        throws CommandFailedException
59   {
60     String JavaDoc msg = null; // For error messages
61
try {
62       if (args.length > 0) {
63     Class JavaDoc cls = Class.forName(args[0]);
64     Operator operator = (Operator)cls.newInstance();
65     String JavaDoc[] params = new String JavaDoc[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) { // From exec
74
throw ex;
75     }
76     catch (ClassNotFoundException JavaDoc ex) { // From forName
77
msg = args[0] + " class not found";
78     }
79     catch (ClassCastException JavaDoc ex) { // From (Operator)newInstance()
80
msg = args[0] + " does not implement Operator";
81     }
82     catch (IllegalAccessException JavaDoc ex) {
83       msg = "Cannot create an instance of " + args[0] +
84     " due to access restrictions";
85     }
86     catch (InstantiationException JavaDoc ex) {
87       msg = "Cannot create an instance of " + args[0];
88     }
89     if (msg != null)
90       throw new CommandFailedException(msg);
91   }
92
93   /**
94    * An entry point for testing class commands.
95    * It simply passes the arguments to the exec operation.
96    * @param args operands for the command
97    */

98   public static void main(String JavaDoc[] args) {
99     PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out, true);
100     try {
101       new ClassOperator().exec(args, out);
102     }
103     catch (Throwable JavaDoc t) {
104       System.err.println("Internal error: " + t.getMessage());
105       t.printStackTrace();
106     }
107     out.println("Class command completed");
108   }
109 }
110
Popular Tags