001: /*
002: * @(#)AppletProps.java 1.30 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.applet;
029:
030: import java.awt.*;
031: import java.io.*;
032: import java.util.Properties;
033: import sun.net.www.http.HttpClient;
034: import sun.net.ftp.FtpClient;
035: import java.security.AccessController;
036: import java.security.PrivilegedAction;
037: import java.security.PrivilegedExceptionAction;
038: import java.security.PrivilegedActionException;
039: import sun.security.action.*;
040:
041: class AppletProps extends Frame {
042: TextField proxyHost;
043: TextField proxyPort;
044: Choice accessMode;
045:
046: AppletProps() {
047: setTitle(amh.getMessage("title"));
048: Panel p = new Panel();
049: p.setLayout(new GridLayout(0, 2));
050: p.add(new Label(amh.getMessage("label.http.server",
051: "Http proxy server:")));
052: p.add(proxyHost = new TextField());
053: p.add(new Label(amh.getMessage("label.http.proxy")));
054: p.add(proxyPort = new TextField());
055: p.add(new Label(amh.getMessage("label.class")));
056: p.add(accessMode = new Choice());
057: accessMode.addItem(amh
058: .getMessage("choice.class.item.restricted"));
059: accessMode.addItem(amh
060: .getMessage("choice.class.item.unrestricted"));
061: add("Center", p);
062: p = new Panel();
063: p.add(new Button(amh.getMessage("button.apply")));
064: p.add(new Button(amh.getMessage("button.reset")));
065: p.add(new Button(amh.getMessage("button.cancel")));
066: add("South", p);
067: move(200, 150);
068: pack();
069: reset();
070: }
071:
072: void reset() {
073: AppletSecurity security = (AppletSecurity) System
074: .getSecurityManager();
075: if (security != null)
076: security.reset();
077: String proxyhost = (String) AccessController
078: .doPrivileged(new GetPropertyAction("http.proxyHost"));
079: String proxyport = (String) AccessController
080: .doPrivileged(new GetPropertyAction("http.proxyPort"));
081: Boolean tmp = (Boolean) AccessController
082: .doPrivileged(new GetBooleanAction(
083: "package.restrict.access.sun"));
084: boolean packageRestrict = tmp.booleanValue();
085: if (packageRestrict) {
086: accessMode.select(amh
087: .getMessage("choice.class.item.restricted"));
088: } else {
089: accessMode.select(amh
090: .getMessage("choice.class.item.unrestricted"));
091: }
092: if (proxyhost != null) {
093: proxyHost.setText(proxyhost);
094: proxyPort.setText(proxyport);
095: HttpClient.proxyPort = Integer.valueOf(proxyport)
096: .intValue();
097: } else {
098: proxyHost.setText("");
099: proxyPort.setText("");
100: }
101: }
102:
103: void apply() {
104: String proxyHostValue = proxyHost.getText().trim();
105: String proxyPortValue = proxyPort.getText().trim();
106: // Get properties
107: final Properties props = (Properties) AccessController
108: .doPrivileged(new PrivilegedAction() {
109: public Object run() {
110: return System.getProperties();
111: }
112: });
113: if (proxyHostValue.length() != 0) {
114: /* 4066402 */
115:
116: /* Check for parsable value in proxy port number field before */
117:
118: /* applying. Display warning to user until parsable value is */
119:
120: /* entered. */
121: int proxyPortNumber = 0;
122: try {
123: proxyPortNumber = Integer.parseInt(proxyPortValue);
124: } catch (NumberFormatException e) {
125: }
126: if (proxyPortNumber <= 0) {
127: proxyPort.selectAll();
128: proxyPort.requestFocus();
129: (new AppletPropsErrorDialog(this , amh
130: .getMessage("title.invalidproxy"), amh
131: .getMessage("label.invalidproxy"), amh
132: .getMessage("button.ok"))).show();
133: return;
134: }
135: /* end 4066402 */
136:
137: props.put("http.proxyHost", proxyHostValue);
138: props.put("http.proxyPort", proxyPortValue);
139: } else {
140: props.put("http.proxyHost", "");
141: }
142: if (amh.getMessage("choice.class.item.restricted").equals(
143: accessMode.getSelectedItem())) {
144: props.put("package.restrict.access.sun", "true");
145: } else {
146: props.put("package.restrict.access.sun", "false");
147: }
148: // Save properties
149: try {
150: reset();
151: AccessController
152: .doPrivileged(new PrivilegedExceptionAction() {
153: public Object run() throws IOException {
154: File dotAV = Main.theUserPropertiesFile;
155: FileOutputStream out = new FileOutputStream(
156: dotAV);
157: Properties avProps = new Properties();
158: for (int i = 0; i < Main.avDefaultUserProps.length; i++) {
159: String avKey = Main.avDefaultUserProps[i][0];
160: avProps.setProperty(avKey, props
161: .getProperty(avKey));
162: }
163: avProps.store(out, amh
164: .getMessage("prop.store"));
165: out.close();
166: return null;
167: }
168: });
169: hide();
170: } catch (java.security.PrivilegedActionException e) {
171: System.out.println(amh.getMessage("apply.exception", e
172: .getException()));
173: // what's the general feeling on stack traces to System.out?
174: e.printStackTrace();
175: reset();
176: }
177: }
178:
179: public boolean action(Event evt, Object obj) {
180: if (amh.getMessage("button.apply").equals(obj)) {
181: apply();
182: return true;
183: }
184: if (amh.getMessage("button.reset").equals(obj)) {
185: reset();
186: return true;
187: }
188: if (amh.getMessage("button.cancel").equals(obj)) {
189: reset();
190: hide();
191: return true;
192: }
193: return false;
194: }
195:
196: private static AppletMessageHandler amh = new AppletMessageHandler(
197: "appletprops");
198: }
199:
200: /* 4066432 */
201:
202: /* Dialog class to display property-related errors to user */
203:
204: class AppletPropsErrorDialog extends Dialog {
205: public AppletPropsErrorDialog(Frame parent, String title,
206: String message, String buttonText) {
207: super (parent, title, true);
208: Panel p = new Panel();
209: add("Center", new Label(message));
210: p.add(new Button(buttonText));
211: add("South", p);
212: pack();
213: Dimension dDim = getSize();
214: Rectangle fRect = parent.getBounds();
215: move(fRect.x + ((fRect.width - dDim.width) / 2), fRect.y
216: + ((fRect.height - dDim.height) / 2));
217: }
218:
219: public boolean action(Event event, Object object) {
220: hide();
221: dispose();
222: return true;
223: }
224: }
225: /* end 4066432 */
|