Author: nbelaevski
Date: 2010-06-12 20:41:34 -0400 (Sat, 12 Jun 2010)
New Revision: 17621
Modified:
root/core/trunk/impl/src/main/java/org/richfaces/resource/AbstractBaseResource.java
root/core/trunk/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java
root/core/trunk/impl/src/main/java/org/richfaces/resource/UserResourceAdaptor.java
root/core/trunk/impl/src/main/java/org/richfaces/util/Util.java
Log:
Restored handling of StateHolder interface for resources
Modified:
root/core/trunk/impl/src/main/java/org/richfaces/resource/AbstractBaseResource.java
===================================================================
---
root/core/trunk/impl/src/main/java/org/richfaces/resource/AbstractBaseResource.java 2010-06-12
23:44:51 UTC (rev 17620)
+++
root/core/trunk/impl/src/main/java/org/richfaces/resource/AbstractBaseResource.java 2010-06-13
00:41:34 UTC (rev 17621)
@@ -21,8 +21,6 @@
package org.richfaces.resource;
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@@ -158,31 +156,7 @@
FacesContext context = FacesContext.getCurrentInstance();
ResourceCodec resourceCodec = ServiceTracker.getService(context,
ResourceCodec.class);
String resourceName = getResourceName();
- Object resourceData = null;
-
- if (this instanceof StateHolderResource) {
- StateHolderResource stateHolder = (StateHolderResource) this;
-
- if (!stateHolder.isTransient()) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream oos = null;
- try {
- oos = new DataOutputStream(baos);
- stateHolder.writeState(context, oos);
- } catch (IOException e) {
- throw new FacesException(e.getMessage(), e);
- } finally {
- try {
- oos.close();
- } catch (IOException e) {
- LOGGER.debug(e.getMessage(), e);
- }
- }
-
- resourceData = baos.toByteArray();
- }
- }
-
+ Object resourceData = Util.saveResourceState(context, this);
String resourceVersion = getResourceVersion();
String resourceUri = resourceCodec.encodeResource(context, resourceName,
resourceData, resourceVersion);
Modified:
root/core/trunk/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java
===================================================================
---
root/core/trunk/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java 2010-06-12
23:44:51 UTC (rev 17620)
+++
root/core/trunk/impl/src/main/java/org/richfaces/resource/ResourceHandlerImpl.java 2010-06-13
00:41:34 UTC (rev 17621)
@@ -21,8 +21,6 @@
package org.richfaces.resource;
-import java.io.ByteArrayInputStream;
-import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -240,37 +238,17 @@
}
}
- if (resource instanceof StateHolderResource) {
- StateHolderResource stateHolder = (StateHolderResource) resource;
- Object decodedData = resourceCodec.decodeResourceData(context,
resourcePath);
+ Object decodedData = resourceCodec.decodeResourceData(context,
resourcePath);
- if (LOGGER.isDebugEnabled()) {
- if (decodedData != null) {
- LOGGER.debug("Resource state data succesfully
decoded");
- } else {
- LOGGER.debug("Resource state data decoded as
null");
- }
- }
-
+ if (LOGGER.isDebugEnabled()) {
if (decodedData != null) {
- DataInputStream dis = null;
- try {
- dis = new DataInputStream(new ByteArrayInputStream((byte[])
decodedData));
- stateHolder.readState(context, dis);
- } finally {
- if (dis != null) {
- try {
- dis.close();
- } catch (IOException e) {
- LOGGER.debug(e.getMessage(), e);
- }
- }
- }
+ LOGGER.debug("Resource state data succesfully
decoded");
} else {
-
- // resource was transient and didn't store data
+ LOGGER.debug("Resource state data decoded as null");
}
}
+
+ Util.restoreResourceState(context, resource, decodedData);
if (resource instanceof AbstractCacheableResource) {
AbstractCacheableResource cacheableResource =
(AbstractCacheableResource) resource;
Modified:
root/core/trunk/impl/src/main/java/org/richfaces/resource/UserResourceAdaptor.java
===================================================================
---
root/core/trunk/impl/src/main/java/org/richfaces/resource/UserResourceAdaptor.java 2010-06-12
23:44:51 UTC (rev 17620)
+++
root/core/trunk/impl/src/main/java/org/richfaces/resource/UserResourceAdaptor.java 2010-06-13
00:41:34 UTC (rev 17621)
@@ -21,20 +21,21 @@
*/
package org.richfaces.resource;
-import java.io.DataInput;
-import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;
+import javax.faces.component.StateHolder;
import javax.faces.context.FacesContext;
+import org.richfaces.util.Util;
+
/**
* @author Nick Belaevski
*
*/
-public class UserResourceAdaptor extends AbstractCacheableResource implements
VersionedResource, StateHolderResource {
+public class UserResourceAdaptor extends AbstractCacheableResource implements
VersionedResource, StateHolder {
private UserResource userResource;
@@ -126,20 +127,26 @@
if (wrappedResource instanceof StateHolderResource) {
return ((StateHolderResource) wrappedResource).isTransient();
}
+
+ if (wrappedResource instanceof StateHolder) {
+ return ((StateHolder) wrappedResource).isTransient();
+ }
return true;
}
- public void writeState(FacesContext context, DataOutput dataOutput) throws
IOException {
- if (wrappedResource instanceof StateHolderResource) {
- ((StateHolderResource) wrappedResource).writeState(context, dataOutput);
+ public void setTransient(boolean newTransientValue) {
+ if (wrappedResource instanceof StateHolder) {
+ ((StateHolder) wrappedResource).setTransient(newTransientValue);
}
}
- public void readState(FacesContext context, DataInput dataInput) throws IOException
{
- if (wrappedResource instanceof StateHolderResource) {
- ((StateHolderResource) wrappedResource).readState(context, dataInput);
- }
+ public Object saveState(FacesContext context) {
+ return Util.saveResourceState(context, wrappedResource);
}
+ public void restoreState(FacesContext context, Object state) {
+ Util.restoreResourceState(context, wrappedResource, state);
+ }
+
}
Modified: root/core/trunk/impl/src/main/java/org/richfaces/util/Util.java
===================================================================
--- root/core/trunk/impl/src/main/java/org/richfaces/util/Util.java 2010-06-12 23:44:51
UTC (rev 17620)
+++ root/core/trunk/impl/src/main/java/org/richfaces/util/Util.java 2010-06-13 00:41:34
UTC (rev 17621)
@@ -43,6 +43,8 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
@@ -74,12 +76,14 @@
import javax.faces.FacesException;
import javax.faces.application.ViewHandler;
+import javax.faces.component.StateHolder;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.ajax4jsf.Messages;
import org.ajax4jsf.util.base64.Codec;
import org.richfaces.log.RichfacesLogger;
+import org.richfaces.resource.StateHolderResource;
import org.slf4j.Logger;
/**
@@ -548,4 +552,66 @@
return builder.toString();
}
+
+ public static Object saveResourceState(FacesContext context, Object resource) {
+ if (resource instanceof StateHolderResource) {
+ StateHolderResource stateHolderResource = (StateHolderResource) resource;
+ if (stateHolderResource.isTransient()) {
+ return null;
+ }
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DataOutputStream dos = new DataOutputStream(baos);
+ try {
+ stateHolderResource.writeState(context, dos);
+ } catch (IOException e) {
+ throw new FacesException(e.getMessage(), e);
+ } finally {
+ try {
+ dos.close();
+ } catch (IOException e) {
+ RESOURCE_LOGGER.debug(e.getMessage(), e);
+ }
+ }
+
+ return baos.toByteArray();
+ } else if (resource instanceof StateHolder) {
+ StateHolder stateHolder = (StateHolder) resource;
+ if (stateHolder.isTransient()) {
+ return null;
+ }
+
+ return stateHolder.saveState(context);
+ }
+
+ return null;
+ }
+
+ public static void restoreResourceState(FacesContext context, Object resource, Object
state) {
+ if (state == null) {
+ //transient resource hasn't provided any data
+ return;
+ }
+
+ if (resource instanceof StateHolderResource) {
+ StateHolderResource stateHolderResource = (StateHolderResource) resource;
+
+ ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) state);
+ DataInputStream dis = new DataInputStream(bais);
+ try {
+ stateHolderResource.readState(context, dis);
+ } catch (IOException e) {
+ throw new FacesException(e.getMessage(), e);
+ } finally {
+ try {
+ dis.close();
+ } catch (IOException e) {
+ RESOURCE_LOGGER.debug(e.getMessage(), e);
+ }
+ }
+ } else if (resource instanceof StateHolder) {
+ StateHolder stateHolder = (StateHolder) resource;
+ stateHolder.restoreState(context, state);
+ }
+ }
}