branch for release 4.1
git-svn-id: http://svn.research-infrastructures.eu/d4science/gcube/branches/common/common-smartgears-app/2.0@132260 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
commit
a8cdc9e6de
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/common-service"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>common-smartgears-app</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
|
||||
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,7 @@
|
|||
#Sun Aug 04 12:32:12 CEST 2013
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/main/resources=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding//src/test/resources=UTF-8
|
||||
encoding/<project>=UTF-8
|
|
@ -0,0 +1,12 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.7
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.source=1.7
|
|
@ -0,0 +1,5 @@
|
|||
#Sun Aug 04 12:32:12 CEST 2013
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||
<wb-module deploy-name="common-smartgears-app">
|
||||
<wb-resource deploy-path="/" source-path="/src/main/java"/>
|
||||
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
|
||||
</wb-module>
|
||||
</project-modules>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="1.7"/>
|
||||
<installed facet="jst.utility" version="1.0"/>
|
||||
</faceted-project>
|
|
@ -0,0 +1,2 @@
|
|||
disabled=06target
|
||||
eclipse.preferences.version=1
|
|
@ -0,0 +1 @@
|
|||
${gcube.license}
|
|
@ -0,0 +1,66 @@
|
|||
The gCube System - ${name}
|
||||
--------------------------------------------------
|
||||
|
||||
${description}
|
||||
|
||||
${gcube.description}
|
||||
|
||||
${gcube.funding}
|
||||
|
||||
|
||||
Version
|
||||
--------------------------------------------------
|
||||
|
||||
${version} (${buildDate})
|
||||
|
||||
Please see the file named "changelog.xml" in this directory for the release notes.
|
||||
|
||||
|
||||
Authors
|
||||
--------------------------------------------------
|
||||
|
||||
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy
|
||||
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy
|
||||
|
||||
|
||||
Maintainers
|
||||
-----------
|
||||
|
||||
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy
|
||||
|
||||
|
||||
Download information
|
||||
--------------------------------------------------
|
||||
|
||||
Source code is available from SVN:
|
||||
${scm.url}
|
||||
|
||||
Binaries can be downloaded from the gCube website:
|
||||
${gcube.website}
|
||||
|
||||
|
||||
Installation
|
||||
--------------------------------------------------
|
||||
|
||||
Installation documentation is available on-line in the gCube Wiki:
|
||||
${gcube.wikiRoot}/Smartgears
|
||||
|
||||
|
||||
Documentation
|
||||
--------------------------------------------------
|
||||
|
||||
Documentation is available on-line in the gCube Wiki:
|
||||
${gcube.wikiRoot}/Smartgears
|
||||
|
||||
|
||||
Support
|
||||
--------------------------------------------------
|
||||
|
||||
Bugs and support requests can be reported in the gCube issue tracking tool:
|
||||
${gcube.issueTracking}
|
||||
|
||||
|
||||
Licensing
|
||||
--------------------------------------------------
|
||||
|
||||
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.
|
|
@ -0,0 +1,8 @@
|
|||
<ReleaseNotes>
|
||||
<Changeset component="${build.finalName}" date="2015-01-10">
|
||||
<Change>Added support for initialization using security token</Change>
|
||||
</Changeset>
|
||||
<Changeset component="org.gcube.core.common-smartgears-app.1-0-0" date="2013-10-24">
|
||||
<Change>First Release</Change>
|
||||
</Changeset>
|
||||
</ReleaseNotes>
|
|
@ -0,0 +1,32 @@
|
|||
<assembly
|
||||
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||
<id>servicearchive</id>
|
||||
<formats>
|
||||
<format>tar.gz</format>
|
||||
</formats>
|
||||
<baseDirectory>/</baseDirectory>
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>${distroDirectory}</directory>
|
||||
<outputDirectory>/</outputDirectory>
|
||||
<useDefaultExcludes>true</useDefaultExcludes>
|
||||
<includes>
|
||||
<include>README</include>
|
||||
<include>LICENSE</include>
|
||||
<include>changelog.xml</include>
|
||||
<include>profile.xml</include>
|
||||
</includes>
|
||||
<fileMode>755</fileMode>
|
||||
<filtered>true</filtered>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
<files>
|
||||
<file>
|
||||
<source>target/${build.finalName}.${project.packaging}</source>
|
||||
<outputDirectory>/${artifactId}</outputDirectory>
|
||||
</file>
|
||||
|
||||
</files>
|
||||
</assembly>
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<ID />
|
||||
<Type>Service</Type>
|
||||
<Profile>
|
||||
<Description>${description}</Description>
|
||||
<Class>Common</Class>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>1.0.0</Version>
|
||||
<Packages>
|
||||
<Software>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>${version}</Version>
|
||||
<MavenCoordinates>
|
||||
<groupId>${groupId}</groupId>
|
||||
<artifactId>${artifactId}</artifactId>
|
||||
<version>${version}</version>
|
||||
</MavenCoordinates>
|
||||
<Files>
|
||||
<File>${build.finalName}.jar</File>
|
||||
</Files>
|
||||
</Software>
|
||||
</Packages>
|
||||
</Profile>
|
||||
</Resource>
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.gcube.tools</groupId>
|
||||
<artifactId>maven-parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-smartgears-app</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<name>Smartgears Application</name>
|
||||
|
||||
|
||||
<properties>
|
||||
<distroDirectory>distro</distroDirectory>
|
||||
</properties>
|
||||
|
||||
<scm>
|
||||
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</connection>
|
||||
<developerConnection>scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</developerConnection>
|
||||
<url>http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</url>
|
||||
</scm>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.reflections</groupId>
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.9.9-RC1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-smartgears</artifactId>
|
||||
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-profile</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>copy-resources</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>target</outputDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>${distroDirectory}</directory>
|
||||
<filtering>true</filtering>
|
||||
<includes>
|
||||
<include>profile.xml</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>servicearchive</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,10 @@
|
|||
package org.gcube.smartgears;
|
||||
|
||||
|
||||
public interface ApplicationManager {
|
||||
|
||||
public void onInit();
|
||||
|
||||
public void onShutdown();
|
||||
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package org.gcube.smartgears;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import javassist.util.proxy.MethodHandler;
|
||||
import javassist.util.proxy.ProxyFactory;
|
||||
import javassist.util.proxy.ProxyObject;
|
||||
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.smartgears.annotations.ManagedBy;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ApplicationManagerProvider {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ApplicationManagerProvider.class);
|
||||
|
||||
static Map<String, Map<String, Future<ApplicationManager>>> appManagerMap = new HashMap<String, Map<String, Future<ApplicationManager>>>();
|
||||
|
||||
private static Map<String, Class<?>> proxyClassMap = new HashMap<String, Class<?>>();
|
||||
|
||||
public static synchronized ApplicationManager get(){
|
||||
final Class<? extends ApplicationManager> applicationManagerClass = retrieveManagerClass();
|
||||
return get(applicationManagerClass);
|
||||
}
|
||||
|
||||
public static synchronized ApplicationManager get(final Class<? extends ApplicationManager> applicationManagerClass){
|
||||
Object obj;
|
||||
try {
|
||||
Class<?> _class = getProxyClass(applicationManagerClass);
|
||||
obj = _class.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error creating proxy ", e);
|
||||
}
|
||||
|
||||
MethodHandler handler = new MethodHandler() {
|
||||
@Override
|
||||
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
|
||||
logger.debug("context is {}", ScopeProvider.instance.get());
|
||||
logger.debug("applicationManagerClass is {}",applicationManagerClass.getCanonicalName());
|
||||
Future<ApplicationManager> appManagerFuture = appManagerMap.get(applicationManagerClass.getCanonicalName()).get(ScopeProvider.instance.get());
|
||||
logger.debug("appmanager future is null? {}", appManagerFuture==null);
|
||||
logger.debug("thisMethod is null? {}", thisMethod==null);
|
||||
return thisMethod.invoke(appManagerFuture.get(), args);
|
||||
}
|
||||
};
|
||||
((ProxyObject)obj).setHandler(handler);
|
||||
|
||||
return applicationManagerClass.cast(obj);
|
||||
}
|
||||
|
||||
private static Class<?> getProxyClass(Class<? extends ApplicationManager> applicationManagerClass){
|
||||
if (proxyClassMap.containsKey(applicationManagerClass.getCanonicalName()))
|
||||
return proxyClassMap.get(applicationManagerClass.getCanonicalName());
|
||||
|
||||
ProxyFactory proxyfactory = new ProxyFactory();
|
||||
proxyfactory.setSuperclass(applicationManagerClass);
|
||||
Class<?> proxyClass=proxyfactory.createClass();
|
||||
proxyClassMap.put(applicationManagerClass.getCanonicalName(), proxyClass);
|
||||
return proxyClass;
|
||||
|
||||
}
|
||||
|
||||
private static Class<? extends ApplicationManager> retrieveManagerClass(){
|
||||
String classname = Thread.currentThread().getStackTrace()[3].getClassName();
|
||||
logger.trace("managed servlet caller is {}",classname);
|
||||
ManagedBy annotation;
|
||||
try {
|
||||
annotation = Class.forName(classname).getAnnotation(ManagedBy.class);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException("error initializing ApplicationManager",e);
|
||||
}
|
||||
|
||||
if (annotation == null){
|
||||
logger.error(" {} is not managed by an ApplicationManager", classname);
|
||||
throw new RuntimeException(classname+" is not managed by an ApplicationManager");
|
||||
}
|
||||
|
||||
return annotation.value();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package org.gcube.smartgears;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
import org.gcube.smartgears.annotations.ManagedBy;
|
||||
import org.gcube.smartgears.context.application.ApplicationContext;
|
||||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.TypeAnnotationsScanner;
|
||||
import org.reflections.util.ClasspathHelper;
|
||||
import org.reflections.util.ConfigurationBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@WebListener
|
||||
public class ContextListener implements ServletContextListener {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(ContextListener.class);
|
||||
|
||||
RegisterApplicationManagerObserver observer;
|
||||
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
|
||||
ApplicationContext context = (ApplicationContext) sce.getServletContext().getAttribute(Constants.context_attribute);
|
||||
|
||||
if (context==null) {
|
||||
String msg = sce.getServletContext().getContextPath()+" is a gCube-aware application but is not managed as a gCube resource: missing or invalid context attribute "+Constants.context_attribute;
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
|
||||
log.info("configuring context provider for {}",context.name());
|
||||
ContextProvider.set(context);
|
||||
|
||||
|
||||
retrieveAndRegisterManagers(context);
|
||||
}
|
||||
|
||||
|
||||
private void retrieveAndRegisterManagers(ApplicationContext context) {
|
||||
Reflections reflection = new ConfigurationBuilder().addUrls(ClasspathHelper.forClassLoader()).setScanners(new TypeAnnotationsScanner()).build();
|
||||
|
||||
Set<Class<?>> toInitialize = reflection.getTypesAnnotatedWith(ManagedBy.class);
|
||||
Set<Class<? extends ApplicationManager>> managers = new HashSet<Class<? extends ApplicationManager>>();
|
||||
for (Class<?> initializer: toInitialize ){
|
||||
ManagedBy manageBy = initializer.getAnnotation(ManagedBy.class);
|
||||
log.info("ApplicationManager added {} @ {}", manageBy.value().getSimpleName(), context.name());
|
||||
managers.add(manageBy.value());
|
||||
}
|
||||
if (managers.size()>0){
|
||||
observer = new RegisterApplicationManagerObserver(managers, context.configuration().startTokens());
|
||||
context.events().subscribe(observer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
if (observer!=null){
|
||||
ApplicationContext context = (ApplicationContext) sce.getServletContext().getAttribute(Constants.context_attribute);
|
||||
context.events().unsubscribe(observer);
|
||||
observer.onStop(context);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package org.gcube.smartgears;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.gcube.common.events.Hub;
|
||||
import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
|
||||
import org.gcube.smartgears.context.Properties;
|
||||
import org.gcube.smartgears.context.application.ApplicationContext;
|
||||
import org.gcube.smartgears.context.container.ContainerContext;
|
||||
import org.gcube.smartgears.lifecycle.application.ApplicationLifecycle;
|
||||
import org.gcube.smartgears.persistence.Persistence;
|
||||
|
||||
/**
|
||||
* Embedded in an application, makes available its context as a gCube resource.
|
||||
*
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
public class ContextProvider {
|
||||
|
||||
private static ApplicationContextProxy context = new ApplicationContextProxy();
|
||||
|
||||
/**
|
||||
* Returns the application context.
|
||||
* @return the context.
|
||||
*/
|
||||
public static ApplicationContext get() {
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the application context.
|
||||
* @param context the context;
|
||||
*
|
||||
* @throws IllegalStateException if the context has not been set because the resource is not managed as a gCube resource
|
||||
*/
|
||||
public static void set(ApplicationContext context) {
|
||||
|
||||
if (context==null)
|
||||
throw new IllegalStateException("no context set for this application: are you sure the application is managed as a gCube resource?");
|
||||
|
||||
ContextProvider.context.delegate = context;
|
||||
}
|
||||
|
||||
|
||||
static class ApplicationContextProxy implements ApplicationContext{
|
||||
|
||||
private ApplicationContext delegate;
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return delegate.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApplicationConfiguration configuration() {
|
||||
return delegate.configuration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T profile(Class<T> type) {
|
||||
return delegate.profile(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApplicationLifecycle lifecycle() {
|
||||
return delegate.lifecycle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hub events() {
|
||||
return delegate.events();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Persistence persistence() {
|
||||
return delegate.persistence();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletContext application() {
|
||||
return delegate.application();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContainerContext container() {
|
||||
return delegate.container();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties properties() {
|
||||
return delegate.properties();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String id() {
|
||||
return delegate.id();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,195 @@
|
|||
package org.gcube.smartgears;
|
||||
|
||||
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.events.Observes;
|
||||
import org.gcube.common.events.Observes.Kind;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.smartgears.context.application.ApplicationContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class RegisterApplicationManagerObserver {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(RegisterApplicationManagerObserver.class);
|
||||
|
||||
private static ExecutorService service = Executors.newCachedThreadPool();
|
||||
|
||||
|
||||
private Set<Class<? extends ApplicationManager>> managersClass ;
|
||||
|
||||
private Map<String, List<Future<ApplicationManager>>> instanciatedManagerPerScope = new HashMap<String, List<Future<ApplicationManager>>>();
|
||||
|
||||
public RegisterApplicationManagerObserver(
|
||||
Set<Class<? extends ApplicationManager>> managersClass, Collection<String> startingTokens) {
|
||||
super();
|
||||
this.managersClass = managersClass;
|
||||
for (String startingToken : startingTokens )
|
||||
this.onRegistation(startingToken);
|
||||
|
||||
}
|
||||
|
||||
@Observes(value=Constants.token_registered, kind=Kind.safe)
|
||||
public synchronized void onRegistation(final String securityToken){
|
||||
log.info("token registered called with token {}", securityToken);
|
||||
List<Future<ApplicationManager>> futureList = new ArrayList<Future<ApplicationManager>>();
|
||||
|
||||
try {
|
||||
final String context = authorizationService().get(securityToken).getContext();
|
||||
|
||||
|
||||
for (Class<? extends ApplicationManager> appManager: managersClass){
|
||||
Future<ApplicationManager> appManagerFuture = service.submit(new InitAppManager(securityToken, context, appManager));
|
||||
log.info("intializing app in context {} with token {} ",context, securityToken);
|
||||
|
||||
futureList.add(appManagerFuture);
|
||||
if (ApplicationManagerProvider.appManagerMap.containsKey(appManager.getCanonicalName()))
|
||||
ApplicationManagerProvider.appManagerMap.get(appManager.getCanonicalName()).put(context, appManagerFuture);
|
||||
else {
|
||||
Map<String, Future<ApplicationManager>> tokenFutureMap = new HashMap<String, Future<ApplicationManager>>();
|
||||
tokenFutureMap.put(context, appManagerFuture);
|
||||
ApplicationManagerProvider.appManagerMap.put(appManager.getCanonicalName(), tokenFutureMap);
|
||||
}
|
||||
}
|
||||
if (!futureList.isEmpty())
|
||||
instanciatedManagerPerScope.put(context, futureList);
|
||||
} catch (ObjectNotFound e1) {
|
||||
log.error("it should never happen (token has just been created)",e1);
|
||||
throw new RuntimeException("it should never happen (token has just been created",e1);
|
||||
} catch (Exception e1) {
|
||||
log.error("something failed getting token",e1);
|
||||
throw new RuntimeException("something failed getting token",e1);
|
||||
}
|
||||
}
|
||||
|
||||
@Observes(value=Constants.token_removed, kind=Kind.critical)
|
||||
public synchronized void onRemove(final String securityToken){
|
||||
|
||||
try {
|
||||
final String context = authorizationService().get(securityToken).getContext();
|
||||
|
||||
for (Future<ApplicationManager> appManager: instanciatedManagerPerScope.get(context)){
|
||||
service.execute(new ShutDownAppManager(securityToken, context, appManager));
|
||||
ApplicationManagerProvider.appManagerMap.get(appManager).remove(context);
|
||||
}
|
||||
|
||||
instanciatedManagerPerScope.remove(context);
|
||||
SecurityTokenProvider.instance.reset();
|
||||
} catch (ObjectNotFound e1) {
|
||||
log.error("it should never happen (token has just been created)",e1);
|
||||
throw new RuntimeException("it should never happen (token has just been created",e1);
|
||||
} catch (Exception e1) {
|
||||
log.error("something failed getting token",e1);
|
||||
throw new RuntimeException("something failed getting token",e1);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void onStop(ApplicationContext appContext){
|
||||
|
||||
for (String token :appContext.configuration().startTokens()){
|
||||
try {
|
||||
String context = authorizationService().get(token).getContext();
|
||||
for (Future<ApplicationManager> appManagerEntry: instanciatedManagerPerScope.get(context)){
|
||||
try{
|
||||
log.info("stoppping {} in context {} ",appContext.name(), context);
|
||||
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
ScopeProvider.instance.set(context);
|
||||
try {
|
||||
appManagerEntry.get().onShutdown();
|
||||
log.info("manager {} correctly suhtdown on context {}",appContext.name(), context);
|
||||
} catch (Exception e){
|
||||
log.warn("problem calling onShutdown for context {}", context, e);
|
||||
}
|
||||
}catch(Exception e){
|
||||
log.error("error retrieving token on shutdown on context {}", context,e);
|
||||
throw new RuntimeException("error retrieving token on shutdown",e);
|
||||
}finally{
|
||||
ScopeProvider.instance.reset();
|
||||
SecurityTokenProvider.instance.reset();
|
||||
}
|
||||
}
|
||||
} catch (ObjectNotFound e1) {
|
||||
log.error("token not found : {}",token,e1);
|
||||
} catch (Exception e1) {
|
||||
log.error("something failed getting token {}",token,e1);
|
||||
}
|
||||
}
|
||||
unregister();
|
||||
}
|
||||
|
||||
public void unregister(){
|
||||
service.shutdownNow();
|
||||
}
|
||||
|
||||
public class InitAppManager implements Callable<ApplicationManager>{
|
||||
|
||||
private Class<? extends ApplicationManager> managerClass;
|
||||
private String securityToken;
|
||||
private String context;
|
||||
|
||||
public InitAppManager(String securityToken, String context, Class<? extends ApplicationManager> managerClass){
|
||||
this.managerClass = managerClass;
|
||||
this.securityToken = securityToken;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApplicationManager call() throws Exception {
|
||||
SecurityTokenProvider.instance.set(securityToken);
|
||||
ScopeProvider.instance.set(context);
|
||||
ApplicationManager manager = managerClass.newInstance();
|
||||
try {
|
||||
log.info("calling on onInit of {} on token {}",manager.getClass().getCanonicalName(), securityToken);
|
||||
manager.onInit();
|
||||
} catch (Exception e) {
|
||||
log.warn("error on onInit of {} on token {}",manager.getClass().getCanonicalName(), securityToken, e);
|
||||
} finally{
|
||||
ScopeProvider.instance.reset();
|
||||
SecurityTokenProvider.instance.reset();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
}
|
||||
|
||||
public class ShutDownAppManager implements Runnable{
|
||||
|
||||
private Future<ApplicationManager> appManager;
|
||||
private String securityToken;
|
||||
private String context;
|
||||
|
||||
public ShutDownAppManager(String securityToken, String context, Future<ApplicationManager> appManager){
|
||||
this.appManager = appManager;
|
||||
this.securityToken = securityToken;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
SecurityTokenProvider.instance.set(securityToken);
|
||||
ScopeProvider.instance.set(context);
|
||||
try {
|
||||
log.info("calling on ShutDown of {} on token {}",appManager.getClass().getCanonicalName(), securityToken);
|
||||
appManager.get().onShutdown();
|
||||
} catch (Exception e) {
|
||||
log.warn("error on onShutdown of {} on token {}",appManager.getClass().getCanonicalName(), securityToken, e);
|
||||
} finally{
|
||||
ScopeProvider.instance.reset();
|
||||
SecurityTokenProvider.instance.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package org.gcube.smartgears.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.gcube.smartgears.ApplicationManager;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface ManagedBy {
|
||||
|
||||
public Class<? extends ApplicationManager> value();
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.gcube.smartgears.stateful;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ResourceBinder {
|
||||
|
||||
@Test
|
||||
public void bind() throws Exception{
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue