This commit is contained in:
Lucio Lelii 2017-02-22 10:59:34 +00:00
parent 527ce352ed
commit b757a258cc
4 changed files with 40 additions and 16 deletions

View File

@ -22,7 +22,6 @@
<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"/>

View File

@ -1,5 +1,8 @@
<ReleaseNotes>
<Changeset component="${build.finalName}" date="2015-01-10">
<Changeset component="${build.finalName}" date="2017-02-22">
<Change>Chaching for proxing of AppManager objects added</Change>
</Changeset>
<Changeset component="org.gcube.core.common-smartgears-app.2-0-0" 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">

22
pom.xml
View File

@ -10,7 +10,7 @@
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>2.0.2-SNAPSHOT</version>
<name>Smartgears Application</name>
@ -38,13 +38,21 @@
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>

View File

@ -1,6 +1,7 @@
package org.gcube.smartgears;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
@ -20,7 +21,9 @@ public class ApplicationManagerProvider {
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<?>>();
private static Map<String, Class<?>> proxyClassMap = Collections.synchronizedMap(new HashMap<String, Class<?>>());
private static Map<String, Object> classProxyObjetMap = Collections.synchronizedMap(new HashMap<String, Object>());
public static synchronized ApplicationManager get(){
final Class<? extends ApplicationManager> applicationManagerClass = retrieveManagerClass();
@ -31,7 +34,16 @@ public class ApplicationManagerProvider {
Object obj;
try {
Class<?> _class = getProxyClass(applicationManagerClass);
obj = _class.newInstance();
if(classProxyObjetMap.containsKey(_class.getCanonicalName())){
obj = classProxyObjetMap.get(_class.getCanonicalName());
logger.trace("getting object {} from cache ",_class.getCanonicalName());
return applicationManagerClass.cast(obj);
} else {
obj = _class.newInstance();
classProxyObjetMap.put(_class.getCanonicalName(), obj);
}
} catch (Exception e) {
throw new RuntimeException("error creating proxy ", e);
}
@ -39,8 +51,8 @@ public class ApplicationManagerProvider {
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());
if (ScopeProvider.instance.get()==null) throw new RuntimeException("error invoking application manager method, scope is not set in this thread");
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);
@ -53,9 +65,11 @@ public class ApplicationManagerProvider {
}
private static Class<?> getProxyClass(Class<? extends ApplicationManager> applicationManagerClass){
if (proxyClassMap.containsKey(applicationManagerClass.getCanonicalName()))
return proxyClassMap.get(applicationManagerClass.getCanonicalName());
if (proxyClassMap.containsKey(applicationManagerClass.getCanonicalName())){
logger.debug("getting proxy class {} for appManager from cache ",applicationManagerClass.getCanonicalName());
return proxyClassMap.get(applicationManagerClass.getCanonicalName());
}
logger.debug("creating new proxy class for appManager "+applicationManagerClass.getCanonicalName());
ProxyFactory proxyfactory = new ProxyFactory();
proxyfactory.setSuperclass(applicationManagerClass);
Class<?> proxyClass=proxyfactory.createClass();