package org.argeo.cms.internal.runtime; import java.net.InetAddress; import java.net.URL; import java.net.UnknownHostException; import javax.security.auth.login.Configuration; import org.argeo.api.cms.CmsLog; import org.argeo.api.cms.CmsState; import org.argeo.cms.auth.ident.IdentClient; import org.osgi.framework.Constants; /** * Implementation of a {@link CmsState}, initialising the required services. */ public class CmsStateImpl implements CmsState { private final static CmsLog log = CmsLog.getLog(CmsStateImpl.class); // REFERENCES private Long availableSince; private String stateUuid; // private final boolean cleanState; private String hostname; public void start() { // Runtime.getRuntime().addShutdownHook(new CmsShutdown()); try { initSecurity(); // initArgeoLogger(); if (log.isTraceEnabled()) log.trace("CMS State started"); this.stateUuid = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID); // this.cleanState = stateUuid.equals(frameworkUuid); try { this.hostname = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { log.error("Cannot set hostname: " + e); } availableSince = System.currentTimeMillis(); if (log.isDebugEnabled()) // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? " // (clean state) " : " ")); log.debug("## CMS starting... (" + stateUuid + ")"); // initI18n(); // initServices(); } catch (RuntimeException e) { log.error("## FATAL: CMS activator failed", e); } } private void initSecurity() { if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) { String jaasConfig = KernelConstants.JAAS_CONFIG; URL url = getClass().getResource(jaasConfig); // System.setProperty(KernelConstants.JAAS_CONFIG_PROP, // url.toExternalForm()); KernelUtils.setJaasConfiguration(url); } // explicitly load JAAS configuration Configuration.getConfiguration(); } public void stop() { if (log.isDebugEnabled()) log.debug("CMS stopping... (" + this.stateUuid + ")"); long duration = ((System.currentTimeMillis() - availableSince) / 1000) / 60; log.info("## ARGEO CMS STOPPED after " + (duration / 60) + "h " + (duration % 60) + "min uptime ##"); } @Override public Long getAvailableSince() { return availableSince; } /* * ACCESSORS */ public String getHostname() { return hostname; } /* * STATIC */ public static IdentClient getIdentClient(String remoteAddr) { if (!IdentClient.isDefaultAuthdPassphraseFileAvailable()) return null; // TODO make passphrase more configurable return new IdentClient(remoteAddr); } }