11package com .alibaba .dcm .agent ;
22
3+ import com .alibaba .dcm .DnsCache ;
4+ import com .alibaba .dcm .DnsCacheEntry ;
35import com .alibaba .dcm .DnsCacheManipulator ;
46
57import java .io .FileOutputStream ;
68import java .io .OutputStreamWriter ;
79import java .io .PrintWriter ;
810import java .io .StringWriter ;
911import java .lang .reflect .Method ;
12+ import java .text .SimpleDateFormat ;
1013import java .util .ArrayList ;
1114import java .util .Arrays ;
1215import java .util .HashMap ;
1821 * @since 1.4.0
1922 */
2023public class DcmAgent {
21- public static final String FILE = "file" ;
22- public static final String DCM_AGENT_SUCCESS_MARK_LINE = "!!DCM SUCCESS!!" ;
24+ static final String FILE = "file" ;
25+ static final String DCM_AGENT_SUCCESS_MARK_LINE = "!!DCM SUCCESS!!" ;
2326
2427 public static void agentmain (String agentArgument ) throws Exception {
2528 System .out .printf ("%s: attached with agent argument: %s.\n " , DcmAgent .class .getName (), agentArgument );
@@ -34,9 +37,11 @@ public static void agentmain(String agentArgument) throws Exception {
3437
3538 FileOutputStream fileOutputStream = null ;
3639 try {
37- final Map <String , List <String >> action2Arguments = parseArgument (agentArgument );
40+ final Map <String , List <String >> action2Arguments = parseAgentArgument (agentArgument );
3841
3942 PrintWriter filePrinter = null ;
43+
44+ // Extract file argument, set file printer if needed
4045 if (action2Arguments .containsKey (FILE )) {
4146 fileOutputStream = new FileOutputStream (action2Arguments .get (FILE ).get (0 ), false );
4247 final OutputStreamWriter outputStreamWriter = new OutputStreamWriter (fileOutputStream , "UTF-8" );
@@ -58,14 +63,7 @@ public static void agentmain(String agentArgument) throws Exception {
5863 for (Map .Entry <String , List <String >> entry : action2Arguments .entrySet ()) {
5964 final String action = entry .getKey ();
6065 final List <String > arguments = entry .getValue ();
61-
62- StringBuilder argumentString = new StringBuilder ();
63- for (String argument : arguments ) {
64- if (argumentString .length () > 0 ) {
65- argumentString .append (" " );
66- }
67- argumentString .append (argument );
68- }
66+ final String argumentString = join (arguments );
6967
7068 if (!action2Method .containsKey (action )) {
7169 System .out .printf ("%s: Unknown action %s, ignore! action: %<s %s!\n " , DcmAgent .class .getName (), action , argumentString );
@@ -80,10 +78,7 @@ public static void agentmain(String agentArgument) throws Exception {
8078 printResult (action , result , filePrinter );
8179 } catch (Exception e ) {
8280 allSuccess = false ;
83-
84- final StringWriter w = new StringWriter ();
85- e .printStackTrace (new PrintWriter (w ));
86- final String exString = w .toString ();
81+ final String exString = throwable2StackString (e );
8782
8883 System .out .printf ("%s: Error to do action %s %s, cause: %s\n " , DcmAgent .class .getName (), action , argumentString , exString );
8984 if (filePrinter != null ) {
@@ -106,7 +101,7 @@ public static void agentmain(String agentArgument) throws Exception {
106101 }
107102 }
108103
109- static Map <String , List <String >> parseArgument (String argument ) {
104+ static Map <String , List <String >> parseAgentArgument (String argument ) {
110105 final String [] split = argument .split ("\\ s+" );
111106
112107 int idx = 0 ;
@@ -130,47 +125,27 @@ static Map<String, List<String>> parseArgument(String argument) {
130125 return action2Arguments ;
131126 }
132127
133- static Object doAction (String action , String [] arguments ) throws Exception {
134- Method method = action2Method .get (action );
135-
136- final Class <?>[] parameterTypes = method .getParameterTypes ();
137- final Object [] methodArgs = convertStringArray2Arguments (action , arguments , parameterTypes );
138- return method .invoke (null , methodArgs );
128+ static String join (List <String > list ) {
129+ return join (list , " " );
139130 }
140131
141- static void printResult ( String action , Object result , PrintWriter writer ) {
142- final Method method = action2Method . get ( action );
143- if ( method . getReturnType () != void . class ) {
144- if (writer != null ) {
145- writer . println ( result . toString () );
132+ static String join ( List < String > list , String separator ) {
133+ StringBuilder ret = new StringBuilder ( );
134+ for ( String argument : list ) {
135+ if (ret . length () > 0 ) {
136+ ret . append ( separator );
146137 }
138+ ret .append (argument );
147139 }
148- if (writer != null ) {
149- writer .printf ("%s DONE.\n " , action );
150- }
140+ return ret .toString ();
151141 }
152142
153- static volatile Map <String , Method > action2Method ;
154-
155- static synchronized void initAction2Method () throws Exception {
156- if (action2Method != null ) return ;
157-
158- Map <String , Method > map = new HashMap <String , Method >();
159- map .put ("set" , DnsCacheManipulator .class .getMethod ("setDnsCache" , String .class , String [].class ));
160- map .put ("get" , DnsCacheManipulator .class .getMethod ("getDnsCache" , String .class ));
161- map .put ("rm" , DnsCacheManipulator .class .getMethod ("removeDnsCache" , String .class ));
162-
163- map .put ("list" , DnsCacheManipulator .class .getMethod ("getWholeDnsCache" ));
164- map .put ("clear" , DnsCacheManipulator .class .getMethod ("clearDnsCache" ));
165-
166- map .put ("setPolicy" , DnsCacheManipulator .class .getMethod ("setDnsCachePolicy" , int .class ));
167- map .put ("getPolicy" , DnsCacheManipulator .class .getMethod ("getDnsCachePolicy" ));
168- map .put ("setNegativePolicy" , DnsCacheManipulator .class .getMethod ("setDnsNegativeCachePolicy" , int .class ));
169- map .put ("getNegativePolicy" , DnsCacheManipulator .class .getMethod ("getDnsNegativeCachePolicy" ));
170-
171- map .put (FILE , null ); // FAKE KEY
143+ static Object doAction (String action , String [] arguments ) throws Exception {
144+ Method method = action2Method .get (action );
172145
173- action2Method = map ;
146+ final Class <?>[] parameterTypes = method .getParameterTypes ();
147+ final Object [] methodArgs = convertStringArray2Arguments (action , arguments , parameterTypes );
148+ return method .invoke (null , methodArgs );
174149 }
175150
176151 static Object [] convertStringArray2Arguments (String action , String [] arguments , Class <?>[] parameterTypes ) {
@@ -214,4 +189,65 @@ static Object[] convertStringArray2Arguments(String action, String[] arguments,
214189
215190 return methodArgs ;
216191 }
192+
193+ static void printResult (String action , Object result , PrintWriter writer ) {
194+ if (writer == null ) {
195+ return ;
196+ }
197+
198+ final Method method = action2Method .get (action );
199+ if (method .getReturnType () == void .class ) {
200+ return ;
201+ }
202+ if (result == null ) {
203+ writer .println ((Object ) null );
204+ } else if (result instanceof DnsCacheEntry ) {
205+ final SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
206+ DnsCacheEntry entry = (DnsCacheEntry ) result ;
207+ writer .printf ("%s %s %s\n " , entry .getHost (), join (Arrays .asList (entry .getIps ()), "," ), dateFormat .format (entry .getExpiration ()));
208+ } else if (result instanceof DnsCache ) {
209+ final SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
210+ DnsCache dnsCache = (DnsCache ) result ;
211+
212+ writer .println ("Dns cache:" );
213+ for (DnsCacheEntry entry : dnsCache .getCache ()) {
214+ writer .printf (" %s %s %s\n " , entry .getHost (), join (Arrays .asList (entry .getIps ()), "," ), dateFormat .format (entry .getExpiration ()));
215+ }
216+ writer .println ("Dns negative cache: " );
217+ for (DnsCacheEntry entry : dnsCache .getNegativeCache ()) {
218+ writer .printf (" %s %s %s\n " , entry .getHost (), join (Arrays .asList (entry .getIps ()), "," ), dateFormat .format (entry .getExpiration ()));
219+ }
220+ } else {
221+ writer .println (result .toString ());
222+ }
223+ }
224+
225+ static String throwable2StackString (Throwable e ) {
226+ final StringWriter w = new StringWriter ();
227+ e .printStackTrace (new PrintWriter (w , true ));
228+ return w .toString ();
229+ }
230+
231+ static volatile Map <String , Method > action2Method ;
232+
233+ static synchronized void initAction2Method () throws Exception {
234+ if (action2Method != null ) return ;
235+
236+ Map <String , Method > map = new HashMap <String , Method >();
237+ map .put ("set" , DnsCacheManipulator .class .getMethod ("setDnsCache" , String .class , String [].class ));
238+ map .put ("get" , DnsCacheManipulator .class .getMethod ("getDnsCache" , String .class ));
239+ map .put ("rm" , DnsCacheManipulator .class .getMethod ("removeDnsCache" , String .class ));
240+
241+ map .put ("list" , DnsCacheManipulator .class .getMethod ("getWholeDnsCache" ));
242+ map .put ("clear" , DnsCacheManipulator .class .getMethod ("clearDnsCache" ));
243+
244+ map .put ("setPolicy" , DnsCacheManipulator .class .getMethod ("setDnsCachePolicy" , int .class ));
245+ map .put ("getPolicy" , DnsCacheManipulator .class .getMethod ("getDnsCachePolicy" ));
246+ map .put ("setNegativePolicy" , DnsCacheManipulator .class .getMethod ("setDnsNegativeCachePolicy" , int .class ));
247+ map .put ("getNegativePolicy" , DnsCacheManipulator .class .getMethod ("getDnsNegativeCachePolicy" ));
248+
249+ map .put (FILE , null ); // FAKE KEY
250+
251+ action2Method = map ;
252+ }
217253}
0 commit comments