From: tcofoegbu Date: Thu, 21 May 2015 10:37:29 +0000 (+0100) Subject: JAL-1740 implemented auto-update of BioJsMSA, introduced FTP client for downloading... X-Git-Tag: Release_2_10_0~657^2~1 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Ffeatures%2FJAL-1740_BioJsAutoUpdater;p=jalview.git JAL-1740 implemented auto-update of BioJsMSA, introduced FTP client for downloading remote BioJs Templates --- diff --git a/.classpath b/.classpath index b7c26a4..442795d 100644 --- a/.classpath +++ b/.classpath @@ -66,5 +66,6 @@ + diff --git a/THIRDPARTYLIBS b/THIRDPARTYLIBS index 824a604..bbef868 100644 --- a/THIRDPARTYLIBS +++ b/THIRDPARTYLIBS @@ -20,6 +20,7 @@ commons-codec-1.3.jar commons-discovery.jar commons-logging-1.1.1.jar commons-logging.jar +commons-net-3.3.jar httpclient-4.0.3.jar httpcore-4.0.1.jar httpmime-4.0.3.jar diff --git a/lib/commons-net-3.3.jar b/lib/commons-net-3.3.jar new file mode 100644 index 0000000..f4f19a9 Binary files /dev/null and b/lib/commons-net-3.3.jar differ diff --git a/resources/templates/BioJSTemplate.txt b/resources/biojs_templates/BioJsMSA_1.0.txt similarity index 100% rename from resources/templates/BioJSTemplate.txt rename to resources/biojs_templates/BioJsMSA_1.0.txt diff --git a/resources/biojs_templates/BioJsMSA_1.1.txt b/resources/biojs_templates/BioJsMSA_1.1.txt new file mode 100644 index 0000000..c47b059 --- /dev/null +++ b/resources/biojs_templates/BioJsMSA_1.1.txt @@ -0,0 +1,9048 @@ + +
BioJS viewer
+ + + + + + + + Jalview Logo + +
+
+ + + + +
+
+ +
press "Run with JS"
+ + + + + + + + \ No newline at end of file diff --git a/resources/biojs_templates/Latest_BioJsMSA_1.2.txt b/resources/biojs_templates/Latest_BioJsMSA_1.2.txt new file mode 100644 index 0000000..c47b059 --- /dev/null +++ b/resources/biojs_templates/Latest_BioJsMSA_1.2.txt @@ -0,0 +1,9048 @@ + +
BioJS viewer
+ + + + + + + + Jalview Logo + +
+
+ + + + +
+
+ +
press "Run with JS"
+ + + + + + + + \ No newline at end of file diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index 0fe1664..e184db7 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -20,6 +20,14 @@ */ package jalview.bin; +import jalview.gui.AlignFrame; +import jalview.gui.Desktop; +import jalview.io.BioJsHTMLOutput; +import jalview.io.HtmlSvgOutput; +import jalview.util.MessageManager; +import jalview.util.Platform; +import jalview.ws.jws2.Jws2Discoverer; + import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; @@ -45,13 +53,6 @@ import java.util.Vector; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; -import jalview.gui.AlignFrame; -import jalview.gui.Desktop; -import jalview.io.HtmlSvgOutput; -import jalview.util.MessageManager; -import jalview.util.Platform; -import jalview.ws.jws2.Jws2Discoverer; - /** * Main class for Jalview Application
*
@@ -93,23 +94,6 @@ public class Jalview System.out.println(System.getProperty("os.arch") + " " + System.getProperty("os.name") + " " + System.getProperty("os.version")); - // if (new Platform().isAMac()) - // { - // // System.setProperty("com.apple.mrj.application.apple.menu.about.name", - // // "Jalview"); - // // System.setProperty("apple.laf.useScreenMenuBar", "true"); - // try - // { - // UIManager.setLookAndFeel(ch.randelshofer.quaqua.QuaquaManager - // .getLookAndFeel()); - // System.out - // .println("--------------------------------------------> in here"); - // } catch (UnsupportedLookAndFeelException e) - // { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } - // } ArgsParser aparser = new ArgsParser(args); boolean headless = false; @@ -241,6 +225,9 @@ public class Jalview desktop.checkForNews(); } + BioJsHTMLOutput bjs = new BioJsHTMLOutput(null, null); + bjs.updateBioJS(); + String file = null, protocol = null, format = null, data = null; jalview.io.FileLoader fileLoader = new jalview.io.FileLoader(); Vector getFeatures = null; // vector of das source nicknames to fetch diff --git a/src/jalview/ftp/FtpClient.java b/src/jalview/ftp/FtpClient.java new file mode 100644 index 0000000..8c9b3e1 --- /dev/null +++ b/src/jalview/ftp/FtpClient.java @@ -0,0 +1,134 @@ +package jalview.ftp; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.SocketException; + +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPFile; + +public class FtpClient +{ + + public static boolean authenticateUser(FTPClient ftpClient, + String username, String password) + { + boolean loggedIn = false; + + try + { + loggedIn = ftpClient.login(username, password); + } catch (IOException e) + { + e.printStackTrace(); + } + disconectFTP(ftpClient); + + return loggedIn; + } + + public static FTPClient getFtpClient(String serverHost) + { + FTPClient ftpClient = new FTPClient(); + try + { + ftpClient.connect(serverHost); + } catch (SocketException e) + { + e.printStackTrace(); + } catch (IOException e) + { + e.printStackTrace(); + } + return ftpClient; + } + + public static void disconectFTP(FTPClient ftpClient) + { + try + { + if (ftpClient.isConnected()) + { + ftpClient.logout(); + ftpClient.disconnect(); + } + } catch (IOException e) + { + e.printStackTrace(); + } + } + + public static void listDirectory(FTPClient ftpClient, String parentDir, + String currentDir, int level) throws IOException + { + String dirToList = parentDir; + if (!currentDir.equals("")) + { + dirToList += "/" + currentDir; + } + FTPFile[] subFiles = ftpClient.listFiles(dirToList); + if (subFiles != null && subFiles.length > 0) + { + for (FTPFile aFile : subFiles) + { + String currentFileName = aFile.getName(); + if (currentFileName.equals(".") || currentFileName.equals("..")) + { + // skip parent directory and directory itself + continue; + } + for (int i = 0; i < level; i++) + { + System.out.print("\t"); + } + if (aFile.isDirectory()) + { + System.out.println("[" + currentFileName + "]"); + listDirectory(ftpClient, dirToList, currentFileName, level + 1); + } + else + { + System.out.println(currentFileName); + } + } + } + } + + public static boolean downloadFile(FTPClient client, String remoteFile, + String local) + { + boolean success = false; + File downloadFile = new File(local); + OutputStream outputStream = null; + try + { + outputStream = new BufferedOutputStream(new FileOutputStream( + downloadFile)); + + success = client.retrieveFile(remoteFile, outputStream); + } catch (IOException e) + { + e.printStackTrace(); + } finally + { + try + { + if (outputStream != null) + { + outputStream.close(); + } + } catch (IOException e) + { + e.printStackTrace(); + } + } + if (success) + { + System.out.println(remoteFile + " has been downloaded successfully."); + } + return success; + } +} diff --git a/src/jalview/io/BioJsHTMLOutput.java b/src/jalview/io/BioJsHTMLOutput.java index 669054d..9c15991 100644 --- a/src/jalview/io/BioJsHTMLOutput.java +++ b/src/jalview/io/BioJsHTMLOutput.java @@ -1,21 +1,49 @@ package jalview.io; import jalview.exceptions.NoFileSelectedException; +import jalview.ftp.FtpClient; import jalview.gui.AlignViewport; import jalview.gui.AlignmentPanel; import jalview.gui.FeatureRenderer; import jalview.util.MessageManager; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.net.URISyntaxException; import java.net.URL; +import java.util.Objects; +import java.util.TreeMap; + +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPFile; + public class BioJsHTMLOutput { private AlignViewport av; + private static File currentBJSTemplateFile; + + private static TreeMap bioJsMSAVersions; + + public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache + .getDefault("biojs_template_directory", "/biojs_templates/"); + + public static final String BJS_FTP_USER = jalview.bin.Cache.getDefault( + "biojs_ftp_user", "test"); + + public static final String BJS_FTP_PWD = jalview.bin.Cache.getDefault( + "biojs_ftp_pwd", "test"); + + public static final String BJS_FTP_PORT = jalview.bin.Cache.getDefault( + "biojs_ftp_port", "22"); + + public static final String BJS_FTP_SERVER = jalview.bin.Cache.getDefault( + "biojs_ftp_server", "localhost"); public BioJsHTMLOutput(AlignmentPanel ap, FeatureRenderer fr1) @@ -36,7 +64,7 @@ public class BioJsHTMLOutput { String outputFile = getOutputFile(); String jalviewAlignmentJson = JSONFile.getJSONData(av); - String bioJSTemplateString = getBioJsTemplateAsString(this); + String bioJSTemplateString = getBioJsTemplateAsString(); String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString .replaceAll( "#sequenceData#", jalviewAlignmentJson) @@ -87,14 +115,16 @@ public class BioJsHTMLOutput } - public static String getBioJsTemplateAsString(Object currentObj) + public static String getBioJsTemplateAsString() throws IOException { InputStreamReader isReader = null; BufferedReader buffReader = null; StringBuilder sb = new StringBuilder(); - URL url = currentObj.getClass().getResource( - "/templates/BioJSTemplate.txt"); + Objects.requireNonNull(getCurrentBJSTemplateFile(), + "BioJsTemplate File not initialized!"); + @SuppressWarnings("deprecation") + URL url = getCurrentBJSTemplateFile().toURL(); if (url != null) { try @@ -126,4 +156,107 @@ public class BioJsHTMLOutput } return sb.toString(); } + + public TreeMap updateBioJSVersionsInfo(String dirName) + throws URISyntaxException + { + URL url = getClass().getResource(dirName); + File directory = new File(url.toURI()); + Objects.requireNonNull(dirName, "dirName MUST not be null!"); + Objects.requireNonNull(directory, "directory MUST not be null!"); + TreeMap versionFileMap = new TreeMap(); + + for (File file : directory.listFiles()) + { + if (file.isFile()) + { + String fileName = file.getName().substring(0, + file.getName().lastIndexOf(".")); + String fileMeta[] = fileName.split("_"); + if (fileMeta.length > 2) + { + setCurrentBJSTemplateFile(file); + versionFileMap.put(fileMeta[2], file); + } + else if (fileMeta.length > 1) + { + versionFileMap.put(fileMeta[1], file); + } + } + } + if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0) + { + setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue()); + } + return versionFileMap; + } + + public void updateBioJS() + { + TreeMap versionLists = null; + try + { + // downlaodNewBioJsTemplates(BJS_TEMPLATES_LOCAL_DIRECTORY); + versionLists = updateBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY); + setBioJsMSAVersions(versionLists); + } catch (URISyntaxException e) + { + e.printStackTrace(); + } + } + + public void downlaodNewBioJsTemplates(String localDirectory) + { + FTPClient client = FtpClient.getFtpClient(BJS_FTP_SERVER); + if (FtpClient.authenticateUser(client, BJS_FTP_USER, BJS_FTP_PWD)) + { + client.enterLocalPassiveMode(); + try + { + client.setFileType(FTP.BINARY_FILE_TYPE); + for (FTPFile fFile : client.listFiles()) + { + String localFileName = BJS_TEMPLATES_LOCAL_DIRECTORY + + fFile.getName(); + String remoteFileName = fFile.getName(); + FtpClient.downloadFile(client, remoteFileName, localFileName); + } + } catch (IOException e) + { + e.printStackTrace(); + } + } + } + + // public static void main(String[] args) throws IOException + // { + // Document doc = Jsoup.connect("http://howto.unixdev.net").get(); + // for (Element file : doc.select("td.right td a")) + // { + // System.out.println(file.attr("href")); + // } + // } + + public static File getCurrentBJSTemplateFile() + { + return currentBJSTemplateFile; + } + + public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile) + { + BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile; + } + + public static TreeMap getBioJsMSAVersions() + { + return bioJsMSAVersions; + } + + public static void setBioJsMSAVersions( + TreeMap bioJsMSAVersions) + { + BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions; + } + + } diff --git a/test/jalview/io/BioJsHTMLOutputTest.java b/test/jalview/io/BioJsHTMLOutputTest.java index 8d9de77..86b5470 100644 --- a/test/jalview/io/BioJsHTMLOutputTest.java +++ b/test/jalview/io/BioJsHTMLOutputTest.java @@ -1,6 +1,9 @@ package jalview.io; +import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; +import java.util.TreeMap; import org.junit.Assert; import org.junit.Test; @@ -16,8 +19,8 @@ public class BioJsHTMLOutputTest String bjsTemplate = null; try { - bjsTemplate = BioJsHTMLOutput - .getBioJsTemplateAsString(bioJsHtmlOutput); + bioJsHtmlOutput.updateBioJS(); + bjsTemplate = BioJsHTMLOutput.getBioJsTemplateAsString(); // System.out.println(bjsTemplate); } catch (IOException e) { @@ -25,4 +28,50 @@ public class BioJsHTMLOutputTest } Assert.assertNotNull(bjsTemplate); } + + @Test(expected = NullPointerException.class) + public void expectedNullPointerException() + { + BioJsHTMLOutput bjs = new BioJsHTMLOutput(null, null); + try + { + bjs.updateBioJSVersionsInfo(null); + } catch (URISyntaxException e) + { + Assert.fail("Expception occured while testing!"); + e.printStackTrace(); + } + } + + @Test + public void getBioJsMSAVersions() + { + BioJsHTMLOutput bjs = new BioJsHTMLOutput(null, null); + TreeMap versions = null; + try + { + versions = bjs + .updateBioJSVersionsInfo(BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY); + } catch (URISyntaxException e) + { + Assert.fail("Expception occured while testing!"); + e.printStackTrace(); + } + Assert.assertNotNull("No versions found", versions); + Assert.assertTrue("One or more Templates required", versions.size() > 0); + System.out + .println("Number of discovered versions : " + + versions.size()); + for (String v : versions.keySet()) + { + System.out.println("version : " + v); + System.out.println("File : " + versions.get(v)); + } + + System.out.println("\nCurrent latest version : " + + BioJsHTMLOutput.getCurrentBJSTemplateFile()); + Assert.assertNotNull("Latest BioJsMSA version NOT found!", + BioJsHTMLOutput.getCurrentBJSTemplateFile()); + + } }