private List<String> birthmarkTypes = new ArrayList<String>();
private List<String> filterTypes = new ArrayList<String>();
private Map<String, String> nameMappings = new HashMap<String, String>();
+ private String format;
/**
* properties which available on a session.
*/
birthmarkTypes.remove(type);
}
+ public String getFormat(){
+ return format;
+ }
+
+ public void setFormat(String format){
+ this.format = format;
+ }
+
public synchronized String[] getBirthmarkTypes(){
return birthmarkTypes.toArray(new String[getBirthmarkTypeSize()]);
}
private List<BirthmarkSpi> getServiceList(){
List<BirthmarkSpi> list = new ArrayList<BirthmarkSpi>();
if(parent != null){
- for(BirthmarkSpi spi : parent.getServices()){
+ for(BirthmarkSpi spi: parent.getServices()){
list.add(spi);
}
}
* $Id$
*/
-import java.io.BufferedReader;
import java.io.File;
-import java.io.FileWriter;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
-import java.util.ResourceBundle;
+import jp.sourceforge.stigmata.command.HelpCommand;
+import jp.sourceforge.stigmata.command.StigmataCommandFactory;
import jp.sourceforge.stigmata.digger.ClasspathContext;
-import jp.sourceforge.stigmata.event.BirthmarkEngineAdapter;
-import jp.sourceforge.stigmata.event.BirthmarkEngineEvent;
-import jp.sourceforge.stigmata.event.WarningMessages;
import jp.sourceforge.stigmata.hook.Phase;
import jp.sourceforge.stigmata.hook.StigmataHookManager;
-import jp.sourceforge.stigmata.printer.BirthmarkServicePrinter;
-import jp.sourceforge.stigmata.printer.ComparisonResultSetPrinter;
-import jp.sourceforge.stigmata.printer.ExtractionResultSetPrinter;
-import jp.sourceforge.stigmata.printer.PrinterManager;
import jp.sourceforge.stigmata.spi.BirthmarkSpi;
-import jp.sourceforge.stigmata.spi.ResultPrinterSpi;
-import jp.sourceforge.stigmata.ui.swing.StigmataFrame;
-import jp.sourceforge.stigmata.utils.ConfigFileExporter;
import jp.sourceforge.talisman.xmlcli.CommandLinePlus;
import jp.sourceforge.talisman.xmlcli.OptionsBuilder;
-import jp.sourceforge.talisman.xmlcli.ResourceHelpFormatter;
import jp.sourceforge.talisman.xmlcli.XmlCliConfigurationException;
import jp.sourceforge.talisman.xmlcli.builder.OptionsBuilderFactory;
import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
* @version $Revision$
*/
public final class Main{
- private PrinterManager manager = PrinterManager.getInstance();
-
/**
* main process.
* @throws org.apache.commons.cli.ParseException
stigmata.configuration(commandLine.getOptionValue("config-file"), commandLine.hasOption("reset-config"));
String[] arguments = commandLine.getArgs();
+ String commandString = "gui";
- String mode = commandLine.getOptionValue("mode");
- String format = commandLine.getOptionValue("format");
-
- if(format == null){
- format = "xml";
- }
- if(mode == null){
- mode = "gui";
+ if(arguments.length > 0){
+ commandString = arguments[0];
}
- BirthmarkContext context = stigmata.createContext();
- updateContext(context, commandLine);
- boolean exitFlag = executeOption(context.getEnvironment(), commandLine, options);
+ arguments = shiftArray(arguments);
- if(!exitFlag){
- if(!("gui".equals(mode) || "list".equals(mode)) && (arguments == null || arguments.length == 0)){
+ BirthmarkContext context = stigmata.createContext();
+ updateContext(context, commandLine);
- printHelp(context.getEnvironment(), options);
- return;
- }
- StigmataHookManager.getInstance().runHook(Phase.SETUP, context.getEnvironment());
+ StigmataCommandFactory factory = StigmataCommandFactory.getInstance();
+ factory.registerCommand("help", new HelpCommand(options));
- if(mode.equals("list")){
- listBirthmarks(context, format);
- }
- else if(mode.equals("extract")){
- extractBirthmarks(stigmata, arguments, format, context);
- }
- else if(mode.equals("compare")){
- compareBirthmarks(stigmata, arguments, format, context);
- }
- else if(mode.equals("gui")){
- StigmataFrame frame = new StigmataFrame(stigmata, context.getEnvironment());
- frame.setVisible(true);
- }
-
- if(!mode.equals("gui")){
- StigmataHookManager.getInstance().runHook(Phase.TEAR_DOWN, context.getEnvironment());
- }
- else{
- final BirthmarkEnvironment env = context.getEnvironment();
- Runtime.getRuntime().addShutdownHook(new Thread(){
- public void run(){
- StigmataHookManager.getInstance().runHook(
- Phase.TEAR_DOWN, env
- );
- }
- });
- }
+ StigmataCommand command = factory.getCommand(commandString);
+ if(!command.isAvailableArguments(arguments)){
+ command = factory.getCommand("help");
}
- }
- /**
- * extract birthmarks.
- */
- private void extractBirthmarks(Stigmata stigmata, String[] args, String format,
- BirthmarkContext context){
- try{
- context.setComparisonMethod(ComparisonMethod.ROUND_ROBIN_SAME_PAIR);
- BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment());
+ StigmataHookManager.getInstance().runHook(Phase.SETUP, context.getEnvironment());
- engine.addBirthmarkEngineListener(new BirthmarkEngineAdapter(){
- public void operationDone(BirthmarkEngineEvent e){
- WarningMessages warnings = e.getMessage();
- for(Iterator<Exception> i = warnings.exceptions(); i.hasNext(); ){
- i.next().printStackTrace();
- }
- }
- });
- ExtractionResultSet ers = engine.extract(args, context);
-
- ResultPrinterSpi spi = manager.getService(format);
- ExtractionResultSetPrinter formatter = spi.getExtractionResultSetPrinter();
- formatter.printResult(new PrintWriter(System.out), ers);
- }catch(Exception ex){
- ex.printStackTrace();
- }
+ command.setUp(context.getEnvironment());
+ command.perform(stigmata, context, arguments);
+ command.tearDown(context.getEnvironment());
}
/**
- *
+ * shift right given array.
+ * @param args
*/
- private void compareBirthmarks(Stigmata stigmata, String[] args, String format,
- BirthmarkContext context){
- try{
- BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment());
- context.setComparisonMethod(ComparisonMethod.ROUND_ROBIN_SAME_PAIR);
- engine.addBirthmarkEngineListener(new BirthmarkEngineAdapter(){
- public void operationDone(BirthmarkEngineEvent e){
- WarningMessages warnings = e.getMessage();
- for(Iterator<Exception> i = warnings.exceptions(); i.hasNext(); ){
- i.next().printStackTrace();
- }
- }
- });
-
- ExtractionResultSet rs = engine.extract(args, context);
- ComparisonResultSet resultset = engine.compare(rs);
- if(context.hasFilter()){
- resultset = engine.filter(resultset);
- }
-
- ResultPrinterSpi spi = manager.getService(format);
- ComparisonResultSetPrinter formatter = spi.getComparisonResultSetPrinter();
- formatter.printResult(new PrintWriter(System.out), resultset);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
- private void listBirthmarks(BirthmarkContext context, String format){
- try{
- BirthmarkSpi[] spis = context.getEnvironment().findServices();
- ResultPrinterSpi spi = manager.getService(format);
- BirthmarkServicePrinter formatter = spi.getBirthmarkServicePrinter();
-
- formatter.printResult(new PrintWriter(System.out), spis);
- }catch(IOException e){
- e.printStackTrace();
+ private String[] shiftArray(String[] args){
+ if(args.length > 0){
+ String[] arguments = new String[args.length - 1];
+ System.arraycopy(args, 1, arguments, 0, arguments.length);
+ args = arguments;
}
+ return args;
}
private void updateContext(BirthmarkContext context, CommandLinePlus cl){
ExtractionUnit unit = ExtractionUnit.valueOf(cl.getOptionValue("extraction-unit"));
context.setExtractionUnit(unit);
}
+ if(cl.hasOption("format")){
+ context.setFormat(cl.getOptionValue("format"));
+ }
+ else{
+ context.setFormat("xml");
+ }
addClasspath(env.getClasspathContext(), cl);
}
}
}
- private boolean executeOption(BirthmarkEnvironment env, CommandLinePlus commandLine, Options options){
- boolean exitFlag = false;
- if(commandLine.hasOption("help")){
- printHelp(env, options);
- exitFlag = true;
- }
- if(commandLine.hasOption("version")){
- printVersion();
- exitFlag = true;
- }
- if(commandLine.hasOption("license")){
- printLicense();
- exitFlag = true;
- }
- if(commandLine.hasOption("export-config")){
- exportConfiguration(env, commandLine.getOptionValue("export-config"));
- exitFlag = true;
- }
- return exitFlag;
- }
-
private Options buildOptions(){
try{
OptionsBuilderFactory factory = OptionsBuilderFactory.getInstance();
return null;
}
- public void exportConfiguration(BirthmarkEnvironment env, String file){
- try{
- PrintWriter out;
- if(file == null){
- out = new PrintWriter(System.out);
- }
- else{
- if(!file.endsWith(".xml")){
- file = file + ".xml";
- }
- out = new PrintWriter(new FileWriter(file));
- }
-
- new ConfigFileExporter(env).export(out);
- out.close();
- }catch(IOException e){
- }
- }
-
- public void printHelp(BirthmarkEnvironment env, Options options){
- Package p = getClass().getPackage();
- ResourceBundle helpResource = ResourceBundle.getBundle("resources.options");
- HelpFormatter formatter = new ResourceHelpFormatter(helpResource);
- formatter.printHelp(
- String.format(
- helpResource.getString("cli.interface"),
- p.getImplementationVersion()
- ),
- options
- );
- System.out.println();
- System.out.println(helpResource.getString("cli.interface.birthmarks"));
- for(BirthmarkSpi service: env.getServices()){
- if(!service.isExpert()){
- System.out.printf(" %-5s (%s): %s%n", service.getType(),
- service.getDisplayType(), service.getDescription());
- }
- }
- System.out.println();
- System.out.println(helpResource.getString("cli.interface.filters"));
- for(ComparisonPairFilterSet filterset: env.getFilterManager().getFilterSets()){
- String matchString = helpResource.getString("cli.interface.filter.matchall");
- if(filterset.isMatchAny()) matchString = helpResource.getString("cli.interface.filter.matchany");
- System.out.printf(" %s (%s)%n", filterset.getName(), matchString);
- for(ComparisonPairFilter filter: filterset){
- System.out.printf(" %s%n", filter);
- }
- }
- System.out.println();
- System.out.println(helpResource.getString("cli.interface.copyright"));
- System.out.println(helpResource.getString("cli.interface.mailto"));
- }
-
- public void printLicense(){
- try{
- InputStream in = getClass().getResourceAsStream("/META-INF/license.txt");
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String line;
-
- while((line = reader.readLine()) != null){
- System.out.println(line);
- }
- reader.close();
- }catch(IOException ex){
- ex.printStackTrace();
- }
- }
-
- public void printVersion(){
- ResourceBundle helpResource = ResourceBundle.getBundle("resources.options");
- Package p = getClass().getPackage();
- System.out.printf("%s %s%n", helpResource.getString("cli.version.header"), p.getImplementationVersion());
- }
-
public static void main(String[] args) throws Exception{
new Main(args);
}
* instance. singleton pattern.
*/
private static Stigmata stigmata;
+ private PrinterManager manager = PrinterManager.getInstance();
private BirthmarkEnvironment defaultEnvironment;
private List<BirthmarkEngineListener> listeners = new ArrayList<BirthmarkEngineListener>();
return engine;
}
+ public PrinterManager getPrinterManager(){
+ return manager;
+ }
+
public void addBirthmarkEngineListener(BirthmarkEngineListener listener){
listeners.add(listener);
}
--- /dev/null
+package jp.sourceforge.stigmata;
+
+public interface StigmataCommand{
+ public boolean isAvailableArguments(String[] args);
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args);
+
+ public void setUp(BirthmarkEnvironment env);
+
+ public void tearDown(BirthmarkEnvironment env);
+
+ public String getCommandString();
+}
* @author Haruaki TAMADA
* @version $Revision$
*/
-abstract class AbstractBirthmarkExtractorService extends AbstractServiceProvider implements BirthmarkExtractorSpi{
+public abstract class AbstractBirthmarkExtractorService extends AbstractServiceProvider implements BirthmarkExtractorSpi{
/**
* returns a type of the birthmark this service provides.
*/
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import jp.sourceforge.stigmata.BirthmarkEnvironment;
+import jp.sourceforge.stigmata.StigmataCommand;
+import jp.sourceforge.stigmata.hook.Phase;
+import jp.sourceforge.stigmata.hook.StigmataHookManager;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public abstract class AbstractStigmataCommand implements StigmataCommand{
+ public boolean isAvailableArguments(String[] args){
+ return true;
+ }
+
+ public void setUp(BirthmarkEnvironment env){
+ StigmataHookManager.getInstance().runHook(Phase.SETUP, env);
+ }
+
+ public void tearDown(BirthmarkEnvironment env){
+ StigmataHookManager.getInstance().runHook(Phase.TEAR_DOWN, env);
+ }
+
+ public abstract String getCommandString();
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.io.PrintWriter;
+import java.util.Iterator;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.BirthmarkEngine;
+import jp.sourceforge.stigmata.ComparisonMethod;
+import jp.sourceforge.stigmata.ComparisonResultSet;
+import jp.sourceforge.stigmata.ExtractionResultSet;
+import jp.sourceforge.stigmata.Stigmata;
+import jp.sourceforge.stigmata.event.BirthmarkEngineAdapter;
+import jp.sourceforge.stigmata.event.BirthmarkEngineEvent;
+import jp.sourceforge.stigmata.event.WarningMessages;
+import jp.sourceforge.stigmata.printer.ComparisonResultSetPrinter;
+import jp.sourceforge.stigmata.spi.ResultPrinterSpi;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class CompareCommand extends AbstractStigmataCommand{
+ public boolean isAvailableArguments(String[] args){
+ return args.length > 0;
+ }
+
+ @Override
+ public String getCommandString(){
+ return "compare";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ try{
+ BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment());
+ context.setComparisonMethod(ComparisonMethod.ROUND_ROBIN_SAME_PAIR);
+ engine.addBirthmarkEngineListener(new BirthmarkEngineAdapter(){
+ public void operationDone(BirthmarkEngineEvent e){
+ WarningMessages warnings = e.getMessage();
+ for(Iterator<Exception> i = warnings.exceptions(); i.hasNext(); ){
+ i.next().printStackTrace();
+ }
+ }
+ });
+
+ ExtractionResultSet rs = engine.extract(args, context);
+ ComparisonResultSet resultset = engine.compare(rs);
+ if(context.hasFilter()){
+ resultset = engine.filter(resultset);
+ }
+
+ ResultPrinterSpi spi = stigmata.getPrinterManager().getService(context.getFormat());
+ ComparisonResultSetPrinter formatter = spi.getComparisonResultSetPrinter();
+ formatter.printResult(new PrintWriter(System.out), resultset);
+ }catch(Exception e){
+ e.printStackTrace();
+ }
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.Stigmata;
+import jp.sourceforge.stigmata.utils.ConfigFileExporter;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class ExportConfigCommand extends AbstractStigmataCommand{
+
+ @Override
+ public String getCommandString(){
+ return "export-config";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ try{
+ PrintWriter out;
+ if(args == null || args.length == 0){
+ out = new PrintWriter(System.out);
+ }
+ else{
+ if(!args[0].endsWith(".xml")){
+ args[0] = args[0] + ".xml";
+ }
+ out = new PrintWriter(new FileWriter(args[0]));
+ }
+
+ new ConfigFileExporter(context.getEnvironment()).export(out);
+ out.close();
+ }catch(IOException e){
+ }
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.io.PrintWriter;
+import java.util.Iterator;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.BirthmarkEngine;
+import jp.sourceforge.stigmata.ComparisonMethod;
+import jp.sourceforge.stigmata.ExtractionResultSet;
+import jp.sourceforge.stigmata.Stigmata;
+import jp.sourceforge.stigmata.event.BirthmarkEngineAdapter;
+import jp.sourceforge.stigmata.event.BirthmarkEngineEvent;
+import jp.sourceforge.stigmata.event.WarningMessages;
+import jp.sourceforge.stigmata.printer.ExtractionResultSetPrinter;
+import jp.sourceforge.stigmata.spi.ResultPrinterSpi;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class ExtractCommand extends AbstractStigmataCommand{
+ public boolean isAvailableArguments(String[] args){
+ return args.length > 0;
+ }
+
+ @Override
+ public String getCommandString(){
+ return "extract";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ try{
+ context.setComparisonMethod(ComparisonMethod.ROUND_ROBIN_SAME_PAIR);
+ BirthmarkEngine engine = new BirthmarkEngine(context.getEnvironment());
+
+ engine.addBirthmarkEngineListener(new BirthmarkEngineAdapter(){
+ public void operationDone(BirthmarkEngineEvent e){
+ WarningMessages warnings = e.getMessage();
+ for(Iterator<Exception> i = warnings.exceptions(); i.hasNext(); ){
+ i.next().printStackTrace();
+ }
+ }
+ });
+ ExtractionResultSet ers = engine.extract(args, context);
+
+ ResultPrinterSpi spi = stigmata.getPrinterManager().getService(context.getFormat());
+ ExtractionResultSetPrinter formatter = spi.getExtractionResultSetPrinter();
+ formatter.printResult(new PrintWriter(System.out), ers);
+ }catch(Exception ex){
+ ex.printStackTrace();
+ }
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.BirthmarkEnvironment;
+import jp.sourceforge.stigmata.Stigmata;
+import jp.sourceforge.stigmata.hook.Phase;
+import jp.sourceforge.stigmata.hook.StigmataHookManager;
+import jp.sourceforge.stigmata.ui.swing.StigmataFrame;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class GuiCommand extends AbstractStigmataCommand{
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ StigmataFrame frame = new StigmataFrame(stigmata, context.getEnvironment());
+ frame.setVisible(true);
+ }
+
+ @Override
+ public String getCommandString(){
+ return "gui";
+ }
+
+ @Override
+ public void tearDown(final BirthmarkEnvironment env){
+ Runtime.getRuntime().addShutdownHook(new Thread(){
+ public void run(){
+ StigmataHookManager.getInstance().runHook(
+ Phase.TEAR_DOWN, env
+ );
+ }
+ });
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.util.ResourceBundle;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.BirthmarkEnvironment;
+import jp.sourceforge.stigmata.ComparisonPairFilter;
+import jp.sourceforge.stigmata.ComparisonPairFilterSet;
+import jp.sourceforge.stigmata.Stigmata;
+import jp.sourceforge.stigmata.spi.BirthmarkSpi;
+import jp.sourceforge.talisman.xmlcli.ResourceHelpFormatter;
+
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class HelpCommand extends AbstractStigmataCommand{
+ private Options options;
+
+ public HelpCommand(Options options){
+ this.options = options;
+ }
+
+ @Override
+ public String getCommandString(){
+ return "help";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ BirthmarkEnvironment env = context.getEnvironment();
+ Package p = getClass().getPackage();
+
+ ResourceBundle helpResource = ResourceBundle.getBundle("resources.options");
+ HelpFormatter formatter = new ResourceHelpFormatter(helpResource);
+ formatter.printHelp(
+ String.format(
+ helpResource.getString("cli.interface"),
+ p.getImplementationVersion()
+ ),
+ options
+ );
+ System.out.println();
+ System.out.println(helpResource.getString("cli.interface.birthmarks"));
+ for(BirthmarkSpi service: env.getServices()){
+ if(!service.isExpert()){
+ System.out.printf(" %-5s (%s): %s%n", service.getType(),
+ service.getDisplayType(), service.getDescription());
+ }
+ }
+ System.out.println();
+ System.out.println(helpResource.getString("cli.interface.filters"));
+ for(ComparisonPairFilterSet filterset: env.getFilterManager().getFilterSets()){
+ String matchString = helpResource.getString("cli.interface.filter.matchall");
+ if(filterset.isMatchAny()) matchString = helpResource.getString("cli.interface.filter.matchany");
+ System.out.printf(" %s (%s)%n", filterset.getName(), matchString);
+ for(ComparisonPairFilter filter: filterset){
+ System.out.printf(" %s%n", filter);
+ }
+ }
+ System.out.println();
+ System.out.println(helpResource.getString("cli.interface.copyright"));
+ System.out.println(helpResource.getString("cli.interface.mailto"));
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.BirthmarkEnvironment;
+import jp.sourceforge.stigmata.Stigmata;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class InstallCommand extends AbstractStigmataCommand{
+ public boolean isAvailableArguments(String[] args){
+ return args.length > 0;
+ }
+
+ @Override
+ public String getCommandString(){
+ return "install";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ File pluginsDir = new File(BirthmarkEnvironment.getStigmataHome(), "plugins");
+ BirthmarkEnvironment env = context.getEnvironment();
+
+ for(int i = 0; i < args.length; i++){
+ File pluginSource = new File(args[i]);
+ File pluginDest = new File(pluginsDir, pluginSource.getName());
+
+ if(!pluginSource.getName().endsWith(".jar")){
+ throw new IllegalArgumentException("plugin is allowed only jar archive: " + args[i]);
+ }
+ if(pluginDest.exists()){
+ String override = env.getProperty("override.exists.plugin");
+ if(override != null && (override.equalsIgnoreCase("true") || override.equalsIgnoreCase("yes"))){
+ pluginDest.delete();
+ }
+ else{
+ File backupFile = new File(pluginDest.getParent(), pluginDest.getName() + ".back");
+ if(backupFile.exists()) backupFile.delete();
+ pluginDest.renameTo(backupFile);
+ }
+ }
+
+ byte[] data = new byte[256];
+ int read;
+
+ try{
+ InputStream in = new FileInputStream(pluginSource);
+ OutputStream out = new FileOutputStream(pluginDest);
+
+ while((read = in.read(data)) != -1){
+ out.write(data, 0, read);
+ }
+ in.close();
+ out.close();
+ } catch(IOException e){
+ }
+ }
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.Stigmata;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class LicenseCommand extends AbstractStigmataCommand{
+ @Override
+ public String getCommandString(){
+ return "license";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ try{
+ InputStream in = getClass().getResourceAsStream("/META-INF/license.txt");
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ String line;
+
+ while((line = reader.readLine()) != null){
+ System.out.println(line);
+ }
+ reader.close();
+ }catch(IOException ex){
+ ex.printStackTrace();
+ }
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.Stigmata;
+import jp.sourceforge.stigmata.printer.BirthmarkServicePrinter;
+import jp.sourceforge.stigmata.spi.BirthmarkSpi;
+import jp.sourceforge.stigmata.spi.ResultPrinterSpi;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class ListBirthmarksCommand extends AbstractStigmataCommand{
+ @Override
+ public String getCommandString(){
+ return "list-birthmarks";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ BirthmarkSpi[] spis = context.getEnvironment().findServices();
+ ResultPrinterSpi spi = stigmata.getPrinterManager().getService(context.getFormat());
+ BirthmarkServicePrinter formatter = spi.getBirthmarkServicePrinter();
+
+ try{
+ PrintWriter out;
+ if(args.length == 0){
+ out = new PrintWriter(System.out);
+ }
+ else{
+ String target = validateTarget(args[0], context.getFormat());
+ out = new PrintWriter(new FileWriter(target));
+ }
+ formatter.printResult(out, spis);
+ }catch(IOException e){
+ e.printStackTrace();
+ }
+ }
+
+ private String validateTarget(String fileName, String format){
+ if(!fileName.endsWith("." + format)){
+ fileName = fileName + "." + format;
+ }
+ return fileName;
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.util.HashMap;
+import java.util.Map;
+
+import jp.sourceforge.stigmata.StigmataCommand;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class StigmataCommandFactory{
+ private static final StigmataCommandFactory factory = new StigmataCommandFactory();
+ private Map<String, StigmataCommand> commands = new HashMap<String, StigmataCommand>();
+
+ private StigmataCommandFactory(){
+ commands.put("compare", new CompareCommand());
+ commands.put("export-config", new ExportConfigCommand());
+ commands.put("extract", new ExtractCommand());
+ commands.put("gui", new GuiCommand());
+ commands.put("install", new InstallCommand());
+ commands.put("license", new LicenseCommand());
+ commands.put("list-birthmarks", new ListBirthmarksCommand());
+ /* this command is not supported in Windows OS.
+ * Deletion/Renaming is failed because plugin file is locked by system.
+ * commands.put("uninstall", new UninstallCommand());
+ */
+ commands.put("version", new VersionCommand());
+ }
+
+ public void registerCommand(String commandString, StigmataCommand command){
+ commands.put(commandString, command);
+ }
+
+ public static StigmataCommandFactory getInstance(){
+ return factory;
+ }
+
+ public StigmataCommand getCommand(String command){
+ return commands.get(command);
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.BirthmarkEnvironment;
+import jp.sourceforge.stigmata.Stigmata;
+import jp.sourceforge.stigmata.birthmarks.BirthmarkService;
+import jp.sourceforge.stigmata.spi.BirthmarkSpi;
+import jp.sourceforge.stigmata.utils.ConfigFileExporter;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+@Deprecated
+public class UninstallCommand extends AbstractStigmataCommand{
+
+ public boolean isAvailableArguments(String[] args){
+ return args.length > 0;
+ }
+
+ @Override
+ public String getCommandString(){
+ return "uninstall";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ File pluginsDir = new File(BirthmarkEnvironment.getStigmataHome(), "plugins");
+ BirthmarkEnvironment env = context.getEnvironment();
+ boolean removeServiceInConfigFile = false;
+
+ for(int i = 0; i < args.length; i++){
+ BirthmarkSpi service = env.getService(args[i]);
+ if(service instanceof BirthmarkService){
+ env.removeService(args[i]);
+ removeServiceInConfigFile = true;
+ }
+ else{
+ String fileName = getPluginFileNameOfService(context, service);
+ if(fileName != null){
+ File pluginFile = new File(pluginsDir, fileName);
+ pluginFile.renameTo(new File(pluginFile.getParentFile(), pluginFile.getName() + ".back"));
+ }
+ }
+ }
+ if(removeServiceInConfigFile){
+ updateConfigFile(env);
+ }
+ }
+
+ private void updateConfigFile(BirthmarkEnvironment env){
+ File configFile = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
+ try{
+ new ConfigFileExporter(env).export(new PrintWriter(new FileWriter(configFile)));
+ } catch(IOException e){
+ e.printStackTrace();
+ }
+ }
+
+ private String getPluginFileNameOfService(BirthmarkContext context, BirthmarkSpi service){
+ Class<?> serviceClass = service.getClass();
+ URL location = serviceClass.getResource("/" + serviceClass.getName().replace('.', '/') + ".class");
+
+ if(location != null){
+ Pattern pattern = Pattern.compile("jar:(.*)/plugins/(.*.jar)!([a-zA-Z0-9$/.]+.class)");
+ Matcher matcher = pattern.matcher(location.toString());
+
+ if(matcher.matches()){
+ try{
+ URL homeLocation = new File(BirthmarkEnvironment.getStigmataHome()).toURI().toURL();
+ String matchedLocation = matcher.group(1) + "/";
+ if(matchedLocation.equals(homeLocation.toString())){
+ return matcher.group(2);
+ }
+ } catch(MalformedURLException e){
+ e.printStackTrace();
+ }
+ }
+ }
+ return null;
+ }
+}
--- /dev/null
+package jp.sourceforge.stigmata.command;
+
+/*
+ * $Id$
+ */
+
+import java.util.ResourceBundle;
+
+import jp.sourceforge.stigmata.BirthmarkContext;
+import jp.sourceforge.stigmata.Stigmata;
+
+/**
+ *
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class VersionCommand extends AbstractStigmataCommand{
+ @Override
+ public String getCommandString(){
+ return "version";
+ }
+
+ public void perform(Stigmata stigmata, BirthmarkContext context, String[] args){
+ ResourceBundle helpResource = ResourceBundle.getBundle("resources.options");
+ Package p = getClass().getPackage();
+ System.out.printf("%s %s%n", helpResource.getString("cli.version.header"), p.getImplementationVersion());
+ }
+}
cli.option.classpath=Append classpath. Separated with comma.
cli.option.birthmark=Set target birthmarks separated with comma. If this option is not specified, following available birthmarks are specified.
-cli.option.mode=Set running mode. Available values are `gui,' `list,' `extract,' `compare,' and `gui.' Default is `gui.'
-cli.option.format=Set output report format. Available value is `csv' and `xml.' Default is `csv.'
+cli.option.format=Set output report format. Available value is `csv' and `xml.' Default is `xml.'
cli.option.filter=Filtering round robin result. This option is ignored when mode is not `compare.'
-cli.option.config-file=Set configuration xml file. If you do not specified this option and you have `$HOME/.stimgata/birthmarks.xml,' this tool read it.
+cli.option.config-file=Set configuration xml file. If this option was not specified, Stigmata read config file from `$STIGMATA_HOME/birthmarks.xml.'
cli.option.reset-config=Reset configuration file.
cli.option.export-config=Export configuration file to specified file or standard output.
cli.option.help=Print this message.
cli.option.version=Print version information.
cli.option.extraction-unit=Extract birthmark from EXTRACT_UNIT. Available values are CLASS, PACKAGE, and ARCHIVE. Default is CLASS
cli.option.store-target=This option represents how to store extracted birthmarks. Available values are MEMORY, and XMLFILE.
-cli.interface=java -jar stigmata-%s.jar <OPTIONS> <TARGETS>%nTARGETS is allowed as jar files, war files, class files, and classpath directory.
+cli.interface=java -jar stigmata-%s.jar <OPTIONS> <COMMAND> <TARGETS>%nCOMMAND: available values are `install,' `export-config', `license,' `version,' `list-birthmarks,' `extract,' `compare,' and `gui.' Default is `gui.'%nTARGETS is allowed as jar files, war files, class files, and classpath directory.
cli.interface.birthmarks=Available birthmarks:
cli.interface.filters=Available filters:
cli.interface.copyright=Copyright (C) by Haruaki Tamada, Ph.D. (Kyoto Sangyo University, Japan)
<multiple-args />
</definition>
- <definition name="mode">
- <short-option>m</short-option>
- <long-option>mode</long-option>
- <description>$(cli.option.mode)</description>
- <argument>REQUIRED</argument>
- <argument-name>MODE</argument-name>
- </definition>
-
<definition name="format">
<short-option>f</short-option>
<long-option>format</long-option>
<argument>NONE</argument>
</definition>
- <definition name="export-config">
- <long-option>export-config</long-option>
- <description>$(cli.option.export-config)</description>
- <argument>OPTIONAL</argument>
- <argument-name>FILE</argument-name>
- </definition>
-
- <definition name="help">
- <short-option>h</short-option>
- <long-option>help</long-option>
- <description>$(cli.option.help)</description>
- <argument>NONE</argument>
- </definition>
-
- <definition name="license">
- <short-option>l</short-option>
- <long-option>license</long-option>
- <description>$(cli.option.license)</description>
- <argument>NONE</argument>
- </definition>
-
- <definition name="version">
- <short-option>v</short-option>
- <long-option>version</long-option>
- <description>$(cli.option.version)</description>
- <argument>NONE</argument>
- </definition>
-
<definition name="extraction-unit">
<short-option>e</short-option>
<long-option>extraction-unit</long-option>
</definitions>
<available-options>
- <option>mode</option>
<option>classpath</option>
<option>birthmark</option>
<option>filter</option>
<option>store-target</option>
<option>config-file</option>
- <option>export-config</option>
<option>reset-config</option>
-
- <option>help</option>
- <option>license</option>
- <option>version</option>
</available-options>
</command-line>
cli.option.classpath=\82±\82Ì\83I\83v\83V\83\87\83\93\82Ì\88ø\90\94\82ð\83N\83\89\83X\83p\83X\82É\92Ç\89Á\82µ\82Ü\82·\81D \88ø\90\94\82Í\83R\83\93\83}\82Å\8bæ\90Ø\82è\82Ü\82·\81D\r
cli.option.birthmark=\92\8a\8fo\82·\82é\83o\81[\83X\83}\81[\83N\82ð\8ew\92è\82µ\82Ü\82·\81D\r
-cli.option.mode=\8eÀ\8ds\83\82\81[\83h\82ð\8ew\92è\82µ\82Ü\82·\81D\88ø\90\94\82É\89½\82à\8ew\92è\82µ\82È\82¯\82ê\82Î `gui' \82ª\8ew\92è\82³\82ê\82½\82Æ\82Ý\82È\82µ\81C GUI\83\82\81[\83h\82Å\97§\82¿\8fã\82ª\82è\82Ü\82·\81D \97L\8cø\82È\92l\82Í `gui,' `list,' `extract,' `compare,' \82Å\82·\81D\r
-cli.option.format=\83\8c\83|\81[\83g\82Ì\8fo\97Í\8c`\8e®\82ð\8ew\92è\82µ\82Ü\82·\81D `csv' \82à\82µ\82\82Í `xml' \82Ì\82Ý\82ª\97L\8cø\82Å\82·\81D \82±\82Ì\83I\83v\83V\83\87\83\93\82ª\8ew\92è\82³\82ê\82È\82¢\8fê\8d\87\82Í `csv' \82ª\8ew\92è\82³\82ê\82½\82Æ\82Ý\82È\82µ\82Ü\82·\81D\r
+cli.option.format=\83\8c\83|\81[\83g\82Ì\8fo\97Í\8c`\8e®\82ð\8ew\92è\82µ\82Ü\82·\81D `csv' \82à\82µ\82\82Í `xml' \82Ì\82Ý\82ª\97L\8cø\82Å\82·\81D \82±\82Ì\83I\83v\83V\83\87\83\93\82ª\8ew\92è\82³\82ê\82È\82¢\8fê\8d\87\82Í `xml' \82ª\8ew\92è\82³\82ê\82½\82Æ\82Ý\82È\82µ\82Ü\82·\81D\r
cli.option.filter=\91\8d\93\96\82½\82è\94ä\8ar\8c\8b\89Ê\82É\82©\82¯\82é\83t\83B\83\8b\83^\82ð\8ew\92è\82µ\82Ü\82·\81D \82±\82Ì\83I\83v\83V\83\87\83\93\82Í\8eÀ\8ds\83\82\81[\83h\82ª `compare' \88È\8aO\82Ì\82Æ\82«\82Í\96³\8e\8b\82³\82ê\82Ü\82·\81D\r
-cli.option.config-file=\8f\89\8aú\90Ý\92è\83t\83@\83C\83\8b\82ð\8ew\92è\82µ\82Ü\82·\81D \82±\82Ì\83I\83v\83V\83\87\83\93\82ª\8ew\92è\82³\82ê\82È\82¢\8fê\8d\87\82Í\83z\81[\83\80\83f\83B\83\8c\83N\83g\83\8a\8fã\82Ì `.stigmata/stigmata.xml' \82ª\8ew\92è\82³\82ê\82½\82Æ\82Ý\82È\82µ\82Ü\82·\81D\r
+cli.option.config-file=\8f\89\8aú\90Ý\92è\83t\83@\83C\83\8b\82ð\8ew\92è\82µ\82Ü\82·\81D \82±\82Ì\83I\83v\83V\83\87\83\93\82ª\8ew\92è\82³\82ê\82È\82¢\8fê\8d\87\82Í `$STIGMATA_HOME/stigmata.xml' \82ª\8ew\92è\82³\82ê\82½\82Æ\82Ý\82È\82µ\82Ü\82·\81D\r
cli.option.reset-config=\8c»\8dÝ\82Ì\90Ý\92è\83t\83@\83C\83\8b\82ð\94j\8aü\82µ\81C \8f\89\8aú\90Ý\92è\82É\96ß\82µ\82Ü\82·\81D\r
cli.option.export-config=\8c»\8dÝ\82Ì\90Ý\92è\82ð\95W\8f\80\8fo\97Í\82É\8f\91\82«\8fo\82µ\82Ü\82·\81D\r
cli.option.help=\82±\82Ì\83\81\83b\83Z\81[\83W\82ð\95\\8e¦\82µ\82Ü\82·\81D\r
cli.option.license=Stigmata\82Ì\83\89\83C\83Z\83\93\83X\82ð\95\\8e¦\82µ\82Ü\82·\81D\r
cli.option.version=Stigmata\82Ì\83o\81[\83W\83\87\83\93\8fî\95ñ\82ð\95\\8e¦\82µ\82Ü\82·\81D\r
-cli.option.extraction-unit=\83o\81[\83X\83}\81[\83N\92\8a\8fo\82Ì\92P\88Ê\82ð\8ew\92è\82µ\82Ü\82·\81D \82±\82Ì\83I\83v\83V\83\87\83\93\82ª\8ew\92è\82³\82ê\82È\82¢\8fê\8d\87\81C `CLASS' \82ª\8ew\92è\82³\82ê\82½\82Æ\82Ý\82È\82µ\82Ü\82·\81D \97L\8cø\82È\92l\82Í`CLASS,' `PACKAGE,' `ARCHIVE'\82Å\82·\81D\r
+cli.option.extraction-unit=\83o\81[\83X\83}\81[\83N\92\8a\8fo\82Ì\92P\88Ê\82ð\8ew\92è\82µ\82Ü\82·\81D \83f\83t\83H\83\8b\83g\82Í `CLASS' \82Å\82·\81D \97L\8cø\82È\92l\82Í`CLASS,' `PACKAGE,' `ARCHIVE'\82Å\82·\81D\r
cli.option.store-target=\83o\81[\83X\83}\81[\83N\82Ì\8ai\94[\95û\96@\82ð\8ew\92è\82µ\82Ü\82·\81D \97L\8cø\82È\92l\82Í `MEMORY,' `XMLFILE'\82Å\82·\81D \82±\82Ì\83I\83v\83V\83\87\83\93\82Í\90Ý\92è\83t\83@\83C\83\8b\82Å\82à\8ew\92è\82·\82é\82±\82Æ\82ª\89Â\94\\82Å\82·\81D\r
-cli.interface=java -jar stigmata-%s.jar <OPTIONS> <TARGETS>%nTARGETS\82É\82Íjar\83t\83@\83C\83\8b\81Cwar\83t\83@\83C\83\8b\81C\83N\83\89\83X\83t\83@\83C\83\8b\82â\83f\83B\83\8c\83N\83g\83\8a\82ª\8ew\92è\89Â\94\\82Å\82·\81D\r
+cli.interface=java -jar stigmata-%s.jar <OPTIONS> <COMMAND> <TARGETS>%n\97L\8cø\82È\83R\83}\83\93\83h: gui, list-birthmarks, install, extract, compare, export-config, help, license, version%nTARGETS\82É\82Íjar\83t\83@\83C\83\8b\81Cwar\83t\83@\83C\83\8b\81C\83N\83\89\83X\83t\83@\83C\83\8b\82â\83f\83B\83\8c\83N\83g\83\8a\82ª\8ew\92è\89Â\94\\82Å\82·\81D\r
cli.interface.birthmarks=\8ew\92è\89Â\94\\82È\83o\81[\83X\83}\81[\83N:\r
cli.interface.filters=\8ew\92è\89Â\94\\82È\83t\83B\83\8b\83^:\r
cli.interface.copyright=Copyright (C) by Haruaki Tamada, Ph.D. (Kyoto Sangyo University)\r
<name>birthmark.store.target</name>
<value>MEMORY</value>
</property>
+ <property>\r
+ <name>override.exists.plugin</name>\r
+ <value>yes</value>\r
+ </property>\r
<!--
<property>
<property-name>name</property-name>
Birthmark birthmark = array[0].getBirthmark("cvfv");
Assert.assertEquals("cvfv", birthmark.getType());
- Assert.assertEquals(3, birthmark.getElementCount());
+ Assert.assertEquals(4, birthmark.getElementCount());
BirthmarkElement[] elements = birthmark.getElements();
Assert.assertEquals("jp.sourceforge.stigmata.birthmarks.cvfv.TypeAndValueBirthmarkElement",
elements[1].getClass().getName());
Assert.assertEquals("jp.sourceforge.stigmata.birthmarks.cvfv.TypeAndValueBirthmarkElement",
elements[2].getClass().getName());
+ Assert.assertEquals("jp.sourceforge.stigmata.birthmarks.cvfv.TypeAndValueBirthmarkElement",
+ elements[3].getClass().getName());
Assert.assertEquals("Ljp/sourceforge/stigmata/Stigmata;",
- ((TypeAndValueBirthmarkElement)elements[0]).getSignature());
+ ((TypeAndValueBirthmarkElement)elements[0]).getSignature());
Assert.assertNull(((TypeAndValueBirthmarkElement)elements[0]).getValue());
- Assert.assertEquals("Ljp/sourceforge/stigmata/BirthmarkEnvironment;",
- ((TypeAndValueBirthmarkElement)elements[1]).getSignature());
+ Assert.assertEquals("Ljp/sourceforge/stigmata/printer/PrinterManager;",
+ ((TypeAndValueBirthmarkElement)elements[1]).getSignature());
Assert.assertNull(((TypeAndValueBirthmarkElement)elements[1]).getValue());
- Assert.assertEquals("Ljava/util/List;",
+ Assert.assertEquals("Ljp/sourceforge/stigmata/BirthmarkEnvironment;",
((TypeAndValueBirthmarkElement)elements[2]).getSignature());
Assert.assertNull(((TypeAndValueBirthmarkElement)elements[2]).getValue());
+
+ Assert.assertEquals("Ljava/util/List;",
+ ((TypeAndValueBirthmarkElement)elements[3]).getSignature());
+ Assert.assertNull(((TypeAndValueBirthmarkElement)elements[3]).getValue());
}
@Test