Class Platform

  • Direct Known Subclasses:
    Platform.UnixPlatform, Platform.WindowsPlatform

    public abstract class Platform
    extends java.lang.Object
    Platform-dependent features required by the SAMP implementation.
    Since:
    14 Jul 2008
    Author:
    Mark Taylor
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Platform.StreamReader
      Thread which reads the contents of a stream into a string buffer.
      private static class  Platform.UnixPlatform
      Platform implementation for Un*x-like systems.
      private static class  Platform.WindowsPlatform
      Platform implementation for Microsoft Windows-like systems.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static Platform instance_  
      private static java.util.logging.Logger logger_  
      private java.lang.String name_  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Platform​(java.lang.String name)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      private static Platform createPlatform()
      Constructs a Platform for the current system.
      private static java.lang.String exec​(java.lang.String[] args)
      Attempts a with a given list of arguments.
      java.lang.String getEnv​(java.lang.String varname)
      Returns the value of an environment variable.
      protected abstract java.lang.String[] getGetenvArgs​(java.lang.String varname)
      Returns an array of words to pass to Runtime.exec(java.lang.String[]) in order to read an environment variable name.
      abstract java.io.File getHomeDirectory()
      Returns SAMP's definition of the "home" directory.
      static Platform getPlatform()
      Returns a Platform instance for the current system.
      protected abstract java.lang.String[] getPrivateReadArgs​(java.io.File file)
      Returns an array of words to pass to Runtime.exec(java.lang.String[]) in order to set permissions on a given file so that it cannot be read by anyone other than its owner.
      void setPrivateRead​(java.io.File file)
      Sets file permissions on a given file so that it cannot be read by anyone other than its owner.
      private static boolean setPrivateReadReflect​(java.io.File file)
      Attempt to use the File.setReadable() method to set permissions on a file so that it cannot be read by anyone other than its owner.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • instance_

        private static Platform instance_
      • name_

        private final java.lang.String name_
      • logger_

        private static final java.util.logging.Logger logger_
    • Constructor Detail

      • Platform

        protected Platform​(java.lang.String name)
        Constructor.
        Parameters:
        name - platform name
    • Method Detail

      • getHomeDirectory

        public abstract java.io.File getHomeDirectory()
        Returns SAMP's definition of the "home" directory.
        Returns:
        directory containing SAMP lockfile
      • getEnv

        public java.lang.String getEnv​(java.lang.String varname)
        Returns the value of an environment variable. If it can't be done, null is returned.
        Parameters:
        varname - name of environment variable
        Returns:
        value of environment variable
      • setPrivateRead

        public void setPrivateRead​(java.io.File file)
                            throws java.io.IOException
        Sets file permissions on a given file so that it cannot be read by anyone other than its owner.
        Parameters:
        file - file whose permissions are to be altered
        Throws:
        java.io.IOException - if permissions cannot be changed
      • getGetenvArgs

        protected abstract java.lang.String[] getGetenvArgs​(java.lang.String varname)
        Returns an array of words to pass to Runtime.exec(java.lang.String[]) in order to read an environment variable name. If null is returned, no way is known to do this with a system command.
        Parameters:
        varname - environment variable name to read
        Returns:
        exec args
      • getPrivateReadArgs

        protected abstract java.lang.String[] getPrivateReadArgs​(java.io.File file)
                                                          throws java.io.IOException
        Returns an array of words to pass to Runtime.exec(java.lang.String[]) in order to set permissions on a given file so that it cannot be read by anyone other than its owner. If null is returned, no way is known to do this with a system command.
        Parameters:
        file - file to alter
        Returns:
        exec args
        Throws:
        java.io.IOException
      • setPrivateReadReflect

        private static boolean setPrivateReadReflect​(java.io.File file)
                                              throws java.io.IOException
        Attempt to use the File.setReadable() method to set permissions on a file so that it cannot be read by anyone other than its owner.
        Parameters:
        file - file to alter
        Returns:
        true if the attempt succeeded, false if it failed because we are running the wrong version of java
        Throws:
        java.io.IOException - if there was some I/O failure
      • exec

        private static java.lang.String exec​(java.lang.String[] args)
                                      throws java.io.IOException
        Attempts a with a given list of arguments. The output from stdout is returned as a string; in the case of error an IOException is thrown with a message giving the output from stderr.

        Note: do not use this for cases in which the output from stdout or stderr might be more than a few characters - blocking or deadlock is possible (see Process).

        Parameters:
        args - array of words to pass to exec
        Returns:
        output from standard output
        Throws:
        java.io.IOException - with text from standard error if there is an error
      • getPlatform

        public static Platform getPlatform()
        Returns a Platform instance for the current system.
        Returns:
        platform instance
      • createPlatform

        private static Platform createPlatform()
        Constructs a Platform for the current system.
        Returns:
        new platform