+ private void normalInit() {
+ ConfigurationAdmin conf = findConfigurationAdmin();
+
+ // HTTP
+ initWebServer(conf);
+ // ServiceReference<ExtendedHttpService> sr =
+ // bc.getServiceReference(ExtendedHttpService.class);
+ // if (sr != null)
+ // addHttpService(sr);
+ // else
+ // log.warn("No http service found");
+
+ // Initialise services
+ initTransactionManager();
+
+ try {
+ Configuration nodeConf = conf.getConfiguration(ArgeoJcrConstants.REPO_PID_NODE);
+ if (nodeConf.getProperties() == null) {
+ Dictionary<String, ?> props = getNodeConfigFromFrameworkProperties();
+ if (props == null) {
+ // TODO interactive configuration
+ if (log.isDebugEnabled())
+ log.debug("No argeo.node.repo.type=localfs|h2|postgresql|memory"
+ + " property defined, entering interactive mode...");
+ return;
+ }
+ nodeConf.update(props);
+ }
+ } catch (IOException e) {
+ throw new CmsException("Cannot get configuration", e);
+ }
+
+ ManagedJackrabbitRepository nodeRepo = new ManagedJackrabbitRepository();
+ String[] clazzes = { ManagedService.class.getName(), Repository.class.getName(),
+ JackrabbitRepository.class.getName() };
+ Hashtable<String, String> serviceProps = new Hashtable<String, String>();
+ serviceProps.put(Constants.SERVICE_PID, ArgeoJcrConstants.REPO_PID_NODE);
+ serviceProps.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, ArgeoJcrConstants.ALIAS_NODE);
+ ServiceRegistration<?> nodeSr = bc.registerService(clazzes, nodeRepo, serviceProps);
+ nodeRepo.waitForInit();
+
+ new JackrabbitDataModel(bc).prepareDataModel(nodeRepo);
+ prepareDataModel(nodeRepo);
+
+ repository = (JackrabbitRepository) bc.getService(nodeSr.getReference());
+
+ if (repository == null)
+ repository = new NodeRepository();
+ if (repositoryFactory == null) {
+ repositoryFactory = new OsgiJackrabbitRepositoryFactory();
+ repositoryFactory.setBundleContext(bc);
+ }
+ userAdmin = new NodeUserAdmin(transactionManager, repository);
+
+ // 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 Dictionary<String, ?> getNodeConfigFromFrameworkProperties() {
+ String repoType = KernelUtils.getFrameworkProp(KernelConstants.NODE_REPO_PROP_PREFIX + RepoConf.type.name());
+ if (repoType == null)
+ return null;
+
+ Hashtable<String, Object> props = new Hashtable<String, Object>();
+ for (RepoConf repoConf : RepoConf.values()) {
+ String value = KernelUtils.getFrameworkProp(KernelConstants.NODE_REPO_PROP_PREFIX + repoConf.name());
+ if (value != null)
+ props.put(repoConf.name(), value);
+ }
+ return props;
+ }
+
+ private void prepareDataModel(ManagedJackrabbitRepository nodeRepo) {
+ Session adminSession = null;
+ try {
+ Set<String> processed = new HashSet<String>();
+ adminSession = nodeRepo.login();
+ bundles: for (Bundle bundle : bc.getBundles()) {
+ BundleWiring wiring = bundle.adapt(BundleWiring.class);
+ if (wiring == null) {
+ if (log.isTraceEnabled())
+ log.error("No wiring for " + bundle.getSymbolicName());
+ continue bundles;
+ }
+ processWiring(adminSession, wiring, processed);
+ }
+ } catch (RepositoryException e) {
+ throw new CmsException("Cannot prepare data model", e);
+ } finally {
+ JcrUtils.logoutQuietly(adminSession);
+ }
+ }
+
+ private void processWiring(Session adminSession, BundleWiring wiring, Set<String> processed) {
+ // recursively process requirements first
+ List<BundleWire> requiredWires = wiring.getRequiredWires(CMS_DATA_MODEL_NAMESPACE);
+ for (BundleWire wire : requiredWires) {
+ processWiring(adminSession, wire.getProviderWiring(), processed);
+ // registerCnd(adminSession, wire.getCapability(), processed);
+ }
+ List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
+ for (BundleCapability capability : capabilities) {
+ registerCnd(adminSession, capability, processed);
+ }
+ }
+
+ private void registerCnd(Session adminSession, BundleCapability capability, Set<String> processed) {
+ Map<String, Object> attrs = capability.getAttributes();
+ String name = attrs.get(DataModelNamespace.CAPABILITY_NAME_ATTRIBUTE).toString();
+ if (processed.contains(name)) {
+ if (log.isTraceEnabled())
+ log.trace("Data model " + name + " has already been processed");
+ return;
+ }
+ String path = attrs.get(DataModelNamespace.CAPABILITY_CND_ATTRIBUTE).toString();
+ URL url = capability.getRevision().getBundle().getResource(path);
+ try (Reader reader = new InputStreamReader(url.openStream())) {
+ CndImporter.registerNodeTypes(reader, adminSession, true);
+ processed.add(name);
+ if (log.isDebugEnabled())
+ log.debug("Registered CND " + url);
+ } catch (Exception e) {
+ throw new CmsException("Cannot read cnd " + url, e);
+ }
+
+ Hashtable<String, Object> properties = new Hashtable<>();
+ properties.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, name);
+ bc.registerService(Repository.class, adminSession.getRepository(), properties);
+ if (log.isDebugEnabled())
+ log.debug("Published data model " + name);
+ }
+
+ 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);
+ }
+