Skip to content

Commit

Permalink
initial import of new cli tools
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Apr 27, 2015
1 parent 2dfe801 commit 5b00b92
Show file tree
Hide file tree
Showing 53 changed files with 3,316 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -9,3 +9,5 @@ rebel.xml
samples/model-client-sample-dotnet/ModelClientSample/ModelClientSample/bin
samples/model-client-sample-dotnet/ModelClientSample/ModelClientSample/obj
velocity.log

dependency-reduced-pom.xml
17 changes: 17 additions & 0 deletions build-system/pom.xml
Expand Up @@ -890,6 +890,23 @@
</execution>
</executions>
</plugin> -->
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.1.11</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</build>
<reporting>
Expand Down
58 changes: 58 additions & 0 deletions cli/common/pom.xml
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2015 Evolveum
~
~ Licensed 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>midPoint CLI Interface - common</name>
<groupId>com.evolveum.midpoint.cli</groupId>
<artifactId>common</artifactId>
<version>3.2-SNAPSHOT</version>

<parent>
<groupId>com.evolveum.midpoint</groupId>
<artifactId>cli</artifactId>
<version>3.2-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
</dependency>

<dependency>
<groupId>com.evolveum.midpoint.model</groupId>
<artifactId>model-client</artifactId>
</dependency>

<!-- TEST -->
<dependency>
<groupId>com.evolveum.midpoint.tools</groupId>
<artifactId>test-ng</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed 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 com.evolveum.midpoint.cli.common;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

/**
* @author Viliam Repan (lazyman)
*/
public class ConsoleFilter extends Filter<ILoggingEvent> {

@Override
public FilterReply decide(ILoggingEvent event) {
if (ToolsUtils.LOGGER_SYS_ERR.equals(event.getLoggerName())
|| ToolsUtils.LOGGER_SYS_OUT.equals(event.getLoggerName())) {
return FilterReply.DENY;
}
return FilterReply.NEUTRAL;
}
}
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed 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 com.evolveum.midpoint.cli.common;

import com.beust.jcommander.Parameter;

/**
* @author lazyman
*/
public class DefaultCommand {

public static final String P_HELP = "-h";
public static final String P_HELP_LONG = "--help";

public static final String P_VERSION = "-v";
public static final String P_VERSION_LONG = "--version";

@Parameter(names = {P_HELP, P_HELP_LONG}, help = true,
description = "Print this help")
private boolean help;

@Parameter(names = {P_VERSION, P_VERSION_LONG},
description = "Print version")
private boolean version;

public boolean isHelp() {
return help;
}

public boolean isVersion() {
return version;
}
}
@@ -0,0 +1,174 @@
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed 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 com.evolveum.midpoint.cli.common;

import com.evolveum.midpoint.model.client.ModelClientUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.sun.org.apache.xml.internal.utils.XMLChar;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.*;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;

/**
* @author lazyman
*/
public class ToolsUtils {

public static final String DESCRIBE_PROPERTIES = "/describe.properties";

public static final String PROP_VERSION = "version";

public static final String LOGGER_SYS_OUT = "SYSOUT";
public static final String LOGGER_SYS_ERR = "SYSERR";

public static final QName C_OBJECTS = new QName(ModelClientUtil.NS_COMMON, "objects");
public static final QName C_OBJECT = new QName(ModelClientUtil.NS_COMMON, "object");

public static final JAXBContext JAXB_CONTEXT;

static {
try {
JAXB_CONTEXT = ModelClientUtil.instantiateJaxbContext();
} catch (JAXBException ex) {
throw new IllegalStateException("Couldn't create jaxb context", ex);
}
}

public static Element getFirstChildElement(Node parent) {
if (parent == null || parent.getChildNodes() == null) {
return null;
}

NodeList nodes = parent.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node child = nodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
return (Element) child;
}
}

return null;
}

public static void serializeObject(Object object, Writer writer) throws JAXBException {
if (object == null) {
return;
}

Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

if (object instanceof ObjectType) {
object = new JAXBElement(C_OBJECT, Object.class, object);
}

marshaller.marshal(object, writer);
}

public static String serializeObject(Object object) throws JAXBException {
if (object == null) {
return null;
}

Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

StringWriter sw = new StringWriter();
marshaller.marshal(object, sw);
return sw.toString();
}

public static void setNamespaceDeclarations(Element element, Map<String, String> rootNamespaceDeclarations) {
for (Map.Entry<String, String> entry : rootNamespaceDeclarations.entrySet()) {
setNamespaceDeclaration(element, entry.getKey(), entry.getValue());
}
}

public static void setNamespaceDeclaration(Element element, String prefix, String namespaceUri) {
Document doc = element.getOwnerDocument();
NamedNodeMap attributes = element.getAttributes();
Attr attr;
if (prefix == null || prefix.isEmpty()) {
// default namespace
attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
} else {
attr = doc.createAttributeNS(
XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix);
}
checkValidXmlChars(namespaceUri);
attr.setValue(namespaceUri);
attributes.setNamedItem(attr);
}

public static void checkValidXmlChars(String stringValue) {
if (stringValue == null) {
return;
}
for (int i = 0; i < stringValue.length(); i++) {
if (!XMLChar.isValid(stringValue.charAt(i))) {
throw new IllegalStateException("Invalid character with regards to XML (code "
+ ((int) stringValue.charAt(i)) + ") in '"
+ makeSafelyPrintable(stringValue, 200) + "'");
}
}
}

private static String makeSafelyPrintable(String text, int maxSize) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (!XMLChar.isValid(c)) {
sb.append('.');
} else if (Character.isWhitespace(c)) {
sb.append(' ');
} else {
sb.append(c);
}
if (i == maxSize) {
sb.append("...");
break;
}
}
return sb.toString();
}

public static String loadVersion() throws IOException {
Properties props = new Properties();
InputStream is = null;
try {
is = ToolsUtils.class.getResourceAsStream(DESCRIBE_PROPERTIES);
props.load(new InputStreamReader(is, "utf-8"));
} finally {
IOUtils.closeQuietly(is);
}

return props.getProperty(PROP_VERSION);
}
}

0 comments on commit 5b00b92

Please sign in to comment.