Skip to content

Commit

Permalink
Merge pull request #12 from pgdurand/prepV5
Browse files Browse the repository at this point in the history
Prep v5
  • Loading branch information
pgdurand authored Dec 24, 2022
2 parents dd09d4b + 3df944f commit b6dea5a
Show file tree
Hide file tree
Showing 15 changed files with 364 additions and 124 deletions.
14 changes: 5 additions & 9 deletions src/bzh/plealog/dbmirror/main/Annotate.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@
*
* @author Patrick G. Durand
*/
public class Annotate {
@BdmTool(command="annotate", description="annotate a BLAST XML formatted file")
public class Annotate implements BdmToolApi {

/**
* Setup the valid command-line of the application.
*/
@SuppressWarnings("static-access")
private static Options getCmdLineOptions() {
private Options getCmdLineOptions() {
Options opts;

Option type = OptionBuilder
Expand Down Expand Up @@ -95,7 +96,8 @@ private static Options getCmdLineOptions() {
return opts;
}

public static boolean doJob(String[] args){
@Override
public boolean execute(String[] args){
PAnnotateBlastResult annotator;
CommandLine cmdLine;
String input, output, writer, type;
Expand All @@ -121,10 +123,4 @@ public static boolean doJob(String[] args){

return annotator.annotate(input, output, writer, type, includeBC);
}

public static void main(String[] args) {
if (!doJob(args)){
System.exit(1);// exit code=1 : do this to report error to calling app
}
}
}
1 change: 1 addition & 0 deletions src/bzh/plealog/dbmirror/main/AutoCheckDescriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
*
* @author Patrick G. Durand
*/
@BdmTool(command="check", description="check whether or not a descriptor is still ok (URL not broken, etc)")
public class AutoCheckDescriptors {
private static final Log LOGGER = LogFactory.getLog(
DBMSAbstractConfig.KDMS_ROOTLOG_CATEGORY + ".DSCChecker");
Expand Down
37 changes: 37 additions & 0 deletions src/bzh/plealog/dbmirror/main/BdmTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright (C) 2022 Patrick G. Durand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/agpl-3.0.txt
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*/
package bzh.plealog.dbmirror.main;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* Annotation aims at marking a class as a BeeDeeM command-line tool.
*
* @author Patrick G. Durand
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface BdmTool {
/**
* Name of command.
*/
String command();
/**
* Description of command.
*/
String description();
}
33 changes: 33 additions & 0 deletions src/bzh/plealog/dbmirror/main/BdmToolApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (C) 2022 Patrick G. Durand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/agpl-3.0.txt
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*/
package bzh.plealog.dbmirror.main;

/**
* Defines API a BeeDeeM command-line tool must implement.
*
* @author Patrick G. Durand
*/
public interface BdmToolApi {
/**
* Execute business logic of a tool.
*
* @param args command-line arguments
*
* @return true whether execution is ok, false otherwise.
*/
public boolean execute(String[] args);
}
113 changes: 77 additions & 36 deletions src/bzh/plealog/dbmirror/main/BeeDeeMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@
*/
package bzh.plealog.dbmirror.main;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import bzh.plealog.dbmirror.ui.resources.DBMSMessages;

Expand All @@ -35,31 +44,54 @@
* @author Patrick G. Durand
*/
public class BeeDeeMain {

private static final String[] TOOL_LIST= {
"Annotate",
"DeleteBank",
"Dump",
"Install",
"Query",
"UiInstall"};


/** Code from: https://www.baeldung.com/java-find-all-classes-in-package*/
public static Set<Class<?>> findAllClassesUsingClassLoader(
String packageName) {
InputStream stream = ClassLoader.getSystemClassLoader()
.getResourceAsStream(packageName.replaceAll("[.]", "/"));
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
return reader.lines().filter(line -> line.endsWith(".class"))
.map(line -> getClass(line, packageName)).collect(Collectors.toSet());
}
private static Class<?> getClass(String className, String packageName) {
try {
return Class.forName(packageName + "."
+ className.substring(0, className.lastIndexOf('.')));
} catch (ClassNotFoundException e) {
System.err.println(e);
}
return null;
}
/** */

private static void dumpHelp() {
Properties props = StarterUtils.getVersionProperties();
System.out.print(props.getProperty("prg.app.name"));
System.out.print(" ");
System.out.println(DBMSMessages.getString("Tool.Master.intro"));
for (String tName : TOOL_LIST) {
Hashtable<String, String> tools = new Hashtable<String, String>();
Set<Class<?>> clazz = findAllClassesUsingClassLoader(new BeeDeeMain().getClass().getPackage().getName());
for(Class<?> c : clazz) {
BdmTool bdmT = c.getAnnotation(BdmTool.class);
if (bdmT != null) {
tools.put(bdmT.command(), bdmT.description());
}
}
List<String> sortedTools = Collections.list(tools.keys());
Collections.sort(sortedTools);
for(String tName : sortedTools) {
System.out.print(" bmd ");
System.out.print(DBMSMessages.getString("Tool."+tName+".cmd"));
if (tName.equals("UiInstall")) {
System.out.print(tName);
if (tName.equals("ui")) {
System.out.print(": ");
}
else {
System.out.print(" [options]: ");
}
System.out.println(DBMSMessages.getString("Tool."+tName+".desc"));
System.out.println(tools.get(tName));
}

StringBuffer buf = new StringBuffer();
System.out.println(DBMSMessages.getString("Tool.Master.more"));
buf.append("--\n");
Expand Down Expand Up @@ -92,33 +124,42 @@ public static void main(String[] args) {
args = Arrays.copyOfRange(args, 1, args.length);
}

switch(cmd) {
case "annotate":
Annotate.main(args);
break;
case "delete":
DeleteBank.main(args);
break;
case "help":
dumpHelp();
break;
case "info":
DumpBankList.main(args);
break;
case "install":
CmdLineInstaller.main(args);
break;
case "query":
CmdLineQuery.main(args);
break;
case "ui":
UiInstaller.main(args);
break;
default:
if (cmd.equalsIgnoreCase("help") ||
cmd.equalsIgnoreCase("-h") ||
cmd.equalsIgnoreCase("--help")) {
dumpHelp();
}
else {
// Get all classes from current package
// to locate ones being annotated as BdmTool classes
boolean cmdOk = false;
Set<Class<?>> clazz = findAllClassesUsingClassLoader(new BeeDeeMain().getClass().getPackage().getName());
for(Class<?> c : clazz) {
BdmTool bdmT = c.getAnnotation(BdmTool.class);
String bdmTCmd = bdmT != null ? bdmT.command() : "-" ;
if (bdmT != null && cmd.equalsIgnoreCase(bdmTCmd)) {
cmdOk = true;
try {
Method method = c.getDeclaredMethod("execute", String[].class);
Object bRet = method.invoke(c.newInstance(), new Object[] {args});
if ( ! bRet.toString().equalsIgnoreCase("true") ) {
System.exit(1);
}
} catch (Exception e) {
System.err.print(DBMSMessages.getString("Tool.Master.err2.cmd"));
System.err.println(cmd);
System.err.println(e.getMessage());
System.exit(1);
}
}
}
// unknown command
if (!cmdOk) {
System.err.print(DBMSMessages.getString("Tool.Master.err.cmd"));
System.err.print(": ");
System.err.println(cmd);
System.exit(1);
}
}
}
}
19 changes: 12 additions & 7 deletions src/bzh/plealog/dbmirror/main/CmdLineInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@
*
* @author Patrick G. Durand
*/
public class CmdLineInstaller {
@BdmTool(command="install", description="install bank(s)")
public class CmdLineInstaller implements BdmToolApi {

private File prepareListOfBanks(String bankListFormat){
File f=null;
FileOutputStream fos=null;

boolean bRet = false;
try {
f = File.createTempFile("bdm-list-of-banks", ".txt");
fos = new FileOutputStream(f);
DumpBankList dbl = new DumpBankList();
dbl.doJob(fos, "all", bankListFormat, "?");
bRet=dbl.doJob(fos, "all", bankListFormat, "?");
fos.flush();
fos.close();
if (f.length()==0){
Expand All @@ -90,7 +91,7 @@ private File prepareListOfBanks(String bankListFormat){
//for now, hide this msg, not really bad
}

return f;
return bRet ? f : null;
}
private void sendTerminationMail(PFTPLoaderDescriptor fDescriptor, String bankListFormat) {
PMailer mailer;
Expand Down Expand Up @@ -241,13 +242,14 @@ private boolean startApplication(
* Options are defined in CmdLineInstallerOptions utility class. Use -h or -help
* option to get software command-line description.
*/
public static void main(String[] args) {
@Override
public boolean execute(String[] args) {
// convert the array of strings into an appropriate object
CommandLine cmd = CmdLineInstallerOptions.handleArguments(args);

// nothing to do, exit!
if (cmd==null){
return;
return false;
}

// do we have a global descriptor name? (first and second cases described above)
Expand All @@ -274,7 +276,10 @@ public static void main(String[] args) {
//go, go, go...
CmdLineInstaller mirror = new CmdLineInstaller();
if (!mirror.startApplication(globalDesc, fDescCmd, bankListFormat, fof)) {
System.exit(1);
return false;
}
else {
return true;
}
}

Expand Down
14 changes: 5 additions & 9 deletions src/bzh/plealog/dbmirror/main/CmdLineQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
*
* @author Patrick G. Durand
*/
public class CmdLineQuery {
@BdmTool(command="query", description="query bank repository to fetch entry(ies) given ID(s)")
public class CmdLineQuery implements BdmToolApi {
private static final String DATABASE = "d";
private static final String SEQID = "i";
private static final String FORMAT = "f";
Expand All @@ -65,7 +66,7 @@ public class CmdLineQuery {
* Setup the valid command-line of the application.
*/
@SuppressWarnings("static-access")
private static Options getCmdLineOptions() {
private Options getCmdLineOptions() {
Options opts;

Option repo = OptionBuilder
Expand Down Expand Up @@ -101,7 +102,8 @@ private static Options getCmdLineOptions() {
return opts;
}

public static boolean doJob(String[] args) {
@Override
public boolean execute(String[] args) {
PQueryMirrorBase qm;
Hashtable<String, String> values;
CommandLine cmdLine;
Expand Down Expand Up @@ -144,10 +146,4 @@ public static boolean doJob(String[] args) {
return true;
}
}

public static void main(String[] args) {
if (!doJob(args)){
System.exit(1);
}
}
}
Loading

0 comments on commit b6dea5a

Please sign in to comment.