Skip to content

Exo Container Integration

Mihail Kuznetsov edited this page Mar 16, 2015 · 4 revisions

Integration EverRest framework with ExoContainer.

ExoContainer

To integrate EverRest with ExoContainer you need extend org.everrest.exoplatform.servlet.EverrestExoContextListener class and create configuration for ExoContainer. See more details about ExoContainer http://platform30.demo.exoplatform.org/docs/refguide/html/part-kernel.html

Configuration

You need to create configuration file that contains description of your JAX-RS components. ExoContainer is IoC container and it manages all configured components and try resolve their dependencies. NOTE: common behavior of ExoContainer is to have single instance of each component. It minds you not able to inject per-request resources to your JAX-RS services by using annotation on fields or parameters of constructor. If you like to have other behavior you need to use subclass of javax.ws.rs.core.Application to deploy you JAX-RS components. In this case you need add FQN your Application instead of add separate components.

<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd http://www.exoplaform.org/xml/ns/kernel_1_0.xsd" xmlns="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd">
   <component>
      <type>org.everrest.exoplatform.MyResource</type>
      <!-- May create suclass of javax.ws.rs.core.Application to deploy this service. 
       In this case specify FQN of it instead of FQN of resource. -->
   </component>
</configuration>

In example above MyResource is JAX-RS resource and it will work in singleton lifecycle.

Startup ServletContextListener

You need to extends org.everrest.exoplatform.servlet.EverrestExoContextListener and implement method getContainer(). ExoContainer returned from this method will be scanned for JAX-RS components and all acceptable components will be registered in EverRest framework. Here simple example how-to get instance of ExoContainer.

public class MyListener extends EverrestExoContextListener
{
   private StandaloneContainer container;

   @Override
   protected ExoContainer getContainer(ServletContext servletContext)
   {
      try
      {
         URL config = servletContext.getResource("/WEB-INF/classes/conf/my-configuration.xml");
         StandaloneContainer.setConfigurationURL(config.toString());
         container = StandaloneContainer.getInstance();
         return container;
      }
      catch (Exception e)
      {
         throw new RuntimeException("Error of ExoContainer initialization. ", e);
      }
   }

   @Override
   public void contextDestroyed(ServletContextEvent sce)
   {
      if (container != null)
         container.stop();
   }
}

All what you need to specify correct URL of your configuration.

URL config = servletContext.getResource("/WEB-INF/classes/conf/my-configuration.xml");

The use configuration for initialization of ExoContainer.

StandaloneContainer.setConfigurationURL(config.toString());
container = StandaloneContainer.getInstance();

Then you need add your subclass of EverrestExoContextListener in web.xml. Result web.xml may be next:

<listener>
   <listener-class>org.everrest.example.MyListener</listener-class>
</listener>
<servlet>
   <servlet-name>EverrestServlet</servlet-name>
   <servlet-class>org.everrest.core.servlet.EverrestServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>EverrestServlet</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

Once you do EverRest and ExoContainer are set up.

Example of EverRest and ExoContainer integration can be found at subversion repository, see project everrest/everrest-integration/everrest-integration-exo-sample. You can simple run it with command mvn jetty:run. See details how-to try example in README.html in correspond project.