Repository: syncope Updated Branches: refs/heads/master 8d270a1e1 -> e36a4a2aa
Fixed #SYNCOPE-584 adding command line interface module - cli tool for logger provide also update all log option Project: http://git-wip-us.apache.org/repos/asf/syncope/repo Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/e36a4a2a Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/e36a4a2a Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/e36a4a2a Branch: refs/heads/master Commit: e36a4a2aafafc38d782c404309d1e9dce7b429f8 Parents: 8d270a1 Author: massi <[email protected]> Authored: Fri Jan 23 13:35:39 2015 +0100 Committer: massi <[email protected]> Committed: Fri Jan 23 13:35:39 2015 +0100 ---------------------------------------------------------------------- cli/pom.xml | 186 +++++++++++++++++++ .../java/org/apache/syncope/cli/SyncopeAdm.java | 78 ++++++++ .../org/apache/syncope/cli/SyncopeServices.java | 41 ++++ .../syncope/cli/commands/LoggerCommand.java | 117 ++++++++++++ .../cli/validators/DebugLevelValidator.java | 61 ++++++ cli/src/main/resources/log4j2.xml | 58 ++++++ cli/src/main/resources/syncope.properties | 19 ++ pom.xml | 7 + 8 files changed, 567 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/cli/pom.xml ---------------------------------------------------------------------- diff --git a/cli/pom.xml b/cli/pom.xml new file mode 100644 index 0000000..1dc8d6a --- /dev/null +++ b/cli/pom.xml @@ -0,0 +1,186 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.syncope</groupId> + <artifactId>syncope</artifactId> + <version>1.3.0-SNAPSHOT</version> + </parent> + + <name>Apache Syncope Command Line Interface</name> + <description>Apache Syncope Command Line Interface</description> + + <groupId>org.apache.syncope</groupId> + <artifactId>syncope-cli</artifactId> + <packaging>jar</packaging> + + <distributionManagement> + <site> + <id>syncope.website</id> + <name>Apache Syncope website</name> + <url>${site.deploymentBaseUrl}/${project.artifactId}</url> + </site> + </distributionManagement> + + <properties> + <log.directory>/tmp</log.directory> + </properties> + + <dependencies> + <dependency> + <groupId>com.beust</groupId> + <artifactId>jcommander</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.syncope</groupId> + <artifactId>syncope-client</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + </dependency> + <dependency> + <groupId>com.lmax</groupId> + <artifactId>disruptor</artifactId> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + </dependency> + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl-over-slf4j</artifactId> + </dependency> + + </dependencies> + + <build> + + <defaultGoal>package</defaultGoal> + + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>2.3</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <createDependencyReducedPom>false</createDependencyReducedPom> + <transformers> + <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <manifestEntries> + <Main-Class>org.apache.syncope.cli.SyncopeAdm</Main-Class> + </manifestEntries> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer"> + <resource>messages.properties</resource> + <file>src/main/resources/messages.properties</file> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer"> + <resource>messages_it_IT.properties</resource> + <file>src/main/resources/messages_it_IT.properties</file> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> + <resource>META-INF/spring.handlers</resource> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> + <resource>META-INF/spring.schemas</resource> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> + <resource>META-INF/cxf/bus-extensions.txt</resource> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer"> + <resource>META-INF/wsdl.plugin.xml</resource> + </transformer> + </transformers> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + + <resources> + <resource> + <directory>src/main/resources</directory> + <filtering>true</filtering> + <targetPath>${project.build.directory}/classes</targetPath> + <includes> + <include>log4j2.xml</include> + </includes> + </resource> + <resource> + <directory>src/main/resources</directory> + <filtering>true</filtering> + <targetPath>${project.build.directory}/classes</targetPath> + <includes> + <include>syncope.properties</include> + </includes> + </resource> + <resource> + <directory>..</directory> + <targetPath>META-INF</targetPath> + <includes> + <include>LICENSE</include> + <include>NOTICE</include> + </includes> + </resource> + </resources> + + </build> + + <profiles> + <profile> + <id>apache-release</id> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-deploy-plugin</artifactId> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project> http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/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 new file mode 100644 index 0000000..4e8966e --- /dev/null +++ b/cli/src/main/java/org/apache/syncope/cli/SyncopeAdm.java @@ -0,0 +1,78 @@ +/* + * 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; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.ParameterException; +import org.apache.syncope.cli.commands.LoggerCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncopeAdm { + + private static final Logger LOG = LoggerFactory.getLogger(SyncopeAdm.class); + + private static final String helpMessage = "Usage: Main [options]\n" + + " Options:\n" + + " logger --help \n"; + + private static LoggerCommand loggerCommand; + + private static final JCommander jc = new JCommander(); + + public static void main(final String[] args) { + LOG.debug("Starting with args \n"); + + for (final String arg : args) { + LOG.debug("Arg: {}", arg); + } + + instantiateCommands(); + + if (args.length == 0) { + System.out.println(helpMessage); + } else { + try { + jc.parse(args); + } catch (final ParameterException ioe) { + System.out.println(helpMessage); + LOG.error("Parameter exception", ioe); + } + executeCommand(); + } + + } + + private static void instantiateCommands() { + LOG.debug("Init JCommander"); + loggerCommand = new LoggerCommand(); + jc.addCommand(loggerCommand); + LOG.debug("Added LoggerCommand"); + } + + private static void executeCommand() { + final String command = jc.getParsedCommand(); + + LOG.debug("Called command {}", command); + + if ("logger".equalsIgnoreCase(command)) { + loggerCommand.execute(); + } + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/cli/src/main/java/org/apache/syncope/cli/SyncopeServices.java ---------------------------------------------------------------------- diff --git a/cli/src/main/java/org/apache/syncope/cli/SyncopeServices.java b/cli/src/main/java/org/apache/syncope/cli/SyncopeServices.java new file mode 100644 index 0000000..8ac0337 --- /dev/null +++ b/cli/src/main/java/org/apache/syncope/cli/SyncopeServices.java @@ -0,0 +1,41 @@ +/* + * 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; + +import java.util.ResourceBundle; +import org.apache.syncope.client.SyncopeClient; +import org.apache.syncope.client.SyncopeClientFactoryBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SyncopeServices { + + private static final Logger LOG = LoggerFactory.getLogger(SyncopeServices.class); + + private final static ResourceBundle syncopeProperties = ResourceBundle.getBundle("syncope"); + + private static final SyncopeClient client = new SyncopeClientFactoryBean() + .setAddress(syncopeProperties.getString("syncope.rest.services")) + .create(syncopeProperties.getString("syncope.user"), syncopeProperties.getString("syncope.password")); + + public static Object get(final Class claz) { + LOG.debug("Creting service for {}", claz.getName()); + return client.getService(claz); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/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 new file mode 100644 index 0000000..3c2a415 --- /dev/null +++ b/cli/src/main/java/org/apache/syncope/cli/commands/LoggerCommand.java @@ -0,0 +1,117 @@ +/* + * 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.util.HashMap; +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.services.LoggerService; +import org.apache.syncope.common.to.LoggerTO; +import org.apache.syncope.common.types.LoggerLevel; +import org.apache.syncope.common.types.LoggerType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Parameters( + commandNames = "logger", + optionPrefixes = "-", + separators = "=", + commandDescription = "Apache Syncope logger service") +public class LoggerCommand { + + private static final Logger LOG = LoggerFactory.getLogger(LoggerCommand.class); + + private static final Class syncopeLoggerClass = 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" + + " -u, --update \n" + + " Syntax: {LOG-NAME}={LOG-LEVEL} \n" + + " -d, --delete \n" + + " Syntax: -d={LOGGER-NAME}"; + + @Parameter(names = {"-h", "--help"}) + public boolean help = false; + + @Parameter(names = {"-l", "--list"}) + public boolean list = false; + + @Parameter(names = {"-ua", "--update-all"}, validateWith = DebugLevelValidator.class) + public String logLevel; + + @Parameter(names = {"-r", "--read"}) + public String logNameToRead; + + @Parameter(names = {"-d", "--delete"}) + public String logNameToDelete; + + @DynamicParameter(names = {"-u", "--update"}) + private final Map<String, String> params = new HashMap<String, String>(); + + public void execute() { + final LoggerService loggerService = ((LoggerService) SyncopeServices.get(syncopeLoggerClass)); + + LOG.debug("Logger service successfully created"); + + if (help) { + LOG.debug("- logger help command"); + 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()); + } + } 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()); + } + } 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 (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!"); + } else { + System.out.println(helpMessage); + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/cli/src/main/java/org/apache/syncope/cli/validators/DebugLevelValidator.java ---------------------------------------------------------------------- diff --git a/cli/src/main/java/org/apache/syncope/cli/validators/DebugLevelValidator.java b/cli/src/main/java/org/apache/syncope/cli/validators/DebugLevelValidator.java new file mode 100644 index 0000000..ce2389b --- /dev/null +++ b/cli/src/main/java/org/apache/syncope/cli/validators/DebugLevelValidator.java @@ -0,0 +1,61 @@ +/* + * 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.validators; + +import com.beust.jcommander.IParameterValidator; +import com.beust.jcommander.ParameterException; + +public class DebugLevelValidator implements IParameterValidator { + + @Override + public void validate(final String name, final String value) throws ParameterException { + if (!Levels.contains(value)) { + final StringBuilder exceptionMessage = new StringBuilder(); + exceptionMessage.append("Parameter ") + .append(name) + .append(" should be :\n"); + for (final Levels l : Levels.values()) { + exceptionMessage.append(l).append("\n"); + } + System.out.println(">>>> " + exceptionMessage.toString()); + } + } + + private enum Levels { + + OFF, + FATAL, + ERROR, + WARN, + INFO, + DEBUG, + TRACE, + ALL; + + public static boolean contains(final String name) { + for (final Levels c : Levels.values()) { + if (c.name().equals(name)) { + return true; + } + } + return false; + } + } + +} http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/cli/src/main/resources/log4j2.xml ---------------------------------------------------------------------- diff --git a/cli/src/main/resources/log4j2.xml b/cli/src/main/resources/log4j2.xml new file mode 100644 index 0000000..0688f6b --- /dev/null +++ b/cli/src/main/resources/log4j2.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<configuration status="WARN"> + + <appenders> + + <RollingRandomAccessFile name="main" fileName="${log.directory}/cli.log" + filePattern="${log.directory}/cli-%d{yyyy-MM-dd}.log.gz" + immediateFlush="false" append="true"> + <PatternLayout> + <pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern> + </PatternLayout> + <Policies> + <TimeBasedTriggeringPolicy/> + <SizeBasedTriggeringPolicy size="250 MB"/> + </Policies> + </RollingRandomAccessFile> + + </appenders> + + <loggers> + + <asyncLogger name="com.beust" additivity="false" level="DEBUG"> + <appender-ref ref="main"/> + </asyncLogger> + + <asyncLogger name="org.apache.syncope.cli" additivity="false" level="DEBUG"> + <appender-ref ref="main"/> + </asyncLogger> + + <asyncLogger name="org.apache.syncope.client" additivity="false" level="OFF"> + <appender-ref ref="main"/> + </asyncLogger> + + <root level="DEBUG"> + <appender-ref ref="main"/> + </root> + + </loggers> + +</configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/cli/src/main/resources/syncope.properties ---------------------------------------------------------------------- diff --git a/cli/src/main/resources/syncope.properties b/cli/src/main/resources/syncope.properties new file mode 100644 index 0000000..e9fd513 --- /dev/null +++ b/cli/src/main/resources/syncope.properties @@ -0,0 +1,19 @@ +# 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. +syncope.rest.services=http://localhost:8080/syncope/rest/ +syncope.user=admin +syncope.password=password \ No newline at end of file http://git-wip-us.apache.org/repos/asf/syncope/blob/e36a4a2a/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index bfaed59..9af6d16 100644 --- a/pom.xml +++ b/pom.xml @@ -948,6 +948,12 @@ under the License. <artifactId>codemirror</artifactId> <version>${codemirror.version}</version> </dependency> + + <dependency> + <groupId>com.beust</groupId> + <artifactId>jcommander</artifactId> + <version>1.47</version> + </dependency> <dependency> <groupId>org.codehaus.izpack</groupId> @@ -1621,5 +1627,6 @@ under the License. <module>standalone</module> <module>installer</module> <module>deb</module> + <module>cli</module> </modules> </project>
