package org.argeo.cms.internal.kernel;
-import static org.argeo.node.DataModelNamespace.CMS_DATA_MODEL_NAMESPACE;
+import static org.argeo.api.DataModelNamespace.CMS_DATA_MODEL_NAMESPACE;
import java.io.File;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.security.auth.callback.CallbackHandler;
import javax.transaction.UserTransaction;
import org.apache.jackrabbit.commons.cnd.CndImporter;
import org.apache.jackrabbit.core.RepositoryContext;
import org.apache.jackrabbit.core.RepositoryImpl;
+import org.argeo.api.DataModelNamespace;
+import org.argeo.api.NodeConstants;
+import org.argeo.api.NodeDeployment;
+import org.argeo.api.NodeState;
+import org.argeo.api.security.CryptoKeyring;
+import org.argeo.api.security.Keyring;
+import org.argeo.cms.ArgeoNames;
import org.argeo.cms.CmsException;
import org.argeo.jcr.JcrUtils;
-import org.argeo.node.DataModelNamespace;
-import org.argeo.node.NodeConstants;
-import org.argeo.node.NodeDeployment;
-import org.argeo.node.NodeState;
-import org.argeo.node.security.CryptoKeyring;
-import org.argeo.node.security.Keyring;
import org.argeo.osgi.useradmin.UserAdminConf;
import org.argeo.util.LangUtils;
+import org.eclipse.equinox.http.jetty.JettyConfigurator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.service.useradmin.UserAdmin;
import org.osgi.util.tracker.ServiceTracker;
+/** Implementation of a CMS deployment. */
public class CmsDeployment implements NodeDeployment {
- // private final static String LEGACY_JCR_REPOSITORY_ALIAS =
- // "argeo.jcr.repository.alias";
-
private final Log log = LogFactory.getLog(getClass());
private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
private DataModels dataModels;
private DeployConfig deployConfig;
- private HomeRepository homeRepository;
private Long availableSince;
private NodeHttp nodeHttp;
+ private boolean argeoDataModelExtensionsAvailable = false;
+
// Readiness
private boolean nodeAvailable = false;
private boolean userAdminAvailable = false;
public void shutdown() {
if (nodeHttp != null)
nodeHttp.destroy();
- if (deployConfig != null)
- deployConfig.save();
+
+ try {
+ for (ServiceReference<JackrabbitLocalRepository> sr : bc
+ .getServiceReferences(JackrabbitLocalRepository.class, null)) {
+ bc.getService(sr).destroy();
+ }
+ } catch (InvalidSyntaxException e1) {
+ log.error("Cannot sclean repsoitories", e1);
+ }
+
+ try {
+ JettyConfigurator.stopServer(KernelConstants.DEFAULT_JETTY_SERVER);
+ } catch (Exception e) {
+ log.error("Cannot stop default Jetty server.", e);
+ }
+
+ if (deployConfig != null) {
+ new Thread(() -> deployConfig.save(), "Save Argeo Deploy Config").start();
+ }
}
- private void checkReadiness() {
+ /**
+ * Checks whether the deployment is available according to expectations, and
+ * mark it as available.
+ */
+ private synchronized void checkReadiness() {
+ if (isAvailable())
+ return;
if (nodeAvailable && userAdminAvailable && (httpExpected ? httpAvailable : true)) {
String data = KernelUtils.getFrameworkProp(KernelUtils.OSGI_INSTANCE_AREA);
String state = KernelUtils.getFrameworkProp(KernelUtils.OSGI_CONFIGURATION_AREA);
}
// home
- prepareDataModel(NodeConstants.NODE, KernelUtils.openAdminSession(deployedNodeRepository));
+ prepareDataModel(NodeConstants.NODE_REPOSITORY, deployedNodeRepository);
}
private void prepareHomeRepository(RepositoryImpl 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, false);
- // register
- bc.registerService(Repository.class, homeRepository, regProps);
-
- new ServiceTracker<CallbackHandler, CallbackHandler>(bc, CallbackHandler.class, null) {
+ Session adminSession = KernelUtils.openAdminSession(deployedRepository);
+ try {
+ argeoDataModelExtensionsAvailable = Arrays
+ .asList(adminSession.getWorkspace().getNamespaceRegistry().getURIs())
+ .contains(ArgeoNames.ARGEO_NAMESPACE);
+ } catch (RepositoryException e) {
+ log.warn("Cannot check whether Argeo namespace is registered assuming it isn't.", e);
+ argeoDataModelExtensionsAvailable = false;
+ } finally {
+ JcrUtils.logoutQuietly(adminSession);
+ }
- @Override
- public CallbackHandler addingService(ServiceReference<CallbackHandler> reference) {
- NodeKeyRing nodeKeyring = new NodeKeyRing(homeRepository);
- CallbackHandler callbackHandler = bc.getService(reference);
- nodeKeyring.setDefaultCallbackHandler(callbackHandler);
- bc.registerService(LangUtils.names(Keyring.class, CryptoKeyring.class, ManagedService.class),
- nodeKeyring, LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_KEYRING_PID));
- return callbackHandler;
- }
+ // Publish home with the highest service ranking
+ Hashtable<String, Object> regProps = new Hashtable<>();
+ regProps.put(NodeConstants.CN, NodeConstants.EGO_REPOSITORY);
+ regProps.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
+ Repository egoRepository = new EgoRepository(deployedRepository, false);
+ bc.registerService(Repository.class, egoRepository, regProps);
+
+ // Keyring only if Argeo extensions are available
+ if (argeoDataModelExtensionsAvailable) {
+ new ServiceTracker<CallbackHandler, CallbackHandler>(bc, CallbackHandler.class, null) {
+
+ @Override
+ public CallbackHandler addingService(ServiceReference<CallbackHandler> reference) {
+ NodeKeyRing nodeKeyring = new NodeKeyRing(egoRepository);
+ CallbackHandler callbackHandler = bc.getService(reference);
+ nodeKeyring.setDefaultCallbackHandler(callbackHandler);
+ bc.registerService(LangUtils.names(Keyring.class, CryptoKeyring.class, ManagedService.class),
+ nodeKeyring, LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_KEYRING_PID));
+ return callbackHandler;
+ }
- }.open();
+ }.open();
+ }
}
/** Session is logged out. */
- private void prepareDataModel(String cn, Session adminSession) {
+ private void prepareDataModel(String cn, Repository repository) {
+ Session adminSession = KernelUtils.openAdminSession(repository);
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);
+ if (NodeConstants.NODE_REPOSITORY.equals(cn))// process all data models
+ processWiring(cn, adminSession, wiring, processed, false);
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);
+ processWiring(cn, adminSession, wiring, processed, false);
}
}
}
}
}
- private void processWiring(String cn, Session adminSession, BundleWiring wiring, Set<String> processed) {
+ private void processWiring(String cn, Session adminSession, BundleWiring wiring, Set<String> processed,
+ boolean importListedAbstractModels) {
// recursively process requirements first
List<BundleWire> requiredWires = wiring.getRequiredWires(CMS_DATA_MODEL_NAMESPACE);
for (BundleWire wire : requiredWires) {
- processWiring(cn, adminSession, wire.getProviderWiring(), processed);
+ processWiring(cn, adminSession, wire.getProviderWiring(), processed, true);
}
List<String> publishAsLocalRepo = new ArrayList<>();
List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
- for (BundleCapability capability : capabilities) {
+ capabilities: for (BundleCapability capability : capabilities) {
+ if (!importListedAbstractModels
+ && KernelUtils.asBoolean((String) capability.getAttributes().get(DataModelNamespace.ABSTRACT))) {
+ continue capabilities;
+ }
boolean publish = registerDataModelCapability(cn, adminSession, capability, processed);
if (publish)
publishAsLocalRepo.add((String) capability.getAttributes().get(DataModelNamespace.NAME));
boolean publishLocalRepo;
if (isStandalone && name.equals(cn))// includes the node itself
publishLocalRepo = true;
- else if (!isStandalone && cn.equals(NodeConstants.NODE))
+ else if (!isStandalone && cn.equals(NodeConstants.NODE_REPOSITORY))
publishLocalRepo = true;
else
publishLocalRepo = false;
private void publishLocalRepo(String dataModelName, Repository repository) {
Hashtable<String, Object> properties = new Hashtable<>();
- // properties.put(LEGACY_JCR_REPOSITORY_ALIAS, name);
properties.put(NodeConstants.CN, dataModelName);
- if (dataModelName.equals(NodeConstants.NODE))
- properties.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
- LocalRepository localRepository = new LocalRepository(repository, dataModelName);
- bc.registerService(Repository.class, localRepository, properties);
- if (log.isDebugEnabled())
- log.debug("Published data model " + dataModelName);
+ LocalRepository localRepository;
+ String[] classes;
+ if (repository instanceof RepositoryImpl) {
+ localRepository = new JackrabbitLocalRepository((RepositoryImpl) repository, dataModelName);
+ classes = new String[] { Repository.class.getName(), LocalRepository.class.getName(),
+ JackrabbitLocalRepository.class.getName() };
+ } else {
+ localRepository = new LocalRepository(repository, dataModelName);
+ classes = new String[] { Repository.class.getName(), LocalRepository.class.getName() };
+ }
+ bc.registerService(classes, localRepository, properties);
+ if (log.isTraceEnabled())
+ log.trace("Published data model " + dataModelName);
}
@Override
- public Long getAvailableSince() {
+ public synchronized Long getAvailableSince() {
return availableSince;
}
+ public synchronized boolean isAvailable() {
+ return availableSince != null;
+ }
+
private class RepositoryContextStc extends ServiceTracker<RepositoryContext, RepositoryContext> {
public RepositoryContextStc() {
RepositoryContext repoContext = bc.getService(reference);
String cn = (String) reference.getProperty(NodeConstants.CN);
if (cn != null) {
- if (cn.equals(NodeConstants.NODE)) {
+ if (cn.equals(NodeConstants.NODE_REPOSITORY)) {
prepareNodeRepository(repoContext.getRepository());
// TODO separate home repository
prepareHomeRepository(repoContext.getRepository());
nodeAvailable = true;
checkReadiness();
} else {
- prepareDataModel(cn, KernelUtils.openAdminSession(repoContext.getRepository()));
+ prepareDataModel(cn, repoContext.getRepository());
}
}
return repoContext;