001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision: 1.2 $
020: */package org.apache.harmony.applet;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.PrintWriter;
025: import java.net.Socket;
026: import java.net.URL;
027: import java.util.HashMap;
028: import java.util.Map;
029: import org.apache.harmony.awt.ContextStorage;
030:
031: /**
032: * Connection between host application and applet infrastructure
033: */
034: public class Connection implements Callback {
035:
036: private final Socket socket;
037:
038: final LineReader reader;
039: // final BufferedReader reader;
040: final PrintWriter writer;
041:
042: final Factory factory;
043:
044: public static void main(String[] args) {
045: ContextStorage.activateMultiContextMode();
046:
047: int port = 0;
048:
049: try {
050: port = Integer.parseInt(args[0]);
051: } catch (Exception e) {
052: throw new IllegalArgumentException("Invalid port");
053: }
054:
055: Connection c = new Connection(port);
056: c.listen();
057: }
058:
059: Connection(int port) {
060: try {
061: socket = new Socket("localhost", port);
062:
063: reader = new LineReader(socket.getInputStream());
064: // reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
065: writer = new PrintWriter(socket.getOutputStream());
066:
067: factory = new Factory(this );
068:
069: } catch (Exception e) {
070: throw new RuntimeException(e);
071: }
072: }
073:
074: void listen() {
075:
076: try {
077: for (String cmd = reader.readLine(); cmd != null; cmd = reader
078: .readLine()) {
079:
080: if (cmd.equals("exit")) {
081: exit();
082: return;
083: }
084: if (cmd.equals("dump")) {
085: dump();
086: continue;
087: }
088: String args[] = cmd.split(" ");
089:
090: if (args[0].equals("create")) {
091: create(args);
092: continue;
093: }
094: if (args[0].equals("unload")) {
095: unload(args);
096: continue;
097: }
098: if (args[0].equals("start")) {
099: start(args);
100: continue;
101: }
102: if (args[0].equals("stop")) {
103: stop(args);
104: continue;
105: }
106: }
107: } catch (Exception e) {
108: throw new RuntimeException(e);
109: }
110: }
111:
112: private void dump() {
113: try {
114: System.err.println(" -----------THREADS--------------");
115: Thread cur = Thread.currentThread();
116: dump(cur.getThreadGroup(), "");
117: System.err.println(" -----------CONTEXT--------------");
118: factory.dump();
119: System.err.println(" -----------END DUMP--------------");
120: } catch (Exception e) {
121: System.err.println(e);
122: }
123: }
124:
125: private void dump(ThreadGroup group, String prefix) {
126: try {
127: System.err.println(prefix + group);
128: } catch (Exception e) {
129: System.err.println(e);
130: }
131:
132: try {
133: Thread threads[] = new Thread[group.activeCount()];
134: for (int cnt = group.enumerate(threads), i = 0; i < cnt; i++) {
135: if (threads[i].getThreadGroup() == group) {
136: System.err.println(prefix + "| " + threads[i]);
137: }
138: }
139: } catch (Exception e) {
140: System.err.println(e);
141: }
142:
143: try {
144: ThreadGroup groups[] = new ThreadGroup[group
145: .activeGroupCount()];
146: for (int cnt = group.enumerate(groups), i = 0; i < cnt; i++) {
147: dump(groups[i], prefix + "> ");
148: }
149: } catch (Exception e) {
150: System.err.println(e);
151: }
152: }
153:
154: private void start(String args[]) {
155:
156: factory.start(Integer.parseInt(args[1]));
157: }
158:
159: private void stop(String args[]) {
160:
161: factory.stop(Integer.parseInt(args[1]));
162: }
163:
164: private void unload(String args[]) {
165:
166: factory.dispose(Integer.parseInt(args[1]));
167: }
168:
169: /**
170: * command synopsis:<br>
171: * CREATE id parentWindowId className docBase docId codeBase<br>
172: * NAME name_in_document<br>
173: * PARAM name value<br>
174: * END<br>
175: * @param args - CREATE command split into tokens
176: */
177: private void create(String args[]) {
178:
179: int id;
180: long parentWindowId;
181: String className;
182: URL documentBase;
183: int docId;
184: URL codeBase;
185: String name = null;
186: Map<String, String> parameters = new HashMap<String, String>();
187:
188: try {
189: id = Integer.parseInt(args[1]);
190: parentWindowId = Long.parseLong(args[2]);
191: className = args[3];
192: documentBase = new URL(args[4]);
193: docId = Integer.parseInt(args[5]);
194: codeBase = new URL(args[6]);
195: } catch (Exception e) {
196: throw new RuntimeException(e);
197: }
198:
199: try {
200: for (String line = reader.readLine(); line != null; line = reader
201: .readLine()) {
202: if (line.equals("exit")) {
203: exit();
204: }
205: if (line.equals("end")) {
206: break;
207: }
208: String parts[] = line.split(" ");
209: if (parts[0].equals("param")) {
210: parameters.put(parts[1], parts[2]);
211: } else if (parts[0].equals("name")) {
212: name = parts[1];
213: }
214: }
215: } catch (IOException e) {
216: throw new RuntimeException(e);
217: }
218:
219: Parameters params = new Parameters(id, parentWindowId,
220: documentBase, docId, codeBase, className, parameters,
221: name, null);
222:
223: factory.createAndRun(params);
224: }
225:
226: void exit() {
227: try {
228: sendCommand("exit");
229: writer.close();
230: socket.close();
231: } catch (IOException e) {
232: e.printStackTrace();
233: }
234: System.exit(0);
235: }
236:
237: private void sendCommand(String cmd) {
238: writer.println(cmd);
239: writer.flush();
240: }
241:
242: public void showDocument(int documentId, URL url, String target) {
243: sendCommand("show " + documentId + " " + url + " " + target);
244: }
245:
246: public void showStatus(int documentId, String status) {
247: sendCommand("status " + documentId + " " + status);
248: }
249:
250: public void appletResize(int appletId, int width, int height) {
251: sendCommand("resize " + appletId + " " + width + " " + height);
252: }
253:
254: static class LineReader {
255:
256: private final StringBuffer buffer = new StringBuffer();
257: private final InputStream in;
258: private boolean eof = false;
259:
260: LineReader(InputStream is) {
261: in = is;
262: }
263:
264: public String readLine() throws IOException {
265: if (eof) {
266: return null;
267: }
268:
269: int pos = buffer.indexOf("\n");
270: if (pos >= 0) {
271: return getLine(pos);
272: }
273:
274: final int BUF_SIZE = 1024;
275: byte[] buf = new byte[BUF_SIZE];
276:
277: int count = 0;
278: while ((count = in.read(buf, 0, BUF_SIZE)) > 0) {
279: buffer.append(new String(buf, 0, count));
280: pos = buffer.indexOf("\n");
281: if (pos >= 0) {
282: return getLine(pos);
283: }
284: }
285:
286: eof = true;
287: return getRemainder();
288: }
289:
290: public void close() throws IOException {
291: in.close();
292: }
293:
294: private String getLine(int endPos) {
295: if (endPos > 0) {
296: String result = buffer.substring(0, endPos);
297: buffer.delete(0, endPos + 1);
298: return result;
299: }
300: if (endPos == 0) {
301: buffer.delete(0, 1);
302: }
303: return new String();
304: }
305:
306: private String getRemainder() {
307: String result = buffer.toString();
308: buffer.delete(0, buffer.length());
309: return (result.length() > 0) ? result : null;
310: }
311: }
312:
313: }
|