Wiki source code of Old Notifications Tutorial

Version 8.1 by Vincent Massol on 2014/05/29

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
2
3 This tutorial uses the old notification mechanism to listen to events (now deprecated in XWiki 2.0). You should follow it if you're using a version of XWiki Enterprise prior to 2.0.
4
5 In order to listen to events you need to write 2 pages:
6
7 * A page containing a Groovy class that registers against the XWiki Event Manager and that has the method to be called when the event happens.
8 * Another page that parses the Groovy page and loads it in the Groovy context.
9
10 = Groovy Notification Class =
11
12 Your Groovy needs to extend the ##com.xpn.xwiki.notify.XWikiDocChangeNotificationInterface## as shown below.
13
14 {{code language="java"}}
15 /* Groovy Class #* */
16
17 import com.xpn.xwiki.api.*;
18 import com.xpn.xwiki.notify.*;
19 import com.xpn.xwiki.*;
20 import com.xpn.xwiki.doc.*;
21
22 public class MyGroovyClass implements XWikiDocChangeNotificationInterface
23 {
24 def xwiki;
25 def rule;
26
27 public MyGroovyClass()
28 {
29 this.rule = new DocChangeRule(this);
30 }
31
32 public void init(xwiki)
33 {
34 this.xwiki = xwiki;
35 xwiki.getXWiki().getNotificationManager().addGeneralRule(this.rule);
36 }
37
38 public void cleanup()
39 {
40 xwiki.getXWiki().getNotificationManager().removeGeneralRule(this.rule);
41 }
42
43 public void notify(XWikiNotificationRule rule, XWikiDocument newdoc, XWikiDocument olddoc,
44 int event, XWikiContext context)
45 {
46 // Do some action here.
47 }
48 }
49
50 /* *# */
51 {{/code}}
52
53 In this example we've used a ##DocChangeRule## rule. There are also {{scm path="xwiki-platform-core/xwiki-platform-legacy/xwiki-platform-legacy-oldcore/src/main/java/com/xpn/xwiki/notify"}}other rules{{/scm}}.
54
55 = Calling the Groovy Class =
56
57 {{code}}
58 #set($mygroovyclass = $xwiki.parseGroovyFromPage("MySpace.MyGroovyClass"))
59 $mygroovyclass.init($xwiki)
60 {{/code}}
61
62 = Example: IRC notification on document change =
63
64 {{warning}}
65 The code below uses the parseGroovyFromPage method which takes 2 parameters. The second one is the name of page containing JARS as attachments. These JARs are put in the classloader used by Groovy when parsing the page. This feature is only working in XWiki Core 1.3 and later.
66 {{/warning}}
67
68 * **Step 1**: Groovy Class(((
69 {{code language="java"}}
70 /* Groovy Class #* */
71
72 import org.jibble.pircbot.*;
73 import java.util.*;
74 import com.xpn.xwiki.api.*;
75 import com.xpn.xwiki.notify.*;
76 import com.xpn.xwiki.*;
77 import com.xpn.xwiki.doc.*;
78
79 public class XWikiBot extends PircBot implements XWikiDocChangeNotificationInterface
80 {
81 def xwiki;
82 def channel;
83 def rule;
84
85 public XWikiBot()
86 {
87 this.setName("xwikibot");
88 this.rule = new DocChangeRule(this);
89 }
90
91 public void init(xwiki, channel)
92 {
93 this.xwiki = xwiki;
94 this.channel = channel;
95 xwiki.getXWiki().getNotificationManager().addGeneralRule(this.rule);
96 }
97
98 public void cleanup()
99 {
100 xwiki.getXWiki().getNotificationManager().removeGeneralRule(this.rule);
101 }
102
103 public void notify(XWikiNotificationRule rule, XWikiDocument newdoc, XWikiDocument olddoc,
104 int event, XWikiContext context)
105 {
106 sendMessage(this.channel, newdoc.getFullName() + " was modified - " + newdoc.getExternalURL("view", context));
107 }
108 }
109
110 /* *# */
111 {{/code}}
112 )))
113 * **Step 2**: Add the PircBot JAR as an attachment to the ##MySpace.MyGroovyClass## page created in step 1.
114 * **Step 3**: Calling page(((
115 {{code}}
116 ## Start by looking for a bot in the servlet context
117 #set ($sc = $context.getContext().getEngineContext().getServletContext())
118
119 ## If the bot isn't in the servlet context, start the bot and put in the context
120 #set ($bot = $sc.getAttribute("ircbot"))
121 #if (!$bot)
122 Bot is not started, starting it...
123 #set($bot = $xwiki.parseGroovyFromPage("MySpace.MyGroovyClass", "MySpace.MyGroovyClass"))
124 #set ($channel = "#xwiki")
125 $bot.init($xwiki, $channel)
126 $bot.connect("irc.freenode.net")
127 $bot.joinChannel($channel)
128 $sc.setAttribute("ircbot", $bot)
129 Bot started!
130
131 ## If the parameter passed is stop then stop the bot
132 #elseif ($request.action && $request.action == "stop")
133 $bot.cleanup()
134 $bot.disconnect()
135 $sc.setAttribute("ircbot", null)
136 Bot disconnected!
137
138 #else
139 Bot already started, doing nothing...
140
141 #end
142 {{/code}}
143 )))
144 * **Step 4**: Creating a Scheduler job so that the Bot is restarted automatically if the server is restarted for example.(((
145 Create a Scheduler job, set it to run every 5 minutes for example and use the following Groovy script in the job:
146
147 {{code language="java"}}
148 // Start by looking for a bot in the servlet context
149 def sc = context.getEngineContext().getServletContext()
150
151 // If the bot isn't in the servlet context, start the bot and put in the context
152 def bot = sc.getAttribute("ircbot")
153 if (bot == null) {
154 // Bot is not started, starting it...
155 bot = xwiki.parseGroovyFromPage("MySpace.MyGroovyClass", "MySpace.MyGroovyClass")
156 def channel = "#xwiki"
157 bot.init(xwiki, channel)
158 bot.connect("irc.freenode.net")
159 bot.joinChannel(channel)
160 sc.setAttribute("ircbot", bot)
161 // Bot started!
162 }
163 {{/code}}
164 )))

Get Connected