| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package org.mozilla.javascript.tools.shell; |
| 16 |
|
| 17 |
import org.mozilla.javascript.Scriptable; |
| 18 |
import org.mozilla.javascript.ScriptRuntime; |
| 19 |
import org.mozilla.javascript.ScriptableObject; |
| 20 |
|
| 21 |
import java.util.Map; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class Environment extends ScriptableObject |
| 30 |
{ |
| 31 |
static final long serialVersionUID = -430727378460177065L; |
| 32 |
|
| 33 |
private Environment thePrototypeInstance = null; |
| 34 |
|
| 35 |
public static void defineClass(ScriptableObject scope) { |
| 36 |
try { |
| 37 |
ScriptableObject.defineClass(scope, Environment.class); |
| 38 |
} catch (Exception e) { |
| 39 |
throw new Error(e.getMessage()); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public String getClassName() { |
| 45 |
return "Environment"; |
| 46 |
} |
| 47 |
|
| 48 |
public Environment() { |
| 49 |
if (thePrototypeInstance == null) |
| 50 |
thePrototypeInstance = this; |
| 51 |
} |
| 52 |
|
| 53 |
public Environment(ScriptableObject scope) { |
| 54 |
setParentScope(scope); |
| 55 |
Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment"); |
| 56 |
if (ctor != null && ctor instanceof Scriptable) { |
| 57 |
Scriptable s = (Scriptable) ctor; |
| 58 |
setPrototype((Scriptable) s.get("prototype", s)); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public boolean has(String name, Scriptable start) { |
| 64 |
if (this == thePrototypeInstance) |
| 65 |
return super.has(name, start); |
| 66 |
|
| 67 |
return (System.getProperty(name) != null); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public Object get(String name, Scriptable start) { |
| 72 |
if (this == thePrototypeInstance) |
| 73 |
return super.get(name, start); |
| 74 |
|
| 75 |
String result = System.getProperty(name); |
| 76 |
if (result != null) |
| 77 |
return ScriptRuntime.toObject(getParentScope(), result); |
| 78 |
else |
| 79 |
return Scriptable.NOT_FOUND; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void put(String name, Scriptable start, Object value) { |
| 84 |
if (this == thePrototypeInstance) |
| 85 |
super.put(name, start, value); |
| 86 |
else |
| 87 |
System.getProperties().put(name, ScriptRuntime.toString(value)); |
| 88 |
} |
| 89 |
|
| 90 |
private Object[] collectIds() { |
| 91 |
Map<Object,Object> props = System.getProperties(); |
| 92 |
return props.keySet().toArray(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Object[] getIds() { |
| 97 |
if (this == thePrototypeInstance) |
| 98 |
return super.getIds(); |
| 99 |
return collectIds(); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public Object[] getAllIds() { |
| 104 |
if (this == thePrototypeInstance) |
| 105 |
return super.getAllIds(); |
| 106 |
return collectIds(); |
| 107 |
} |
| 108 |
} |