Source Code Cross Referenced for SystemServiceRequestProtocolAMS.java in  » 6.0-JDK-Modules » j2me » com » sun » midp » services » 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.services 
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:
027:        package com.sun.midp.services;
028:
029:        import com.sun.midp.links.*;
030:        import java.io.*;
031:
032:        final class SystemServiceRequestProtocolAMS {
033:            final static int SERVICE_REQUEST_STATUS_OK = 0;
034:            final static int SERVICE_REQUEST_STATUS_ERROR = 1;
035:
036:            private final static int INVALID_STATE = -1;
037:            private final static int WAIT_FOR_BEGIN_SESSION_STATE = 1;
038:            private final static int WAIT_FOR_SERVICE_ID_STATE = 2;
039:            private final static int SEND_SERVICE_REQUEST_STATUS_STATE = 3;
040:            private final static int SEND_SERVICE_TO_CLIENT_LINK_STATE = 4;
041:            private final static int SEND_CLIENT_TO_SERVICE_LINK_STATE = 5;
042:            private final static int WAIT_FOR_LINKS_RECEIVED_ACK_STATE = 6;
043:            private final static int WAIT_FOR_END_SESSION_STATE = 7;
044:            private final static int END_SESSION_STATE = 8;
045:            private final static int END_STATE = 9;
046:
047:            private SystemServiceRequestListener requestListener;
048:
049:            private int state = INVALID_STATE;
050:
051:            SystemServiceRequestProtocolAMS(
052:                    SystemServiceRequestListener requestListener) {
053:
054:                /**
055:                 * Argument sanity check
056:                 */
057:                if (requestListener == null) {
058:                    throw new NullPointerException();
059:                }
060:
061:                this .requestListener = requestListener;
062:            }
063:
064:            SystemServiceConnectionLinks handleServiceRequest(
065:                    SystemServiceConnectionLinks sendReceiveLinks)
066:                    throws ClosedLinkException, InterruptedIOException,
067:                    IOException {
068:
069:                Link sendLink = sendReceiveLinks.getSendLink();
070:                Link receiveLink = sendReceiveLinks.getReceiveLink();
071:
072:                /**
073:                 * Arguments sanity checks
074:                 */
075:                if (sendLink == null || receiveLink == null) {
076:                    throw new NullPointerException();
077:                }
078:
079:                if (!sendLink.isOpen() || !receiveLink.isOpen()) {
080:                    throw new IllegalStateException();
081:                }
082:
083:                SystemServiceConnectionLinks connectionLinks = null;
084:
085:                state = WAIT_FOR_BEGIN_SESSION_STATE;
086:                try {
087:                    connectionLinks = doHandleServiceRequest(sendLink,
088:                            receiveLink);
089:                } finally {
090:                    state = INVALID_STATE;
091:                }
092:
093:                return connectionLinks;
094:            }
095:
096:            private SystemServiceConnectionLinks doHandleServiceRequest(
097:                    Link sendLink, Link receiveLink)
098:                    throws ClosedLinkException, InterruptedIOException,
099:                    IOException {
100:
101:                String serviceID = "";
102:                SystemServiceConnectionLinks connectionLinks = null;
103:
104:                if (state == INVALID_STATE) {
105:                    throw new IllegalStateException();
106:                }
107:
108:                while (state != END_STATE) {
109:                    switch (state) {
110:                    case WAIT_FOR_BEGIN_SESSION_STATE: {
111:                        // wait for session begin request 
112:                        LinkMessage msg = receiveLink.receive();
113:                        String str = msg.extractString();
114:
115:                        // check request validity
116:                        if (!str
117:                                .equals(SystemServiceRequestProtocolClient.START_SESSION_STR)) {
118:                            throw new IllegalStateException();
119:                        }
120:
121:                        // advance to next state
122:                        state = WAIT_FOR_SERVICE_ID_STATE;
123:                        break;
124:                    }
125:
126:                    case WAIT_FOR_SERVICE_ID_STATE: {
127:                        // wait for service id
128:                        LinkMessage msg = receiveLink.receive();
129:                        serviceID = msg.extractString();
130:
131:                        // advance to next state
132:                        state = SEND_SERVICE_REQUEST_STATUS_STATE;
133:                        break;
134:                    }
135:
136:                    case SEND_SERVICE_REQUEST_STATUS_STATE: {
137:                        // try to obtain connection to service
138:                        int status = SERVICE_REQUEST_STATUS_OK;
139:                        try {
140:                            connectionLinks = null;
141:                            connectionLinks = requestListener
142:                                    .onServiceRequest(serviceID);
143:                        } finally {
144:                            if (connectionLinks == null) {
145:                                status = SERVICE_REQUEST_STATUS_ERROR;
146:                            }
147:
148:                            // send status
149:                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
150:                            DataOutputStream os = new DataOutputStream(bos);
151:                            os.writeInt(status);
152:                            byte[] data = bos.toByteArray();
153:
154:                            LinkMessage msg = LinkMessage.newDataMessage(data);
155:                            sendLink.send(msg);
156:
157:                            // advance to next state
158:                            if (status == SERVICE_REQUEST_STATUS_OK) {
159:                                state = SEND_SERVICE_TO_CLIENT_LINK_STATE;
160:                            } else {
161:                                state = END_STATE;
162:                            }
163:                        }
164:
165:                        break;
166:                    }
167:
168:                    case SEND_SERVICE_TO_CLIENT_LINK_STATE: {
169:                        // send service to client link
170:                        Link link = connectionLinks.getSendLink();
171:                        LinkMessage msg = LinkMessage.newLinkMessage(link);
172:                        sendLink.send(msg);
173:
174:                        // advance to next state
175:                        state = SEND_CLIENT_TO_SERVICE_LINK_STATE;
176:                        break;
177:                    }
178:
179:                    case SEND_CLIENT_TO_SERVICE_LINK_STATE: {
180:                        // send client to service link
181:                        Link link = connectionLinks.getReceiveLink();
182:                        LinkMessage msg = LinkMessage.newLinkMessage(link);
183:                        sendLink.send(msg);
184:
185:                        // advance to next state
186:                        state = WAIT_FOR_LINKS_RECEIVED_ACK_STATE;
187:                        break;
188:                    }
189:
190:                    case WAIT_FOR_LINKS_RECEIVED_ACK_STATE: {
191:                        // wait for links recieved ack
192:                        LinkMessage msg = receiveLink.receive();
193:                        String str = msg.extractString();
194:
195:                        // check request validity
196:                        if (!str
197:                                .equals(SystemServiceRequestProtocolClient.LINKS_RECEIVED_ACK_STR)) {
198:                            throw new IllegalStateException();
199:                        }
200:
201:                        // notify listener about connection passed to client
202:                        if (connectionLinks != null) {
203:                            requestListener
204:                                    .onLinksPassedToClient(connectionLinks);
205:                        }
206:
207:                        // advance to next state
208:                        state = WAIT_FOR_END_SESSION_STATE;
209:                        break;
210:                    }
211:
212:                    case WAIT_FOR_END_SESSION_STATE: {
213:                        // wait for session end request
214:                        LinkMessage msg = receiveLink.receive();
215:                        String str = msg.extractString();
216:
217:                        // check request validity
218:                        if (!str
219:                                .equals(SystemServiceRequestProtocolClient.END_SESSION_STR)) {
220:                            throw new IllegalStateException();
221:                        }
222:
223:                        // advance to next state
224:                        state = END_SESSION_STATE;
225:                        break;
226:                    }
227:
228:                    case END_SESSION_STATE: {
229:                        // advance to next state
230:                        state = END_STATE;
231:                        break;
232:                    }
233:                    }
234:                }
235:
236:                state = INVALID_STATE;
237:
238:                return connectionLinks;
239:            }
240:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.