import javax.sql.DataSource;\r
\r
import org.collectionspace.authentication.AuthN;\r
-\r
+ import org.collectionspace.services.common.api.JEEServerDeployment;\r
+ import org.collectionspace.services.common.api.Tools;\r
import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;\r
import org.collectionspace.services.common.config.ConfigReader;\r
import org.collectionspace.services.common.config.ConfigUtils;\r
import org.collectionspace.services.nuxeo.client.java.NuxeoConnectorEmbedded;\r
import org.collectionspace.services.nuxeo.client.java.TenantRepository;\r
import org.jboss.resteasy.spi.ResteasyProviderFactory;\r
- import org.apache.tomcat.dbcp.dbcp.BasicDataSource;\r
-\r
+ import org.dom4j.Document;\r
-\r
import org.slf4j.Logger;\r
import org.slf4j.LoggerFactory;\r
\r
private TenantBindingConfigReaderImpl tenantBindingConfigReader;\r
private UriTemplateRegistry uriTemplateRegistry = new UriTemplateRegistry();\r
\r
- private static final String SERVER_HOME_PROPERTY = "catalina.home";\r
- private static final boolean USE_APP_GENERATED_CONFIG = true;\r
\r
+ private static final String COMPONENT_EXTENSION_XPATH = "/component/extension[@point='%s']";\r
+ private static final String REPOSITORY_EXTENSION_POINT_XPATH =\r
+ String.format(COMPONENT_EXTENSION_XPATH, "repository");\r
+ private static final String REPOSITORIES_EXTENSION_POINT_XPATH =\r
+ String.format(COMPONENT_EXTENSION_XPATH, "repositories");\r
+ \r
private ServiceMain() {\r
//empty\r
}\r
}\r
\r
private void initialize() throws Exception {\r
- if (logger.isTraceEnabled() == true) {\r
- System.out.print("About to initialize ServiceMain singleton - Pausing 5 seconds for you to attached the debugger");\r
- long startTime, currentTime;\r
- currentTime = startTime = System.currentTimeMillis();\r
- long stopTime = startTime + 5 * 1000; //5 seconds\r
- do {\r
- if (currentTime % 1000 == 0) {\r
- System.out.print(".");\r
- }\r
- currentTime = System.currentTimeMillis();\r
- } while (currentTime < stopTime);\r
- \r
- System.out.println();\r
- System.out.println("Resuming cspace services initialization.");\r
- }\r
- \r
+ // set our root directory\r
setServerRootDir();\r
- readConfig();\r
- setDataSources();\r
+ \r
+ // read in and set our Services config\r
+ readAndSetServicesConfig();\r
+ \r
+ // Set our AuthN's datasource to for the cspaceDataSource\r
+ AuthN.setDataSource(JDBCTools.getDataSource(JDBCTools.CSPACE_DATASOURCE_NAME));\r
+ \r
+ // Please document this step\r
propagateConfiguredProperties();\r
- \r
- // Create each tenant's Nuxeo database\r
+ createOrUpdateNuxeoRepositoryConfigFiles();\r
+ \r
+ createNuxeoDatabases();\r
+\r
//\r
- // Start up and initialize our embedded Nuxeo server instance\r
+ // Start up and initialize our embedded Nuxeo instance.\r
//\r
if (getClientType().equals(ClientType.JAVA)) {\r
nuxeoConnector = NuxeoConnectorEmbedded.getInstance();\r
}\r
return uriTemplateRegistry;\r
}\r
-\r
+ /**\r
+ * Ensure that Nuxeo repository configuration files exist for each repository\r
+ * specified in tenant bindings. Create or update these files, as needed.\r
+ */\r
+ private void createOrUpdateNuxeoRepositoryConfigFiles() throws Exception {\r
+ \r
+ // Get the prototype copy of the Nuxeo repository config file.\r
+ File prototypeNuxeoConfigFile =\r
+ new File(getCspaceServicesConfigDir() + File.separator + getNuxeoProtoConfigFilename());\r
+ // FIXME: Consider checking for the presence of existing configuration files,\r
+ // rather than always failing outright if the prototype file for creating\r
+ // new or updated files can't be located.\r
+ if (! prototypeNuxeoConfigFile.canRead()) {\r
+ String msg = String.format("Could not find and/or read the prototype Nuxeo config file '%s'",\r
+ prototypeNuxeoConfigFile.getCanonicalPath());\r
+ throw new RuntimeException(msg);\r
+ }\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace("Found and can read prototype Nuxeo config file at path %s", prototypeNuxeoConfigFile.getAbsolutePath());\r
+ }\r
+ Document prototypeConfigDoc = XmlTools.fileToXMLDocument(prototypeNuxeoConfigFile);\r
+ Document repositoryConfigDoc = null;\r
+ // FIXME: Can the variable below reasonably be made a class variable? Its value\r
+ // is used in at least one other method in this class.\r
+ Hashtable<String, TenantBindingType> tenantBindingTypeMap = tenantBindingConfigReader.getTenantBindings();\r
+ for (TenantBindingType tbt : tenantBindingTypeMap.values()) {\r
+ List<String> repositoryNameList = ConfigUtils.getRepositoryNameList(tbt);\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace("Getting repository name(s) for tenant " + tbt.getName());\r
+ }\r
+ if (repositoryNameList == null || repositoryNameList.isEmpty() == true) {\r
+ logger.warn(String.format("Could not get repository name(s) for tenant %s", tbt.getName()));\r
+ continue;\r
+ } else {\r
+ for (String repositoryName : repositoryNameList) {\r
+ if (Tools.isBlank(repositoryName)) {\r
+ continue;\r
+ }\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace(String.format("Repository name is %s", repositoryName));\r
+ }\r
+ // FIXME: As per above, we might check for the presence of an existing\r
+ // config file for this repository and, if present, not fail even if\r
+ // the code below fails to update that file on any given system startup.\r
+ //\r
+ // Clone the prototype copy of the Nuxeo repository config file,\r
+ // thus creating a separate config file for the current repository.\r
+ repositoryConfigDoc = (Document) prototypeConfigDoc.clone();\r
+ // Update this config file by inserting values pertinent to the\r
+ // current repository.\r
+ repositoryConfigDoc = updateRepositoryConfigDoc(repositoryConfigDoc, repositoryName);\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace("Updated Nuxeo repo config file contents=\n" + repositoryConfigDoc.asXML());\r
+ }\r
+ // Write this config file to the Nuxeo server config directory.\r
+ File repofile = new File(getNuxeoConfigDir() + File.separator +\r
+ repositoryName + JEEServerDeployment.NUXEO_REPO_CONFIG_FILENAME_SUFFIX);\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace(String.format("Attempting to write Nuxeo repo config file to %s", repofile.getAbsolutePath()));\r
+ }\r
+ XmlTools.xmlDocumentToFile(repositoryConfigDoc, repofile);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ private Document updateRepositoryConfigDoc(Document repoConfigDoc, String repositoryName) {\r
+ // FIXME: Remove this temporary placeholder variable used only during development.\r
+ final String PLACEHOLDER = "placeholder";\r
+ repoConfigDoc = XmlTools.setAttributeValue(repoConfigDoc,\r
+ "/component", "name", String.format("config:%s-repository", repositoryName));\r
+ // Text substitutions within first extension point, "repository"\r
+ repoConfigDoc = XmlTools.setAttributeValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository", "name", repositoryName);\r
+ repoConfigDoc = XmlTools.setAttributeValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository/repository", "name", repositoryName);\r
+ repoConfigDoc = XmlTools.setElementValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository/repository/xa-datasource", PLACEHOLDER);\r
+ repoConfigDoc = XmlTools.setElementValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository/repository/property[@name='URL']", PLACEHOLDER);\r
+ repoConfigDoc = XmlTools.setElementValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository/repository/property[@name='ServerName']", PLACEHOLDER);\r
+ repoConfigDoc = XmlTools.setElementValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository/repository/property[@name='DatabaseName']", repositoryName);\r
+ repoConfigDoc = XmlTools.setElementValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository/repository/property[@name='User']", PLACEHOLDER);\r
+ repoConfigDoc = XmlTools.setElementValue(repoConfigDoc,\r
+ REPOSITORY_EXTENSION_POINT_XPATH + "/repository/repository/property[@name='Password']", PLACEHOLDER);\r
+ // Text substitutions within second extension point, "repositories"\r
+ repoConfigDoc = XmlTools.setElementValue(repoConfigDoc,\r
+ REPOSITORIES_EXTENSION_POINT_XPATH + "/documentation", PLACEHOLDER);\r
+ repoConfigDoc = XmlTools.setAttributeValue(repoConfigDoc,\r
+ REPOSITORIES_EXTENSION_POINT_XPATH + "/repository", "name", repositoryName);\r
+ repoConfigDoc = XmlTools.setAttributeValue(repoConfigDoc,\r
+ REPOSITORIES_EXTENSION_POINT_XPATH + "/repository", "label", PLACEHOLDER);\r
+ return repoConfigDoc;\r
+ }\r
-\r
}\r