@@ -19,11 +19,14 @@ public class DcmAgent {
1919 public static final String FILE = "file" ;
2020
2121 public static void agentmain (String agentArgument ) throws Exception {
22+ System .out .printf ("Run %s!\n " , DcmAgent .class .getName ());
23+
2224 agentArgument = agentArgument .trim ();
2325 if (agentArgument .isEmpty ()) {
2426 System .out .println (DcmAgent .class .getName () + ": empty agent argument, do nothing!" );
2527 return ;
2628 }
29+ System .out .println ("Arguments: " + agentArgument );
2730
2831 initAction2Method ();
2932
@@ -32,23 +35,36 @@ public static void agentmain(String agentArgument) throws Exception {
3235 try {
3336 final Map <String , List <String >> action2Arguments = parseArgument (agentArgument );
3437 if (action2Arguments .containsKey (FILE )) {
35- outputStream = new FileOutputStream (action2Arguments .get (FILE ).get (0 ), true );
38+ outputStream = new FileOutputStream (action2Arguments .get (FILE ).get (0 ), false );
3639 final OutputStreamWriter outputStreamWriter = new OutputStreamWriter (outputStream , "UTF-8" );
3740
3841 writer = new PrintWriter (outputStreamWriter , true );
3942 action2Arguments .remove (FILE );
4043 }
41- writer .printf ("Run %s!\n " , DcmAgent .class .getName ());
42- writer .println ("Arguments: " + agentArgument );
4344
4445 if (action2Arguments .isEmpty ()) {
45- writer .println (DcmAgent .class .getName () + ": No action in agent argument, do nothing!" );
46+ System .out .println (DcmAgent .class .getName () + ": No action in agent argument, do nothing!" );
47+ writer .println ("No action in agent argument, do nothing! agent argument: " + agentArgument );
48+ return ;
4649 }
4750
4851 for (Map .Entry <String , List <String >> entry : action2Arguments .entrySet ()) {
4952 final String action = entry .getKey ();
5053 final List <String > arguments = entry .getValue ();
51- doAction (action , arguments .toArray (new String [0 ]), writer );
54+
55+ if (!action2Method .containsKey (action )) {
56+ StringBuilder sb = new StringBuilder ();
57+ for (String argument : arguments ) {
58+ if (sb .length () > 0 ) {
59+ sb .append (" " );
60+ }
61+ sb .append (argument );
62+ }
63+ writer .printf ("Unknown action %s! ignore %<s %s !\n " , action , sb );
64+ }
65+
66+ final Object result = doAction (action , arguments .toArray (new String [0 ]));
67+ printResult (action , result , writer );
5268 }
5369 } finally {
5470 if (outputStream != null ) {
@@ -63,7 +79,7 @@ static Map<String, List<String>> parseArgument(String argument) {
6379 int idx = 0 ;
6480 Map <String , List <String >> action2Arguments = new HashMap <String , List <String >>();
6581 while (idx < split .length ) {
66- final String action = split [idx ++]. toLowerCase () ;
82+ final String action = split [idx ++];
6783 if (!action2Method .containsKey (action )) {
6884 continue ; // TODO error message
6985 }
@@ -81,26 +97,16 @@ static Map<String, List<String>> parseArgument(String argument) {
8197 return action2Arguments ;
8298 }
8399
84- static void doAction (String action , String [] arguments , PrintWriter writer ) throws Exception {
100+ static Object doAction (String action , String [] arguments ) throws Exception {
85101 Method method = action2Method .get (action );
86102
87- if (method == null ) {
88- StringBuilder sb = new StringBuilder ();
89- for (String argument : arguments ) {
90- if (sb .length () > 0 ) {
91- sb .append (" " );
92- }
93- sb .append (argument );
94- }
95- writer .printf ("Unknown action %s! ignore %<s %s !\n " , action , sb );
96- }
97-
98103 final Class <?>[] parameterTypes = method .getParameterTypes ();
99104 final Object [] methodArgs = convertStringArray2Arguments (action , arguments , parameterTypes );
100- final Object ret = method .invoke (null , methodArgs );
101- if (ret != null ) {
102- writer .println (ret );
103- }
105+ return method .invoke (null , methodArgs );
106+ }
107+
108+ static void printResult (String action , Object result , PrintWriter writer ) {
109+
104110 }
105111
106112 static volatile Map <String , Method > action2Method ;
0 commit comments