You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by Bernhard Hoisl <be...@wu.ac.at> on 2009/12/23 14:09:06 UTC

Wookie Sibling Rules

Hi,

as I need to have inter widget communication in a slightly different 
manner than the yet implemented sibling rules, can someone point me to 
the Java classes and methods where these sibling rules are defined and 
can be changed? I want to have a look at them to understand the 
behaviour fully.

Thanks,
Bernhard


Re: Wookie Sibling Rules

Posted by Scott Wilson <sc...@gmail.com>.
On 24 Dec 2009, at 11:41, Ross Gardler wrote:

> On 23/12/2009 13:09, Bernhard Hoisl wrote:
>> Hi,
>>
>> as I need to have inter widget communication in a slightly different
>> manner than the yet implemented sibling rules, can someone point me  
>> to
>> the Java classes and methods where these sibling rules are defined  
>> and
>> can be changed? I want to have a look at them to understand the
>> behaviour fully.
>
> I'm not familiar with the internals of Wookie yet, but a search of  
> the source files points to:
>
> package org.apache.wookie.helpers;
...
> public class Notifier {
>
> I'm not sure if that is what you are looking for or not, but it's a  
> good starting point.
>
> I'm sure someone with a more intimate knowledge of the code base  
> will be around at some point during the holidays.

I'm back! (sort of)

The starting information you need is here:

http://mail-archives.apache.org/mod_mbox/incubator-wookie-dev/200911.mbox/%3c1063DDC4-2D2D-4283-B6AA-E61507DDAE34@gmail.com%3e

In the patch for WOOKIE-65 (notifications-patch2.txt) there is already  
a stub for this functionality in the patched Notifier class:

+	/**
+	 * Calls a script in all sibling widget instances within the scope  
of the current DWR thread (all widgets with same origin URL)
+	 * TODO this does not currently work as we don't set the attribute  
in the ScriptSession. I've included the code
+	 * here as the starting point for anyone wanting to implement  
features using single-user cross-widget messaging
+	 * @param call the JS method to call on the widget start file
+	 */
+	/*
+	public static void callSiblingsByUser(WidgetInstance instance,  
String call){
+		WebContext wctx = WebContextFactory.get();
+        ScriptBuffer script = new ScriptBuffer();
+        script.appendScript(call);
+        // Loop over all the users on the current page
+        Collection<?> pages = wctx.getAllScriptSessions();
+        for (Iterator<?> it = pages.iterator(); it.hasNext();){
+            ScriptSession otherSession = (ScriptSession) it.next();
+            WidgetInstance otherInstance =  
WidgetInstance.findByIdKey((String)otherSession.getAttribute("idkey"));
+            if  
(otherInstance.getApiKey().equals(instance.getApiKey()) &&  
otherInstance.getUserId().equals(instance.getUserId()))
+            	otherSession.addScript(script);
+        }	
+	}

To make this function you would need to:

1. Apply the patch
2. Uncomment the Notifier.callSiblingsByUser method
3. Create a new Feature impl that sets the session key (see below for  
an example)
4. Add the Feature methods to dwr.xml
5. Debug, debug, debug
6. Submit the working code as a new patch!

public class Messaging implements IFeature {

	// I've not tested this code at all, its just an idea to get you  
started.
	// a widget would call:
	// messaging.startSession(Widget.instanceid_key);
	// and then:
	// messaging.sendMessage(Widget.instanceid_key, "hello world");
	// and implement:
	// this.onMessageReceived = function(msg){alert(msg});

	/* (non-Javadoc)
	 * @see org.apache.wookie.feature.IFeature#getJavaScriptImpl()
	 */
	public String getJavaScriptImpl() {
		// this JS code is auto-generated when you add methods in this class  
to dwr.xml
		return "/wookie/dwr/interface/Messaging.js";
	}

	/* (non-Javadoc)
	 * @see org.apache.wookie.feature.IFeature#getJavaScriptWrapper()
	 */
	public String getJavaScriptWrapper() {
		// this is where you could declare a "nice" API for widget authors,  
e.g. add id_key automatically
		// see Wookie-wrapper for examples
		// return "/wookie/shared/js/messaging.js";
		return null;
	}

	public String startSession(String id_key){
		HttpServletRequest request =  
WebContextFactory.get().getHttpServletRequest();
		Messages localizedMessages = LocaleHandler.localizeMessages(request);
		WidgetInstance widgetInstance = WidgetInstance.findByIdKey(id_key);
		if(widgetInstance == null) return  
localizedMessages.getString("WidgetAPIImpl.0");
		WebContext wctx = WebContextFactory.get();
		wctx.getScriptSession().setAttribute("idkey", id_key);
		return "okay"; //$NON-NLS-1$
	}
	
	public String sendMessage(String id_key, String message){
		HttpServletRequest request =  
WebContextFactory.get().getHttpServletRequest();
		Messages localizedMessages = LocaleHandler.localizeMessages(request);
		WidgetInstance widgetInstance = WidgetInstance.findByIdKey(id_key);
		if(widgetInstance == null) return  
localizedMessages.getString("WidgetAPIImpl.0");
		if(widgetInstance.isLocked()) return  
localizedMessages.getString("WidgetAPIImpl.2");
		String call = "onMessageReceived(\""+message+"\")";
		Notifier.callSiblingsByUser(widgetInstance, call);
		return "okay"; //$NON-NLS-1$
	}

There are other kinds of sibling rules that could be implemented, but  
basically the Notifier is the place to look once the patch is applied.  
You need to define a method for comparing sessions to identify a  
common key or property. This is done for shared data keys using the  
SiblingPageNormalizer class - for other rules you need to write  
something that either uses a session key (as in the example above) or  
some other characteristics of the URL or widget instance to determine  
which script sessions to invoke.

- S

Re: Wookie Sibling Rules

Posted by Ross Gardler <rg...@apache.org>.
On 24/12/2009 11:41, Ross Gardler wrote:
> On 23/12/2009 13:09, Bernhard Hoisl wrote:
>> Hi,
>>
>> as I need to have inter widget communication in a slightly different
>> manner than the yet implemented sibling rules, can someone point me to
>> the Java classes and methods where these sibling rules are defined and
>> can be changed? I want to have a look at them to understand the
>> behaviour fully.
>
> I'm not familiar with the internals of Wookie yet, but a search of the
> source files points to:

I also noticed an issue in te issue tracker with a patch set that needs 
reviewing: https://issues.apache.org/jira/browse/WOOKIE-65

Ross


>
> package org.apache.wookie.helpers;
>
>
> /**
> * Propagates server-driven events out to sibling widget instances
> * TODO only propagate to real siblings - at moment tells too many instances
> * @author scott
> *
> */
> public class Notifier {
>
> I'm not sure if that is what you are looking for or not, but it's a good
> starting point.
>
> I'm sure someone with a more intimate knowledge of the code base will be
> around at some point during the holidays.
>
> Ross


Re: Wookie Sibling Rules

Posted by Ross Gardler <rg...@apache.org>.
On 23/12/2009 13:09, Bernhard Hoisl wrote:
> Hi,
>
> as I need to have inter widget communication in a slightly different
> manner than the yet implemented sibling rules, can someone point me to
> the Java classes and methods where these sibling rules are defined and
> can be changed? I want to have a look at them to understand the
> behaviour fully.

I'm not familiar with the internals of Wookie yet, but a search of the 
source files points to:

package org.apache.wookie.helpers;


/**
  * Propagates server-driven events out to sibling widget instances
  * TODO only propagate to real siblings - at moment tells too many 
instances
  * @author scott
  *
  */
public class Notifier {

I'm not sure if that is what you are looking for or not, but it's a good 
starting point.

I'm sure someone with a more intimate knowledge of the code base will be 
around at some point during the holidays.

Ross