<chapter id="dynamic">

   <title>Dynamic AOP</title>

   <sect1 id="dyn-2" revision="1">
      <title>Hot Deploment</title>
      <para>
         With JBoss AOP you can change advice and interceptor bindings at runtime.  You can unregister
         existing bindings, and hot deploy new bindings if the given joinpoints have been instrumented.

         Hot-deploying within the JBoss application server is as easy as putting (or removing) a
         <literal>*-aop.xml</literal>
         file or
         <literal>.aop</literal> jar file within the deploy/ directory.

         There is also a runtime API for adding advice bindings at runtime.  Getting an instance of
         <literal>org.jboss.aop.AspectManager.instance()</literal>, you can add your binding.
      </para>
      <programlisting>      org.jboss.aop.advice.AdviceBinding binding = new AdviceBinding("execution(POJO->new(..))", null);
      binding.addInterceptor(SimpleInterceptor.class);
      AspectManager.instance().addBinding(binding);</programlisting>
      <para>
         First, you allocated an
         <literal>AdviceBinding</literal> passing in a pointcut expression.  Then you add
         the interceptor via its class and then add the binding through the AspectManager.  When the binding is added
         the AspectManager will iterate through ever loaded class to see if the pointcut expression matches any
         of the joinpoints within those classes.
      </para>
   </sect1>

   <sect1 id="dyn-3" revision="1">
      <title>Per Instance AOP</title>
      <para>
          Any class that is instrumented by JBoss AOP, is forced to implement the
         <literal>org.jboss.aop.Advised interface.</literal>
      </para>
      <programlisting>public interface InstanceAdvised
{
   public InstanceAdvisor _getInstanceAdvisor();
   public void _setInstanceAdvisor(InstanceAdvisor newAdvisor);
}

public interface Advised extends InstanceAdvised
{
   public Advisor _getAdvisor();
}</programlisting>
      <para>
         The InstanceAdvisor is the interesting interface here.  InstanceAdvisor allows you to insert Interceptors
         at the beginning or the end of the class's advice chain.
      </para>
      <programlisting>public interface InstanceAdvisor
{
   public void insertInterceptor(Interceptor interceptor);
   public void removeInterceptor(String name);
   public void appendInterceptor(Interceptor interceptor);

   public void insertInterceptorStack(String stackName);
   public void removeInterceptorStack(String name);
   public void appendInterceptorStack(String stackName);

   public SimpleMetaData getMetaData();

}</programlisting>
      <para>
         So, there are three advice chains that get executed consecutively in the same java call stack.  Those
         interceptors that are added with the
         <literal>insertInterceptor()</literal> method for the given object
         instance are executed first.  Next, those advices/interceptors that were bound using regular
         <literal>bind</literal>s
         .  Finally, those interceptors added with the
         <literal>appendInterceptor()</literal> method to the object
         instance are executed.  You can also reference
         <literal>stack</literal>s and insert/append full stacks
         into the pre/post chains.
      </para>

      <para>
         Besides interceptors, you can also append untyped metadata to the object instance via the getMetaData() method.
      </para>
   </sect1>

   <sect1 id="dyn-1" revision="1">
      <title>Preparation</title>
      <para>
         Dynamic AOP cannot be used unless the particular joinpoint has been instrumented.  You can force intrumentation
         with the
         <literal>prepare</literal> functionality
      </para>
   </sect1>

</chapter>

