/*******************************************************************************
*                                                                              *
*  File:        System.java                             Revision:  1.0         *
*                                                                              *
*  Purpose:     provides system-specific information for "Rhino" scripts       *
*                                                                              *
*  Creation:    27.10.2001                     Last Modification:  27.10.2001  *
*                                                                              *
*  Platform:    IBM-compatible PC running Windows 98SE                         *
*                                                                              *
*  Environment: Java 1.3, Rhino 1.5                                            *
*                                                                              *
*  Author:      Andreas Rozek           Phone:  ++49 (711) 6770682             *
*               Kirschblütenweg 15      Fax:    -                              *
*             D-70569 Stuttgart         EMail:  Andreas.Rozek@T-Online.De      *
*               Germany                                                        *
*                                                                              *
*  URL:         http://www.Andreas-Rozek.de/                                   *
*                                                                              *
*  Copyright:   this software is published under the  "GNU Lesser General Pub- *
*               lic  License"  (see  "http://www.fsf.org/copyleft/lesser.html" *
*               for additional information)                                    *
*                                                                              *
*  Comments:    unfortunately, the "System" object has to be implemented as a  *
*               Rhino "Host Object" (although an user should never instantiate *
*               it). Reason: "LiveConnect" seems to ignore certain properties  *
*               of a simple "Java" object - properties which are essential for *
*               the object's functionality                                     *
*                                                                              *
*               This class identifies itself as "_System" and will be instan-  *
*               tiated exactly once by an author's Rhino shell - please, don't *
*               instantiate it yourself...                                     *
*                                                                              *
*  Known Bugs:  the "jsSet..." methods would do better with                    *
*                - some argument checking and                                  *
*                - better exception handling                                   *
*               but this may be added later                                    *
*                                                                              *
*******************************************************************************/

package sunda.rhino;

import org.mozilla.javascript.*;

public class System extends ScriptableObject {

/*******************************************************************************
*                                                                              *
*                                 Constructor                                  *
*                                                                              *
*******************************************************************************/

  public System () {    // is used (by Rhino) to create the "System" "prototype"
    super();                                              // just to be complete
  };

/*******************************************************************************
*                                                                              *
*                         Non-Public Instance Methods                          *
*                                                                              *
*******************************************************************************/

/*******************************************************************************
*                                                                              *
* getSystemProperty                      yields a given (Java) system property *
*                                                                              *
*******************************************************************************/

  Object getSystemProperty (String PropertyName) {
    try {
      return java.lang.System.getProperty(PropertyName);
    } catch (SecurityException Signal) {
      return Context.getUndefinedValue();
    };
  };

/*******************************************************************************
*                                                                              *
* setSystemProperty                     updates a given (Java) system property *
*                                                                              *
*******************************************************************************/

  Object setSystemProperty (String PropertyName, String Value) {
    try {
      return java.lang.System.setProperty(PropertyName,Value);
    } catch (Exception Signal) {
      return Context.getUndefinedValue();
    };
  };

/*******************************************************************************
*                                                                              *
*                             Public Class Methods                             *
*                                                                              *
*******************************************************************************/

/*******************************************************************************
*                                                                              *
* getClassName                      yields the JavaScript name of this "class" *
*                                                                              *
*******************************************************************************/

  public String getClassName () {
    return "_System";                   // this name is "internal" by convention
  };

/*******************************************************************************
*                                                                              *
* jsConstructor                  the JavaScript "constructor" for this "class" *
*                                                                              *
*******************************************************************************/

  public void jsConstructor () {
    /* nop */   // this object should only be instantiated by the runtime system
  };

/*******************************************************************************
*                                                                              *
* jsGet_OSArchitecture, ~Name, ~Version    yield infos about the underlying OS *
*                                                                              *
*******************************************************************************/

  public Object jsGet_OSArchitecture () {
    return getSystemProperty("os.arch");
  };

  public Object jsGet_OSName () {
    return getSystemProperty("os.name");
  };

  public Object jsGet_OSVersion () {
    return getSystemProperty("os.version");
  };

/*******************************************************************************
*                                                                              *
* jsGet_FileSeparator       yields the separator character used for path names *
*                                                                              *
*******************************************************************************/

  public Object jsGet_FileSeparator () {
    return getSystemProperty("file.separator");
  };

/*******************************************************************************
*                                                                              *
* jsGet_ImplementationVersion   yields the actual Rhino implementation version *
*                                                                              *
*******************************************************************************/

  public String jsGet_ImplementationVersion () {
    return Context.getCurrentContext().getImplementationVersion();
  };

/*******************************************************************************
*                                                                              *
* jsGet_LanguageVersion       yields the actually supported JavaScript version *
*                                                                              *
*******************************************************************************/

  public int jsGet_LanguageVersion () {
    return Context.getCurrentContext().getLanguageVersion();
  };

/*******************************************************************************
*                                                                              *
* jsGet_LineSeparator       yields the line-end character(s) for the actual OS *
*                                                                              *
*******************************************************************************/

  public Object jsGet_LineSeparator () {
    return getSystemProperty("line.separator");
  };

/*******************************************************************************
*                                                                              *
* jsGet_PathSeparator     yields the separator character used for search paths *
*                                                                              *
*******************************************************************************/

  public Object jsGet_PathSeparator () {
    return getSystemProperty("path.separator");
  };

/*******************************************************************************
*                                                                              *
* jsGet_UserDir, ~Home, ~Name             yield some user-specific information *
*                                                                              *
*******************************************************************************/

  public Object jsGet_UserDir () {
    return getSystemProperty("user.dir");
  };

  public Object jsGet_UserHome () {
    return getSystemProperty("user.home");
  };

  public Object jsGet_UserName () {
    return getSystemProperty("user.name");
  };

/*******************************************************************************
*                                                                              *
* jsSet_UserDir, ~Home, ~Name            update some user-specific information *
*                                                                              *
*******************************************************************************/

  public Object jsSet_UserDir (String Value) {
    return setSystemProperty("user.dir", Value);
  };

  public Object jsSet_UserHome (String Value) {
    return setSystemProperty("user.home", Value);
  };

  public Object jsSet_UserName (String Value) {
    return setSystemProperty("user.name", Value);
  };
};

