import java.io.Reader;
import java.lang.management.ManagementFactory;
import java.net.URL;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.security.auth.callback.CallbackHandler;
+import javax.transaction.UserTransaction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ManagedService;
+import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.UserAdmin;
import org.osgi.util.tracker.ServiceTracker;
ServiceTracker<?, ?> userAdminSt = new ServiceTracker<UserAdmin, UserAdmin>(bc, UserAdmin.class, null) {
@Override
public UserAdmin addingService(ServiceReference<UserAdmin> reference) {
+ UserAdmin userAdmin = super.addingService(reference);
+ addStandardSystemRoles(userAdmin);
userAdminAvailable = true;
checkReadiness();
- return super.addingService(reference);
+ return userAdmin;
}
};
// userAdminSt.open();
KernelUtils.asyncOpen(confAdminSt);
}
+ private void addStandardSystemRoles(UserAdmin userAdmin) {
+ // we assume UserTransaction is already available (TODO make it more robust)
+ UserTransaction userTransaction = bc.getService(bc.getServiceReference(UserTransaction.class));
+ try {
+ userTransaction.begin();
+ Role adminRole = userAdmin.getRole(NodeConstants.ROLE_ADMIN);
+ if (adminRole == null) {
+ adminRole = userAdmin.createRole(NodeConstants.ROLE_ADMIN, Role.GROUP);
+ }
+ if (userAdmin.getRole(NodeConstants.ROLE_USER_ADMIN) == null) {
+ Group userAdminRole = (Group) userAdmin.createRole(NodeConstants.ROLE_USER_ADMIN, Role.GROUP);
+ userAdminRole.addMember(adminRole);
+ }
+ userTransaction.commit();
+ } catch (Exception e) {
+ try {
+ userTransaction.rollback();
+ } catch (Exception e1) {
+ // silent
+ }
+ throw new CmsException("Cannot add standard system roles", e);
+ }
+ }
+
private void loadIpaJaasConfiguration() {
if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
String jaasConfig = KernelConstants.JAAS_CONFIG_IPA;
public void shutdown() {
if (nodeHttp != null)
nodeHttp.destroy();
- if (deployConfig != null)
- deployConfig.save();
+
+ 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);
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));
+ bc.registerService(LangUtils.names(Keyring.class, CryptoKeyring.class, ManagedService.class),
+ nodeKeyring, LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_KEYRING_PID));
return callbackHandler;
}
for (BundleWire wire : requiredWires) {
processWiring(cn, adminSession, wire.getProviderWiring(), processed);
}
+
+ List<String> publishAsLocalRepo = new ArrayList<>();
List<BundleCapability> capabilities = wiring.getCapabilities(CMS_DATA_MODEL_NAMESPACE);
for (BundleCapability capability : capabilities) {
- registerDataModelCapability(cn, adminSession, capability, processed);
+ boolean publish = registerDataModelCapability(cn, adminSession, capability, processed);
+ if (publish)
+ publishAsLocalRepo.add((String) capability.getAttributes().get(DataModelNamespace.NAME));
}
+ // Publish all at once, so that bundles with multiple CNDs are consistent
+ for (String dataModelName : publishAsLocalRepo)
+ publishLocalRepo(dataModelName, adminSession.getRepository());
}
- private void registerDataModelCapability(String cn, Session adminSession, BundleCapability capability,
+ private boolean 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;
+ return false;
}
// CND
}
if (KernelUtils.asBoolean((String) attrs.get(DataModelNamespace.ABSTRACT)))
- return;
+ return false;
// Non abstract
boolean isStandalone = deployConfig.isStandalone(name);
boolean publishLocalRepo;
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);
- }
+ return publishLocalRepo;
+ }
+
+ 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.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() {