|
| 1 | +package com.marklogic.appdeployer.cli; |
| 2 | + |
| 3 | +import ch.qos.logback.classic.Level; |
| 4 | +import com.beust.jcommander.JCommander; |
| 5 | +import com.marklogic.appdeployer.AppConfig; |
| 6 | +import com.marklogic.appdeployer.DefaultAppConfigFactory; |
| 7 | +import com.marklogic.appdeployer.command.Command; |
| 8 | +import com.marklogic.appdeployer.command.CommandMapBuilder; |
| 9 | +import com.marklogic.appdeployer.impl.SimpleAppDeployer; |
| 10 | +import com.marklogic.mgmt.DefaultManageConfigFactory; |
| 11 | +import com.marklogic.mgmt.ManageClient; |
| 12 | +import com.marklogic.mgmt.ManageConfig; |
| 13 | +import com.marklogic.mgmt.admin.AdminConfig; |
| 14 | +import com.marklogic.mgmt.admin.AdminManager; |
| 15 | +import com.marklogic.mgmt.admin.DefaultAdminConfigFactory; |
| 16 | +import com.marklogic.mgmt.util.PropertySource; |
| 17 | +import com.marklogic.mgmt.util.SimplePropertySource; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.io.FileInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Properties; |
| 26 | +import java.util.TreeMap; |
| 27 | + |
| 28 | +public class Main { |
| 29 | + |
| 30 | + private final static Logger logger = LoggerFactory.getLogger(Main.class); |
| 31 | + |
| 32 | + /** |
| 33 | + * @param args |
| 34 | + */ |
| 35 | + public static void main(String[] args) throws IOException { |
| 36 | + Options options = new Options(); |
| 37 | + JCommander.Builder builder = JCommander |
| 38 | + .newBuilder() |
| 39 | + .addObject(options); |
| 40 | + |
| 41 | + addCommandsToBuilder(builder); |
| 42 | + |
| 43 | + JCommander commander = builder.build(); |
| 44 | + commander.setProgramName("java -jar <name of jar>"); |
| 45 | + commander.parse(args); |
| 46 | + |
| 47 | + String parsedCommand = commander.getParsedCommand(); |
| 48 | + if (parsedCommand == null) { |
| 49 | + commander.usage(); |
| 50 | + } else { |
| 51 | + ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); |
| 52 | + root.setLevel(Level.toLevel(options.getLogLevel())); |
| 53 | + |
| 54 | + JCommander parsedCommander = commander.getCommands().get(parsedCommand); |
| 55 | + CommandArray commandArray = (CommandArray) parsedCommander.getObjects().get(0); |
| 56 | + PropertySource propertySource = buildPropertySource(options); |
| 57 | + runCommand(commandArray, propertySource, options); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Use CommandMapBuilder to construct the list of all known commands. Each will then be added to JCommander. |
| 63 | + * |
| 64 | + * @param builder |
| 65 | + */ |
| 66 | + private static void addCommandsToBuilder(JCommander.Builder builder) { |
| 67 | + CommandMapBuilder commandMapBuilder = new CommandMapBuilder(); |
| 68 | + Map<String, List<Command>> commandMap = commandMapBuilder.buildCommandMap(); |
| 69 | + |
| 70 | + // Combine all commands into an ordered map so that usage() lists them in alphabetical order |
| 71 | + Map<String, CommandArray> orderedCommandMap = new TreeMap<>(); |
| 72 | + orderedCommandMap.put("mlDeploy", new DeployCommand(commandMap)); |
| 73 | + |
| 74 | + for (String commandGroup : commandMap.keySet()) { |
| 75 | + for (Command command : commandMap.get(commandGroup)) { |
| 76 | + String className = command.getClass().getSimpleName(); |
| 77 | + if (className.endsWith("Command")) { |
| 78 | + className = className.substring(0, className.length() - "Command".length()); |
| 79 | + } |
| 80 | + orderedCommandMap.put("ml" + className, new CommandWrapper(command)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + for (String commandName : orderedCommandMap.keySet()) { |
| 85 | + builder.addCommand(commandName, orderedCommandMap.get(commandName)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Properties can be read from a properties file, and/or specified via the -P flag. If a file is specified, any |
| 91 | + * properties specified via -P will override what's in the properties file. |
| 92 | + * |
| 93 | + * @param options |
| 94 | + * @return |
| 95 | + * @throws IOException |
| 96 | + */ |
| 97 | + private static PropertySource buildPropertySource(Options options) throws IOException { |
| 98 | + final String propertiesFilePath = options.getPropertiesFilePath(); |
| 99 | + if (propertiesFilePath != null) { |
| 100 | + Properties props = new Properties(); |
| 101 | + if (logger.isInfoEnabled()) { |
| 102 | + logger.info("Reading properties from file path: " + propertiesFilePath); |
| 103 | + } |
| 104 | + FileInputStream fis = new FileInputStream(propertiesFilePath); |
| 105 | + try { |
| 106 | + props.load(fis); |
| 107 | + } finally { |
| 108 | + fis.close(); |
| 109 | + } |
| 110 | + |
| 111 | + // Dynamic params override what's in the properties file |
| 112 | + Map<String, String> params = options.getParams(); |
| 113 | + if (params != null) { |
| 114 | + for (String key : params.keySet()) { |
| 115 | + props.setProperty(key, params.get(key)); |
| 116 | + } |
| 117 | + } |
| 118 | + return new SimplePropertySource(props); |
| 119 | + } else { |
| 120 | + return (name) -> options.getParams().get(name); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param commandArray |
| 126 | + * @param propertySource |
| 127 | + * @param options |
| 128 | + */ |
| 129 | + private static void runCommand(CommandArray commandArray, PropertySource propertySource, Options options) { |
| 130 | + AppConfig appConfig = new DefaultAppConfigFactory(propertySource).newAppConfig(); |
| 131 | + ManageConfig manageConfig = new DefaultManageConfigFactory(propertySource).newManageConfig(); |
| 132 | + ManageClient manageClient = new ManageClient(manageConfig); |
| 133 | + AdminConfig adminConfig = new DefaultAdminConfigFactory(propertySource).newAdminConfig(); |
| 134 | + AdminManager adminManager = new AdminManager(adminConfig); |
| 135 | + |
| 136 | + SimpleAppDeployer deployer = new SimpleAppDeployer(manageClient, adminManager, commandArray.getCommands()); |
| 137 | + if (options.isUndo()) { |
| 138 | + deployer.undeploy(appConfig); |
| 139 | + } else { |
| 140 | + deployer.deploy(appConfig); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments