package org.gcube.smartgears.configuration.container; import java.io.File; import java.io.InputStream; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; import java.util.stream.Collectors; import org.gcube.smartgears.configuration.ProxyAddress; import org.gcube.smartgears.handlers.container.ContainerHandler; import org.gcube.smartgears.handlers.container.lifecycle.AccountingManager; import org.gcube.smartgears.handlers.container.lifecycle.ProfileContainerManager; import org.gcube.smartgears.persistence.LocalPersistence; import org.gcube.smartgears.persistence.PersistenceWriter; import org.gcube.smartgears.security.AuthorizationProvider; import org.gcube.smartgears.security.AuthorizationProviderFactory; import org.gcube.smartgears.security.Credentials; import org.gcube.smartgears.security.defaults.DefaultAuthorizationProviderFactory; import org.gcube.smartgears.utils.Utils; import org.ini4j.Ini; import org.ini4j.Profile.Section; /** * Binds {@link ContainerConfiguration}s to and from XML serialisations. * * @author Fabio Simeoni * */ public class ContainerConfigurationBinder { public ContainerConfiguration load(InputStream stream) { try { Ini configurator = new Ini(stream); ContainerConfiguration conf = new ContainerConfiguration(); Section nodeSection = configurator.get("node"); if (nodeSection != null ) { BaseConfiguration nodeConf = new BaseConfiguration(); nodeSection.to(nodeConf); conf.setBaseConfiguration(nodeConf); } Section propertiesSection = configurator.get("properties"); if (propertiesSection!=null) conf.setProperties(propertiesSection.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, Entry::getValue))); Section siteSection = configurator.get("site"); if (siteSection != null) { Site siteConf = new Site(); siteSection.to(siteConf); conf.setSite(siteConf); } initAuthorizationPart(configurator, conf); initPersistencePart(configurator, conf); initProxyPart(configurator, conf); //TODO: find a solution for this shit String location = Utils.home()+"/state"; File dir = new File(location); if (!dir.exists()) dir.mkdirs(); conf.setAccountingFallbackLocation(location); // END Shit return conf; }catch (Exception e) { throw new RuntimeException(e); } } private void initProxyPart(Ini configurator, ContainerConfiguration conf) throws Exception{ Section proxySection = configurator.get("proxy"); if (proxySection != null) { ProxyAddress proxyConf = new ProxyAddress(); proxySection.to(proxyConf); conf.setProxy(proxyConf); } } private void initPersistencePart(Ini configurator, ContainerConfiguration conf) throws Exception{ Section persistenceSection = configurator.get("persistence"); if (persistenceSection != null) { String type = persistenceSection.get("class"); if (type ==null) throw new Exception("ini file error: type not found in \"persistence\" section"); PersistenceWriter persistenceWriter; try { Object persistenceImpl = Class.forName(type).newInstance(); persistenceWriter = PersistenceWriter.class.cast(persistenceImpl); }catch (Exception e) { throw new Exception("ini file error: invalid persistence type in \"persistence\" section", e); } persistenceSection.to(persistenceWriter); conf.setPersistenceManager(persistenceWriter); } else { String location = Utils.home()+"/state"; File dir = new File(location); if (!dir.exists()) dir.mkdirs(); conf.setPersistenceManager(new LocalPersistence(location)); } } private void initAuthorizationPart(Ini configurator, ContainerConfiguration conf) throws Exception{ Section authorizationSection = configurator.get("authorization"); if (authorizationSection != null) { String provider = authorizationSection.get("factory"); AuthorizationProviderFactory authProviderFactory; if (provider!=null) { try { Object authProviderImpl = Class.forName(provider).newInstance(); authProviderFactory = AuthorizationProviderFactory.class.cast(authProviderImpl); }catch (Exception e) { throw new Exception("ini file error: invalid provider type in \"authorization\" section", e); } } else authProviderFactory = new DefaultAuthorizationProviderFactory(); String type = authorizationSection.get("credentials.class"); if (type ==null) throw new Exception("ini file error: credentials type not found in \"authorization\" section"); Credentials credentials; try { Object credentialsImpl = Class.forName(type).newInstance(); credentials = Credentials.class.cast(credentialsImpl); }catch (Exception e) { throw new Exception("ini file error: invalid credentials type in \"authorization\" section", e); } authorizationSection.to(credentials, "credentials."); AuthorizationProvider authProvider = authProviderFactory.connect(credentials); conf.setAuthorizationProvider(authProvider); } } /** * Returns the handlers of the container from their XML serialisation. * * @param stream the serialisation * @return the handlers * @throws RuntimeException if the serialisation is invalid */ public List bindHandlers(ClassLoader classloader) { LinkedList handlers = new LinkedList(); //ADDING BASE Handler (order is important) handlers.add(new AccountingManager()); handlers.add(new ProfileContainerManager()); handlers.addAll(scanForContainerHadlers(classloader)); return handlers; } private List scanForContainerHadlers(ClassLoader classloader) throws RuntimeException { //TODO: scan for Container Handler return Collections.emptyList(); } }