Source Code Cross Referenced for ChatProvider.java in  » Portal » Open-Portal » com » sun » portal » sample » j2ee » chat » provider » 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 » Portal » Open Portal » com.sun.portal.sample.j2ee.chat.provider 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.sun.portal.sample.j2ee.chat.provider;
002:
003:        import java.io.PrintWriter;
004:        import java.io.StringWriter;
005:        import java.util.Date;
006:        import java.util.Iterator;
007:        import java.util.Set;
008:
009:        import com.iplanet.am.sdk.AMException;
010:        import com.iplanet.am.sdk.AMStoreConnection;
011:        import com.iplanet.am.sdk.AMUser;
012:        import com.iplanet.sso.SSOToken;
013:        import com.iplanet.sso.SSOException;
014:        import com.iplanet.sso.SSOTokenManager;
015:
016:        import com.sun.portal.providers.context.ProviderContext;
017:        import com.sun.portal.providers.jsp.JSPProvider;
018:        import com.sun.portal.providers.ProviderException;
019:
020:        import javax.servlet.http.HttpSession;
021:        import javax.servlet.http.HttpServletRequest;
022:        import javax.servlet.http.HttpServletResponse;
023:
024:        import javax.naming.InitialContext;
025:        import javax.naming.NamingException;
026:
027:        import javax.jms.JMSException;
028:        import javax.jms.TextMessage;
029:        import javax.jms.Topic;
030:        import javax.jms.TopicConnection;
031:        import javax.jms.TopicConnectionFactory;
032:        import javax.jms.TopicPublisher;
033:        import javax.jms.TopicSession;
034:        import javax.jms.TopicSubscriber;
035:
036:        public class ChatProvider extends JSPProvider {
037:
038:            private static final String LOOKUP_STRING_FACTORY = "java:comp/env/jms/sampleTCF";
039:            private static final String LOOKUP_STRING_TOPIC = "java:comp/env/jms/sampleTopic";
040:            private static TopicConnectionFactory TCFactory = null;
041:            private static Topic topic = null;
042:
043:            private boolean init() {
044:                boolean result = true;
045:                InitialContext ic = null;
046:
047:                try {
048:                    ic = new InitialContext();
049:                    TCFactory = (TopicConnectionFactory) ic
050:                            .lookup(LOOKUP_STRING_FACTORY);
051:                    topic = (Topic) ic.lookup(LOOKUP_STRING_TOPIC);
052:                } catch (Exception e) {
053:                    result = false;
054:                }
055:
056:                return result;
057:            }
058:
059:            private String getCommonName(HttpServletRequest req) {
060:                String result = null;
061:
062:                try {
063:                    SSOTokenManager tokenManager = SSOTokenManager
064:                            .getInstance();
065:
066:                    if (tokenManager != null) {
067:                        SSOToken token = tokenManager.createSSOToken(req);
068:                        AMStoreConnection conn = new AMStoreConnection(token);
069:                        AMUser user = conn.getUser(token.getPrincipal()
070:                                .getName());
071:                        Set s = user.getAttribute("cn");
072:                        Iterator i = s.iterator();
073:                        if (i.hasNext()) {
074:                            result = (String) i.next();
075:                        }
076:                    }
077:                } catch (SSOException ssoe) {
078:                } catch (AMException ame) {
079:                }
080:
081:                return result;
082:            }
083:
084:            public StringBuffer getContent(HttpServletRequest req,
085:                    HttpServletResponse res) throws ProviderException {
086:
087:                String principal = getCommonName(req);
088:                TopicSession tSession = null;
089:                TopicConnection tConnection = null;
090:                TopicPublisher tPublisher = null;
091:                TopicSubscriber tSubscriber = null;
092:                TextMessage msgReceived = null;
093:                StringWriter sw = new StringWriter();
094:                PrintWriter pw = new PrintWriter(sw);
095:                ProviderContext ctx = getProviderContext();
096:
097:                if ((TCFactory == null) || (topic == null)) {
098:                    if (!init()) {
099:                        ctx
100:                                .setSessionProperty(
101:                                        "errMsg",
102:                                        "Failed to initialize TopicConnectionFactory or Topic object. Check JNDI registration.");
103:                        return super .getContent(req, res);
104:                    }
105:                }
106:
107:                try {
108:                    tConnection = TCFactory.createTopicConnection();
109:                    tSession = tConnection.createTopicSession(true,
110:                            TopicSession.CLIENT_ACKNOWLEDGE);
111:                    tPublisher = tSession.createPublisher(topic);
112:                    if (principal != null) {
113:                        tSubscriber = tSession.createDurableSubscriber(topic,
114:                                principal);
115:                    } else {
116:                        tSubscriber = tSession.createSubscriber(topic);
117:                    }
118:
119:                    // get any message that needs to be published
120:                    String mesg = (String) req.getParameter("chat_mesg");
121:                    if (mesg != null) {
122:
123:                        TextMessage msgPublised = tSession.createTextMessage();
124:                        if (principal != null) {
125:                            msgPublised.setText(principal + ": [" + new Date()
126:                                    + "] : " + mesg);
127:                            tPublisher
128:                                    .setDeliveryMode(javax.jms.DeliveryMode.PERSISTENT);
129:                        } else {
130:                            msgPublised.setText("[" + new Date() + "] : "
131:                                    + mesg);
132:                            tPublisher
133:                                    .setDeliveryMode(javax.jms.DeliveryMode.NON_PERSISTENT);
134:                        }
135:                        // publishing the message to the topic destination
136:                        tPublisher.publish(msgPublised);
137:                        tSession.commit();
138:                    }
139:
140:                    tConnection.start();
141:                    while (true) {
142:                        msgReceived = (TextMessage) tSubscriber.receive(2000);
143:                        if (msgReceived != null) {
144:                            pw.println(msgReceived.getText());
145:                        } else {
146:                            break;
147:                        }
148:                    }
149:                    pw.flush();
150:                    sw.flush();
151:                    ctx.setSessionProperty("msgReceived", sw.toString());
152:                } catch (JMSException e) {
153:                    ctx.setSessionProperty("errMsg",
154:                            "Failed to publish or recieve msg, error="
155:                                    + e.getLinkedException().toString());
156:                } finally {
157:                    if (tConnection != null) {
158:                        try {
159:                            if (tPublisher != null) {
160:                                tPublisher.close();
161:                            }
162:
163:                            if (tSubscriber != null) {
164:                                tSubscriber.close();
165:                            }
166:
167:                            if (tSession != null) {
168:                                tSession.close();
169:                            }
170:
171:                            tConnection.close();
172:                        } catch (JMSException e) {
173:                            ctx
174:                                    .setSessionProperty("errMsg",
175:                                            "Failed to cleanup, error="
176:                                                    + e.getLinkedException()
177:                                                            .toString());
178:                        }
179:                    }
180:                    try {
181:                        pw.close();
182:                        sw.close();
183:                    } catch (java.io.IOException ioe) {
184:                    }
185:                }
186:                return super.getContent(req, res);
187:            }
188:        }
ww_w___.ja_v___a___2_s_._c__o__m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.