  <chapter id="jboss_integration">
    <title>Running JBoss Cache within JBoss Application Server</title>

    <para>If JBoss Cache is run in JBoss AS then JBoss Cache can be deployed as an
    MBean. The steps below illustrate how to do this. We do not deploy JBoss Cache as a
    Service Archive (SAR), but as a JAR (<literal>jboss-cache.jar</literal> in the lib directory) and
    an XML file defining the MBean. Of course, JBoss Cache can also be
    deployed as a SAR, or even as part of a WAR, EJB or EAR.</para>

    <para>First, the <literal>jboss-cache.jar</literal> file has to be copied to the /lib
    directory and JBoss AS has to be restarted. Then a regular JBoss Cache
    configuration file in XML format has to be copied to the /deploy
    directory. The XML file format is the same as discussed in
    the Configuration chapter.</para>

    <para>In order to be used from a client such as a servlet in the Tomcat web
    container inside the same JBoss container, JMX can be used:</para>

    <programlisting>MBeanServer server=MBeanServerLocator.locateJBoss();
TreeCacheMBean cache;
cache=(TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, "jboss.cache:service=TreeCache", server);
cache.put("/a/b/c", null);</programlisting>

    <para>The MBeanServerLocator class is a helper to find the (only) JBoss
    MBean server inside the current VM. The static create() method creates a
    dynamic proxy to the given interface and uses JMX to dynamically dispatch
    methods invoked against the generated interface. The name used to look up
    the MBean is the same as defined in the configuration file.</para>

    <section>
		<title>Running as an MBean</title>
      <para>If JBoss Cache is run inside of JBoss AS (as an MBean), we can bind it
      into JNDI using JrmpProxyFactory, just like any other MBean. Below is an
      example of how to do this: <programlisting> &lt;mbean
      code="org.jboss.invocation.jrmp.server.JRMPProxyFactory"
      name="mydomain:service=proxyFactory,type=jrmp,target=factory"&gt;
      &lt;attribute
      name="InvokerName"&gt;jboss:service=invoker,type=jrmp&lt;/attribute&gt;
      &lt;attribute
      name="TargetName"&gt;jboss.cache:service=TreeCache&lt;/attribute&gt;
      &lt;attribute name="JndiName"&gt;MyCache&lt;/attribute&gt; &lt;attribute
      name="InvokeTargetMethod"&gt;true&lt;/attribute&gt; &lt;attribute
      name="ExportedInterface"&gt;org.jboss.cache.TreeCacheMBean&lt;/attribute&gt;
      &lt;attribute name="ClientInterceptors"&gt; &lt;iterceptors&gt;
      &lt;interceptor&gt;org.jboss.proxy.ClientMethodInterceptor&lt;/interceptor&gt;
      &lt;interceptor&gt;org.jboss.proxy.SecurityInterceptor&lt;/interceptor&gt;
      &lt;interceptor&gt;org.jboss.invocation.InvokerInterceptor&lt;/interceptor&gt;
      &lt;/iterceptors&gt; &lt;/attribute&gt;
      &lt;depends&gt;jboss:service=invoker,type=jrmp&lt;/depends&gt;
      &lt;depends&gt;jboss.cache:service=TreeCache&lt;/depends&gt;
      &lt;/mbean&gt; </programlisting></para>

      <para>The <literal>InvokerName</literal> attribute needs to point to a
      valid JBoss invoker MBean. <literal>TargetName</literal> is the JMX name
      of the MBean that needs to be bound into JNDI.
      <literal>JndiName</literal> is the name under which the MBean will be
      bound, and <literal>ExportedInterface</literal> is the interface name of
      the MBean.</para>
    </section>
</chapter>