Source Code Cross Referenced for Connection.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » applet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.applet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.