Source Code Cross Referenced for EmulationClient.java in  » 6.0-JDK-Modules » j2me » com » sun » midp » jsr82emul » 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 » 6.0 JDK Modules » j2me » com.sun.midp.jsr82emul 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *   
003:         *
004:         * Copyright  1990-2007 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:        package com.sun.midp.jsr82emul;
027:
028:        import java.io.IOException;
029:        import java.io.InputStream;
030:        import java.io.OutputStream;
031:        import javax.microedition.io.SocketConnection;
032:        import com.sun.midp.main.Configuration;
033:        import com.sun.midp.security.SecurityToken;
034:        import com.sun.midp.security.ImplicitlyTrustedClass;
035:        import com.sun.midp.jsr082.SecurityInitializer;
036:
037:        /**
038:         * Represents a client for JSR 82 emulation environment. 
039:         * It is not a part of JSR 82 implementation and is only used within 
040:         * JSR 82 emulation mode. The emulation mode allows running tests without
041:         * real native Bluetooth libraries or hardware.
042:         * 
043:         * In the emulation mode the client runs under J2ME VM control and connects
044:         * thru a socket to a server which runs somewhere on the Internet under J2SE
045:         * VM control.
046:         *
047:         * A client represents a Bluetooth local or remote device, connection 
048:         * or anything that requires Bluetooth ether communication. A server 
049:         * in turn represents Bluetooth ether.
050:         */
051:        public class EmulationClient {
052:
053:            /**
054:             * Inner class to request security token from SecurityInitializer.
055:             * SecurityInitializer should be able to check this inner class name.
056:             */
057:            static private class SecurityTrusted implements 
058:                    ImplicitlyTrustedClass {
059:            };
060:
061:            /** Internal security token that grants access to restricted API. */
062:            protected static SecurityToken internalSecurityToken = SecurityInitializer
063:                    .requestToken(new SecurityTrusted());
064:
065:            /** Keeps default port number for server connections. */
066:            private final static int EMUL_PORT = Configuration.getIntProperty(
067:                    "com.sun.midp.jsr82emul.serverPort", 1234);
068:
069:            /** IP address of EmulationServer. */
070:            private static String serverIP;
071:
072:            /** 
073:             * Keeps messenger that proveds utilities for communicating with server.
074:             */
075:            protected Messenger messenger = new Messenger();
076:
077:            /** Keeps socket connection to the server. */
078:            protected SocketConnection connection = null;
079:            /** Keeps input stream from server. */
080:            protected InputStream fromServer = null;
081:            /** Keeps output stream to server. */
082:            protected OutputStream toServer = null;
083:
084:            /**
085:             * Creates an instance.
086:             */
087:            protected EmulationClient() {
088:            }
089:
090:            /** 
091:             * Retrieves port of emulation server.
092:             * @return EmulationServer IP address from JSR82_EMUL_IP environment
093:             *        variable, <code>null</code> if the variable is not defined.
094:             */
095:            private static native String getServerIP();
096:
097:            /**
098:             * Retrieves local IP address.
099:             * @return <code>String</code> object containing local IP address, 
100:             *         <code>null</code> if retrieving failed.
101:             */
102:            static native String getLocalIP();
103:
104:            /**
105:             * Retrieves emulation server IP address, launches server if needed.
106:             * @return emulation server IP address.
107:             */
108:            private static synchronized String getServer() {
109:                if (serverIP != null) {
110:                    return serverIP;
111:                }
112:
113:                serverIP = getServerIP();
114:
115:                // Local IP of emulation server means that it is possibly
116:                // expected that current application will launch the server.
117:                if (serverIP == null || serverIP.length() == 0
118:                        || serverIP.equals("127.0.0.1")) {
119:                    EmulationServer.launch();
120:                    serverIP = "localhost";
121:                } else if (serverIP.equals(getLocalIP())) {
122:                    EmulationServer.launch();
123:                }
124:
125:                return serverIP;
126:            }
127:
128:            /**
129:             * Opens socket connection to current host at current port.
130:             *
131:             * @exception IOException if there is an open connection or 
132:             * en error ocurred while trying to connect.
133:             */
134:            protected void connect() throws IOException {
135:                if (connection != null) {
136:                    throw new IOException("connection is already open");
137:                }
138:
139:                connection = (SocketConnection) new com.sun.midp.io.j2me.socket.Protocol()
140:                        .openPrim(internalSecurityToken, "//" + getServer()
141:                                + ":" + EMUL_PORT);
142:
143:                fromServer = connection.openDataInputStream();
144:                toServer = connection.openDataOutputStream();
145:            }
146:
147:            /**
148:             * Closes current socket connection.
149:             *
150:             * @exception IOException if an error occured or ocurred.
151:             */
152:            protected void disconnect() throws IOException {
153:                if (connection != null) {
154:                    messenger.send(toServer, Messenger.DONE, "");
155:
156:                    toServer.close();
157:                    fromServer.close();
158:                    connection.close();
159:
160:                    toServer = null;
161:                    fromServer = null;
162:                    connection = null;
163:                }
164:            }
165:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.