X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=inline;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Finternal%2Fkernel%2FKernel.java;h=8a1cf8e96c5655442caef71be8cb84586f36a721;hb=5146af93007c599cf9edb35d8b173753bc45a52f;hp=68b3fa2891bf784ab5be47a33c254e1fa67fdc33;hpb=86db10fcb2299ebf71d5599a80dc54444b26f893;p=lgpl%2Fargeo-commons.git
diff --git a/org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java b/org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java
index 68b3fa289..8a1cf8e96 100644
--- a/org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java
+++ b/org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java
@@ -2,17 +2,23 @@ package org.argeo.cms.internal.kernel;
import static bitronix.tm.TransactionManagerServices.getTransactionManager;
import static bitronix.tm.TransactionManagerServices.getTransactionSynchronizationRegistry;
+import static java.util.Locale.ENGLISH;
import static org.argeo.cms.internal.kernel.KernelUtils.getFrameworkProp;
-import static org.argeo.cms.internal.kernel.KernelUtils.getOsgiInstancePath;
+import static org.argeo.cms.internal.kernel.KernelUtils.getOsgiInstanceDir;
import static org.argeo.jcr.ArgeoJcrConstants.ALIAS_NODE;
import static org.argeo.jcr.ArgeoJcrConstants.JCR_REPOSITORY_ALIAS;
+import static org.argeo.util.LocaleChoice.asLocaleList;
import static org.osgi.framework.Constants.FRAMEWORK_UUID;
import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Hashtable;
+import java.util.List;
+import java.util.Locale;
import java.util.Map;
import javax.jcr.Repository;
@@ -22,6 +28,7 @@ import javax.transaction.TransactionManager;
import javax.transaction.TransactionSynchronizationRegistry;
import javax.transaction.UserTransaction;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jackrabbit.util.TransientFileFactory;
@@ -30,17 +37,23 @@ import org.argeo.ArgeoLogger;
import org.argeo.cms.CmsException;
import org.argeo.jackrabbit.OsgiJackrabbitRepositoryFactory;
import org.argeo.jcr.ArgeoJcrConstants;
+import org.eclipse.equinox.http.jetty.JettyConfigurator;
+import org.eclipse.equinox.http.jetty.JettyConstants;
import org.eclipse.equinox.http.servlet.ExtendedHttpService;
+import org.eclipse.rap.rwt.application.ApplicationConfiguration;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.log.LogReaderService;
import org.osgi.service.useradmin.UserAdmin;
+import org.osgi.util.tracker.ServiceTracker;
import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.BitronixTransactionSynchronizationRegistry;
-import bitronix.tm.Configuration;
import bitronix.tm.TransactionManagerServices;
/**
@@ -54,7 +67,11 @@ import bitronix.tm.TransactionManagerServices;
*
OS access
*
*/
-final class Kernel implements KernelConstants, ServiceListener {
+final class Kernel implements KernelHeader, KernelConstants, ServiceListener {
+ /*
+ * SERVICE REFERENCES
+ */
+ private ServiceReference configurationAdmin;
/*
* REGISTERED SERVICES
*/
@@ -84,8 +101,13 @@ final class Kernel implements KernelConstants, ServiceListener {
private DataHttp dataHttp;
private KernelThread kernelThread;
+ private Locale defaultLocale = null;
+ private List locales = null;
+
public Kernel() {
nodeSecurity = new NodeSecurity();
+ // log.debug(bc.getDataFile(""));
+ // log.debug(bc.getDataFile("test"));
}
final void init() {
@@ -100,29 +122,45 @@ final class Kernel implements KernelConstants, ServiceListener {
}
private void doInit() {
+ long begin = System.currentTimeMillis();
+ ConfigurationAdmin conf = findConfigurationAdmin();
+ // Use CMS bundle classloader
ClassLoader currentContextCl = Thread.currentThread()
.getContextClassLoader();
Thread.currentThread().setContextClassLoader(
Kernel.class.getClassLoader());
- long begin = System.currentTimeMillis();
+ try {
+ if (nodeSecurity.isFirstInit())
+ firstInit();
- // KernelUtils.logFrameworkProperties(log);
+ defaultLocale = new Locale(getFrameworkProp(I18N_DEFAULT_LOCALE,
+ ENGLISH.getLanguage()));
+ locales = asLocaleList(getFrameworkProp(I18N_LOCALES));
+
+ ServiceTracker logReaderService = new ServiceTracker(
+ bc, LogReaderService.class, null);
+ logReaderService.open();
+ logger = new NodeLogger(logReaderService.getService());
+ logReaderService.close();
- try {
// Initialise services
- logger = new NodeLogger();
- // transactionManager = new SimpleTransactionManager();
- initBitronixTransactionManager();
- repository = new NodeRepository(bc);
+ initTransactionManager();
+ repository = new NodeRepository();
repositoryFactory = new OsgiJackrabbitRepositoryFactory();
userAdmin = new NodeUserAdmin(transactionManager, repository);
// HTTP
+ initWebServer(conf);
ServiceReference sr = bc
.getServiceReference(ExtendedHttpService.class);
if (sr != null)
addHttpService(sr);
+ UserUi userUi = new UserUi();
+ Hashtable props = new Hashtable();
+ props.put("contextName", "user");
+ bc.registerService(ApplicationConfiguration.class, userUi, props);
+
// Kernel thread
kernelThread = new KernelThread(this);
kernelThread.setContextClassLoader(Kernel.class.getClassLoader());
@@ -146,12 +184,53 @@ final class Kernel implements KernelConstants, ServiceListener {
directorsCut(initDuration);
}
- private void initBitronixTransactionManager() {
- Configuration tmConf = TransactionManagerServices.getConfiguration();
+ private void firstInit() {
+ log.info("## FIRST INIT ##");
+ String nodeInit = getFrameworkProp(NODE_INIT);
+ if (nodeInit == null)
+ nodeInit = "../../init";
+ File initDir;
+ if (nodeInit.startsWith("."))
+ initDir = KernelUtils.getExecutionDir(nodeInit);
+ else
+ initDir = new File(nodeInit);
+ // TODO also uncompress archives
+ if (initDir.exists())
+ try {
+ FileUtils.copyDirectory(initDir, getOsgiInstanceDir(),
+ new FileFilter() {
+
+ @Override
+ public boolean accept(File pathname) {
+ if (pathname.getName().equals(".svn")
+ || pathname.getName().equals(".git"))
+ return false;
+ return true;
+ }
+ });
+ log.info("CMS initialized from " + initDir.getCanonicalPath());
+ } catch (IOException e) {
+ throw new CmsException("Cannot initialize from " + initDir, e);
+ }
+ }
+
+ /** Can be null */
+ private ConfigurationAdmin findConfigurationAdmin() {
+ configurationAdmin = bc.getServiceReference(ConfigurationAdmin.class);
+ if (configurationAdmin == null) {
+ return null;
+ }
+ return bc.getService(configurationAdmin);
+ }
+
+ private void initTransactionManager() {
+ bitronix.tm.Configuration tmConf = TransactionManagerServices
+ .getConfiguration();
tmConf.setServerId(getFrameworkProp(FRAMEWORK_UUID));
- File tmBaseDir = new File(getFrameworkProp(TRANSACTIONS_HOME,
- getOsgiInstancePath("transactions")));
+ // File tmBaseDir = new File(getFrameworkProp(TRANSACTIONS_HOME,
+ // getOsgiInstancePath(DIR_TRANSACTIONS)));
+ File tmBaseDir = bc.getDataFile(DIR_TRANSACTIONS);
File tmDir1 = new File(tmBaseDir, "btm1");
tmDir1.mkdirs();
tmConf.setLogPart1Filename(new File(tmDir1, tmDir1.getName() + ".tlog")
@@ -164,6 +243,44 @@ final class Kernel implements KernelConstants, ServiceListener {
transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
}
+ private void initWebServer(ConfigurationAdmin conf) {
+ String httpPort = getFrameworkProp("org.osgi.service.http.port");
+ String httpsPort = getFrameworkProp("org.osgi.service.http.port.secure");
+ try {
+ if (httpPort != null || httpsPort != null) {
+ Hashtable jettyProps = new Hashtable();
+ if (httpPort != null) {
+ jettyProps.put(JettyConstants.HTTP_PORT, httpPort);
+ jettyProps.put(JettyConstants.HTTP_ENABLED, true);
+ }
+ if (httpsPort != null) {
+ jettyProps.put(JettyConstants.HTTPS_PORT, httpsPort);
+ jettyProps.put(JettyConstants.HTTPS_ENABLED, true);
+ jettyProps.put(JettyConstants.SSL_KEYSTORETYPE, "PKCS12");
+ jettyProps.put(JettyConstants.SSL_KEYSTORE, nodeSecurity
+ .getHttpServerKeyStore().getCanonicalPath());
+ jettyProps.put(JettyConstants.SSL_PASSWORD, "changeit");
+ jettyProps.put(JettyConstants.SSL_WANTCLIENTAUTH, true);
+ }
+ if (conf != null) {
+ // TODO make filter more generic
+ String filter = "(" + JettyConstants.HTTP_PORT + "="
+ + httpPort + ")";
+ if (conf.listConfigurations(filter) != null)
+ return;
+ Configuration jettyConf = conf.createFactoryConfiguration(
+ JETTY_FACTORY_PID, null);
+ jettyConf.update(jettyProps);
+ } else {
+ JettyConfigurator.startServer("default", jettyProps);
+ }
+ }
+ } catch (Exception e) {
+ throw new CmsException("Cannot initialize web server on "
+ + httpPortsMsg(httpPort, httpsPort), e);
+ }
+ }
+
private void publish() {
// Listen to service publication (also ours)
bc.addServiceListener(Kernel.this);
@@ -274,27 +391,24 @@ final class Kernel implements KernelConstants, ServiceListener {
Object httpsPort = sr.getProperty("https.port");
dataHttp = new DataHttp(httpService, repository);
if (log.isDebugEnabled())
- log.debug("HTTP " + httpPort
- + (httpsPort != null ? " - HTTPS " + httpsPort : ""));
+ log.debug(httpPortsMsg(httpPort, httpsPort));
+ }
+
+ private String httpPortsMsg(Object httpPort, Object httpsPort) {
+ return "HTTP " + httpPort
+ + (httpsPort != null ? " - HTTPS " + httpsPort : "");
+ }
+
+ @Override
+ public Locale getDefaultLocale() {
+ return defaultLocale;
}
- // private ExtendedHttpService waitForHttpService() {
- // final ServiceTracker st = new
- // ServiceTracker(
- // bc, ExtendedHttpService.class, null);
- // st.open();
- // ExtendedHttpService httpService;
- // try {
- // httpService = st.waitForService(1000);
- // } catch (InterruptedException e) {
- // httpService = null;
- // }
- //
- // if (httpService == null)
- // throw new CmsException("Could not find "
- // + ExtendedHttpService.class + " service.");
- // return httpService;
- // }
+ /** Can be null. */
+ @Override
+ public List getLocales() {
+ return locales;
+ }
final private static void directorsCut(long initDuration) {
// final long ms = 128l + (long) (Math.random() * 128d);