Repository: syncope Updated Branches: refs/heads/master 9df1e03de -> f32528c1f
Fixed #SYNCOPE-581 Project: http://git-wip-us.apache.org/repos/asf/syncope/repo Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/c6006673 Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/c6006673 Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/c6006673 Branch: refs/heads/master Commit: c60066731b034948b0ea96fb50458cc90d41c2fd Parents: e36a4a2 Author: massi <[email protected]> Authored: Fri Jan 23 16:37:13 2015 +0100 Committer: massi <[email protected]> Committed: Fri Jan 23 16:37:13 2015 +0100 ---------------------------------------------------------------------- .../java/org/apache/syncope/cli/SyncopeAdm.java | 19 +- .../syncope/cli/commands/AbstractCommand.java | 11 + .../cli/commands/ConfigurationCommand.java | 225 +++++++++++++++++++ .../syncope/cli/commands/LoggerCommand.java | 108 ++++++--- 4 files changed, 324 insertions(+), 39 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/syncope/blob/c6006673/cli/src/main/java/org/apache/syncope/cli/SyncopeAdm.java ---------------------------------------------------------------------- diff --git a/cli/src/main/java/org/apache/syncope/cli/SyncopeAdm.java b/cli/src/main/java/org/apache/syncope/cli/SyncopeAdm.java index 4e8966e..296614f 100644 --- a/cli/src/main/java/org/apache/syncope/cli/SyncopeAdm.java +++ b/cli/src/main/java/org/apache/syncope/cli/SyncopeAdm.java @@ -20,6 +20,7 @@ package org.apache.syncope.cli; import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; +import org.apache.syncope.cli.commands.ConfigurationCommand; import org.apache.syncope.cli.commands.LoggerCommand; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,11 +31,14 @@ public class SyncopeAdm { private static final String helpMessage = "Usage: Main [options]\n" + " Options:\n" - + " logger --help \n"; + + " logger --help \n" + + " config --help \n"; + + private static final JCommander jcommander = new JCommander(); private static LoggerCommand loggerCommand; - private static final JCommander jc = new JCommander(); + private static ConfigurationCommand configurationCommand; public static void main(final String[] args) { LOG.debug("Starting with args \n"); @@ -49,7 +53,7 @@ public class SyncopeAdm { System.out.println(helpMessage); } else { try { - jc.parse(args); + jcommander.parse(args); } catch (final ParameterException ioe) { System.out.println(helpMessage); LOG.error("Parameter exception", ioe); @@ -62,17 +66,22 @@ public class SyncopeAdm { private static void instantiateCommands() { LOG.debug("Init JCommander"); loggerCommand = new LoggerCommand(); - jc.addCommand(loggerCommand); + jcommander.addCommand(loggerCommand); LOG.debug("Added LoggerCommand"); + configurationCommand = new ConfigurationCommand(); + jcommander.addCommand(configurationCommand); + LOG.debug("Added ConfigurationCommand"); } private static void executeCommand() { - final String command = jc.getParsedCommand(); + final String command = jcommander.getParsedCommand(); LOG.debug("Called command {}", command); if ("logger".equalsIgnoreCase(command)) { loggerCommand.execute(); + } else if ("config".equalsIgnoreCase(command)) { + configurationCommand.execute(); } } } http://git-wip-us.apache.org/repos/asf/syncope/blob/c6006673/cli/src/main/java/org/apache/syncope/cli/commands/AbstractCommand.java ---------------------------------------------------------------------- diff --git a/cli/src/main/java/org/apache/syncope/cli/commands/AbstractCommand.java b/cli/src/main/java/org/apache/syncope/cli/commands/AbstractCommand.java new file mode 100644 index 0000000..5dd3d38 --- /dev/null +++ b/cli/src/main/java/org/apache/syncope/cli/commands/AbstractCommand.java @@ -0,0 +1,11 @@ +package org.apache.syncope.cli.commands; + +import com.beust.jcommander.Parameter; + +public abstract class AbstractCommand { + + @Parameter(names = {"-h", "--help"}) + protected boolean help = false; + + public abstract void execute(); +} http://git-wip-us.apache.org/repos/asf/syncope/blob/c6006673/cli/src/main/java/org/apache/syncope/cli/commands/ConfigurationCommand.java ---------------------------------------------------------------------- diff --git a/cli/src/main/java/org/apache/syncope/cli/commands/ConfigurationCommand.java b/cli/src/main/java/org/apache/syncope/cli/commands/ConfigurationCommand.java new file mode 100644 index 0000000..f0f54ee --- /dev/null +++ b/cli/src/main/java/org/apache/syncope/cli/commands/ConfigurationCommand.java @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.cli.commands; + +import com.beust.jcommander.DynamicParameter; +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import java.io.File; +import java.io.IOException; +import java.io.SequenceInputStream; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.apache.commons.lang3.StringUtils; +import org.apache.cxf.helpers.IOUtils; +import org.apache.syncope.cli.SyncopeServices; +import org.apache.syncope.common.SyncopeClientException; +import org.apache.syncope.common.services.ConfigurationService; +import org.apache.syncope.common.to.AttributeTO; +import org.apache.syncope.common.to.ConfTO; +import org.apache.syncope.common.wrap.MailTemplate; +import org.apache.syncope.common.wrap.Validator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +@Parameters( + commandNames = "config", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope configuration service") +public class ConfigurationCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(ConfigurationCommand.class); + + private static final Class SYNCOPE_CONFIGURATION_CLASS = ConfigurationService.class; + + private static final String EXPORT_FILE_NAME = "/content.xml"; + + private final String helpMessage = "Usage: config [options]\n" + + " Options:\n" + + " -h, --help \n" + + " -l, --list \n" + + " -r, --read \n" + + " Syntax: -r={CONF-NAME} \n" + + " -u, --update \n" + + " Syntax: {CONF-NAME}={CONF-VALUE} \n" + + " -c, --create \n" + + " Syntax: {CONF-NAME}={CONF-VALUE} \n" + + " -d, --delete \n" + + " Syntax: -d={CONF-NAME}" + + " -v, --validators \n" + + " -mt, --mail-templates \n" + + " -e, --export \n" + + " Syntax: -e={WHERE-DIR} \n"; + + @Parameter(names = {"-l", "--list"}) + public boolean list = false; + + @Parameter(names = {"-r", "--read"}) + public String confNameToRead; + + @DynamicParameter(names = {"-u", "--update"}) + private final Map<String, String> updateConf = new HashMap<String, String>(); + + @DynamicParameter(names = {"-c", "--create"}) + private final Map<String, String> createConf = new HashMap<String, String>(); + + @Parameter(names = {"-d", "--delete"}) + public String confNameToDelete; + + @Parameter(names = {"-v", "--validators"}) + public boolean validators = false; + + @Parameter(names = {"-mt", "--mail-templates"}) + public boolean mailTemplates = false; + + @Parameter(names = {"-e", "--export"}) + public String export; + + @Override + public void execute() { + final ConfigurationService configurationService = ((ConfigurationService) SyncopeServices. + get(SYNCOPE_CONFIGURATION_CLASS)); + + LOG.debug("Logger service successfully created"); + + if (help) { + LOG.debug("- configuration help command"); + System.out.println(helpMessage); + } else if (list) { + LOG.debug("- configuration list command"); + try { + final ConfTO confTO = configurationService.list(); + for (final AttributeTO attrTO : confTO.getAttrMap().values()) { + System.out.println(" - Conf " + attrTO.getSchema() + " has value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(confNameToRead)) { + LOG.debug("- configuration read {} command", confNameToRead); + try { + final AttributeTO attrTO = configurationService.read(confNameToRead); + System.out.println(" - Conf " + attrTO.getSchema() + " has value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (!updateConf.isEmpty()) { + LOG.debug("- configuration update command with params {}", updateConf); + try { + for (final Map.Entry<String, String> entrySet : updateConf.entrySet()) { + final AttributeTO attrTO = configurationService.read(entrySet.getKey()); + attrTO.getValues().clear(); + attrTO.getValues().add(entrySet.getValue()); + configurationService.set(entrySet.getKey(), attrTO); + System.out.println(" - Conf " + attrTO.getSchema() + " has value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (!createConf.isEmpty()) { + LOG.debug("- configuration create command with params {}", createConf); + try { + for (final Map.Entry<String, String> entrySet : createConf.entrySet()) { + final AttributeTO attrTO = new AttributeTO(); + attrTO.setSchema(entrySet.getKey()); + attrTO.getValues().add(entrySet.getValue()); + configurationService.set(entrySet.getKey(), attrTO); + System.out.println(" - Conf " + attrTO.getSchema() + " created with value(s) " + attrTO.getValues() + + " - readonly: " + attrTO.isReadonly()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(confNameToDelete)) { + try { + LOG.debug("- configuration delete {} command", confNameToDelete); + configurationService.delete(confNameToDelete); + System.out.println(" - Conf " + confNameToDelete + " deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (validators) { + LOG.debug("- configuration validators command"); + try { + for (final Validator validator : configurationService.getValidators()) { + System.out.println(" - Conf validator class: " + validator.getElement()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (mailTemplates) { + LOG.debug("- configuration mailTemplates command"); + try { + System.out.println(" - Conf mail template for:"); + for (final MailTemplate mailTemplate : configurationService.getMailTemplates()) { + System.out.println(" *** " + mailTemplate.getElement()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(export)) { + LOG.debug("- configuration export command, directory where xml will be export: {}", export); + + try { + TransformerFactory.newInstance().newTransformer() + .transform(new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( + new InputSource(new StringReader(IOUtils.toString( + (SequenceInputStream) configurationService. + export().getEntity()))))), + new StreamResult(new File(export + EXPORT_FILE_NAME))); + System.out.println(" - " + export + EXPORT_FILE_NAME + " successfully created"); + } catch (final IOException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final ParserConfigurationException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final SAXException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final TransformerConfigurationException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final TransformerException ex) { + LOG.error("Error creating content.xml file in {} directory", export, ex); + System.out.println(" - Error creating " + export + EXPORT_FILE_NAME + " " + ex.getMessage()); + } catch (final SyncopeClientException ex) { + LOG.error("Error calling configuration service", ex); + System.out.println(" - Error calling configuration service " + ex.getMessage()); + } + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/c6006673/cli/src/main/java/org/apache/syncope/cli/commands/LoggerCommand.java ---------------------------------------------------------------------- diff --git a/cli/src/main/java/org/apache/syncope/cli/commands/LoggerCommand.java b/cli/src/main/java/org/apache/syncope/cli/commands/LoggerCommand.java index 3c2a415..e4d3a1c 100644 --- a/cli/src/main/java/org/apache/syncope/cli/commands/LoggerCommand.java +++ b/cli/src/main/java/org/apache/syncope/cli/commands/LoggerCommand.java @@ -26,6 +26,7 @@ import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.syncope.cli.SyncopeServices; import org.apache.syncope.cli.validators.DebugLevelValidator; +import org.apache.syncope.common.SyncopeClientException; import org.apache.syncope.common.services.LoggerService; import org.apache.syncope.common.to.LoggerTO; import org.apache.syncope.common.types.LoggerLevel; @@ -38,43 +39,48 @@ import org.slf4j.LoggerFactory; optionPrefixes = "-", separators = "=", commandDescription = "Apache Syncope logger service") -public class LoggerCommand { +public class LoggerCommand extends AbstractCommand { private static final Logger LOG = LoggerFactory.getLogger(LoggerCommand.class); - private static final Class syncopeLoggerClass = LoggerService.class; + private static final Class SYNCOPE_LOGGER_CLASS = LoggerService.class; private final String helpMessage = "Usage: logger [options]\n" + " Options:\n" + " -h, --help \n" + " -l, --list \n" - + " -ua, --update-all \n" - + " Syntax: -ua={LOGGER-LEVEL} \n" + + " -r, --read \n" + + " Syntax: -r={LOG-NAME} \n" + " -u, --update \n" + " Syntax: {LOG-NAME}={LOG-LEVEL} \n" + + " -ua, --update-all \n" + + " Syntax: -ua={LOG-LEVEL} \n" + + " -c, --create \n" + + " Syntax: {LOG-NAME}={LOG-LEVEL} \n" + " -d, --delete \n" - + " Syntax: -d={LOGGER-NAME}"; - - @Parameter(names = {"-h", "--help"}) - public boolean help = false; + + " Syntax: -d={LOG-NAME}"; @Parameter(names = {"-l", "--list"}) public boolean list = false; + @Parameter(names = {"-r", "--read"}) + public String logNameToRead; + + @DynamicParameter(names = {"-u", "--update"}) + private final Map<String, String> updateLogs = new HashMap<String, String>(); + @Parameter(names = {"-ua", "--update-all"}, validateWith = DebugLevelValidator.class) public String logLevel; - @Parameter(names = {"-r", "--read"}) - public String logNameToRead; + @DynamicParameter(names = {"-u", "--update"}) + private final Map<String, String> createLogs = new HashMap<String, String>(); @Parameter(names = {"-d", "--delete"}) public String logNameToDelete; - @DynamicParameter(names = {"-u", "--update"}) - private final Map<String, String> params = new HashMap<String, String>(); - + @Override public void execute() { - final LoggerService loggerService = ((LoggerService) SyncopeServices.get(syncopeLoggerClass)); + final LoggerService loggerService = ((LoggerService) SyncopeServices.get(SYNCOPE_LOGGER_CLASS)); LOG.debug("Logger service successfully created"); @@ -83,32 +89,66 @@ public class LoggerCommand { System.out.println(helpMessage); } else if (list) { LOG.debug("- logger list command"); - for (final LoggerTO loggerTO : loggerService.list(LoggerType.LOG)) { - System.out.println(" - " + loggerTO.getName() + " -> " + loggerTO.getLevel()); + try { + for (final LoggerTO loggerTO : loggerService.list(LoggerType.LOG)) { + System.out.println(" - " + loggerTO.getName() + " -> " + loggerTO.getLevel()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (StringUtils.isNotBlank(logNameToRead)) { + LOG.debug("- logger read {} command", logNameToRead); + try { + final LoggerTO loggerTO = loggerService.read(LoggerType.LOG, logNameToRead); + System.out.println(" - Logger " + loggerTO.getName() + " with level -> " + loggerTO.getLevel()); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } + } else if (!updateLogs.isEmpty()) { + LOG.debug("- logger update command with params {}", updateLogs); + try { + for (final Map.Entry<String, String> entrySet : updateLogs.entrySet()) { + final LoggerTO loggerTO = loggerService.read(LoggerType.LOG, entrySet.getKey()); + loggerTO.setLevel(LoggerLevel.valueOf(entrySet.getValue())); + loggerService.update(LoggerType.LOG, loggerTO.getName(), loggerTO); + System.out.println(" - Logger " + loggerTO.getName() + " new level -> " + loggerTO.getLevel()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); } } else if (StringUtils.isNotBlank(logLevel)) { LOG.debug("- logger update all command with level {}", logLevel); - for (final LoggerTO loggerTO : loggerService.list(LoggerType.LOG)) { - loggerTO.setLevel(LoggerLevel.valueOf(logLevel)); - loggerService.update(LoggerType.LOG, loggerTO.getName(), loggerTO); - System.out.println(" - Logger " + loggerTO.getName() + " new value -> " + loggerTO.getLevel()); + try { + + for (final LoggerTO loggerTO : loggerService.list(LoggerType.LOG)) { + loggerTO.setLevel(LoggerLevel.valueOf(logLevel)); + loggerService.update(LoggerType.LOG, loggerTO.getName(), loggerTO); + System.out.println(" - Logger " + loggerTO.getName() + " new level -> " + loggerTO.getLevel()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); } - } else if (!params.isEmpty()) { - LOG.debug("- logger update command with params {}", params); - for (final Map.Entry<String, String> entrySet : params.entrySet()) { - final LoggerTO loggerTO = loggerService.read(LoggerType.LOG, entrySet.getKey()); - loggerTO.setLevel(LoggerLevel.valueOf(entrySet.getValue())); - loggerService.update(LoggerType.LOG, loggerTO.getName(), loggerTO); - System.out.println(" - Logger " + loggerTO.getName() + " new value -> " + loggerTO.getLevel()); + } else if (!createLogs.isEmpty()) { + LOG.debug("- logger create command with params {}", createLogs); + try { + for (final Map.Entry<String, String> entrySet : createLogs.entrySet()) { + final LoggerTO loggerTO = new LoggerTO(); + loggerTO.setName(entrySet.getKey()); + loggerTO.setLevel(LoggerLevel.valueOf(entrySet.getValue())); + loggerService.update(LoggerType.LOG, loggerTO.getName(), loggerTO); + System.out.println(" - Logger " + loggerTO.getName() + " created with level -> " + loggerTO.getLevel()); + } + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); } - } else if (StringUtils.isNotBlank(logNameToRead)) { - LOG.debug("- logger read {} command", logNameToRead); - final LoggerTO loggerTO = loggerService.read(LoggerType.LOG, logNameToRead); - System.out.println(" - Logger " + loggerTO.getName() + " with level -> " + loggerTO.getLevel()); } else if (StringUtils.isNotBlank(logNameToDelete)) { - LOG.debug("- logger delete {} command", logNameToDelete); - loggerService.delete(LoggerType.LOG, logNameToDelete); - System.out.println(" - Logger " + logNameToDelete + " deleted!"); + try { + LOG.debug("- logger delete {} command", logNameToDelete); + loggerService.delete(LoggerType.LOG, logNameToDelete); + System.out.println(" - Logger " + logNameToDelete + " deleted!"); + } catch (final SyncopeClientException ex) { + System.out.println(" - Error: " + ex.getMessage()); + } } else { System.out.println(helpMessage); }
