Author: nbelaevski
Date: 2008-10-16 14:03:22 -0400 (Thu, 16 Oct 2008)
New Revision: 10784
Added:
trunk/sandbox/ui/queue/src/main/java/org/richfaces/
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueDiscoveryVisitor.java
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueRegistry.java
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/UIQueue.java
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/QueueRenderer.java
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScript.java
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScriptResourceRenderer.java
trunk/sandbox/ui/queue/src/main/resources/
trunk/sandbox/ui/queue/src/main/resources/META-INF/
trunk/sandbox/ui/queue/src/main/resources/META-INF/services/
trunk/sandbox/ui/queue/src/main/resources/META-INF/services/org.richfaces.component.RenderPhaseComponentVisitor
trunk/sandbox/ui/queue/src/test/
trunk/sandbox/ui/queue/src/test/java/
trunk/sandbox/ui/queue/src/test/java/org/
trunk/sandbox/ui/queue/src/test/java/org/richfaces/
trunk/sandbox/ui/queue/src/test/java/org/richfaces/component/
trunk/sandbox/ui/queue/src/test/java/org/richfaces/component/QueueRegistryTest.java
Modified:
trunk/sandbox/samples/queue-sample/src/main/java/org/richfaces/Bean.java
trunk/sandbox/samples/queue-sample/src/main/webapp/WEB-INF/web.xml
trunk/sandbox/samples/queue-sample/src/main/webapp/pages/index.jsp
trunk/sandbox/ui/queue/src/main/config/component/queue.xml
Log:
Draft implementation of queues checked in
Modified: trunk/sandbox/samples/queue-sample/src/main/java/org/richfaces/Bean.java
===================================================================
--- trunk/sandbox/samples/queue-sample/src/main/java/org/richfaces/Bean.java 2008-10-16
17:37:26 UTC (rev 10783)
+++ trunk/sandbox/samples/queue-sample/src/main/java/org/richfaces/Bean.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -20,10 +20,59 @@
*/
package org.richfaces;
+
+import java.util.Map;
+
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+
+import org.ajax4jsf.event.AjaxEvent;
+
/**
* @author $Autor$
*
*/
public class Bean {
+ private long processRequestDelay = 300;
+
+ private static final String AJAX_REQUESTS_COUNT_ATTRIBUTE =
"ajaxRequestsCount";
+
+ public void setProcessRequestDelay(long processRequestDelay) {
+ this.processRequestDelay = processRequestDelay;
+ }
+
+ public long getProcessRequestDelay() {
+ return processRequestDelay;
+ }
+
+ public void processAction() {
+ if (processRequestDelay != 0) {
+ try {
+ Thread.sleep(processRequestDelay);
+ } catch (InterruptedException e) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ExternalContext externalContext = facesContext.getExternalContext();
+
+ externalContext.log(e.getMessage(), e);
+ }
+ }
+ }
+
+ public void resetAjaxCounter() {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ExternalContext externalContext = facesContext.getExternalContext();
+ externalContext.getSessionMap().put(AJAX_REQUESTS_COUNT_ATTRIBUTE, Long.valueOf(0));
+ }
+
+ public void processAjax(AjaxEvent event) {
+ ExternalContext externalContext =
FacesContext.getCurrentInstance().getExternalContext();
+ Map<String, Object> sessionMap = externalContext.getSessionMap();
+ Long count = (Long) sessionMap.get(AJAX_REQUESTS_COUNT_ATTRIBUTE);
+ if (count == null) {
+ count = Long.valueOf(0);
+ }
+
+ sessionMap.put(AJAX_REQUESTS_COUNT_ATTRIBUTE, ++count);
+ }
}
\ No newline at end of file
Modified: trunk/sandbox/samples/queue-sample/src/main/webapp/WEB-INF/web.xml
===================================================================
--- trunk/sandbox/samples/queue-sample/src/main/webapp/WEB-INF/web.xml 2008-10-16 17:37:26
UTC (rev 10783)
+++ trunk/sandbox/samples/queue-sample/src/main/webapp/WEB-INF/web.xml 2008-10-16 18:03:22
UTC (rev 10784)
@@ -10,6 +10,10 @@
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
+ <context-param>
+ <param-name>org.ajax4jsf.COMPRESS_SCRIPT</param-name>
+ <param-value>false</param-value>
+ </context-param>
<!--
-->
<filter>
Modified: trunk/sandbox/samples/queue-sample/src/main/webapp/pages/index.jsp
===================================================================
--- trunk/sandbox/samples/queue-sample/src/main/webapp/pages/index.jsp 2008-10-16 17:37:26
UTC (rev 10783)
+++ trunk/sandbox/samples/queue-sample/src/main/webapp/pages/index.jsp 2008-10-16 18:03:22
UTC (rev 10784)
@@ -8,14 +8,55 @@
</head>
<body>
<f:view>
+ <a4j:status startText="...running..." startStyle="color: green"
/>
+
+ <q:queue name="threeQueue" requestDelay="1000"
size="3" />
+ <q:queue name="singleQueue" requestDelay="1000"
size="1" />
+ <q:queue name="unlimitedQueue" requestDelay="1000"
size="-1" />
+
+ <br />
+ Queue settings: <br /><br />
+ threeQueue: requestDelay = 1000, size = 3 <br />
+ singleQueue: requestDelay = 1000, size = 1 <br />
+ unlimitedQueue: requestDelay = 1000, size = -1 <br />
+ <br />
+
<h:form>
- <q:queue name="queue" requestDelay="1000"/>
- <h:outputText id ="outputText"
value="Test"></h:outputText>
- <a4j:commandButton value="commandButton1"
reRender="outputText" eventsQueue="queue"/>
- <a4j:commandButton value="commandButton2"
reRender="outputText" eventsQueue="queue"/>
- <a4j:commandButton value="commandButton3"
reRender="outputText" eventsQueue="queue"/>
- <a4j:commandButton value="commandButton4"
reRender="outputText" eventsQueue="queue"/>
+ <a4j:outputPanel ajaxRendered="true" layout="inline">
+ <h:outputText value="#{empty ajaxRequestsCount ? 0 :
ajaxRequestsCount}" />
+ </a4j:outputPanel>
+
+  
+
+ <a4j:commandLink value="Reset ajax counter"
action="#{bean.resetAjaxCounter}" />
+ <br /><br />
+
+ <a4j:region ajaxListener="#{bean.processAjax}">
+
+ <h:outputLabel for="processRequestDelay" value="Process request
delay " />
+ <h:inputText id="processRequestDelay"
value="#{bean.processRequestDelay}">
+ <f:convertNumber />
+ </h:inputText>
+
+ <br />
+
+ <a4j:commandButton action="#{bean.processAction}" value="3
elements queue" reRender="outputText"
eventsQueue="threeQueue"/>
+ <a4j:commandButton action="#{bean.processAction}" value="3
elements queue/ignoreDupResponses" ignoreDupResponses="true"
reRender="outputText" eventsQueue="threeQueue"/>
+
+ <a4j:commandButton action="#{bean.processAction}" value="3
elements queue/requestDelay" requestDelay="600"
+ reRender="outputText" eventsQueue="threeQueue"/>
+
+ <a4j:commandButton action="#{bean.processAction}" value="3
elements queue/ignoreDupResponses/requestDelay" requestDelay="600"
+ ignoreDupResponses="true" reRender="outputText"
eventsQueue="threeQueue"/>
+
+ <br />
+
+ <a4j:commandButton action="#{bean.processAction}" value="single
element queue" reRender="outputText"
eventsQueue="singleQueue"/>
+ <a4j:commandButton action="#{bean.processAction}" value="unlimited
queue" reRender="outputText" eventsQueue="unlimitedQueue"/>
+ <a4j:commandButton action="#{bean.processAction}" value="global
queue" reRender="outputText" />
+ </a4j:region>
</h:form>
+ <a4j:log popup="false" />
</f:view>
</body>
</html>
Modified: trunk/sandbox/ui/queue/src/main/config/component/queue.xml
===================================================================
--- trunk/sandbox/ui/queue/src/main/config/component/queue.xml 2008-10-16 17:37:26 UTC
(rev 10783)
+++ trunk/sandbox/ui/queue/src/main/config/component/queue.xml 2008-10-16 18:03:22 UTC
(rev 10784)
@@ -5,49 +5,50 @@
<name>org.richfaces.Queue</name>
<family>org.richfaces.Queue</family>
<classname>org.richfaces.component.html.HtmlQueue</classname>
- <superclass>javax.faces.component.UIComponentBase</superclass>
+ <superclass>org.richfaces.component.UIQueue</superclass>
<description>
<![CDATA[The <rich:queue> tag.]]>
</description>
- <tag generate="false" bodyContent="empty">
+ <tag generate="true" bodyContent="empty">
<name>queue</name>
- <classname>org.ajax4jsf.taglib.html.jsp.QueueTag</classname>
+ <classname>org.richfaces.taglib.html.jsp.QueueTag</classname>
+ <superclass>org.ajax4jsf.webapp.taglib.HtmlComponentTagBase</superclass>
<test/>
</tag>
- <taghandler >
+ <renderer>
+ <name>org.richfaces.QueueRenderer</name>
+ <classname>org.richfaces.renderkit.html.QueueRenderer</classname>
+ </renderer>
+ <!--
+ <taghandler>
<classname>org.ajax4jsf.taglib.html.facelets.QueueHandler</classname>
</taghandler>
+ -->
<property>
<name>name</name>
<classname>java.lang.String</classname>
- <description>A name of this queue.</description>
+ <defaultvalue>GLOBAL_QUEUE_NAME</defaultvalue>
</property>
- <property >
+ <property>
<name>limitToList</name>
<classname>boolean</classname>
- <description>If "true", updates on client side ONLY elements
from this 'reRender' property. If "false" (default) updates all rendered
by ajax region components</description>
</property>
<property >
<name>requestDelay</name>
<classname>int</classname>
- <description>
- Attribute defines the time (in ms.) that the request will be wait in the queue before it
is ready to send.
- When the delay time is over, the request will be sent to the server or removed if the
newest 'similar' request is in a queue already
- </description>
</property>
<property>
<name>ignoreDupResponses</name>
<classname>boolean</classname>
- <description>
- Attribute allows to ignore an Ajax Response produced by a request if the newest
'similar' request is
- in a queue already. ignoreDupResponses="true" does not cancel the request
while it is processed on the server,
- but just allows to avoid unnecessary updates on the client side if the response
isn't actual now
- </description>
</property>
<property>
<name>timeout</name>
<classname>int</classname>
- <description>Response waiting time on a particular request. If a response is not
received during this time, the request is aborted</description>
</property>
+ <property>
+ <name>size</name>
+ <classname>int</classname>
+ <defaultvalue>1</defaultvalue>
+ </property>
</component>
</components>
Added:
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueDiscoveryVisitor.java
===================================================================
---
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueDiscoveryVisitor.java
(rev 0)
+++
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueDiscoveryVisitor.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1,98 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.component;
+
+import java.util.List;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+import javax.faces.event.PhaseEvent;
+
+import org.richfaces.event.RenderPhaseComponentVisitor;
+
+/**
+ * <p>This visitor is intended to build lookup table for queues.</p>
+ * <p>
+ * The reason to use visitor and not queue component renderer is the fact that queue
lookup table will be
+ * used by view components just after the start of view rendering. Render phase visitor
guarantees that component
+ * placed in component tree <i>before</i> the queue can lookup the queue when
necessary.
+ * </p>
+ * @author Nick Belaevski
+ * @since 3.3.0
+ */
+
+//TODO create light version of component tree visitor for view root only
+public class QueueDiscoveryVisitor implements RenderPhaseComponentVisitor {
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.event.RenderPhaseComponentVisitor#afterComponent(javax.faces.component.UIComponent,
javax.faces.event.PhaseEvent, java.lang.Object)
+ */
+ public void afterComponent(UIComponent component, PhaseEvent event,
+ Object state) {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * <p>
+ * Collects all queue components and registers them in queue registry.
+ * </p>
+ * <p>
+ * <b>Note:</b> assumes that queues can be used only as
<i>immediate</i> children of UIViewRoot
+ * </p>
+ * @see
org.richfaces.event.RenderPhaseComponentVisitor#afterRoot(javax.faces.event.PhaseEvent,
java.lang.Object)
+ */
+ public void afterRoot(PhaseEvent event, Object state) {
+ FacesContext context = event.getFacesContext();
+ UIViewRoot viewRoot = context.getViewRoot();
+
+ if (viewRoot.getChildCount() > 0) {
+ List<UIComponent> children = viewRoot.getChildren();
+ for (UIComponent component : children) {
+ if (component instanceof UIQueue) {
+ UIQueue queue = (UIQueue) component;
+
+ QueueRegistry.addQueue(context, queue);
+ }
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.event.RenderPhaseComponentVisitor#beforeComponent(javax.faces.component.UIComponent,
javax.faces.event.PhaseEvent, java.lang.Object)
+ */
+ public void beforeComponent(UIComponent component, PhaseEvent event,
+ Object state) {
+ // TODO Auto-generated method stub
+
+ }
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.event.RenderPhaseComponentVisitor#beforeRoot(javax.faces.event.PhaseEvent)
+ */
+ public Object beforeRoot(PhaseEvent event) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
Added: trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueRegistry.java
===================================================================
--- trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueRegistry.java
(rev 0)
+++
trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/QueueRegistry.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1,78 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+package org.richfaces.component;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.0
+ */
+public class QueueRegistry {
+
+ private static final String QUEUE_DISCOVERY_SERVICE_ATTRIBUTE_NAME =
QueueRegistry.class.getName();
+
+ public static void addQueue(FacesContext context, UIQueue queue) {
+ Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
+
+ Map<String, UIQueue> queuesMap = (Map<String, UIQueue>)
requestMap.get(QUEUE_DISCOVERY_SERVICE_ATTRIBUTE_NAME);
+ if (queuesMap == null) {
+ queuesMap = new HashMap<String, UIQueue>();
+
+ requestMap.put(QUEUE_DISCOVERY_SERVICE_ATTRIBUTE_NAME, queuesMap);
+ }
+
+ queuesMap.put(queue.getName(), queue);
+ }
+
+ public static UIQueue getQueue(FacesContext context, String name) {
+ UIQueue result = null;
+
+ Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
+
+ Map<String, UIQueue> queuesMap = (Map<String, UIQueue>)
requestMap.get(QUEUE_DISCOVERY_SERVICE_ATTRIBUTE_NAME);
+ if (queuesMap != null) {
+ result = queuesMap.get(name);
+ }
+
+ return result;
+ }
+
+ public static UIQueue[] getQueues(FacesContext context) {
+ UIQueue[] result = null;
+
+ Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
+
+ Map<String, UIQueue> queuesMap = (Map<String, UIQueue>)
requestMap.get(QUEUE_DISCOVERY_SERVICE_ATTRIBUTE_NAME);
+ if (queuesMap != null) {
+ Collection<UIQueue> queues = queuesMap.values();
+ result = queues.toArray(new UIQueue[queues.size()]);
+ }
+
+ return result;
+ }
+}
Added: trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/UIQueue.java
===================================================================
--- trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/UIQueue.java
(rev 0)
+++ trunk/sandbox/ui/queue/src/main/java/org/richfaces/component/UIQueue.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1,61 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.component;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIComponentBase;
+import javax.faces.component.UIViewRoot;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.0
+ */
+public abstract class UIQueue extends UIComponentBase {
+
+ public static final String COMPONENT_TYPE = "org.richfaces.Queue";
+
+ public static final String COMPONENT_FAMILY = "org.richfaces.Queue";
+
+ //That is used in JavaScript code also
+ public static final String GLOBAL_QUEUE_NAME = "org.richfaces.queue.global";
+
+ public abstract String getName();
+ public abstract void setName(String name);
+
+ public abstract int getSize();
+ public abstract void setSize(int size);
+
+ @Override
+ public String getFamily() {
+ return COMPONENT_FAMILY;
+ }
+
+ @Override
+ public void setParent(UIComponent parent) {
+ if (!(parent instanceof UIViewRoot)) {
+ throw new IllegalArgumentException("Queue component:" +
getClientId(getFacesContext()) +
+ " should have UIViewRoot as its immediate parent");
+ }
+
+ super.setParent(parent);
+ }
+}
Added:
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/QueueRenderer.java
===================================================================
--- trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/QueueRenderer.java
(rev 0)
+++
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/QueueRenderer.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1,61 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.renderkit.html;
+
+import javax.faces.component.UIComponent;
+
+import org.ajax4jsf.javascript.AjaxScript;
+import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
+import org.ajax4jsf.resource.InternetResource;
+import org.richfaces.component.UIQueue;
+import org.richfaces.renderkit.html.scripts.QueueScript;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.0
+ */
+public class QueueRenderer extends HeaderResourcesRendererBase {
+
+ private volatile InternetResource[] scripts;
+
+ @Override
+ protected InternetResource[] getScripts() {
+ if (scripts == null) {
+ synchronized (this) {
+ if (scripts == null) {
+ scripts = new InternetResource[] {
+ getResource(AjaxScript.class.getName()),
+ getResource(QueueScript.class.getName())
+ };
+ }
+ }
+ }
+
+ return scripts;
+ }
+
+ @Override
+ protected Class<? extends UIComponent> getComponentClass() {
+ return UIQueue.class;
+ }
+
+}
Added:
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScript.java
===================================================================
---
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScript.java
(rev 0)
+++
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScript.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1,52 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.renderkit.html.scripts;
+
+import org.ajax4jsf.resource.InternetResourceBase;
+import org.ajax4jsf.resource.ResourceContext;
+import org.ajax4jsf.resource.ResourceRenderer;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.0
+ */
+public class QueueScript extends InternetResourceBase {
+
+ private static final ResourceRenderer renderer = new QueueScriptResourceRenderer();
+
+ private String key = getClass().getName();
+
+ @Override
+ public String getKey() {
+ return key;
+ }
+
+ @Override
+ public ResourceRenderer getRenderer(ResourceContext resourceContext) {
+ return renderer;
+ }
+
+ @Override
+ public boolean isCacheable(ResourceContext resourceContext) {
+ return false;
+ }
+}
Added:
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScriptResourceRenderer.java
===================================================================
---
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScriptResourceRenderer.java
(rev 0)
+++
trunk/sandbox/ui/queue/src/main/java/org/richfaces/renderkit/html/scripts/QueueScriptResourceRenderer.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1,120 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+package org.richfaces.renderkit.html.scripts;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+
+import org.ajax4jsf.context.AjaxContext;
+import org.ajax4jsf.javascript.JSObject;
+import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.ajax4jsf.resource.BaseResourceRenderer;
+import org.ajax4jsf.resource.InternetResource;
+import org.richfaces.component.QueueRegistry;
+import org.richfaces.component.UIQueue;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.0
+ */
+public class QueueScriptResourceRenderer extends BaseResourceRenderer {
+
+ protected void doEncode(InternetResource resource, FacesContext context,
+ Object data, Map<String, Object> attributes) throws IOException {
+
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
+ if (!ajaxContext.isAjaxRequest(context)) {
+ Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
+
+ String resourceKey = resource.getKey();
+ if (requestMap.get(resourceKey) == null) {
+ requestMap.put(resourceKey, Boolean.TRUE);
+
+ UIQueue[] queues = QueueRegistry.getQueues(context);
+
+ if (queues != null && queues.length != 0) {
+ super.encode(resource, context, queues, attributes);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void encode(InternetResource resource, FacesContext context,
+ Object data, Map<String, Object> attributes) throws IOException {
+
+ doEncode(resource, context, data, attributes);
+ }
+
+ @Override
+ public void encode(InternetResource resource, FacesContext context,
+ Object data) throws IOException {
+
+ doEncode(resource, context, data, Collections.EMPTY_MAP);
+ }
+
+ @Override
+ public void encodeEnd(InternetResource resource,
+ FacesContext context, Object data) throws IOException {
+
+ ResponseWriter writer = context.getResponseWriter();
+ ExternalContext externalContext = context.getExternalContext();
+ UIQueue[] queues = (UIQueue[]) data;
+
+ for (UIQueue queue : queues) {
+ String name = externalContext.encodeNamespace(queue.getName());
+ int size = queue.getSize();
+
+ writer.writeText("A4J.AJAX.EventQueue.addQueue(", null);
+ writer.writeText(new JSObject("A4J.AJAX.EventQueue", name, size).toScript(),
null);
+ writer.writeText(");", null);
+ }
+
+ super.encodeEnd(resource, context, data);
+ }
+
+ @Override
+ protected String[][] getCommonAttrs() {
+ return new String[][]{{HTML.TYPE_ATTR, getContentType()}};
+ }
+
+ @Override
+ protected String getHrefAttr() {
+ return null;
+ }
+
+ @Override
+ protected String getTag() {
+ return HTML.SCRIPT_ELEM;
+ }
+
+ public String getContentType() {
+ return "text/javascript";
+ }
+
+}
Added:
trunk/sandbox/ui/queue/src/main/resources/META-INF/services/org.richfaces.component.RenderPhaseComponentVisitor
===================================================================
---
trunk/sandbox/ui/queue/src/main/resources/META-INF/services/org.richfaces.component.RenderPhaseComponentVisitor
(rev 0)
+++
trunk/sandbox/ui/queue/src/main/resources/META-INF/services/org.richfaces.component.RenderPhaseComponentVisitor 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1 @@
+org.richfaces.component.QueueDiscoveryVisitor
\ No newline at end of file
Added:
trunk/sandbox/ui/queue/src/test/java/org/richfaces/component/QueueRegistryTest.java
===================================================================
--- trunk/sandbox/ui/queue/src/test/java/org/richfaces/component/QueueRegistryTest.java
(rev 0)
+++
trunk/sandbox/ui/queue/src/test/java/org/richfaces/component/QueueRegistryTest.java 2008-10-16
18:03:22 UTC (rev 10784)
@@ -0,0 +1,87 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+package org.richfaces.component;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.0
+ */
+public class QueueRegistryTest extends AbstractAjax4JsfTestCase {
+
+ public QueueRegistryTest(String name) {
+ super(name);
+ }
+
+ public void testRegisterSingleQueue() throws Exception {
+ UIQueue[] queues = QueueRegistry.getQueues(facesContext);
+ assertNull(queues);
+
+ UIQueue queue = (UIQueue) application.createComponent(UIQueue.COMPONENT_TYPE);
+ queue.setName("testQueue");
+
+ QueueRegistry.addQueue(facesContext, queue);
+
+ assertSame(queue, QueueRegistry.getQueue(facesContext, "testQueue"));
+ assertNull(QueueRegistry.getQueue(facesContext, UIQueue.GLOBAL_QUEUE_NAME));
+ assertNull(QueueRegistry.getQueue(facesContext, "someOtherQueue"));
+
+ queues = QueueRegistry.getQueues(facesContext);
+
+ assertNotNull(queues);
+ assertEquals(1, queues.length);
+ assertSame(queue, queues[0]);
+ }
+
+ public void testRegisterQueues() throws Exception {
+ UIQueue[] queues = QueueRegistry.getQueues(facesContext);
+ assertNull(queues);
+
+ UIQueue queue = (UIQueue) application.createComponent(UIQueue.COMPONENT_TYPE);
+ queue.setName("testQueue");
+
+ UIQueue globalQueue = (UIQueue) application.createComponent(UIQueue.COMPONENT_TYPE);
+
+ QueueRegistry.addQueue(facesContext, queue);
+ QueueRegistry.addQueue(facesContext, globalQueue);
+
+ assertSame(queue, QueueRegistry.getQueue(facesContext, "testQueue"));
+ assertSame(globalQueue, QueueRegistry.getQueue(facesContext,
UIQueue.GLOBAL_QUEUE_NAME));
+ assertNull(QueueRegistry.getQueue(facesContext, "someOtherQueue"));
+
+ queues = QueueRegistry.getQueues(facesContext);
+
+ assertNotNull(queues);
+ assertEquals(2, queues.length);
+ assertTrue((queues[0] == queue && queues[1] == globalQueue) || (queues[0] ==
globalQueue && queues[1] == queue));
+ }
+
+ public void testEmptyQueue() throws Exception {
+ UIQueue[] queues = QueueRegistry.getQueues(facesContext);
+ assertNull(queues);
+
+ assertNull(QueueRegistry.getQueue(facesContext, "testQueue"));
+ assertNull(QueueRegistry.getQueue(facesContext, UIQueue.GLOBAL_QUEUE_NAME));
+ }
+}