2016-11-25 15:42:29 +01:00
|
|
|
package org.gcube.smartgears.configuration.application;
|
|
|
|
|
2023-03-31 14:16:23 +02:00
|
|
|
import java.io.File;
|
2016-11-25 15:42:29 +01:00
|
|
|
import java.io.InputStream;
|
2022-05-25 18:56:42 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2016-11-25 15:42:29 +01:00
|
|
|
|
2023-02-06 17:34:18 +01:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
2023-03-31 14:16:23 +02:00
|
|
|
import org.gcube.smartgears.configuration.PersistenceConfiguration;
|
2022-05-25 18:56:42 +02:00
|
|
|
import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler;
|
|
|
|
import org.gcube.smartgears.handlers.application.RequestHandler;
|
2022-06-10 17:08:44 +02:00
|
|
|
import org.gcube.smartgears.handlers.application.lifecycle.ApplicationProfileManager;
|
2022-05-25 18:56:42 +02:00
|
|
|
import org.gcube.smartgears.handlers.application.request.RequestAccounting;
|
2023-02-01 17:18:13 +01:00
|
|
|
import org.gcube.smartgears.handlers.application.request.RequestMetrics;
|
2022-05-25 18:56:42 +02:00
|
|
|
import org.gcube.smartgears.handlers.application.request.RequestValidator;
|
2023-03-31 14:16:23 +02:00
|
|
|
import org.gcube.smartgears.persistence.LocalWriter;
|
|
|
|
import org.gcube.smartgears.persistence.LocalWriterConfiguration;
|
|
|
|
import org.gcube.smartgears.utils.Utils;
|
2023-02-06 17:34:18 +01:00
|
|
|
import org.yaml.snakeyaml.Yaml;
|
2016-11-25 15:42:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds {@link ApplicationConfiguration}s to and from XML serialisations.
|
|
|
|
*
|
|
|
|
* @author Fabio Simeoni
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ApplicationConfigurationBinder {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the application configuration from its XML serialisation.
|
|
|
|
*
|
|
|
|
* @param stream the serialisation
|
|
|
|
* @return the configuration
|
|
|
|
* @throws RuntimeException if the serialisation is invalid
|
|
|
|
*/
|
2023-02-06 17:34:18 +01:00
|
|
|
public ApplicationConfiguration load(InputStream stream) {
|
2016-11-25 15:42:29 +01:00
|
|
|
try {
|
2023-02-06 17:34:18 +01:00
|
|
|
Yaml yaml = new Yaml();
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
String mapAsString = mapper.writeValueAsString(yaml.load(stream));
|
2023-03-31 14:16:23 +02:00
|
|
|
|
|
|
|
ApplicationConfiguration conf = mapper.readValue(mapAsString, ApplicationConfiguration.class);
|
|
|
|
|
|
|
|
if (conf.persistenceConfiguration() == null) {
|
|
|
|
String location = String.format("%s/state/%s_%s", Utils.home(), conf.group(), conf.name());
|
|
|
|
File dir = new File(location);
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkdirs();
|
|
|
|
|
|
|
|
conf.persistenceConfiguration(
|
|
|
|
new PersistenceConfiguration(LocalWriter.class, new LocalWriterConfiguration(location)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
} catch (Exception e) {
|
2023-02-06 17:34:18 +01:00
|
|
|
throw new RuntimeException(e);
|
2016-11-25 15:42:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the handlers of the application from their XML serialisation.
|
|
|
|
*
|
|
|
|
* @param stream the serialisation
|
|
|
|
* @return the handlers
|
|
|
|
* @throws RuntimeException if the serialisation is invalid
|
|
|
|
*/
|
2022-05-25 18:56:42 +02:00
|
|
|
public ApplicationHandlers bindHandlers(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
List<RequestHandler> requestHandlers = new LinkedList<RequestHandler>();
|
2023-03-31 14:16:23 +02:00
|
|
|
|
|
|
|
// ADDING BASE Handler (order is important)
|
2023-02-01 17:18:13 +01:00
|
|
|
requestHandlers.add(new RequestMetrics());
|
2022-05-25 18:56:42 +02:00
|
|
|
requestHandlers.add(new RequestValidator());
|
|
|
|
requestHandlers.add(new RequestAccounting());
|
2023-03-31 14:16:23 +02:00
|
|
|
|
|
|
|
// TODO scan RequestHAndler form classloader
|
|
|
|
|
2022-05-25 18:56:42 +02:00
|
|
|
List<ApplicationLifecycleHandler> lifecycleHandlers = new LinkedList<ApplicationLifecycleHandler>();
|
2023-03-31 14:16:23 +02:00
|
|
|
|
|
|
|
// ADDING BASE Handler (order is important)
|
2022-06-10 17:08:44 +02:00
|
|
|
lifecycleHandlers.add(new ApplicationProfileManager());
|
2023-03-31 14:16:23 +02:00
|
|
|
|
|
|
|
// TODO scan ApplicationLifecycleHandler form classloader
|
|
|
|
|
2022-05-25 18:56:42 +02:00
|
|
|
return new ApplicationHandlers(lifecycleHandlers, requestHandlers);
|
2023-03-31 14:16:23 +02:00
|
|
|
|
2016-11-25 15:42:29 +01:00
|
|
|
}
|
2023-03-31 14:16:23 +02:00
|
|
|
|
2016-11-25 15:42:29 +01:00
|
|
|
/**
|
|
|
|
* Returns the extensions of the application from their XML serialisation.
|
|
|
|
*
|
|
|
|
* @param stream the serialisation
|
|
|
|
* @return the extensions
|
|
|
|
* @throws RuntimeException if the serialisation is invalid
|
2023-03-31 14:16:23 +02:00
|
|
|
*
|
|
|
|
* public ApplicationExtensions
|
|
|
|
* bindExtensions(InputStream stream) {
|
|
|
|
*
|
|
|
|
* //collects handler classes Set<Class<?>> classes =
|
|
|
|
* scanForExtensions();
|
|
|
|
*
|
|
|
|
* try {
|
|
|
|
*
|
|
|
|
* JAXBContext ctx =
|
|
|
|
* JAXBContext.newInstance(classes.toArray(new
|
|
|
|
* Class<?>[0]));
|
|
|
|
*
|
|
|
|
* return (ApplicationExtensions)
|
|
|
|
* ctx.createUnmarshaller().unmarshal(stream);
|
|
|
|
*
|
|
|
|
* } catch (JAXBException e) {
|
|
|
|
*
|
|
|
|
* throw unchecked(e);
|
|
|
|
*
|
|
|
|
* } finally { closeSafely(stream); } }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* private Set<Class<?>> scanForExtensions() throws
|
|
|
|
* RuntimeException {
|
|
|
|
*
|
|
|
|
* @SuppressWarnings("all")
|
|
|
|
* ServiceLoader<ApplicationExtension> handlerLoader =
|
|
|
|
* (ServiceLoader)
|
|
|
|
* ServiceLoader.load(ApplicationExtension.class);
|
|
|
|
*
|
|
|
|
* Set<Class<?>> scanned = new HashSet<Class<?>>();
|
|
|
|
*
|
|
|
|
* for (ApplicationExtension handler : handlerLoader) {
|
|
|
|
* Class<?> handlerClass = handler.getClass(); if
|
|
|
|
* (handlerClass.isInterface() ||
|
|
|
|
* handlerClass.getModifiers() == Modifier.ABSTRACT)
|
|
|
|
* continue; else scanned.add(handlerClass); }
|
|
|
|
*
|
|
|
|
* //add top-level configuration
|
|
|
|
* scanned.add(ApplicationExtensions.class);
|
|
|
|
*
|
|
|
|
* return scanned; }
|
|
|
|
*/
|
2016-11-25 15:42:29 +01:00
|
|
|
|
2022-05-25 18:56:42 +02:00
|
|
|
public void scanForApplicationHandlers(ClassLoader currentClassLoader) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
2023-03-31 14:16:23 +02:00
|
|
|
|
2016-11-25 15:42:29 +01:00
|
|
|
}
|