001 package org.maltparser.core.helper; 002 003 import java.io.File; 004 import java.io.IOException; 005 import java.net.URL; 006 import java.net.URLDecoder; 007 import java.util.jar.Attributes; 008 import java.util.jar.JarFile; 009 import java.util.jar.Manifest; 010 011 import org.maltparser.core.exception.MaltChainedException; 012 import org.maltparser.core.options.OptionManager; 013 /** 014 * 015 * 016 * @author Johan Hall 017 */ 018 public class SystemInfo { 019 private static SystemInfo uniqueInstance = new SystemInfo(); 020 private static String version; 021 private static String buildDate; 022 private static Attributes manifestAttributes; 023 private static File maltJarPath; 024 025 private SystemInfo() { 026 try { 027 getManifestInfo(); 028 029 String[] jarfiles = System.getProperty("java.class.path").split(File.pathSeparator); 030 for (int i = 0; i < jarfiles.length; i++) { 031 if (jarfiles[i].endsWith("malt.jar")) { 032 maltJarPath = new File(new File(jarfiles[i]).getAbsolutePath()); 033 } 034 } 035 } catch (MaltChainedException e) { 036 if (SystemLogger.logger().isDebugEnabled()) { 037 SystemLogger.logger().debug("",e); 038 } else { 039 SystemLogger.logger().error(e.getMessageChain()); 040 } 041 System.exit(1); 042 } 043 } 044 045 /** 046 * Returns a reference to the single instance. 047 */ 048 public static SystemInfo instance() { 049 return uniqueInstance; 050 } 051 052 /** 053 * Returns the application header 054 * 055 * @return the application header 056 */ 057 public static String header() { 058 StringBuilder sb = new StringBuilder(); 059 sb.append( 060 "-----------------------------------------------------------------------------\n"+ 061 " MaltParser "+version+" \n"+ 062 "-----------------------------------------------------------------------------\n"+ 063 " MALT (Models and Algorithms for Language Technology) Group \n"+ 064 " Vaxjo University and Uppsala University \n"+ 065 " Sweden \n"+ 066 "-----------------------------------------------------------------------------\n"); 067 return sb.toString(); 068 } 069 070 /** 071 * Returns a short version of the help 072 * 073 * @return a short version of the help 074 */ 075 public static String shortHelp() { 076 StringBuilder sb = new StringBuilder(); 077 sb.append("\n"+ 078 "Usage: \n"+ 079 " java -jar malt.jar -f <path to option file> <options>\n"+ 080 " java -jar malt.jar -h for more help and options\n\n"+ 081 OptionManager.instance().getOptionDescriptions().toStringOptionGroup("system")+ 082 "Documentation: docs/index.html\n"); 083 return sb.toString(); 084 } 085 086 /** 087 * Returns a set of attributes present in the jar manifest file 088 * 089 * @return a set of attributes present in the jar manifest file 090 */ 091 public static Attributes getManifestAttributes() { 092 return manifestAttributes; 093 } 094 095 /** 096 * Returns the version number as string 097 * 098 * @return the version number as string 099 */ 100 public static String getVersion() { 101 return version; 102 } 103 104 /** 105 * Returns the build date 106 * 107 * @return the build date 108 */ 109 public static String getBuildDate() { 110 return buildDate; 111 } 112 113 public static File getMaltJarPath() { 114 return maltJarPath; 115 } 116 117 /** 118 * Loads the manifest attributes from the manifest in the jar-file 119 * 120 * @throws MaltChainedException 121 */ 122 private void getManifestInfo() throws MaltChainedException { 123 try { 124 URL codeBase = SystemInfo.class.getProtectionDomain().getCodeSource().getLocation(); 125 if(codeBase != null && codeBase.getPath().endsWith(".jar")) { 126 JarFile jarfile = new JarFile(URLDecoder.decode(codeBase.getPath(),java.nio.charset.Charset.defaultCharset().name())); 127 Manifest manifest = jarfile.getManifest(); 128 Attributes manifestAttributes = manifest.getMainAttributes(); 129 version = manifestAttributes.getValue("Implementation-Version"); 130 buildDate = manifestAttributes.getValue("Build-Date"); 131 } 132 } catch(IOException e) { 133 version = ""; 134 buildDate = "Not available"; 135 e.printStackTrace(); 136 } 137 } 138 }