KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > internal > runtime > GlobalVariables


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * either of the GNU General Public License Version 2 or later (the "GPL"),
22  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23  * in which case the provisions of the GPL or the LGPL are applicable instead
24  * of those above. If you wish to allow use of your version of this file only
25  * under the terms of either the GPL or the LGPL, and not to allow others to
26  * use your version of this file under the terms of the CPL, indicate your
27  * decision by deleting the provisions above and replace them with the notice
28  * and other provisions required by the GPL or the LGPL. If you do not delete
29  * the provisions above, a recipient may use your version of this file under
30  * the terms of any one of the CPL, the GPL or the LGPL.
31  ***** END LICENSE BLOCK *****/

32 package org.jruby.internal.runtime;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import org.jruby.Ruby;
39 import org.jruby.exceptions.RaiseException;
40 import org.jruby.runtime.IAccessor;
41 import org.jruby.runtime.builtin.IRubyObject;
42
43 /**
44  *
45  * @author jpetersen
46  */

47 public class GlobalVariables {
48     private Ruby runtime;
49     private Map JavaDoc globalVariables = new HashMap JavaDoc();
50
51     public GlobalVariables(Ruby runtime) {
52         this.runtime = runtime;
53     }
54
55     public void define(String JavaDoc name, IAccessor accessor) {
56         assert name != null;
57         assert accessor != null;
58         assert name.startsWith("$");
59
60         globalVariables.put(name, new GlobalVariable(accessor));
61     }
62     
63     public void defineReadonly(String JavaDoc name, IAccessor accessor) {
64         assert name != null;
65         assert accessor != null;
66         assert name.startsWith("$");
67
68         globalVariables.put(name, new GlobalVariable(new ReadonlyAccessor(name, accessor)));
69     }
70
71     public boolean isDefined(String JavaDoc name) {
72         assert name != null;
73         assert name.startsWith("$");
74         
75         GlobalVariable variable = (GlobalVariable)globalVariables.get(name);
76         return variable != null && !(variable.getAccessor() instanceof UndefinedAccessor);
77     }
78
79     /** Creates a new global variable which links to
80      * the oldName global variable.
81      *
82      * <b>WANRING</b> we are already using the 1.7.1 behaviour.
83      */

84     public void alias(String JavaDoc name, String JavaDoc oldName) {
85         assert name != null;
86         assert oldName != null;
87         assert name.startsWith("$");
88         assert oldName.startsWith("$");
89
90         if (runtime.getSafeLevel() >= 4) {
91             throw runtime.newSecurityError("Insecure: can't alias global variable");
92         }
93
94         GlobalVariable oldVariable = createIfNotDefined(oldName);
95         GlobalVariable variable = (GlobalVariable)globalVariables.get(name);
96
97         if (variable != null && oldVariable != variable && variable.isTracing()) {
98             throw new RaiseException(runtime, runtime.getClass("RuntimeError"), "can't alias in tracer", false);
99         }
100
101         globalVariables.put(name, oldVariable);
102     }
103
104     public IRubyObject get(String JavaDoc name) {
105         assert name != null;
106         assert name.startsWith("$");
107     
108         GlobalVariable variable = (GlobalVariable)globalVariables.get(name);
109         if (variable != null) {
110             return variable.getAccessor().getValue();
111         }
112         runtime.getWarnings().warning("global variable `" + name + "' not initialized");
113         return runtime.getNil();
114     }
115
116     public IRubyObject set(String JavaDoc name, IRubyObject value) {
117         assert name != null;
118         assert name.startsWith("$");
119
120         if (runtime.getSafeLevel() >= 4) {
121             throw runtime.newSecurityError("Insecure: can't change global variable value");
122         }
123
124         GlobalVariable variable = createIfNotDefined(name);
125         IRubyObject result = variable.getAccessor().setValue(value);
126         // variable.trace();
127
return result;
128     }
129
130     public Iterator JavaDoc getNames() {
131         return globalVariables.keySet().iterator();
132     }
133
134     private GlobalVariable createIfNotDefined(String JavaDoc name) {
135         GlobalVariable variable = (GlobalVariable)globalVariables.get(name);
136         if (variable == null) {
137             variable = GlobalVariable.newUndefined(runtime, name);
138             globalVariables.put(name, variable);
139         }
140         return variable;
141     }
142 }
143
Popular Tags