エージェント

4507 ワード

       3 :
    ,    ,Cglib  

        ,
    ,        
Cglib  :     

 Spring AOP   :
                , JDK  ,       
            , Cglib  
       
      

public interface agentInterface {
      public void dealTask(String taskName);   
}

   :   
public class realclass  implements agentInterface{

    
    @Override
    public void dealTask(String taskName) {
    System.out.print("      "+taskName);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        
    }

}
   :   
public class proxyClass implements agentInterface {
    //           。  
    private realclass real;
    public proxyClass(realclass real) {
        this.real=real;
    }

    @Override
    public void dealTask(String taskName) {
          long stime = System.currentTimeMillis();   
          //             
          real.dealTask(taskName);  
          long ftime = System.currentTimeMillis();   
          System.out.println("      "+(ftime - stime)+"  ");  
            
        
    }

}
   :   
public class staticFactory {

    public static agentInterface    getInstance(){
          return new proxyClass(new realclass());  
    }

}

   :   

public class clients {

    public static void main(String args[])
    {
        agentInterface proxy = staticFactory.getInstance();
        proxy.dealTask("linking!!!!!!!!!!!");
    }
}

ダイナミックエージェント:文字コード
   
public class GetPostEncodingFilter implements Filter {
    public void destroy() {
    }
    public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
        //  
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        //     
        RequestProxy requestProxy = new RequestProxy(request);
        //         
        response.setContentType("text/html;charset=UTF-8");
        //    ,  GetPostServlet
        chain.doFilter(requestProxy.getProxy(),response);
    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

   
/**
 * @author Administrator
 *     HttpServletRequest       
 */
class RequestProxy{
    /**
     *     
     */
    private HttpServletRequest request;
    /**
     *       request   
     */
    public RequestProxy(HttpServletRequest request) {
        this.request = request;
    }
    /**
     *   HttpServletRequest     (  )
     */
    public HttpServletRequest getProxy(){
        return (HttpServletRequest)Proxy.newProxyInstance(
                this.getClass().getClassLoader(), 
                request.getClass().getInterfaces(), 
                new InvocationHandler(){
                    public Object invoke(Object proxy, Method method,Object[] args) throws Throwable {
                        Object returnValue = "";
                        String methodName = method.getName();
                        if("getParameter".equals(methodName)){
                            //          
                            String requestType = request.getMethod();
                            //   GET    
                            if("GET".equals(requestType)){
                                //             
                                String temp = (String) method.invoke(request,args);
                                //    
                                if(temp!=null && temp.trim().length()>0){
                                    //  
                                    byte[] buf = temp.getBytes("ISO8859-1");
                                    //    
                                    returnValue = new String(buf,"UTF-8");
                                }
                            //   POST    
                            }else if("POST".equals(requestType)){
                                //      
                                request.setCharacterEncoding("UTF-8");
                                //             
                                returnValue = method.invoke(request,args);
                            }
                        }else{
                            returnValue = method.invoke(request,args);
                        }
                        return returnValue;
                    }
                });
    }
}