Re: [xwiki-devs] XWiki Jabber, Google Talk, Skype Integration
Hi, I have been looking at how to notify when an event has been finished. Here what I have currently understood and I got some confusion. In order to get notified when login event finished, I think I have to do the following things. 1. Create a LoginListener class that implements EventListener 2. Create object of DefaultObservationManager class (DefaultObservationManager implements ObservationManager) 3. Define class UserLoginEvent similar to DocumentSaveEvent 4. call addListener() of ObjectManager interface (using DefaultObjectManager) with parameters - object of UserLoginEvent and reference to login listener. 5. get reference to DefaultObjectManager instance 6. call notify() of DefaultObjectManager instance when user successfully logged in, with parameters - object of UserLoginEvent, doc and context 7. Implement notify() in LoginListener class. Q. But I have several confusions when I looked at XWiki class, saveDocument() 1. Where does it create and add a listener to ObservationManager object?? 2. And also it takes ObservationManager object from Util.getComponent()but where does this observationManager instance is added or set ? Please clarify me on what I currently understood and let me know if its the right steps to go with. Kind Regards, -Tharindu
Tharindu Madushanka wrote:
Hi, I have been looking at how to notify when an event has been finished. Here what I have currently understood and I got some confusion. In order to get notified when login event finished, I think I have to do the following things.
1. Create a LoginListener class that implements EventListener Yes
2. Create object of DefaultObservationManager class (DefaultObservationManager implements ObservationManager) No, the component manager will give you the right instance. You should read about this here: http://en.wikipedia.org/wiki/Inversion_of_control and here: http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents All you need to do is declare a dependency on the ObservationManager component.
3. Define class UserLoginEvent similar to DocumentSaveEvent Yes
4. call addListener() of ObjectManager interface (using DefaultObjectManager) with parameters - object of UserLoginEvent and reference to login listener. Yes. This is done by the client that wants to be notified of login events, which is different from the class sending those events.
5. get reference to DefaultObjectManager instance See the observation on 2.
6. call notify() of DefaultObjectManager instance when user successfully logged in, with parameters - object of UserLoginEvent, doc and context Yes. The tricky part is knowing when this happened, or what is the code that knows that.
7. Implement notify() in LoginListener class. Yes.
Q. But I have several confusions when I looked at XWiki class, saveDocument() 1. Where does it create and add a listener to ObservationManager object?? 2. And also it takes ObservationManager object from Util.getComponent()but where does this observationManager instance is added or set ?
The component manager takes care of instantiating objects. There are several component frameworks, from the heavier Spring and OSGi to the lightweight picocontainer and Guice. We're currently using Plexus for this. So, com.xpn.xwiki.XWiki does not create an instance of ObservationManager, it gets one from the ComponentManager through the Util class. Normally, other components should just declare a dependency on the needed component, and the CM would automatically inject it, without any calls to Util.getComponent, but the XWiki class is not a component yet, so it is not managed by the component manager. The code inside the xwiki-core module is not made of components yet, while most of the other core modules are 100% components, with a few exceptions (xwiki-webdav and xwiki-rest).
Please clarify me on what I currently understood and let me know if its the right steps to go with.
Yes, the steps are almost right. Good job so far. -- Sergiu Dumitriu http://purl.org/net/sergiu/
Hi, I was little bit busy this week with University just started for us. And I am having 3 days holiday from tomorrow. Therefore, I thought I use that time to fix JIRA issue on my work. Steps Revised 1. Create a LoginListener class that implements EventListener can I use the package org.xwiki.observation.event to add this class ? 2. I do not need to create any DefaultObservationManager instance. components.xml file is defined with DefaultObservationManager implementation. I get reference to it by calling (Step 5) when ever nececary ObservationManager om = (ObservationManager) Utils.getComponent(ObservationManager.class); 3. Define class UserLoginEvent similar to DocumentSaveEvent in package org.xwiki.observation.event *[TODO]* 4. Need to find the place where I call the addListener() of ObservationManager with parameters - object of UserLoginEvent and reference to login listener. I think when user initiated login I need to add the listener. I looked at LoginAction class, but could not figure out a way. 6. Need to find when to call notify() of DefaultObjectManager instance when user successfully logged in 7. Implement notify() in LoginListener I think I need to get familiar with com.xpn.xwiki.web package to learn how to do steps 4 and 6. If anyone could point me some points to look for it would be really helpful. Kind regards, Tharindu
Hi Tharindu, On Thu, Apr 30, 2009 at 12:21 PM, Tharindu Madushanka <[email protected]
wrote:
Hi, I was little bit busy this week with University just started for us. And I am having 3 days holiday from tomorrow. Therefore, I thought I use that time to fix JIRA issue on my work.
Steps Revised
1. Create a LoginListener class that implements EventListener can I use the package org.xwiki.observation.event to add this class ?
Yes, but it should be LoginEvent. You see we fire events through the ObservationManager and we implement EventListener only at the destination where we want to receive the event.
2. I do not need to create any DefaultObservationManager instance. components.xml file is defined with DefaultObservationManager implementation. I get reference to it by calling (Step 5) when ever nececary ObservationManager om = (ObservationManager) Utils.getComponent(ObservationManager.class);
Ummm, if you are already inside a component you should not use Utils.getComponent() method. Instead you should wire the observation manager via dependancy injection. But if you are working on some old code (xwiki-core and the like) you may use Utils.getComponent().
3. Define class UserLoginEvent similar to DocumentSaveEvent in package org.xwiki.observation.event
I think this is what i described above.
4. Need to find the place where I call the addListener() of ObservationManager with parameters - object of UserLoginEvent and reference to login listener. I think when user initiated login I need to add the listener. I looked at LoginAction class, but could not figure out a way.
I think the registration (against events) can happen from any place within your code. But I'm not esactly sure where to do this in your case. Better wait for sergiu's comment :)
6. Need to find when to call notify() of DefaultObjectManager instance when user successfully logged in
Yes.
7. Implement notify() in LoginListener
Ummm, inside listener you have to implement onEvent() do whatever you need to do when the event occurs.
I think I need to get familiar with com.xpn.xwiki.web package to learn how to do steps 4 and 6. If anyone could point me some points to look for it would be really helpful.
Remote debugging is your friend :) Thanks. - Asiri
Hi, I have defined the class UserLoginEvent in package org.xwiki.observation.event Then in order to be notified I think we could change the LoginSubmitAction class render() it the message is null we could notify the listener of a successful login. Index: xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java =================================================================== --- xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java (revision 19222) +++ xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java (working copy) @@ -22,14 +22,25 @@ import javax.servlet.http.HttpServletResponse; +import org.xwiki.observation.ObservationManager; +import org.xwiki.observation.event.UserLoginEvent; + import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWikiException; -public class LoginSubmitAction extends XWikiAction { - public String render(XWikiContext context) throws XWikiException { - String msg = (String)context.get("message"); - if(msg != null && !msg.trim().equals("")) { +public class LoginSubmitAction extends XWikiAction +{ + public String render(XWikiContext context) throws XWikiException + { + String msg = (String) context.get("message"); + if (msg != null && !msg.trim().equals("")) { context.getResponse().setStatus(HttpServletResponse.SC_FORBIDDEN); + } else { + ObservationManager om = (ObservationManager) Utils.getComponent(ObservationManager.class); + if (om != null) { + om.notify(new UserLoginEvent(context.getDoc().getWikiName() + ":" + context.getDoc().getFullName()), + context.getDoc(), context); + } } return "login"; } Next thing is I am still not sure is how the listener are added. In existing save and delete I could not find how it has added listeners. to OM.
Hi Tharindu, On Fri, May 1, 2009 at 8:05 AM, Tharindu Madushanka <[email protected]>wrote:
Hi, I have defined the class UserLoginEvent in package org.xwiki.observation.event
Then in order to be notified I think we could change the LoginSubmitAction class render() it the message is null we could notify the listener of a successful login.
Index: xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java =================================================================== --- xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java (revision 19222) +++ xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java (working copy) @@ -22,14 +22,25 @@
import javax.servlet.http.HttpServletResponse;
+import org.xwiki.observation.ObservationManager; +import org.xwiki.observation.event.UserLoginEvent; + import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWikiException;
-public class LoginSubmitAction extends XWikiAction { - public String render(XWikiContext context) throws XWikiException { - String msg = (String)context.get("message"); - if(msg != null && !msg.trim().equals("")) { +public class LoginSubmitAction extends XWikiAction +{ + public String render(XWikiContext context) throws XWikiException + { + String msg = (String) context.get("message"); + if (msg != null && !msg.trim().equals("")) {
context.getResponse().setStatus(HttpServletResponse.SC_FORBIDDEN); + } else { + ObservationManager om = (ObservationManager) Utils.getComponent(ObservationManager.class); + if (om != null) {
I don't think you need this check, if the component lookup fails, it will throw an exception.
+ om.notify(new UserLoginEvent(context.getDoc().getWikiName() + ":" + context.getDoc().getFullName()), + context.getDoc(), context);
UserLoginEvent, why does it require a document name in the constructor? And even if you need it, I think context.getDoc().getFullName() already prepends the wiki name (not sure). And why do you need other parameters in the notify() method? can't you simply use notify(Event event, Object source) ?
+ } } return "login"; }
Next thing is I am still not sure is how the listener are added. In existing save and delete I could not find how it has added listeners. to OM.
According to your JIRA issue: http://jira.xwiki.org/jira/browse/XWIKI-3697you don't need to worry about adding listeners. For an example, if I want to be notified when a user logs into the system, I will have to implement the EventListner inside my module and register it against OM, it's not upto you. But when you do implement the XMPP integration, you will have to register for this event :) I think all you need now is UserLogoutEvent, SessionCreatedEvent event and fire them at appropriate locations :) Thanks. - Asiri
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
Tharindu Madushanka wrote:
Hi, I have defined the class UserLoginEvent in package org.xwiki.observation.event
Then in order to be notified I think we could change the LoginSubmitAction class render() it the message is null we could notify the listener of a successful login.
Index: xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java =================================================================== --- xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java (revision 19222) +++ xwiki-core/src/main/java/com/xpn/xwiki/web/LoginSubmitAction.java (working copy) @@ -22,14 +22,25 @@
import javax.servlet.http.HttpServletResponse;
+import org.xwiki.observation.ObservationManager; +import org.xwiki.observation.event.UserLoginEvent; + import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWikiException;
-public class LoginSubmitAction extends XWikiAction { - public String render(XWikiContext context) throws XWikiException { - String msg = (String)context.get("message"); - if(msg != null && !msg.trim().equals("")) { +public class LoginSubmitAction extends XWikiAction +{ + public String render(XWikiContext context) throws XWikiException + { + String msg = (String) context.get("message"); + if (msg != null && !msg.trim().equals("")) {
context.getResponse().setStatus(HttpServletResponse.SC_FORBIDDEN); + } else { + ObservationManager om = (ObservationManager) Utils.getComponent(ObservationManager.class); + if (om != null) { + om.notify(new UserLoginEvent(context.getDoc().getWikiName() + ":" + context.getDoc().getFullName()),
The login event should contain the name of the user, not the document. Try context.getUser().
+ context.getDoc(), context); + } } return "login"; }
I need to test this to see if it is right. I guess it should be, but I'm not sure. -- Sergiu Dumitriu http://purl.org/net/sergiu/
Hi, I have been fixing JIRA issue XWIKI-3697 and finished two parts of JIRA issue http://jira.xwiki.org/jira/browse/XWIKI-3697 1. Sending Observation event for user successful login 2. Sending Observation event for user successful logout [TODO] 3. Writing Test cases for finished part - I think Writing selenium tests for xwiki tutorial would help 4. Sending observation when user create a new session Q - Sending observation when user create a new session Still I am not clear on how sessions are created and kept when user logs in. Can any one kindly point me some ways to look into this. Get more familiar with how user information and login information kept, etc with in xwiki
Tharindu Madushanka wrote:
1. Create a LoginListener class that implements EventListener can I use the package org.xwiki.observation.event to add this class ?
I don't think you need to create this class yet. Better leave it for when you will start working on the chat module. All you need to do for this task is to send the events, not receive them. When you will need to write the listener, it won't be in the org.xwiki.observation.event package, but in your own package (something like org.xwiki.chat.events.UserLoginListener).
2. I do not need to create any DefaultObservationManager instance. components.xml file is defined with DefaultObservationManager implementation. I get reference to it by calling (Step 5) when ever nececary ObservationManager om = (ObservationManager) Utils.getComponent(ObservationManager.class);
Step 2 depends on step 1, ignore it for the moment. But as Asiri said, you will probably get this as an automatic dependency injection, and not using the component manager.
3. Define class UserLoginEvent similar to DocumentSaveEvent in package org.xwiki.observation.event *[TODO]*
Yes.
4. Need to find the place where I call the addListener() of ObservationManager with parameters - object of UserLoginEvent and reference to login listener. I think when user initiated login I need to add the listener. I looked at LoginAction class, but could not figure out a way.
This also depends on step 1. Ignore it for the moment. If you will write your listener as a component, there is a special interface that components can implement, Initializable, which adds an initialize() method which will be called when the component is created and all its dependencies are in place.
6. Need to find when to call notify() of DefaultObjectManager instance when user successfully logged in
A first idea is xwiki-core/com.xpn.xwiki.web.LoginSubmitAction. This will be refined later, since users that have cookies use a different mechanism. But LoginSubmitAction is a pretty good initial approximation.
7. Implement notify() in LoginListener
Ignore.
I think I need to get familiar with com.xpn.xwiki.web package to learn how to do steps 4 and 6. If anyone could point me some points to look for it would be really helpful.
Only steps 3 and 6 should be implemented now. -- Sergiu Dumitriu http://purl.org/net/sergiu/
participants (3)
-
Asiri Rathnayake -
Sergiu Dumitriu -
Tharindu Madushanka