package aj.hadoop.monitor.examples;

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.lang.management.*;
import java.util.StringTokenizer;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;

import aj.hadoop.monitor.util.PickerClient;

@Aspect
public class WordSearchMonitor {

	private String HOST = "127.0.0.1";
	private int PORT = 55000;

	/** log file */
	public static final String LOGFILE = "Hanoi-MethodTraceMonitor.log";

	/**
	 * pointcut
	 * @param word
	 * @param one
	 */
	@Pointcut ("call(void org.apache.hadoop.mapreduce.Mapper.Context.write" +
			"(" +
			"java.lang.Object, " +
			"java.lang.Object" +
			"))" +
			"&& cflow(execution(public void org.apache.hadoop.examples.WordSearch$TokenizerMapper.map(..)))" +
			"&& args(word, one)")
	public void catch_map_method(Object word, Object one){}

	@Before ( value = "catch_map_method( key, one )")
	public void logging_current_method( JoinPoint thisJoinPoint,
			Object key,
			Object one) {
		String ret = "";
		try {
			String outfile = "/tmp" + "/" + this.LOGFILE;
			FileOutputStream fos = new FileOutputStream(outfile, true);
			OutputStreamWriter out = new OutputStreamWriter(fos);
			ret += "key:" + key;
            ret += "\n";
			out.write(ret);
			out.close();
		} catch (IOException ioe) {
			System.out.println(ioe);
		} catch (Exception e) {
			System.out.println(e);
		}
		System.err.println("** [POINTCUT]" + ret);
		System.out.println("** [POINTCUT]" + ret);

		try { 
			PickerClient client = new PickerClient();
			client.send(ret);
			System.out.println("** " + ret);
			client.socketClose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

@@ -1474,492 +1474,8 @@
 e) %7B
-%0A%09%09String ret = %22%22;%0A%09%09try %7B%0A%09%09%09String outfile = %22/tmp%22 + %22/%22 + this.LOGFILE;%0A%09%09%09FileOutputStream fos = new FileOutputStream(outfile, true);%0A%09%09%09OutputStreamWriter out = new OutputStreamWriter(fos);%0A%09%09%09ret += %22key:%22 + key;%0A            ret += %22%5Cn%22;%0A%09%09%09out.write(ret);%0A%09%09%09out.close();%0A%09%09%7D catch (IOException ioe) %7B%0A%09%09%09System.out.println(ioe);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09System.out.println(e);%0A%09%09%7D%0A%09%09System.err.println(%22** %5BPOINTCUT%5D%22 + ret);%0A%09%09System.out.println(%22** %5BPOINTCUT%5D%22 + ret);
 %0A%0A%09%09
@@ -1545,47 +1545,11 @@
 end(
-ret);%0A%09%09%09System.out.println(%22** %22 + ret
+key
 );%0A%09

remove debug System.out/err code.