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