Skip to content

Commit

Permalink
#165 Using new ml-javaclient-util approach for loading modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Jun 13, 2017
2 parents c6fd9fb + c38f287 commit a462013
Show file tree
Hide file tree
Showing 22 changed files with 378 additions and 280 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ dependencies {
}

testCompile('commons-io:commons-io:2.5')

// Forcing Spring to use logback instead of commons-logging
runtime "ch.qos.logback:logback-classic:1.1.8"
runtime group: "org.slf4j", name: "jcl-over-slf4j", version: "1.7.22"
runtime group: "org.slf4j", name: "slf4j-api", version: "1.7.22"
}

// This ensures that Gradle includes in the published jar any non-java files under src/main/java
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group=com.marklogic
javadocsDir=../gh-pages-marklogic-java/javadocs
version=2.7.0
mlJavaclientUtilVersion=2.14.0
version=DEV
mlJavaclientUtilVersion=45
133 changes: 45 additions & 88 deletions src/main/java/com/marklogic/appdeployer/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.DatabaseClientFactory.Authentication;
import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier;
import com.marklogic.client.modulesloader.impl.StaticChecker;
import com.marklogic.client.modulesloader.impl.XccAssetLoader;
import com.marklogic.client.modulesloader.impl.XccStaticChecker;
import com.marklogic.client.ext.tokenreplacer.PropertiesSource;
import com.marklogic.client.modulesloader.impl.PropertiesModuleManager;
import com.marklogic.client.modulesloader.ssl.SimpleX509TrustManager;
import com.marklogic.client.modulesloader.tokenreplacer.DefaultModuleTokenReplacer;
import com.marklogic.client.modulesloader.tokenreplacer.ModuleTokenReplacer;
import com.marklogic.client.modulesloader.tokenreplacer.PropertiesSource;
import com.marklogic.client.modulesloader.tokenreplacer.RoxyModuleTokenReplacer;
import com.marklogic.client.modulesloader.xcc.DefaultDocumentFormatGetter;
import com.marklogic.xcc.template.XccTemplate;

import javax.net.ssl.SSLContext;
import java.io.FileFilter;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Encapsulates common configuration properties for an application deployed to MarkLogic. These properties include not
Expand Down Expand Up @@ -68,6 +64,10 @@ public class AppConfig {
private SSLHostnameVerifier restSslHostnameVerifier;
private Authentication restAuthentication = Authentication.DIGEST;

private SSLContext appServicesSslContext;
private SSLHostnameVerifier appServicesSslHostnameVerifier;
private Authentication appServicesAuthentication = Authentication.DIGEST;

private Integer restPort = DEFAULT_PORT;
private Integer testRestPort;
private Integer appServicesPort = 8000;
Expand All @@ -87,12 +87,12 @@ public class AppConfig {
private boolean staticCheckAssets = false;
private boolean staticCheckLibraryAssets = false;
private boolean bulkLoadAssets = true;
private String moduleTimestampsPath;
private String moduleTimestampsPath = PropertiesModuleManager.DEFAULT_FILE_PATH;

private String schemasPath;
private ConfigDir configDir;

// Passed into the TokenReplacer that subclasses of AbstractCommand use
// Passed into the PayloadTokenReplacer that subclasses of AbstractCommand use
private Map<String, String> customTokens = new HashMap<>();

// Allows for creating a triggers database without a config file for one
Expand Down Expand Up @@ -183,88 +183,21 @@ public DatabaseClient newTestDatabaseClient() {
getRestAdminPassword(), getRestAuthentication(), getRestSslContext(), getRestSslHostnameVerifier());
}

public DatabaseClient newModulesDatabaseClient() {
return DatabaseClientFactory.newClient(getHost(), getAppServicesPort(), getModulesDatabaseName(),
getRestAdminUsername(), getRestAdminPassword(), getAppServicesAuthentication(), getAppServicesSslContext(),
getAppServicesSslHostnameVerifier());
}

/**
* Like newDatabaseClient, but connects to schemas database.
*
* @return
*/
public DatabaseClient newSchemasDatabaseClient() {
return DatabaseClientFactory.newClient(getHost(), getRestPort(), getSchemasDatabaseName(),
getRestAdminUsername(), getRestAdminPassword(), getRestAuthentication(), getRestSslContext(),
getRestSslHostnameVerifier());
}

public StaticChecker newStaticChecker() {
if (isStaticCheckAssets()) {
String xccUri = "xcc://%s:%s@%s:%d";
xccUri = String.format(xccUri, getRestAdminUsername(), getRestAdminPassword(), getHost(), getRestPort());
XccStaticChecker checker = new XccStaticChecker(new XccTemplate(xccUri));
checker.setBulkCheck(isBulkLoadAssets());
checker.setCheckLibraryModules(isStaticCheckLibraryAssets());
return checker;
}
return null;
}

/**
* @return an XccAssetLoader based on the configuration properties in this class
*/
public XccAssetLoader newXccAssetLoader() {
XccAssetLoader l = new XccAssetLoader();
l.setHost(getHost());
l.setUsername(getRestAdminUsername());
l.setPassword(getRestAdminPassword());
l.setDatabaseName(getModulesDatabaseName());
if (getAppServicesPort() != null) {
l.setPort(getAppServicesPort());
}

String permissions = getModulePermissions();
if (permissions != null) {
l.setPermissions(permissions);
}

String[] extensions = getAdditionalBinaryExtensions();
if (extensions != null) {
DefaultDocumentFormatGetter getter = new DefaultDocumentFormatGetter();
for (String ext : extensions) {
getter.getBinaryExtensions().add(ext);
}
l.setDocumentFormatGetter(getter);
}

if (assetFileFilter != null) {
l.setFileFilter(assetFileFilter);
}

if (isReplaceTokensInModules()) {
l.setModuleTokenReplacer(buildModuleTokenReplacer());
}

l.setBulkLoad(isBulkLoadAssets());
return l;
}

protected ModuleTokenReplacer buildModuleTokenReplacer() {
DefaultModuleTokenReplacer r = isUseRoxyTokenPrefix() ? new RoxyModuleTokenReplacer() : new DefaultModuleTokenReplacer();
if (customTokens != null && !customTokens.isEmpty()) {
r.addPropertiesSource(new PropertiesSource() {
@Override
public Properties getProperties() {
Properties p = new Properties();
p.putAll(customTokens);
return p;
}
});
}

if (getModuleTokensPropertiesSources() != null) {
for (PropertiesSource ps : getModuleTokensPropertiesSources()) {
r.addPropertiesSource(ps);
}
}

return r;
return DatabaseClientFactory.newClient(getHost(), getAppServicesPort(), getSchemasDatabaseName(),
getRestAdminUsername(), getRestAdminPassword(), getAppServicesAuthentication(), getAppServicesSslContext(),
getAppServicesSslHostnameVerifier());
}

/**
Expand Down Expand Up @@ -737,4 +670,28 @@ public boolean isNoRestServer() {
public void setNoRestServer(boolean noRestServer) {
this.noRestServer = noRestServer;
}

public SSLContext getAppServicesSslContext() {
return appServicesSslContext;
}

public void setAppServicesSslContext(SSLContext appServicesSslContext) {
this.appServicesSslContext = appServicesSslContext;
}

public SSLHostnameVerifier getAppServicesSslHostnameVerifier() {
return appServicesSslHostnameVerifier;
}

public void setAppServicesSslHostnameVerifier(SSLHostnameVerifier appServicesSslHostnameVerifier) {
this.appServicesSslHostnameVerifier = appServicesSslHostnameVerifier;
}

public Authentication getAppServicesAuthentication() {
return appServicesAuthentication;
}

public void setAppServicesAuthentication(Authentication appServicesAuthentication) {
this.appServicesAuthentication = appServicesAuthentication;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class AbstractCommand extends LoggingObject implements Command {
private int executeSortOrder = Integer.MAX_VALUE;
private boolean storeResourceIdsAsCustomTokens = false;

protected TokenReplacer tokenReplacer = new DefaultTokenReplacer();
protected PayloadTokenReplacer payloadTokenReplacer = new DefaultPayloadTokenReplacer();
private FilenameFilter resourceFilenameFilter = new ResourceFilenameFilter();

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ protected String copyFileToString(File f) {
*/
protected String copyFileToString(File f, CommandContext context) {
String str = copyFileToString(f);
return str != null ? tokenReplacer.replaceTokens(str, context.getAppConfig(), false) : str;
return str != null ? payloadTokenReplacer.replaceTokens(str, context.getAppConfig(), false) : str;
}

/**
Expand Down Expand Up @@ -146,8 +146,8 @@ protected File[] listFilesInDirectory(File dir) {
return files;
}

public void setTokenReplacer(TokenReplacer tokenReplacer) {
this.tokenReplacer = tokenReplacer;
public void setPayloadTokenReplacer(PayloadTokenReplacer payloadTokenReplacer) {
this.payloadTokenReplacer = payloadTokenReplacer;
}

public void setExecuteSortOrder(int executeSortOrder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.marklogic.appdeployer.AppConfig;

public class DefaultTokenReplacer implements TokenReplacer {
public class DefaultPayloadTokenReplacer implements PayloadTokenReplacer {

public String replaceTokens(String payload, AppConfig appConfig, boolean isTestResource) {
payload = replaceDefaultTokens(payload, appConfig, isTestResource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Typically, the tokens are replaced by values in the AppConfig instance. This allows for configuration files to be
* reused across applications with different names.
*/
public interface TokenReplacer {
public interface PayloadTokenReplacer {

public String replaceTokens(String payload, AppConfig appConfig, boolean isTestResource);
String replaceTokens(String payload, AppConfig appConfig, boolean isTestResource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public void execute(CommandContext context) {

String payload = copyFileToString(f);

String json = tokenReplacer.replaceTokens(payload, appConfig, false);
String json = payloadTokenReplacer.replaceTokens(payload, appConfig, false);
mgr.save(json);

if (appConfig.isTestPortSet()) {
json = tokenReplacer.replaceTokens(payload, appConfig, true);
json = payloadTokenReplacer.replaceTokens(payload, appConfig, true);
mgr.save(json);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void execute(CommandContext context) {
for (File f : configDir.listFiles()) {
if (f.isFile() && f.getName().startsWith("local-cluster")) {
String payload = copyFileToString(f);
payload = tokenReplacer.replaceTokens(payload, context.getAppConfig(), false);
payload = payloadTokenReplacer.replaceTokens(payload, context.getAppConfig(), false);
new ClusterManager(context.getManageClient()).modifyLocalCluster(payload, context.getAdminManager());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void execute(CommandContext context) {
String payload = getPayload(context);
if (payload != null) {
DatabaseManager dbMgr = new DatabaseManager(context.getManageClient());
String json = tokenReplacer.replaceTokens(payload, appConfig, true);
String json = payloadTokenReplacer.replaceTokens(payload, appConfig, true);
SaveReceipt receipt = dbMgr.save(json);
if (shouldCreateForests(context, payload)) {
buildDeployForestsCommand(payload, receipt, context).execute(context);
Expand All @@ -74,12 +74,12 @@ public void undo(CommandContext context) {
if (node != null) {
logger.info("No content database files found, so not deleting content databases");
String payload = node.toString();
String json = tokenReplacer.replaceTokens(payload, appConfig, false);
String json = payloadTokenReplacer.replaceTokens(payload, appConfig, false);

DatabaseManager dbMgr = newDatabaseManageForDeleting(context);
dbMgr.delete(json);
if (appConfig.isTestPortSet()) {
json = tokenReplacer.replaceTokens(payload, appConfig, true);
json = payloadTokenReplacer.replaceTokens(payload, appConfig, true);
dbMgr.delete(json);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ protected String getForestDeleteLevel(AppConfig appConfig) {

/**
* Builds the XML or JSON payload for this command, based on the given CommandContext.
*
*
* @param context
* @return
*/
public String buildPayload(CommandContext context) {
String payload = getPayload(context);
return payload != null ? tokenReplacer.replaceTokens(payload, context.getAppConfig(), false) : null;
return payload != null ? payloadTokenReplacer.replaceTokens(payload, context.getAppConfig(), false) : null;
}

/**
* Get the payload based on the given CommandContext. Only loads the payload, does not replace any tokens in it.
* Call buildPayload to construct a payload with all tokens replaced.
*
*
* @param context
* @return
*/
Expand Down Expand Up @@ -196,7 +196,7 @@ protected boolean customForestsExist(CommandContext context, String dbName) {

/**
* Allows for how an instance of DeployForestsCommand is built to be overridden by a subclass.
*
*
* @param dbPayload
* Needed so we can look up forest counts based on the database name
* @param receipt
Expand All @@ -217,7 +217,7 @@ protected DeployForestsCommand buildDeployForestsCommand(String dbPayload, SaveR
/**
* Checks the forestCounts map in AppConfig to see if the client has specified a number of forests per host for this
* database.
*
*
* @param dbPayload
* @param context
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected void createForests(String originalPayload, CommandContext context) {
for (int i = countOfExistingForests + 1; i <= desiredNumberOfForests;) {
for (String hostName : hostNames) {
if (i <= desiredNumberOfForests) {
String payload = tokenReplacer.replaceTokens(originalPayload, appConfig, false);
String payload = payloadTokenReplacer.replaceTokens(originalPayload, appConfig, false);
payload = payload.replace("%%FOREST_HOST%%", hostName);
String forestName = getForestName(appConfig, i);
payload = payload.replace("%%FOREST_NAME%%", forestName);
Expand Down
Loading

0 comments on commit a462013

Please sign in to comment.