Introduction of AOP in Nanowar

Source: Internet
Author: User
Tags aop expression key regular expression return throwable
Through the Nanocontainer Nanowar and its struts plug-in perfect to achieve the IOC mode of transparency, but not in the standard Nanowar introduced NANOAOP have to say is a regret. After looking at Nanocontainer's code, I decided to add AOP support to Nanowar. To support AOP in components and instances, first introduce interceptor chains. Consider introducing the <aspect interceptor= ' interceptorname ' pointcuts= ' Filter '/> tag in the XML definition. This <aspect> tag is at the same level as the <parameter> label of the component or instance. Where interceptor indicates the name of the Interceptor. The implementation of the Interceptor can be registered in the same configuration file through a standard component. Pointcuts is the filtering expression for the interception method (PERL5 regular expression, and NANOAOP predefined several filtering rules). To implement this <aspect> tag in Nanowar, you need to modify Scopedcotainercomposer, Xmlcontainerbuilder, Containerrecorder, Servletcontainerlistener, Defaultlifecyclecontainerbuider class. Because the interceptor cannot get an instance in the process of initializing the IOC container. Therefore, it is also necessary to introduce a new generic interceptor Class (Interceptordelegate) as a real interceptor instance and a call bridge for the component to be intercepted. Because of the large amount of source code churn, simply precede the original class with the aspectable prefix to introduce the new class, but in addition to the AOP part, the other remains unchanged. Scopedcotainercomposer => aspectablescopedcontainercomposer.java    Public Aspectablescopedcontainercomposer () throws ClassNotFoundException {        this (New Dynaopaspectablepicocontainerfactory (). CreateContainer ());   }xmlcoNtainerbuilder => aspectablexmlcontainerbuilder.javaprotected picocontainer createContainerFromScript (  ....            aspectablepicocontainerfactory Containerfactory = new Dynaopaspectablepicocontainerfactory ();             Aspectablepicocontainer childcontainer = containerfactory                     CreateContainer ( Componentadapterfactory, Parentcontainer);            Cuts = Childcontainer.getpointcutsfactory ();  .}     private void Registercomponentimplementation (Nanocontainer container,             element Element) throws classnotfoundexception,             malformedurlexception {  &nbsp ......    Map interceptors = createinterceptors (element);    ......      if (interceptors!= null) {            Registercomponentinterceptor (container, Clazz, interceptors);       }    }    .......    private void Registercomponentinterceptor (Nanocontainer container,            Class component, String pointcuts, String Interceptorkey) {        if (NotSet (pointcuts)) {             pointcuts = all_methods;       }         if (Container.getpico () instanceof Aspectablepicocontainer) {             Aspectablepicocontainer Aspectcontainer = (Aspectablepicocontainer) container                     Getpico ();             Aspectcontainer.registerinterceptor (cuts.instancesof (component),                     Getmethodpointcut ( pointcuts), New Interceptordelegate (                             Aspectcontainer, Interceptorkey));       }   }.......    Private map Createinterceptors (element Element) {        Map Interceptormap = new HashMap ();        nodelist children = element.getchildnodes ();       &nbSp for (int i = 0; i < children.getlength (); i++) {            if (Children.item (i) instanceof Element) {                Element childElement = (Element) children.item (i);                 if (Aspect.equals (Childelement.getnodename ()) {                     String Interceptor = Childelement.getattribute ( Interceptor);                     if (NotSet (Interceptor))                          throw new Nanocontainermarkupexception (" ' "+ key                                 + "' attribute not specified for"                                   + element.getnodename ());                     String pointcuts = Childelement.getattribute (pointcuts);                      Interceptormap.put (Interceptor, Pointcuts);                }           }        }        if (Interceptormap.isempty ())              return null;        Else             return interceptormap;   }     private Methodpointcut getmethodpointcut (String pointcuts) {         if (Object_methods.equals (pointcuts))             Return Cuts.objectmethods ();        if (Is_methods.equals (pointcuts))              return Cuts.ismethods ();         if (all_methods.equals (pointcuts))              return Cuts.allmethods ();        if (Get_methods.equals (pointcuts) )             return Cuts.getmethods ();    & nbsp;    if (set_methods.equals (pointcuts))              return Cuts.setmethods ();        return cuts.signature (pointcuts);    }containerrecorder => aspectablecontainerrecorder.java    Public Mutablepicocontainer getcontainerproxy () {        if (This.container instanceof Aspectablepicocontainer)             return ( Mutablepicocontainer) Proxy.newproxyinstance (GetClass (). getClassLoader (),                     New class[]{ Aspectablepicocontainer.class}, Invocationrecorder);        else              return (Mutablepicocontainer) proxy.newproxyinstance ( GetClass (). getClassLoader (), &NBSP;&NBSP;&Nbsp;             New Class[]{ Mutablepicocontainer.class}, Invocationrecorder);   }defaultlifecyclecontainerbuider => aspectablelifecyclecontainerbuider.java    protected Picocontainer CreateContainer (PicoContainer Parentcontainer, Object assemblyscope) {        aspectablepicocontainerfactory Containerfactory = new Dynaopaspectablepicocontainerfactory ();        return Containerfactory.createcontainer (Parentcontainer);   }servletcontainerlistener => Aspectableservletcontainerlistener.javaprivate Containerbuilder Createbuilder (ServletContext context)              throws ClassNotFoundException {    ...     if (initparameter.equals (container_composer)) {        Containercomposer Containercomposer =                createcontainercomposer (context);              return new Aspectablelifecyclecontainerbuilder ( Containercomposer);       }interceptordelegate. Javaimport Org.aopalliance.intercept.methodinterceptor;import Org.aopalliance.intercept.methodinvocation;import Org.picocontainer.picocontainer;import DYNAOP. Interceptor;import DYNAOP. Invocation;public class Interceptordelegate implements Interceptor, Methodinterceptor {    private Picocontainer pico;    Private Object key;        public Interceptordelegate (Picocontainer Pico, Object key) {        This.pico = Pico;         This.key = key;            }   /* (non-javadoc)      * @see DYnaop. Interceptor#intercept (DYNAOP. Invocation)      */    public Object intercept (invocation invocation) throws Throwable {        return ((Interceptor) pico.getcomponentinstance (key). Intercept (invocation);   }   /* (non-javadoc)      * @see Org.aopalliance.intercept.methodinterceptor#invoke (org.aopalliance.intercept.MethodInvocation)       */    Public Object invoke (Methodinvocation invocation) throws Throwable {         return ((methodinterceptor) pico.getcomponentinstance (key)). Invoke (invocation);    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.