+ private void normalInit() {
+ ConfigurationAdmin conf = findConfigurationAdmin();
+ // Initialise services
+ initTransactionManager();
+ if (repository == null)
+ repository = new NodeRepository();
+ if (repositoryFactory == null)
+ repositoryFactory = new OsgiJackrabbitRepositoryFactory();
+ userAdmin = new NodeUserAdmin(transactionManager, repository);
+
+ // HTTP
+ initWebServer(conf);
+ ServiceReference<ExtendedHttpService> sr = bc
+ .getServiceReference(ExtendedHttpService.class);
+ if (sr != null)
+ addHttpService(sr);
+
+ // ADMIN UIs
+ UserUi userUi = new UserUi();
+ Hashtable<String, String> props = new Hashtable<String, String>();
+ props.put("contextName", "user");
+ bc.registerService(ApplicationConfiguration.class, userUi, props);
+
+ // Kernel thread
+ kernelThread = new KernelThread(this);
+ kernelThread.setContextClassLoader(Kernel.class.getClassLoader());
+ kernelThread.start();
+
+ // Publish services to OSGi
+ publish();
+ }
+
+ private boolean isMaintenance() {
+ String startLevel = KernelUtils.getFrameworkProp("osgi.startLevel");
+ if (startLevel == null)
+ return false;
+ int bundleStartLevel = bc.getBundle().adapt(BundleStartLevel.class)
+ .getStartLevel();
+ // int frameworkStartLevel =
+ // bc.getBundle(0).adapt(BundleStartLevel.class)
+ // .getStartLevel();
+ int frameworkStartLevel = Integer.parseInt(startLevel);
+ // int frameworkStartLevel = bc.getBundle(0)
+ // .adapt(FrameworkStartLevel.class).getStartLevel();
+ return bundleStartLevel == frameworkStartLevel;
+ }
+
+ private void maintenanceInit() {
+ log.info("## MAINTENANCE ##");
+ bc.addServiceListener(Kernel.this);
+ initWebServer(null);
+ MaintenanceUi maintenanceUi = new MaintenanceUi();
+ Hashtable<String, String> props = new Hashtable<String, String>();
+ props.put("contextName", "maintenance");
+ bc.registerService(ApplicationConfiguration.class, maintenanceUi, props);
+ }
+
+ private void firstInit() {
+ log.info("## FIRST INIT ##");
+ String nodeInit = getFrameworkProp(NODE_INIT);
+ if (nodeInit == null)
+ nodeInit = "../../init";
+ if (nodeInit.startsWith("http")) {
+ remoteFirstInit(nodeInit);
+ return;
+ }
+ 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);
+ }
+ }
+
+ private void remoteFirstInit(String uri) {
+ try {
+ repository = new NodeRepository();
+ repositoryFactory = new OsgiJackrabbitRepositoryFactory();
+ Repository remoteRepository = ArgeoJcrUtils.getRepositoryByUri(
+ repositoryFactory, uri);
+ Session remoteSession = remoteRepository
+ .login(new SimpleCredentials("root", "demo".toCharArray()),
+ "main");
+ Session localSession = this.repository.login();
+ // FIXME register node type
+ // if (false)
+ // CndImporter.registerNodeTypes(null, localSession);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ remoteSession.exportSystemView("/", out, true, false);
+ ByteArrayInputStream in = new ByteArrayInputStream(
+ out.toByteArray());
+ localSession.importXML("/", in,
+ ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
+ // JcrUtils.copy(remoteSession.getRootNode(),
+ // localSession.getRootNode());
+ } catch (Exception e) {
+ throw new CmsException("Cannot first init from " + uri, 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(DIR_TRANSACTIONS)));
+ File tmBaseDir = bc.getDataFile(DIR_TRANSACTIONS);
+ File tmDir1 = new File(tmBaseDir, "btm1");
+ tmDir1.mkdirs();
+ tmConf.setLogPart1Filename(new File(tmDir1, tmDir1.getName() + ".tlog")
+ .getAbsolutePath());
+ File tmDir2 = new File(tmBaseDir, "btm2");
+ tmDir2.mkdirs();
+ tmConf.setLogPart2Filename(new File(tmDir2, tmDir2.getName() + ".tlog")
+ .getAbsolutePath());
+ transactionManager = getTransactionManager();
+ 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<String, Object> jettyProps = new Hashtable<String, Object>();
+ 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);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private void publish() {
+ // Listen to service publication (also ours)
+ bc.addServiceListener(Kernel.this);
+
+ // Logging
+ loggerReg = bc.registerService(ArgeoLogger.class, logger, null);
+ // Transaction
+ tmReg = bc.registerService(TransactionManager.class,
+ transactionManager, null);
+ utReg = bc.registerService(UserTransaction.class, transactionManager,
+ null);
+ tsrReg = bc.registerService(TransactionSynchronizationRegistry.class,
+ transactionSynchronizationRegistry, null);
+ // User admin
+ userAdminReg = bc.registerService(UserAdmin.class, userAdmin,
+ userAdmin.currentState());
+ // JCR
+ Hashtable<String, String> regProps = new Hashtable<String, String>();
+ regProps.put(JCR_REPOSITORY_ALIAS, ALIAS_NODE);
+ repositoryReg = (ServiceRegistration<? extends Repository>) bc
+ .registerService(new String[] { Repository.class.getName(),
+ JackrabbitRepository.class.getName() }, repository,
+ regProps);
+ repositoryFactoryReg = bc.registerService(RepositoryFactory.class,
+ repositoryFactory, null);
+ }
+