From c239cb468f846930611f51facce92f28d3f32e62 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Fri, 28 Feb 2014 17:42:13 -0800 Subject: [PATCH] CSPACE-6329: Preliminary work toward writing per-repo Nuxeo config files. Entirely untested at present. --- .../5.5-HF07/config/proto-repo-config.xml | 28 ++++++++++ .../common/api/JEEServerDeployment.java | 3 ++ .../services/common/ServiceMain.java | 51 ++++++++++++++++-- .../services/common/XmlTools.java | 52 +++++++++++++++++-- 4 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 3rdparty/nuxeo/nuxeo-server/5.5-HF07/config/proto-repo-config.xml diff --git a/3rdparty/nuxeo/nuxeo-server/5.5-HF07/config/proto-repo-config.xml b/3rdparty/nuxeo/nuxeo-server/5.5-HF07/config/proto-repo-config.xml new file mode 100644 index 000000000..88fca73b0 --- /dev/null +++ b/3rdparty/nuxeo/nuxeo-server/5.5-HF07/config/proto-repo-config.xml @@ -0,0 +1,28 @@ + + + + + + + + + + @XA_DATASOURCE@ + @DB_URL@ + @DB_SERVER_HOSTNAME@ + nuxeo + @NUXEO_USER@ + @NUXEO_PW@ + + + + + + The default repository + + + diff --git a/services/common-api/src/main/java/org/collectionspace/services/common/api/JEEServerDeployment.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/JEEServerDeployment.java index fc6108180..a66a24464 100644 --- a/services/common-api/src/main/java/org/collectionspace/services/common/api/JEEServerDeployment.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/JEEServerDeployment.java @@ -12,6 +12,9 @@ public interface JEEServerDeployment { public final static String NUXEO_CLIENT_DIR = "nuxeo-client"; public final static String NUXEO_SERVER_DIR = "nuxeo-server"; + public final static String NUXEO_CONFIG_DIR = "config"; + public final static String NUXEO_SERVER_CONFIG_DIR = NUXEO_SERVER_DIR + File.separator + NUXEO_CONFIG_DIR; + public final static String NUXEO_PROTOTYPE_CONFIG_FILENAME = "proto-repo-config.xml"; public final static String NUXEO_PLUGINS_DIR = "plugins"; public final static String NUXEO_SERVER_PLUGINS_DIR = NUXEO_SERVER_DIR + File.separator + NUXEO_PLUGINS_DIR; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java index cd0ec5bd6..d46bb6563 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -6,8 +6,6 @@ package org.collectionspace.services.common; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; @@ -22,6 +20,7 @@ import javax.sql.DataSource; import org.collectionspace.authentication.AuthN; +import org.collectionspace.services.common.api.JEEServerDeployment; import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon; import org.collectionspace.services.common.config.ConfigReader; import org.collectionspace.services.common.config.ConfigUtils; @@ -43,7 +42,7 @@ import org.collectionspace.services.nuxeo.client.java.NuxeoConnectorEmbedded; import org.collectionspace.services.nuxeo.client.java.TenantRepository; import org.jboss.resteasy.spi.ResteasyProviderFactory; -import org.apache.tomcat.dbcp.dbcp.BasicDataSource; +import org.dom4j.Document; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,7 +70,7 @@ public class ServiceMain { private static final String SERVER_HOME_PROPERTY = "catalina.home"; private static final boolean USE_APP_GENERATED_CONFIG = true; - + private ServiceMain() { //empty } @@ -156,6 +155,42 @@ public class ServiceMain { readConfig(); setDataSources(); propagateConfiguredProperties(); + + // Ensure that Nuxeo config files exist for each repository + // specified in tenant bindings + File prototypeNuxeoConfigFile = + new File(getNuxeoConfigDir() + File.separator + getNuxeoProtoConfigFilename()); + logger.warn("Prototype Nuxeo config file path=" + prototypeNuxeoConfigFile.getCanonicalPath()); + if (! prototypeNuxeoConfigFile.canRead()) { + // FIXME: Handle this error appropriately here. + } + logger.warn("Can read prototype Nuxeo config file."); + Document prototypeDoc = XmlTools.fileToXMLDocument(prototypeNuxeoConfigFile); + // FIXME: Can the variable below reasonably be made a class variable? Its value + // is used in at least one other method in this class. + Hashtable tenantBindingTypeMap = tenantBindingConfigReader.getTenantBindings(); + for (TenantBindingType tbt : tenantBindingTypeMap.values()) { + List repositoryNameList = ConfigUtils.getRepositoryNameList(tbt); + if (repositoryNameList != null && repositoryNameList.isEmpty() == false) { + Document repoDoc = null; + for (String repositoryName : repositoryNameList) { + logger.warn("Repository name=" + repositoryName); + repoDoc = (Document) prototypeDoc.clone(); + logger.warn("Before attribute edits=\n" + repoDoc.asXML()); + // FIXME: Set up constants and/or methods for XPath expressions, element and attribute names + repoDoc = XmlTools.setAttributeValue(repoDoc, "/component/extension[@point='repository']/repository", "name", repositoryName); + logger.warn("After first attribute edit=\n" + repoDoc.asXML()); + repoDoc = XmlTools.setAttributeValue(repoDoc, "/component/extension[@point='repository']/repository/repository", "name", repositoryName); + logger.warn("After second attribute edit=\n" + repoDoc.asXML()); + repoDoc = XmlTools.setAttributeValue(repoDoc, "/component/extension[@point='repositories']/repository", "name", repositoryName); + logger.warn("After third attribute edit=\n" + repoDoc.asXML()); + // FIXME: Edit additional element and/or attribute values. + // FIXME: Emit serialized XML and write it to an appropriately named file + // in the Nuxeo server config directory. + repoDoc = null; + } + } + } // // Start up and initialize our embedded Nuxeo server instance // @@ -368,6 +403,14 @@ public class ServiceMain { return serverRootDir; } + public String getNuxeoConfigDir() { + return getServerRootDir() + JEEServerDeployment.NUXEO_SERVER_CONFIG_DIR; + } + + public String getNuxeoProtoConfigFilename() { + return JEEServerDeployment.NUXEO_PROTOTYPE_CONFIG_FILENAME; + } + /** * @return the server resources path */ diff --git a/services/common/src/main/java/org/collectionspace/services/common/XmlTools.java b/services/common/src/main/java/org/collectionspace/services/common/XmlTools.java index 6a344e80f..b51e672c1 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/XmlTools.java +++ b/services/common/src/main/java/org/collectionspace/services/common/XmlTools.java @@ -1,15 +1,18 @@ package org.collectionspace.services.common; +import java.io.File; +import java.io.StringWriter; import org.collectionspace.services.common.api.Tools; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.Node; import org.dom4j.io.HTMLWriter; import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; -import java.io.StringWriter; - public class XmlTools { @@ -85,7 +88,7 @@ public class XmlTools { } /** - * Returns an XML document, when provided with a String + * Returns a dom4j XML document, when provided with a String * representation of that XML document. * @param xmlStr A String representation of an XML document. * @return A dom4j XML document. @@ -100,6 +103,49 @@ public class XmlTools { } return doc; } + + /** + * Returns a dom4j XML document, when provided with a file + * containing a well-formed XML document. + * @param file A file containing a well-formed XML document. + * @return A dom4j XML document. + */ + public static Document fileToXMLDocument(File file) throws Exception { + Document doc = null; + try { + SAXReader reader = new SAXReader(); + doc = reader.read(file); + } catch (DocumentException e) { + throw e; + } + return doc; + } + + /** + * Sets the value of a specified attribute in a dom4j XML document. + * @param doc A dom4j XML document. + * @param xpathExpr An XPath expression intended to match a single element + * in the XML document, in the default namespace. + * @param attributeName An attribute name. + * @param attributeValue The value that the attribute should contain. + * @return The document with the specified attribute of the matched element, if + * any, set to the provided value. If the attribute doesn't already exist, + * it will be created and assigned the provided value. If the provided + * value is null, the attribute, if any, will be removed. + */ + public static Document setAttributeValue(Document doc, String xpathExpr, + String attributeName, String attributeValue) { + if (Tools.isBlank(xpathExpr) || Tools.isBlank(attributeName)) { + return doc; + } + Node node = doc.selectSingleNode(xpathExpr); + if ((node == null) || (node.getNodeType() != Node.ELEMENT_NODE)) { + return doc; + } + Element element = (Element) node; + element.addAttribute(attributeName, attributeValue); + return doc; + } public static String prettyPrint(String xml) throws Exception { Document doc = textToXMLDocument(xml); -- 2.47.3