package org.gcube.smartgears.configuration.application; import java.io.InputStream; import java.util.LinkedList; import java.util.List; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler; import org.gcube.smartgears.handlers.application.RequestHandler; import org.gcube.smartgears.handlers.application.lifecycle.ApplicationProfileManager; import org.gcube.smartgears.handlers.application.request.RequestAccounting; import org.gcube.smartgears.handlers.application.request.RequestMetrics; import org.gcube.smartgears.handlers.application.request.RequestValidator; import org.yaml.snakeyaml.Yaml; /** * 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 */ public ApplicationConfiguration load(InputStream stream) { try { Yaml yaml = new Yaml(); ObjectMapper mapper = new ObjectMapper(); String mapAsString = mapper.writeValueAsString(yaml.load(stream)); return mapper.readValue(mapAsString, ApplicationConfiguration.class); }catch (Exception e) { throw new RuntimeException(e); } } /** * Returns the handlers of the application from their XML serialisation. * * @param stream the serialisation * @return the handlers * @throws RuntimeException if the serialisation is invalid */ public ApplicationHandlers bindHandlers(ClassLoader classLoader) { List requestHandlers = new LinkedList(); //ADDING BASE Handler (order is important) requestHandlers.add(new RequestMetrics()); requestHandlers.add(new RequestValidator()); requestHandlers.add(new RequestAccounting()); //TODO scan RequestHAndler form classloader List lifecycleHandlers = new LinkedList(); //ADDING BASE Handler (order is important) lifecycleHandlers.add(new ApplicationProfileManager()); //TODO scan ApplicationLifecycleHandler form classloader return new ApplicationHandlers(lifecycleHandlers, requestHandlers); } /** * Returns the extensions of the application from their XML serialisation. * * @param stream the serialisation * @return the extensions * @throws RuntimeException if the serialisation is invalid public ApplicationExtensions bindExtensions(InputStream stream) { //collects handler classes Set> 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> scanForExtensions() throws RuntimeException { @SuppressWarnings("all") ServiceLoader handlerLoader = (ServiceLoader) ServiceLoader.load(ApplicationExtension.class); Set> scanned = new HashSet>(); 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; } */ public void scanForApplicationHandlers(ClassLoader currentClassLoader) { // TODO Auto-generated method stub } }