1.2.4. Containers

In order to access a service you need to use a Container. Let's see the Java files in https://github.com/exoplatform/kernel/tree/stable/2.3.x/exo.kernel.container/src/main/java/org/exoplatform/container.

Among the classes you see in this directory, you only will be interested in these three container types:

Use only one container Even if there are several container types, you always use exactly one. The RootContainer is never directly used. It depends on the execution mode if you use the PortalContainer or the StandaloneContainer. You will ask how to find out the execution mode in your application and how to manage these two modes. It's easy, you don't have to worry about it because the ExoContainerContext class provides a static method that allows you to get the right container from anywhere (see How to get the container).

PicoContainer All containers inherit from the ExoContainer class which itself inherits from a PicoContainer. PicoContainer is a framework which allows eXo to apply the IoC (Inversion of Control) principles. The precise implementation of any service is unknown at compile time. Various implementations can be used, eXo supplies different implementations but they also may be delivered by other vendors. The decision which service to use during runtime is made in configuration files.

These configuration files are read by the container, the container adds all services to a list or more exactly a Java HashTable. It's completely correct to suppose that the configuration.xml you already saw plays an important role. But there are more places where a configuration for a service can be defined as you see in the next section.

How to get the container In your Java code you have to use

ExoContainer myContainer = ExoContainerContext.getCurrentContainer();

in order to access the current container. It doesn't greatly matter to your application if the current container is a PortalContainer or a StandaloneContainer. Once you have your container you may access any service registered in the container using

MyService myService = (MyService) myContainer.getComponentInstance(MyService.class);

You easily realize that MyService.class is the name of the service interface.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus