From 035bdeb5a42ce2eeb49ba733c7e56edf7a9759af Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Fri, 12 May 2017 15:18:07 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science-ii.research-infrastructures.eu/gcube/branches/common/common-smartgears/2.1@148609 82a268e6-3cf1-43bd-a215-b396298e98cf --- endpoint.xml | Bin 43 -> 0 bytes endpoint.xml.backup | Bin 47 -> 0 bytes pom.xml | 25 +++-- .../java/org/gcube/smartgears/Constants.java | 7 +- .../container/ContainerHandlers.java | 6 + .../managers/ApplicationManager.java | 7 +- .../smartgears/provider/DefaultProvider.java | 104 ++++++++++++++++-- 7 files changed, 123 insertions(+), 26 deletions(-) delete mode 100644 endpoint.xml delete mode 100644 endpoint.xml.backup diff --git a/endpoint.xml b/endpoint.xml deleted file mode 100644 index 283934fb3130164f548ac3ad928e7b595492d89a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 43 ycmZ4UmVvc|K_$)9BE=#x#X#30&B9XGB+O%g3Ebd$_YQgjWIQc^9=l9N&ljEn)M CWDi;Z diff --git a/pom.xml b/pom.xml index f2a116c..5038c76 100644 --- a/pom.xml +++ b/pom.xml @@ -10,9 +10,21 @@ org.gcube.core common-smartgears - 2.1.2-SNAPSHOT + 2.2.0-SNAPSHOT SmartGears - + + + + + org.gcube.distribution + gcube-bom + LATEST + pom + import + + + + distro 7.0.42 @@ -31,43 +43,36 @@ org.gcube.common authorization-client - [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) org.gcube.common common-authorization - [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) org.gcube.data.publishing document-store-lib - [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) org.gcube.accounting accounting-lib - [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) org.slf4j slf4j-api - 1.7.5 org.gcube.resources registry-publisher - [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) org.gcube.resources common-gcore-resources - [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) @@ -79,7 +84,6 @@ org.gcube.core common-scope - [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) @@ -164,7 +168,6 @@ org.apache.maven.plugins maven-assembly-plugin - 2.2 ${distroDirectory}/descriptor.xml diff --git a/src/main/java/org/gcube/smartgears/Constants.java b/src/main/java/org/gcube/smartgears/Constants.java index fa61b32..32f3007 100644 --- a/src/main/java/org/gcube/smartgears/Constants.java +++ b/src/main/java/org/gcube/smartgears/Constants.java @@ -43,6 +43,8 @@ public class Constants { */ public static final String container_handlers_file_path = "/META-INF/container-handlers.xml"; + public static final String container_handlers_file_name = "gcube-container-handlers.xml"; + /** * The library configuration resource path. */ @@ -77,7 +79,8 @@ public class Constants { */ public static final String default_handlers_file_path = "/META-INF/default-handlers.xml"; - + public static final String application_handlers_file_name = "gcube-application-handlers.xml"; + /** * The wildcard exclude directive. */ @@ -202,7 +205,7 @@ public class Constants { public static final String allow="Allow"; - /** + /**jar * The name of the XML media type. */ public static final String plain_text="text/plain"; diff --git a/src/main/java/org/gcube/smartgears/configuration/container/ContainerHandlers.java b/src/main/java/org/gcube/smartgears/configuration/container/ContainerHandlers.java index 77a30fd..73534b8 100644 --- a/src/main/java/org/gcube/smartgears/configuration/container/ContainerHandlers.java +++ b/src/main/java/org/gcube/smartgears/configuration/container/ContainerHandlers.java @@ -41,4 +41,10 @@ public class ContainerHandlers { return this; } + public void mergeWith(ContainerHandlers other){ + List handlers = other.get(); + for (ContainerHandler handler : handlers) + if (!this.get().contains(handler)) + this.get().add(handler); + } } \ No newline at end of file diff --git a/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java b/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java index b4f9ad8..0d8dec5 100644 --- a/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java +++ b/src/main/java/org/gcube/smartgears/managers/ApplicationManager.java @@ -15,6 +15,7 @@ import java.io.ObjectOutputStream; import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Map.Entry; import java.util.Set; import javax.servlet.FilterRegistration; @@ -70,7 +71,11 @@ public class ApplicationManager { try { context = provider().contextFor(container, application); - + + for (Entry servlet : application.getServletRegistrations().entrySet()) + log.trace("servlet {} : {} {} ", application.getServletContextName(),servlet.getKey(), servlet.getValue().getMappings()); + + context.configuration().validate(); if (context.configuration().secure() && diff --git a/src/main/java/org/gcube/smartgears/provider/DefaultProvider.java b/src/main/java/org/gcube/smartgears/provider/DefaultProvider.java index b75d5a2..1466d25 100644 --- a/src/main/java/org/gcube/smartgears/provider/DefaultProvider.java +++ b/src/main/java/org/gcube/smartgears/provider/DefaultProvider.java @@ -3,6 +3,8 @@ package org.gcube.smartgears.provider; import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.smartgears.Constants.configuration_file_path; import static org.gcube.smartgears.Constants.container_configuraton_file_path; +import static org.gcube.smartgears.Constants.container_handlers_file_name; +import static org.gcube.smartgears.Constants.application_handlers_file_name; import static org.gcube.smartgears.Constants.container_handlers_file_path; import static org.gcube.smartgears.Constants.container_profile_file_path; import static org.gcube.smartgears.Constants.default_extensions_file_path; @@ -18,14 +20,22 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.ObjectInputStream; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.UUID; import javax.servlet.ServletContext; - import org.gcube.common.authorization.client.proxy.AuthorizationProxy; import org.gcube.common.events.Hub; import org.gcube.common.events.impl.DefaultHub; +import org.gcube.common.scan.ClasspathScanner; +import org.gcube.common.scan.ClasspathScannerFactory; +import org.gcube.common.scan.matchers.NameMatcher; +import org.gcube.common.scan.resources.ClasspathResource; import org.gcube.informationsystem.publisher.RegistryPublisherFactory; import org.gcube.informationsystem.publisher.ScopedPublisher; import org.gcube.smartgears.configuration.Mode; @@ -60,7 +70,7 @@ import org.slf4j.LoggerFactory; public class DefaultProvider implements Provider { private static Logger log = LoggerFactory.getLogger(Provider.class); - + private ContainerContext containerContext; //TODO: do the same with applicationContext (with a map) @@ -126,8 +136,45 @@ public class DefaultProvider implements Provider { throw new IllegalStateException("invalid distribution: cannot find " + container_handlers_file_path); ContainerConfigurationBinder binder = new ContainerConfigurationBinder(); + ContainerHandlers defaultHandlers = binder.bindHandlers(config); + + ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); + if (currentClassLoader.getParent()!=null && !currentClassLoader.getParent().equals(ClassLoader.getSystemClassLoader())){ + log.trace("probably i'm in a webapp classloader"); + currentClassLoader = currentClassLoader.getParent(); + } + + + try{ + if (currentClassLoader instanceof URLClassLoader){ + URL[] urls = ((URLClassLoader) currentClassLoader).getURLs() ; - return binder.bindHandlers(config); + if (urls!=null && urls.length>0){ + ClasspathScanner scanner = ClasspathScannerFactory.scanner(new HashSet(Arrays.asList(urls))); + Collection resources = scanner.scan(new NameMatcher(container_handlers_file_name)); + + for (URL url: urls) + log.trace("URL: "+ url.toString()); + + if (resources==null || resources.isEmpty()) + log.info("no custom container handlers found in the classpath"); + + for (ClasspathResource res : resources){ + try{ + ContainerHandlers customHandlers= binder.bindHandlers(res.stream()); + defaultHandlers.mergeWith(customHandlers); + log.trace("container hadlers found in {}",res.name()); + }catch(Exception e){ + log.warn("error loading not default container handlers {}",res.name(),e); + } + } + } + } else log.info("this classloader is not instance of {} : ",URLClassLoader.class.getName(), currentClassLoader.getClass().getName()); + }catch(Exception e){ + log.warn("cannot load custom handlers for container from the root classloader",e); + } + + return defaultHandlers; } catch (RuntimeException e) { @@ -208,14 +255,47 @@ public class DefaultProvider implements Provider { InputStream defaultHandlersStream = getClass().getResourceAsStream(default_handlers_file_path); if (defaultHandlersStream == null) - throw new IllegalStateException("invalid distribution: cannot find " + default_handlers_file_path); + throw new IllegalStateException("invalid distribution: cannot find " + default_handlers_file_path); + + ApplicationConfigurationBinder binder = new ApplicationConfigurationBinder(); + + ApplicationHandlers defaultHandlers = binder.bindHandlers(defaultHandlersStream); + + //searching for smartegars related application handlers in the common classloader + ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); + if (currentClassLoader.getParent()!=null && !currentClassLoader.getParent().equals(ClassLoader.getSystemClassLoader())){ + log.trace("probably i'm in a webapp classloader"); + currentClassLoader = currentClassLoader.getParent(); + } + + + try{ + if (currentClassLoader instanceof URLClassLoader){ + URL[] urls = ((URLClassLoader) currentClassLoader).getURLs() ; + + if (urls!=null && urls.length>0){ + ClasspathScanner scanner = ClasspathScannerFactory.scanner(new HashSet(Arrays.asList(urls))); + Collection resources = scanner.scan(new NameMatcher(application_handlers_file_name)); + if (resources==null || resources.isEmpty()) + log.info("no custom smartgears related application handlers found in the classpath"); + + for (ClasspathResource res : resources){ + try{ + ApplicationHandlers customHandlers= binder.bindHandlers(res.stream()); + defaultHandlers.mergeWith(customHandlers); + log.trace("application hadlers found in {}",res.name()); + }catch(Exception e){ + log.warn("error loading smartgears related application handlers {}",res.name(),e); + } + } + } + } + }catch(Exception e){ + log.warn("cannot load smartgears related handlers for application from the root classloader",e); + } InputStream appSpecificHandlersStream = context.application().getResourceAsStream(handlers_file_path); - ApplicationConfigurationBinder binder = new ApplicationConfigurationBinder(); - - ApplicationHandlers defaultHandlers = binder.bindHandlers(defaultHandlersStream); - if (appSpecificHandlersStream !=null ){ defaultHandlers.mergeWith(binder.bindHandlers(appSpecificHandlersStream)); log.trace("{} uses default lifecycle with app spceific handler as it includes {}", context.name(), handlers_file_path); @@ -223,7 +303,7 @@ public class DefaultProvider implements Provider { log.trace("{} uses the default lifecycle as it does not include {}", context.name(), handlers_file_path); return defaultHandlers; - + } catch (RuntimeException e) { @@ -232,7 +312,7 @@ public class DefaultProvider implements Provider { } } - + @Override public ApplicationExtensions extensionsFor(ApplicationContext context) { @@ -357,7 +437,7 @@ public class DefaultProvider implements Provider { return configuration; } -/* + /* @Override public RegistryPublisher publisherFor(ContainerContext context) { return context.configuration().mode()==Mode.online? @@ -369,7 +449,7 @@ public class DefaultProvider implements Provider { return context.configuration().mode()==Mode.online? RegistryPublisherFactory.create(): new OfflinePublisher(); }*/ - + @Override public ScopedPublisher publisherFor(ContainerContext context) { return context.configuration().mode()==Mode.online? RegistryPublisherFactory.scopedPublisher()