Source Code Cross Referenced for News.java in  » IDE-Netbeans » collab » org » netbeans » lib » collab » sample » 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 » IDE Netbeans » collab » org.netbeans.lib.collab.sample 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.lib.collab.sample;
043:
044:        import org.netbeans.lib.collab.*;
045:        import java.io.*;
046:        import java.util.*;
047:
048:        /**
049:         * Sample Command line client
050:         * to read messages from news channels and
051:         * post new messages to news channels
052:         *
053:         */
054:        class NewsChannelListenerImpl implements  NewsChannelListener {
055:
056:            NewsChannel _nc;
057:            boolean _subscribed;
058:            private Hashtable _messages = new Hashtable();
059:
060:            public NewsChannelListenerImpl(NewsService s, String name,
061:                    boolean create, boolean subscribe) throws Exception {
062:                _subscribed = subscribe;
063:                if (create) {
064:                    _nc = s.newNewsChannel(name, this , Conference.MANAGE);
065:                } else {
066:                    _nc = s.getNewsChannel(name, this );
067:                }
068:                if (_nc == null)
069:                    throw new Exception(
070:                            "Failed to create or access news channel");
071:            }
072:
073:            //add a message to the current conference
074:            public void addMessage(String subject, String body)
075:                    throws Exception {
076:                Message newMsg = _nc.createMessage();
077:                newMsg.setContent(body);
078:                newMsg.setHeader("subject", subject);
079:                _nc.addMessage(newMsg);
080:                _messages.put(newMsg.getMessageId(), newMsg);
081:            }
082:
083:            /**
084:             * invoked when a message has been posted to the bulletin board
085:             *
086:             * @param message the message
087:             */
088:            public void onMessageModified(String messageId, Message message) {
089:            }
090:
091:            /**
092:             * invoked when a message has been posted to the bulletin board
093:             *
094:             * @param message the message
095:             */
096:            public void onMessageAdded(Message message) {
097:                if (_subscribed) {
098:                    System.out.println("[" + _nc.getDestination()
099:                            + "] Message added");
100:                    News.printMessage(message);
101:                    System.out.print("News> ");
102:                }
103:                _messages.put(message.getMessageId(), message);
104:            }
105:
106:            /**
107:             * invoked when a message has been posted to the bulletin board
108:             *
109:             * @param message the message
110:             */
111:            public void onMessageRemoved(String messageId) {
112:                if (_subscribed) {
113:                    System.out.println("[" + _nc.getDestination()
114:                            + "] Message deleted: " + messageId);
115:                    System.out.print("News> ");
116:                }
117:                _messages.remove(messageId);
118:            }
119:
120:            /**
121:             * invoked when an unexpected failure has happed.
122:             * The listener is no longer
123:             * valid after this method has been called.
124:             * @param e describes the nature of the problem
125:             */
126:            public void onError(org.netbeans.lib.collab.CollaborationException e) {
127:                e.printStackTrace();
128:                System.out.print("News> ");
129:            }
130:
131:            public void remove() {
132:            }
133:
134:            public void removeMessage(String messageId) throws Exception {
135:                _nc.removeMessage(messageId);
136:                _messages.remove(messageId);
137:            }
138:
139:            public void onEvent(String event) {
140:            }
141:
142:            public void listMessages() throws Exception {
143:                if (_messages.size() == 0) {
144:                    // give it some time to receive messages
145:                    Thread.sleep(200);
146:                }
147:                java.util.Collection c = _messages.values();
148:                System.out.println("[" + _nc.getDestination()
149:                        + "] begin message list");
150:                for (java.util.Iterator i = c.iterator(); i.hasNext();) {
151:                    org.netbeans.lib.collab.Message m = (org.netbeans.lib.collab.Message) i
152:                            .next();
153:                    News.printMessage(m);
154:                }
155:                System.out.println("[" + _nc.getDestination()
156:                        + "] end message list");
157:                System.out.print("News> ");
158:
159:            }
160:
161:            /** invoked when a moderated message is received from another user participating
162:             * in this session.
163:             * @param message the message
164:             */
165:            public void onModeratedMessageAdded(Message message) {
166:            }
167:
168:            /** invoked when there is a change in the status of the moderated message
169:             * @param message the message
170:             * @param status The status of the message as defined in Conference.
171:             */
172:            public void onModeratedMessageStatus(Message message, int status,
173:                    String reason) {
174:            }
175:
176:        }
177:
178:        public class News extends Thread implements 
179:                CollaborationSessionListener {
180:
181:            // sessions
182:            private NewsService _service;
183:
184:            // contains all open conferences and news channels
185:            private Hashtable _channels = new Hashtable();
186:
187:            //create a connection using the command line
188:            public News(CollaborationSessionFactory fac, String server,
189:                    String user, String password) throws Exception {
190:                CollaborationSession s = fac.getSession(server, user, password,
191:                        this );
192:                _service = s.getNewsService();
193:                //_service.initialize(this);
194:            }
195:
196:            public void onError(org.netbeans.lib.collab.CollaborationException e) {
197:                e.printStackTrace();
198:            }
199:
200:            public void run() {
201:
202:                for (;;) {
203:
204:                    try {
205:                        System.out.print("News> ");
206:
207:                        String s = _reader.readLine();
208:                        if (s == null)
209:                            break;
210:                        s = s.trim();
211:                        if (s.equals(""))
212:                            continue;
213:
214:                        if (s.startsWith("?") || s.equalsIgnoreCase("help")) {
215:                            System.out
216:                                    .println("read <news-channel>      // read messages from a news channel");
217:                            System.out
218:                                    .println("post <news-channel>      // post a new message");
219:                            System.out
220:                                    .println("delete <news-channel>    // remove a news channel message");
221:                            System.out
222:                                    .println("create <news-channel>    // create a new news channel");
223:                        } else {
224:                            StringTokenizer st = new StringTokenizer(s);
225:                            String cmd = st.nextToken();
226:                            String channel = null;
227:                            try {
228:                                channel = st.nextToken();
229:                            } catch (Exception e) {
230:                                System.out.println("Missing news channel name");
231:                                continue;
232:                            }
233:
234:                            if (s.startsWith("read ")) {
235:                                read(channel);
236:
237:                            } else if (s.startsWith("post ")) {
238:                                postMessage(channel);
239:
240:                            } else if (s.startsWith("delete ")) {
241:                                removeMessage(channel);
242:
243:                            } else if (s.startsWith("create ")) {
244:                                createChannel(channel);
245:
246:                            } else if (s.startsWith("remove ")) {
247:                                removeChannel(channel);
248:
249:                            } else {
250:                                System.out.println("unrecognized command: "
251:                                        + cmd);
252:                            }
253:
254:                        }
255:
256:                    } catch (Exception e) {
257:                        System.out.println("Error " + e.toString());
258:                        e.printStackTrace();
259:                    }
260:
261:                }
262:
263:            }
264:
265:            private NewsChannelListenerImpl getChannel(String name)
266:                    throws Exception {
267:                NewsChannelListenerImpl c = (NewsChannelListenerImpl) _channels
268:                        .get(name);
269:                if (c == null) {
270:                    c = new NewsChannelListenerImpl(_service, name, false,
271:                            false);
272:                }
273:                return c;
274:            }
275:
276:            private void read(String name) throws Exception {
277:                NewsChannelListenerImpl c = getChannel(name);
278:                if (c != null) {
279:                    c.listMessages();
280:                } else {
281:                    System.out.println("No such news channel: " + name);
282:                }
283:            }
284:
285:            private void removeChannel(String name) throws Exception {
286:                NewsChannelListenerImpl c = getChannel(name);
287:                if (c != null) {
288:                    c.remove();
289:                } else {
290:                    System.out.println("No such news channel: " + name);
291:                }
292:            }
293:
294:            private void createChannel(String name) throws Exception {
295:                NewsChannelListenerImpl c = null;
296:                try {
297:                    c = getChannel(name);
298:                } catch (Exception e) {
299:                    // ignore news channel not found
300:                }
301:
302:                if (c == null) {
303:                    c = new NewsChannelListenerImpl(_service, name, true, false);
304:                } else {
305:                    System.out
306:                            .println("There is already a news channel called "
307:                                    + name);
308:                }
309:            }
310:
311:            private void postMessage(String name) throws Exception {
312:                NewsChannelListenerImpl c = getChannel(name);
313:                if (c != null) {
314:                    String subject = prompt("Subject: ", false, "");
315:                    String body = prompt("Content: ", true, "");
316:                    c.addMessage(subject, body);
317:                } else {
318:                    System.out.println("No such news channel: " + name);
319:                }
320:            }
321:
322:            private void removeMessage(String name) throws Exception {
323:                NewsChannelListenerImpl c = getChannel(name);
324:                if (c != null) {
325:                    c.listMessages();
326:                    String id = prompt("ID of message to remove: ", false, "");
327:                    c.removeMessage(id);
328:                } else {
329:                    System.out.println("No such news channel: " + name);
330:                }
331:            }
332:
333:            //
334:            // STATIC CODE
335:            //
336:
337:            // source to read commands from
338:            private static BufferedReader _reader;
339:
340:            //login to the server    
341:            public static void main(String args[]) {
342:
343:                _reader = new BufferedReader(new InputStreamReader(System.in));
344:
345:                String user, password, server;
346:
347:                if (args.length > 0 && args[0].equals("?")) {
348:                    System.out.println("Talk host username password");
349:                    return;
350:                }
351:
352:                // prompt server        
353:                if (args.length >= 1) {
354:                    server = args[0];
355:                } else {
356:                    server = prompt("Hostname [localhost:9909]\t: ", false,
357:                            "localhost:9909");
358:                }
359:
360:                if (args.length >= 2) {
361:                    user = args[1];
362:                } else {
363:                    String username = System.getProperty("user.name");
364:                    user = prompt("User name [" + username + "]\t: ", false,
365:                            username);
366:                }
367:
368:                if (args.length >= 3) {
369:                    password = args[2];
370:                } else {
371:                    password = prompt("Password [iplanet]\t: ", false,
372:                            "iplanet");
373:                }
374:
375:                try {
376:
377:                    CollaborationSessionFactory fac = new CollaborationSessionFactory();
378:                    News talk = new News(fac, server, user, password);
379:                    talk.start();
380:
381:                    talk.join();
382:
383:                } catch (Exception e) {
384:                    e.printStackTrace();
385:                }
386:            }
387:
388:            public static void printMessage(Message message) {
389:                // display the message
390:                System.out.println("    Message-Id: " + message.getMessageId());
391:                System.out.println("    From: " + message.getOriginator());
392:                System.out.println("    Subject: "
393:                        + message.getHeader("subject"));
394:                System.out.println("    Content-type: "
395:                        + message.getContentType());
396:                MessagePart[] parts = message.getParts();
397:                for (int i = 0; i < parts.length; i++) {
398:                    try {
399:                        BufferedReader br = new BufferedReader(
400:                                new InputStreamReader(parts[i].getInputStream()));
401:                        String str;
402:                        while ((str = br.readLine()) != null)
403:                            System.out.println(str);
404:                    } catch (Exception e) {
405:                        e.printStackTrace();
406:                    }
407:                }
408:            }
409:
410:            private static String prompt(String ask, boolean newline) {
411:                return prompt(ask, newline, "");
412:            }
413:
414:            private static String prompt(String ask, boolean newline,
415:                    String defaultValue) {
416:                String ret = defaultValue;
417:                if (newline) {
418:                    System.out.println(ask);
419:                } else {
420:                    System.out.print(ask);
421:                }
422:
423:                try {
424:                    String s = _reader.readLine();
425:                    if (s.length() > 0)
426:                        ret = s;
427:                } catch (Exception e) {
428:                    e.printStackTrace();
429:                }
430:
431:                return ret;
432:            }
433:
434:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.