In order Slice to work, you have to write and set up activator of your application. The activator will be responsible for creating and storing injector of your application.
...
Sample activator
Below code presents how an activator of your application should look like:
Code Block | ||
---|---|---|
| ||
package com.cognifide.app.core; import java.util.ArrayList; import java.util.List; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import com.cognifide.slice.api.context.ContextScope; import com.cognifide.slice.api.injector.InjectorRunner; import com.cognifide.slice.commons.SliceModulesFactory; import com.cognifide.slice.core.internal.context.SliceContextScope; import com.cognifide.slice.cq.module.CQModulesFactory; import com.cognifide.slice.validation.ValidationModulesFactory; import com.google.inject.Module; public class Activator implements BundleActivator { private public static final String BUNDLEINJECTOR_NAME_FILTER = "com\\.cognifide\\.app\\..*"; private static final String BASEBUNDLE_NAME_PACKAGEFILTER = "com.cognifide.appapp-.*"; private static final String INJECTORBASE_NAMEPACKAGE = "myappcom.cognifide.app"; @Override public void start(final BundleContext bundleContext) throws Exception { final ContextScope scope = new SliceContextScope(); final InjectorRunner injectorRunner = new InjectorRunner(bundleContext, INJECTOR_NAME, scope); List<Module> sliceModules = SliceModulesFactory.createModules(bundleContext, INJECTOR_NAME, BUNDLE_NAME_FILTER, BASE_PACKAGE); final List<Module> cqModulessliceModules = CQModulesFactorySliceModulesFactory.createModules(bundleContext); // CQModulesFactory is a class coming from Slice Addons https://cognifide.atlassian.net/wiki/display/SLICE/Slice+CQ+Addons+-+4.0 final List<Module> validationModulescqModules = ValidationModulesFactoryCQModulesFactory.createModules(); final List<Module> customModules = createCustomModules(); injectorRunner.installModules(sliceModules); injectorRunner.installModules(cqModules); injectorRunner.installModules(validationModules); injectorRunner.installModules(customModules); injectorRunner.start(); } private List<Module> createCustomModules() { List<Module> applicationModules = new ArrayList<Module>(); //populate the list with your modules return applicationModules; } } |
You have to change the following things to adjust to your application:
INJECTOR_NAME
valueBUNDLE_NAME_FILTER
valueBASE_PACKAGE
valueINJECTOR_NAME
value- populate a list of your application modules
...
Here's what's happening in start method:
...
InjectorRunner
is created. It's a helper which allows you to add modules, creates injector and register it in injector repository. It needsINJECTOR_NAME
to register injector under this String. Please read more about injectors repository to get to know how does it work and what name to choose. List of Slice modules is created. The meaning of constants is as follows:INJECTOR_NAME
- name of the injector under which is registeredOther arguments:BUNDLE_NAME_FILTER
- the pattern that defines which bundles to scan in order to find classes annotated by@SliceResource
annotation. Only bundles which symbolic names matche matches the pattern are scanned.BASE_PACKAGE
- the base package that defines which packages inside bundles specified above to scan in order to find classes annotated by@SliceResource
annotation.List of Slice modules is created.
- Slice and AEM/CQ modules are createdValidation modules are created. If you don't use AEM add-on you don't need to install these modules.
- List of your modules is created. You should instantiate all the custom modules of your application in the
createCustomModules
method. - All the modules are installed
InjectorRunner
is started which means that it creates the Guice injector, install all the provided modules and registered the injector in the injector repository under the specified name.
...
Code Block | ||||
---|---|---|---|---|
| ||||
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package>com.cognifide.app.core*;version="${project.version}"</Export-Package> <Bundle-Activator>com.cognifide.app.core.Activator</Bundle-Activator> <Bundle-SymbolicName>com.cognifide.app.SymbolicName>app-core</Bundle-SymbolicName> </instructions> </configuration> </plugin> |