1
/*
2
 * $Id$
3
 *
4
 * Copyright (C) 2003-2009 JNode.org
5
 *
6
 * This library is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful, but 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this library; If not, write to the Free Software Foundation, Inc., 
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
 */
20
 
21
package org.jnode.vm;
22
23
import java.io.InputStream;
24
import java.io.PrintStream;
25
import java.util.Map;
26
import java.util.Properties;
27
28
/**
29
 * This is the implementation of the IOContext API that is be used when
30
 * 'proclet' mode is not enabled.  It also provides static methods for
31
 * getting and setting the 'global' versions of the Stream state,
32
 * the System properties and the System environment.  The 'global' state 
33
 * is used and (in the case of the properties and 'env' map, updated) in 
34
 * 'proclet' mode when the current thread is not part of a 'proclet'.
35
 * 
36
 * @author Levente S\u00e1ntha
37
 * @author crawley@jnode.org
38
 */
39
public class VmIOContext implements IOContext {
40
    // FIXME ... restrict visibility (if possible) and add Java security
41
    // access controls.
42
    
43
    private static InputStream globalInStream;
44
    private static PrintStream globalOutStream;
45
    private static PrintStream globalErrStream;
46
    private static Properties globalSysProps;
47
    private static Map<String, String> globalEnv;
48
49
    public void setSystemIn(InputStream in) {
50
        globalInStream = in;
51
        VmSystem.setStaticField(System.class, "in", in);
52
    }
53
54
    public void setSystemOut(PrintStream out) {
55
        globalOutStream = out;
56
        VmSystem.setStaticField(System.class, "out", out);
57
    }
58
59
    public void setSystemErr(PrintStream err) {
60
        globalErrStream = err;
61
        VmSystem.setStaticField(System.class, "err", err);
62
    }
63
64
    public void enterContext() {
65
        // No-op
66
    }
67
68
    public void exitContext() {
69
        // No-op
70
    }
71
72
    public PrintStream getRealSystemErr() {
73
        return System.err;
74
    }
75
76
    public InputStream getRealSystemIn() {
77
        return System.in;
78
    }
79
80
    public PrintStream getRealSystemOut() {
81
        return System.out;
82
    }
83
84
    public Map<String, String> getEnv() {
85
        return globalEnv;
86
    }
87
    
88
    public Properties getProperties() {
89
        return globalSysProps;
90
    }
91
92
    public void setEnv(Map<String, String> env) {
93
        globalEnv = env;
94
    }
95
96
    public void setProperties(Properties props) {
97
        globalSysProps = props;
98
    }
99
    
100
    /**
101
     * Set the 'global' view of {@link System#in}. 
102
     * @param in the new input stream.
103
     */
104
    public static void setGlobalInStream(InputStream in) {
105
        globalInStream = in;
106
    }
107
108
    /**
109
     * Set the 'global' view of {@link System#out}. 
110
     * @param out the new output stream.
111
     */
112
    public static void setGlobalOutStream(PrintStream out) {
113
        globalOutStream = out;
114
    }
115
116
    /**
117
     * Get the 'global' view of {@link System#out}. 
118
     * @return the 'global' output stream.
119
     */
120
    public static PrintStream getGlobalOutStream() {
121
        return globalOutStream;
122
    }
123
124
    /**
125
     * Set the 'global' view of {@link System#err}. 
126
     * @param err the new error stream.
127
     */
128
    public static void setGlobalErrStream(PrintStream err) {
129
        globalErrStream = err;
130
    }
131
132
    /**
133
     * Get the 'global' view of {@link System#err}. 
134
     * @return the 'global' error stream.
135
     */
136
    public static PrintStream getGlobalErrStream() {
137
        return globalErrStream;
138
    }
139
140
    /**
141
     * Get the 'global' view of {@link System#in}. 
142
     * @return the 'global' input stream.
143
     */
144
    public static InputStream getGlobalInStream() {
145
        return globalInStream;
146
    }
147
148
    /**
149
     * Set the 'global' view of the environment returned by {@link System#getenv()}. 
150
     * @param env the new 'global' environment.
151
     */
152
    public static void setGlobalEnv(Map<String, String> env) {
153
        globalEnv = env;
154
    }
155
156
    /**
157
     * Set the 'global' view of the Properties returned by {@link System#getProperties()}. 
158
     * @param props the new 'global' properties.
159
     */
160
    public static void setGlobalProperties(Properties props) {
161
        globalSysProps = props;
162
    }
163
164
    /**
165
     * Get the 'global' view of the environment returned by {@link System#getenv()}. 
166
     * @return the current 'global' environment.
167
     */
168
    public static Map<String, String> getGlobalEnv() {
169
        return globalEnv;
170
    }
171
    
172
    /**
173
     * Get the 'global' view of the Properties returned by {@link System#getProperties()}. 
174
     * @return the current 'global' Properties.
175
     */
176
    public static Properties getGlobalProperties() {
177
        return globalSysProps;
178
    }
179
}