001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1998.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: *
026: * Alternatively, the contents of this file may be used under the terms of
027: * the GNU General Public License Version 2 or later (the "GPL"), in which
028: * case the provisions of the GPL are applicable instead of those above. If
029: * you wish to allow use of your version of this file only under the terms of
030: * the GPL and not to allow others to use your version of this file under the
031: * MPL, indicate your decision by deleting the provisions above and replacing
032: * them with the notice and other provisions required by the GPL. If you do
033: * not delete the provisions above, a recipient may use your version of this
034: * file under either the MPL or the GPL.
035: *
036: * ***** END LICENSE BLOCK ***** */
037:
038: /*
039: Environment.java
040:
041: Wraps java.lang.System properties.
042:
043: by Patrick C. Beard <beard@netscape.com>
044: */
045:
046: package org.mozilla.javascript.tools.shell;
047:
048: import org.mozilla.javascript.Scriptable;
049: import org.mozilla.javascript.ScriptRuntime;
050: import org.mozilla.javascript.ScriptableObject;
051:
052: import java.util.Vector;
053: import java.util.Enumeration;
054: import java.util.Properties;
055:
056: /**
057: * Environment, intended to be instantiated at global scope, provides
058: * a natural way to access System properties from JavaScript.
059: *
060: * @author Patrick C. Beard
061: */
062: public class Environment extends ScriptableObject {
063: static final long serialVersionUID = -430727378460177065L;
064:
065: private Environment thePrototypeInstance = null;
066:
067: public static void defineClass(ScriptableObject scope) {
068: try {
069: ScriptableObject.defineClass(scope, Environment.class);
070: } catch (Exception e) {
071: throw new Error(e.getMessage());
072: }
073: }
074:
075: public String getClassName() {
076: return "Environment";
077: }
078:
079: public Environment() {
080: if (thePrototypeInstance == null)
081: thePrototypeInstance = this ;
082: }
083:
084: public Environment(ScriptableObject scope) {
085: setParentScope(scope);
086: Object ctor = ScriptRuntime.getTopLevelProp(scope,
087: "Environment");
088: if (ctor != null && ctor instanceof Scriptable) {
089: Scriptable s = (Scriptable) ctor;
090: setPrototype((Scriptable) s.get("prototype", s));
091: }
092: }
093:
094: public boolean has(String name, Scriptable start) {
095: if (this == thePrototypeInstance)
096: return super .has(name, start);
097:
098: return (System.getProperty(name) != null);
099: }
100:
101: public Object get(String name, Scriptable start) {
102: if (this == thePrototypeInstance)
103: return super .get(name, start);
104:
105: String result = System.getProperty(name);
106: if (result != null)
107: return ScriptRuntime.toObject(getParentScope(), result);
108: else
109: return Scriptable.NOT_FOUND;
110: }
111:
112: public void put(String name, Scriptable start, Object value) {
113: if (this == thePrototypeInstance)
114: super .put(name, start, value);
115: else
116: System.getProperties().put(name,
117: ScriptRuntime.toString(value));
118: }
119:
120: private Object[] collectIds() {
121: Properties props = System.getProperties();
122: Enumeration names = props.propertyNames();
123: Vector keys = new Vector();
124: while (names.hasMoreElements())
125: keys.addElement(names.nextElement());
126: Object[] ids = new Object[keys.size()];
127: keys.copyInto(ids);
128: return ids;
129: }
130:
131: public Object[] getIds() {
132: if (this == thePrototypeInstance)
133: return super .getIds();
134: return collectIds();
135: }
136:
137: public Object[] getAllIds() {
138: if (this == thePrototypeInstance)
139: return super.getAllIds();
140: return collectIds();
141: }
142: }
|