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