Source Code Cross Referenced for SampleMuxHandler.java in  » Web-Server » Jigsaw » org » w3c » www » mux » 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 » Web Server » Jigsaw » org.w3c.www.mux 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // SampleMuxHandler.java
002:        // $Id: SampleMuxHandler.java,v 1.5 2000/08/16 21:38:02 ylafon Exp $
003:        // (c) COPYRIGHT MIT and INRIA, 1996.
004:        // Please first read the full copyright statement in file COPYRIGHT.html
005:
006:        package org.w3c.www.mux;
007:
008:        import java.io.IOException;
009:        import java.io.PrintStream;
010:
011:        import java.util.Hashtable;
012:
013:        public class SampleMuxHandler implements  MuxStreamHandler {
014:            /**
015:             * Debug state.
016:             */
017:            private static final boolean debug = false;
018:            /**
019:             * Well known protocols - The echo protocol identifier.
020:             */
021:            public static final int ECHO = 7;
022:            /**
023:             * Well known protocols - The echo protocol identifier.
024:             */
025:            public static final int DISCARD = 9;
026:
027:            /**
028:             * The sigle instance of that class.
029:             */
030:            protected static SampleMuxHandler sample = null;
031:            /**
032:             * The hashtable of accepted protocols.
033:             */
034:            protected Hashtable protocols = null;
035:
036:            /**
037:             * Log an error.
038:             * @param msg The message to log.
039:             */
040:
041:            protected void log(String msg) {
042:                System.out.println("[" + getClass().getName() + "]: " + msg);
043:            }
044:
045:            /**
046:             * Get an instance of that sample mux stream handler.
047:             * @return A MuxStreamHandler conformant instance.
048:             */
049:
050:            public static synchronized MuxStreamHandler getStreamHandler() {
051:                // Of course, we should go through a factory, etc as usual:
052:                if (sample == null)
053:                    sample = new SampleMuxHandler();
054:                return sample;
055:            }
056:
057:            /**
058:             * Are we willing to speak the given protocol on the given session.
059:             * @param stream The stream that received the new session.
060:             * @param sessid The proposed session identifier.
061:             * @param protid The protocol to be spoken on that session.
062:             * @return A bolean, <strong>true</strong> if the session is accepted,
063:             * <strong>false</strong> otherwise.
064:             */
065:
066:            public boolean acceptSession(MuxStream stream, int sessid,
067:                    int protid) {
068:                Object o = protocols.get(new Integer(protid));
069:                // Reject unknown protocols straight:
070:                if (o == null) {
071:                    if (debug)
072:                        System.out.println("Rejecting " + protid + " on "
073:                                + sessid + ".");
074:                    return false;
075:                } else {
076:                    if (debug)
077:                        System.out.println("Accepting " + protid + " on "
078:                                + sessid + ".");
079:                    return true;
080:                }
081:            }
082:
083:            /**
084:             * Setup the appropriate protocol handler for that accepted session.
085:             * @param session The newly accepted session.
086:             */
087:
088:            public void notifySession(MuxSession session) {
089:                int protid = session.getProtocolIdentifier();
090:                Integer iprotid = new Integer(protid);
091:                Object o = protocols.get(iprotid);
092:                // This should not happen (except for some race conditions):
093:                if (o == null) {
094:                    log("SampleMuxHandler: unknown protocol " + protid);
095:                    try {
096:                        session.shutdown();
097:                    } catch (Exception ex) {
098:                    }
099:                }
100:                // Find (or instantiate) appropriate protocol handler:
101:                MuxProtocolHandler handler = null;
102:                if (o instanceof  String) {
103:                    String strcls = (String) o;
104:                    try {
105:                        Class c = Class.forName(strcls);
106:                        handler = (MuxProtocolHandler) c.newInstance();
107:                    } catch (Exception ex) {
108:                        log("Instantiating handler for " + protid
109:                                + " of class \"" + strcls + "\" failed.");
110:                        ex.printStackTrace();
111:                        try {
112:                            session.shutdown();
113:                        } catch (IOException exex) {
114:                        }
115:                    }
116:                } else if (o instanceof  MuxProtocolHandler) {
117:                    handler = (MuxProtocolHandler) o;
118:                } else {
119:                    log("SampleMuxHandler: unknown protocol " + protid);
120:                    try {
121:                        session.shutdown();
122:                    } catch (Exception ex) {
123:                    }
124:                }
125:                // We now have a handler, launch:
126:                try {
127:                    handler.initialize(session);
128:                } catch (Exception ex) {
129:                    log("Launching handler for " + protid + " of class \""
130:                            + handler.getClass() + "\" failed.");
131:                    ex.printStackTrace();
132:                    try {
133:                        session.shutdown();
134:                    } catch (IOException exex) {
135:                    }
136:                }
137:            }
138:
139:            /**
140:             * Register a protocol handler for the given protocol identifier.
141:             * This method register a class to handle all new connections for the 
142:             * given protocol identifier: each new connection will result in a new 
143:             * instance of the given class being created (the easy, but slow way).
144:             * @param protid The protocol identifier.
145:             * @param handler The name of the class to instantiate in order
146:             * to get a suitable handler for that protocol.
147:             * @see MuxProtocolHandler
148:             */
149:
150:            public void registerHandler(int protid, String strhandler) {
151:                protocols.put(new Integer(protid), strhandler);
152:            }
153:
154:            /**
155:             * Register an instantiated protocol handler for the given protocol id.
156:             * This other method of registering protocol handler is faster then
157:             * the previous one: it allows you to spare the instantiation of a protocol
158:             * handler on each new sessions.
159:             * <p>The given handler will be invoked for all new sessions willing to
160:             * speak the advertized protocol.
161:             * @param protid The protocol identifier.
162:             * @param handler The instantiated protocol handler.
163:             */
164:
165:            public void registerHandler(int protid, MuxProtocolHandler handler) {
166:                protocols.put(new Integer(protid), handler);
167:            }
168:
169:            /**
170:             * Unregister any handler for that protocol.
171:             * @param protid The identifier of the protocol to unregister.
172:             */
173:
174:            public void unregisterHandler(int protid) {
175:                protocols.remove(new Integer(protid));
176:            }
177:
178:            /**
179:             * Register default protocol handlers for that stream handler.
180:             * This is the right method to override in order to either prevent
181:             * well known protocols from being registered, or add new protocol
182:             * handlers.
183:             * <p>Default protocols registered by this class are:
184:             * <dl>
185:             * <dt>echo<dd>The echo protocol.
186:             * <dt>discard<dd>The discard protocol.
187:             * </dt>
188:             */
189:
190:            public void registerDefaultHandlers() {
191:                registerHandler(ECHO, "org.w3c.www.mux.handlers.Echo");
192:                registerHandler(DISCARD, "org.w3c.www.mux.handlers.Discard");
193:            }
194:
195:            public SampleMuxHandler() {
196:                super ();
197:                // Initialize instance variables:
198:                this .protocols = new Hashtable();
199:                // Register default protocols:
200:                registerDefaultHandlers();
201:            }
202:
203:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.