KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > plugin > RegistrationPlugin


1 /**
2  * Copyright (C) 2005 Jive Software. All rights reserved.
3  *
4  * This software is published under the terms of the GNU Public License (GPL),
5  * a copy of which is included in this distribution.
6  */

7
8 package org.jivesoftware.messenger.plugin;
9
10 import org.jivesoftware.messenger.MessageRouter;
11 import org.jivesoftware.messenger.XMPPServer;
12 import org.jivesoftware.messenger.container.Plugin;
13 import org.jivesoftware.messenger.container.PluginManager;
14 import org.jivesoftware.messenger.event.UserEventDispatcher;
15 import org.jivesoftware.messenger.event.UserEventListener;
16 import org.jivesoftware.messenger.user.User;
17 import org.jivesoftware.util.JiveGlobals;
18 import org.xmpp.packet.JID;
19 import org.xmpp.packet.Message;
20 import org.xmpp.packet.Packet;
21
22 import java.io.File JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Registration plugin.
27  *
28  * TODO: document plugin property names here.
29  *
30  * @author Ryan Graham.
31  */

32 public class RegistrationPlugin implements Plugin {
33
34     private RegistrationUserEventListener listener = new RegistrationUserEventListener();
35     
36     private String JavaDoc serverName;
37     private JID serverAddress;
38     private MessageRouter router;
39
40     private boolean registrationNotificationEnabled;
41     private boolean registrationWelcomeEnabled;
42     private String JavaDoc contact;
43     
44     public RegistrationPlugin() {
45         serverName = XMPPServer.getInstance().getServerInfo().getName();
46         serverAddress = new JID(serverName);
47         router = XMPPServer.getInstance().getMessageRouter();
48
49         registrationNotificationEnabled = JiveGlobals.getBooleanProperty(
50                 "registration.notification.enabled", false);
51         setRegistrationNotificationEnabled(registrationNotificationEnabled);
52         
53         registrationWelcomeEnabled = JiveGlobals.getBooleanProperty(
54                 "registration.welcome.enabled", false);
55         setRegistrationNotificationEnabled(registrationWelcomeEnabled);
56         
57         contact = JiveGlobals.getProperty("registration.notification.contact", "admin");
58                 
59         UserEventDispatcher.addListener(listener);
60     }
61
62     public void processPacket(Packet packet) {
63     }
64
65     public void initializePlugin(PluginManager manager, File JavaDoc pluginDirectory) {
66     }
67
68     public void destroyPlugin() {
69         UserEventDispatcher.removeListener(listener);
70         serverAddress = null;
71         listener = null;
72         router = null;
73     }
74     
75     public void setRegistrationNotificationEnabled(boolean enable) {
76         registrationNotificationEnabled = enable;
77         JiveGlobals.setProperty("registration.notification.enabled", enable ? "true" : "false");
78     }
79     
80     public boolean registrationNotificationEnabled() {
81         return JiveGlobals.getBooleanProperty("registration.notification.enabled", false);
82     }
83     
84     public void setContact(String JavaDoc contact) {
85         this.contact = contact;
86         JiveGlobals.setProperty("registration.notification.contact", contact);
87     }
88     
89     public String JavaDoc getContact() {
90         return contact;
91     }
92     
93     public void setRegistrationWelcomeEnabled(boolean enable) {
94         registrationWelcomeEnabled = enable;
95         JiveGlobals.setProperty("registration.welcome.enabled", enable ? "true" : "false");
96     }
97     
98     public boolean registrationWelcomeEnabled() {
99         return JiveGlobals.getBooleanProperty("registration.welcome.enabled", false);
100     }
101     
102     public void setWelcomeMessage(String JavaDoc message) {
103         JiveGlobals.setProperty("registration.welcome.message", message);
104     }
105     
106     public String JavaDoc getWelcomeMessage() {
107         return JiveGlobals.getProperty("registration.welcome.message", "Welcome to Jive Messenger!");
108     }
109     
110     private void sendRegistrationNotificatonMessage(User user) {
111         String JavaDoc msg = " A new user with the username of '" + user.getUsername() + "' just registered";
112         router.route(createServerMessage(getContact() + "@" + serverName,
113                 "Registration Notification", msg));
114     }
115     
116     private void sendWelcomeMessage(User user) {
117         router.route(createServerMessage(user.getUsername() + "@" + serverName, "Welcome",
118                 getWelcomeMessage()));
119     }
120     
121     private Message createServerMessage(String JavaDoc to, String JavaDoc subject, String JavaDoc body) {
122         Message message = new Message();
123         message.setTo(to);
124         message.setFrom(serverAddress);
125         if (subject != null) {
126             message.setSubject(subject);
127         }
128         message.setBody(body);
129         return message;
130     }
131
132     //TODO JM-170
133
//TODO add the ability for the admin to monitor when users are deleted?
134
private class RegistrationUserEventListener implements UserEventListener {
135         public void userCreated(User user, Map JavaDoc params) {
136             if (registrationNotificationEnabled) {
137                 sendRegistrationNotificatonMessage(user);
138             }
139             
140             if (registrationWelcomeEnabled) {
141                 sendWelcomeMessage(user);
142             }
143         }
144
145         public void userDeleting(User user, Map JavaDoc params) {
146         }
147
148         public void userModified(User user, Map JavaDoc params) {
149         }
150     }
151 }
152
Popular Tags