- private void prepareNodeRepository(Repository deployedNodeRepository) {
- if (availableSince != null) {
- throw new CmsException("Deployment is already available");
- }
-
- // home
- prepareDataModel(NodeConstants.NODE, KernelUtils.openAdminSession(deployedNodeRepository));
- }
-
- private void prepareHomeRepository(Repository deployedRepository) {
- Hashtable<String, String> regProps = new Hashtable<String, String>();
- regProps.put(NodeConstants.CN, NodeConstants.HOME);
- // regProps.put(LEGACY_JCR_REPOSITORY_ALIAS, NodeConstants.HOME);
- homeRepository = new HomeRepository(deployedRepository);
- // register
- bc.registerService(Repository.class, homeRepository, regProps);
-
- new ServiceTracker<CallbackHandler, CallbackHandler>(bc, CallbackHandler.class, null) {
-
- @Override
- public CallbackHandler addingService(ServiceReference<CallbackHandler> reference) {
- NodeKeyRing nodeKeyring = new NodeKeyRing(homeRepository);
- CallbackHandler callbackHandler = bc.getService(reference);
- nodeKeyring.setDefaultCallbackHandler(callbackHandler);
- bc.registerService(LangUtils.names(CryptoKeyring.class, ManagedService.class), nodeKeyring,
- LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_KEYRING_PID));
- return callbackHandler;
- }
-
- }.open();
- }
-
- /** Session is logged out. */
- private void prepareDataModel(String cn, Session adminSession) {
- try {
- Set<String> processed = new HashSet<String>();
- bundles: for (Bundle bundle : bc.getBundles()) {
- BundleWiring wiring = bundle.adapt(BundleWiring.class);
- if (wiring == null)
- continue bundles;
- if (NodeConstants.NODE.equals(cn))// process all data models
- processWiring(cn, adminSession, wiring, processed);
- else {
- List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
- for (BundleCapability capability : capabilities) {
- String dataModelName = (String) capability.getAttributes().get(DataModelNamespace.NAME);
- if (dataModelName.equals(cn))// process only own data model
- processWiring(cn, adminSession, wiring, processed);
- }
- }
- }
- } finally {
- JcrUtils.logoutQuietly(adminSession);
- }
- }
-
- private void processWiring(String cn, 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(cn, adminSession, wire.getProviderWiring(), processed);
- }
- List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
- for (BundleCapability capability : capabilities) {
- registerDataModelCapability(cn, adminSession, capability, processed);
- }
- }
-
- private void registerDataModelCapability(String cn, Session adminSession, BundleCapability capability,
- Set<String> processed) {
- Map<String, Object> attrs = capability.getAttributes();
- String name = (String) attrs.get(DataModelNamespace.NAME);
- if (processed.contains(name)) {
- if (log.isTraceEnabled())
- log.trace("Data model " + name + " has already been processed");
- return;
- }
-
- // CND
- String path = (String) attrs.get(DataModelNamespace.CND);
- if (path != null) {
- File dataModel = bc.getBundle().getDataFile("dataModels/" + path);
- if (!dataModel.exists()) {
- URL url = capability.getRevision().getBundle().getResource(path);
- if (url == null)
- throw new CmsException("No data model '" + name + "' found under path " + path);
- try (Reader reader = new InputStreamReader(url.openStream())) {
- CndImporter.registerNodeTypes(reader, adminSession, true);
- processed.add(name);
- dataModel.getParentFile().mkdirs();
- dataModel.createNewFile();
- if (log.isDebugEnabled())
- log.debug("Registered CND " + url);
- } catch (Exception e) {
- throw new CmsException("Cannot import CND " + url, e);
- }
- }
- }
-
- if (KernelUtils.asBoolean((String) attrs.get(DataModelNamespace.ABSTRACT)))
- return;
- // Non abstract
- boolean isStandalone = deployConfig.isStandalone(name);
- boolean publishLocalRepo;
- if (isStandalone && name.equals(cn))// includes the node itself
- publishLocalRepo = true;
- else if (!isStandalone && cn.equals(NodeConstants.NODE))
- publishLocalRepo = true;
- else
- publishLocalRepo = false;
-
- if (publishLocalRepo) {
- Hashtable<String, Object> properties = new Hashtable<>();
- // properties.put(LEGACY_JCR_REPOSITORY_ALIAS, name);
- properties.put(NodeConstants.CN, name);
- if (name.equals(NodeConstants.NODE))
- properties.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
- LocalRepository localRepository = new LocalRepository(adminSession.getRepository(), capability);
- bc.registerService(Repository.class, localRepository, properties);
- if (log.isDebugEnabled())
- log.debug("Published data model " + name);
- }
- }
-