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