mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
added scm-cli and scm-server-util
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -11,6 +11,8 @@
|
||||
|
||||
<modules>
|
||||
<module>scm-core</module>
|
||||
<module>scm-cli</module>
|
||||
<module>scm-server-util</module>
|
||||
<module>plugins</module>
|
||||
<module>scm-agent</module>
|
||||
<module>scm-webapp</module>
|
||||
|
||||
18
scm-cli/pom.xml
Normal file
18
scm-cli/pom.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>scm</artifactId>
|
||||
<groupId>sonia.scm</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>sonia.scm</groupId>
|
||||
<artifactId>scm-cli</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>scm-cli</name>
|
||||
|
||||
</project>
|
||||
57
scm-cli/src/main/java/sonia/scm/cli/Argument.java
Normal file
57
scm-cli/src/main/java/sonia/scm/cli/Argument.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (c) 2009, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of JAB; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://kenai.com/projects/jab
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Documented
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Argument
|
||||
{
|
||||
String value();
|
||||
String longName() default "";
|
||||
String description() default "";
|
||||
boolean required() default false;
|
||||
}
|
||||
82
scm-cli/src/main/java/sonia/scm/cli/CliException.java
Normal file
82
scm-cli/src/main/java/sonia/scm/cli/CliException.java
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Copyright (c) 2009, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of JAB; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://kenai.com/projects/jab
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class CliException extends Exception
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public CliException() {}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
public CliException(String string)
|
||||
{
|
||||
super(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param thrwbl
|
||||
*/
|
||||
public CliException(Throwable thrwbl)
|
||||
{
|
||||
super(thrwbl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param string
|
||||
* @param thrwbl
|
||||
*/
|
||||
public CliException(String string, Throwable thrwbl)
|
||||
{
|
||||
super(string, thrwbl);
|
||||
}
|
||||
}
|
||||
56
scm-cli/src/main/java/sonia/scm/cli/CliHelpBuilder.java
Normal file
56
scm-cli/src/main/java/sonia/scm/cli/CliHelpBuilder.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Copyright (c) 2009, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of JAB; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://kenai.com/projects/jab
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public interface CliHelpBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param arguments
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String createHelp(List<Argument> arguments);
|
||||
}
|
||||
199
scm-cli/src/main/java/sonia/scm/cli/CliParser.java
Normal file
199
scm-cli/src/main/java/sonia/scm/cli/CliParser.java
Normal file
@@ -0,0 +1,199 @@
|
||||
/**
|
||||
* Copyright (c) 2009, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of JAB; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://kenai.com/projects/jab
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class CliParser
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param helpBuilder
|
||||
* @param clazz
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String createHelp(CliHelpBuilder helpBuilder, Class clazz)
|
||||
{
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
List<Argument> arguments = new ArrayList<Argument>();
|
||||
|
||||
for (Field field : fields)
|
||||
{
|
||||
Argument argument = field.getAnnotation(Argument.class);
|
||||
|
||||
if (argument != null)
|
||||
{
|
||||
arguments.add(argument);
|
||||
}
|
||||
}
|
||||
|
||||
return helpBuilder.createHelp(arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param helpBuilder
|
||||
* @param object
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String createHelp(CliHelpBuilder helpBuilder, Object object)
|
||||
{
|
||||
return createHelp(helpBuilder, object.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param object
|
||||
* @param arguments
|
||||
*
|
||||
* @throws CliException
|
||||
*/
|
||||
public void parse(Object object, String[] arguments) throws CliException
|
||||
{
|
||||
Field[] fields = object.getClass().getDeclaredFields();
|
||||
int length = arguments.length;
|
||||
|
||||
for (Field field : fields)
|
||||
{
|
||||
Argument argument = field.getAnnotation(Argument.class);
|
||||
|
||||
if (argument != null)
|
||||
{
|
||||
String name = "-" + argument.value();
|
||||
String longName = "--" + argument.longName();
|
||||
boolean found = false;
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (arguments[i].equals(name)
|
||||
|| (!longName.equals("--") && arguments[i].startsWith(longName)))
|
||||
{
|
||||
found = true;
|
||||
|
||||
if (field.getType().isAssignableFrom(Boolean.class))
|
||||
{
|
||||
setArgument(object, field, Boolean.TRUE);
|
||||
}
|
||||
else if (arguments[i].equals(name) && (i + 1 < length))
|
||||
{
|
||||
setArgument(object, field,
|
||||
ConvertUtil.convertString(field.getType(),
|
||||
arguments[i + 1]));
|
||||
}
|
||||
else if (arguments[i].startsWith(longName + "="))
|
||||
{
|
||||
String value = arguments[i].substring(longName.length() + 1);
|
||||
|
||||
if ((value != null) && (value.length() > 0))
|
||||
{
|
||||
setArgument(object, field,
|
||||
ConvertUtil.convertString(field.getType(), value));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CliException("missing parameter for " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found && argument.required())
|
||||
{
|
||||
throw new CliRequiredException(name + " is required");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param object
|
||||
* @param field
|
||||
* @param value
|
||||
*
|
||||
* @throws CliException
|
||||
*/
|
||||
private void setArgument(Object object, Field field, Object value)
|
||||
throws CliException
|
||||
{
|
||||
try
|
||||
{
|
||||
boolean modifyAccess = false;
|
||||
|
||||
if (!field.isAccessible())
|
||||
{
|
||||
field.setAccessible(true);
|
||||
modifyAccess = true;
|
||||
}
|
||||
|
||||
field.set(object, value);
|
||||
|
||||
if (modifyAccess)
|
||||
{
|
||||
field.setAccessible(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new CliException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Copyright (c) 2009, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of JAB; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://kenai.com/projects/jab
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class CliRequiredException extends CliException
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
public CliRequiredException(String string)
|
||||
{
|
||||
super(string);
|
||||
}
|
||||
}
|
||||
85
scm-cli/src/main/java/sonia/scm/cli/ConvertUtil.java
Normal file
85
scm-cli/src/main/java/sonia/scm/cli/ConvertUtil.java
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class ConvertUtil
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(ConvertUtil.class.getName());
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @param value
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Object convertString(Class<?> type, String value)
|
||||
{
|
||||
Object result = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (type.isAssignableFrom(String.class))
|
||||
{
|
||||
result = value;
|
||||
}
|
||||
else if (type.isAssignableFrom(Short.class))
|
||||
{
|
||||
result = Short.parseShort(value);
|
||||
}
|
||||
else if (type.isAssignableFrom(Integer.class))
|
||||
{
|
||||
result = Integer.parseInt(value);
|
||||
}
|
||||
else if (type.isAssignableFrom(Long.class))
|
||||
{
|
||||
result = Long.parseLong(value);
|
||||
}
|
||||
else if (type.isAssignableFrom(BigInteger.class))
|
||||
{
|
||||
result = new BigInteger(value);
|
||||
}
|
||||
else if (type.isAssignableFrom(Float.class))
|
||||
{
|
||||
result = Float.parseFloat(value);
|
||||
}
|
||||
else if (type.isAssignableFrom(Double.class))
|
||||
{
|
||||
result = Double.parseDouble(value);
|
||||
}
|
||||
else if (type.isAssignableFrom(Boolean.class))
|
||||
{
|
||||
result = Boolean.parseBoolean(value);
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
logger.log(Level.FINER, null, ex);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
241
scm-cli/src/main/java/sonia/scm/cli/DefaultCliHelpBuilder.java
Normal file
241
scm-cli/src/main/java/sonia/scm/cli/DefaultCliHelpBuilder.java
Normal file
@@ -0,0 +1,241 @@
|
||||
/**
|
||||
* Copyright (c) 2009, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of JAB; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://kenai.com/projects/jab
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cli;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class DefaultCliHelpBuilder implements CliHelpBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public DefaultCliHelpBuilder() {}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param prefix
|
||||
* @param suffix
|
||||
*/
|
||||
public DefaultCliHelpBuilder(String prefix, String suffix)
|
||||
{
|
||||
this.prefix = prefix;
|
||||
this.suffix = suffix;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param arguments
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String createHelp(List<Argument> arguments)
|
||||
{
|
||||
String s = System.getProperty("line.separator");
|
||||
int paramLength = 20;
|
||||
List<HelpLine> list = new ArrayList<HelpLine>();
|
||||
|
||||
if (arguments != null)
|
||||
{
|
||||
for (Argument argument : arguments)
|
||||
{
|
||||
HelpLine line = new HelpLine();
|
||||
String name = argument.value();
|
||||
String longName = argument.longName();
|
||||
String description = argument.description();
|
||||
StringBuilder result = new StringBuilder("-");
|
||||
|
||||
result.append(name);
|
||||
|
||||
if (longName.length() > 0)
|
||||
{
|
||||
result.append(",--").append(longName);
|
||||
}
|
||||
|
||||
line.parameter = result.toString();
|
||||
|
||||
if (line.parameter.length() > paramLength)
|
||||
{
|
||||
paramLength = line.parameter.length();
|
||||
}
|
||||
|
||||
if (description.length() > 0)
|
||||
{
|
||||
line.description = description;
|
||||
}
|
||||
|
||||
list.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
paramLength += 2;
|
||||
Collections.sort(list);
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
if ((prefix != null) && (prefix.trim().length() > 0))
|
||||
{
|
||||
result.append(prefix).append(s);
|
||||
}
|
||||
|
||||
for (HelpLine line : list)
|
||||
{
|
||||
int length = line.parameter.length();
|
||||
|
||||
result.append(line.parameter);
|
||||
|
||||
for (int i = length; i < paramLength; i++)
|
||||
{
|
||||
result.append(" ");
|
||||
}
|
||||
|
||||
result.append(line.description).append(s);
|
||||
}
|
||||
|
||||
if ((suffix != null) && (suffix.trim().length() > 0))
|
||||
{
|
||||
result.append(suffix).append(s);
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getPrefix()
|
||||
{
|
||||
return prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getSuffix()
|
||||
{
|
||||
return suffix;
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param prefix
|
||||
*/
|
||||
public void setPrefix(String prefix)
|
||||
{
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param suffix
|
||||
*/
|
||||
public void setSuffix(String suffix)
|
||||
{
|
||||
this.suffix = suffix;
|
||||
}
|
||||
|
||||
//~--- inner classes --------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class description
|
||||
*
|
||||
*
|
||||
* @version Enter version here..., 10/03/06
|
||||
* @author Enter your name here...
|
||||
*/
|
||||
private static class HelpLine implements Comparable<HelpLine>
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param o
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(HelpLine o)
|
||||
{
|
||||
return parameter.compareTo(o.parameter);
|
||||
}
|
||||
|
||||
//~--- fields -------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private String description;
|
||||
|
||||
/** Field description */
|
||||
private String parameter;
|
||||
}
|
||||
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private String prefix;
|
||||
|
||||
/** Field description */
|
||||
private String suffix;
|
||||
}
|
||||
28
scm-server-util/pom.xml
Normal file
28
scm-server-util/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>scm</artifactId>
|
||||
<groupId>sonia.scm</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>sonia.scm.server</groupId>
|
||||
<artifactId>scm-server-util</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>scm-server-util</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>sonia.scm</groupId>
|
||||
<artifactId>scm-cli</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -64,13 +64,13 @@
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>2.0</version>
|
||||
<version>${guice.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.inject.extensions</groupId>
|
||||
<artifactId>guice-servlet</artifactId>
|
||||
<version>2.0</version>
|
||||
<version>${guice.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
@@ -92,41 +92,8 @@
|
||||
|
||||
<properties>
|
||||
<jersey.version>1.4-ea06</jersey.version>
|
||||
<guice.version>2.0</guice.version>
|
||||
</properties>
|
||||
|
||||
<profiles>
|
||||
|
||||
<profile>
|
||||
<id>endorsed</id>
|
||||
<activation>
|
||||
<property>
|
||||
<name>sun.boot.class.path</name>
|
||||
</property>
|
||||
</activation>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.1</version>
|
||||
<configuration>
|
||||
<compilerArguments>
|
||||
<bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
|
||||
</compilerArguments>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-endorsed-api</artifactId>
|
||||
<version>6.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
|
||||
</project>
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@ public class ContextListener extends GuiceServletContextListener
|
||||
protected void configureServlets()
|
||||
{
|
||||
bind(Authenticator.class).to(DemoAuthenticator.class);
|
||||
bind(SCMContextProvider.class).toInstance(SCMContext.getContext());
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user