Author: julien_viet
Date: 2010-01-18 05:43:15 -0500 (Mon, 18 Jan 2010)
New Revision: 1346
Added:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/AbstractSerializablePageList.java
Removed:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java
Modified:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/EmptySerializablePageList.java
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/StatelessPageList.java
Log:
refactor the serializable page list stuff to have more reuse of code
Copied:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/AbstractSerializablePageList.java
(from rev 1343,
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java)
===================================================================
---
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/AbstractSerializablePageList.java
(rev 0)
+++
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/AbstractSerializablePageList.java 2010-01-18
10:43:15 UTC (rev 1346)
@@ -0,0 +1,249 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import java.io.IOException;
+import java.io.InvalidObjectException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Field;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.util.List;
+
+/**
+ * <p>This class defines the common functionalities for the serializable subclasses
of {@link PageList}. Note
+ * that itself it does not implement the {@link java.io.Serializable} interface for the
reason that it needs to
+ * define a no arg constructor in a non serializble class (serialization
constraint).</p>
+ *
+ * <p>The method defines an abstract {@link #connect()} method that is used to
connect the the page list to the
+ * underlying data.</p>
+ *
+ * <p>The methods {@link #readState(java.io.ObjectInputStream)} and {@link
#writeState(java.io.ObjectOutputStream)}
+ * are defined for the subclasses and should be called by <code>void
readObject(ObjectInputStream in)</code> and
+ * <code>void writeObject(ObjectOutputStream out)</code> custom serialization
protocol.</p>
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public abstract class AbstractSerializablePageList<E> extends PageList<E>
+{
+
+ /** . */
+ private static final Field pageSizeField;
+
+ static
+ {
+ try
+ {
+ pageSizeField = PageList.class.getDeclaredField("pageSize_");
+ pageSizeField.setAccessible(true);
+ }
+ catch (NoSuchFieldException e)
+ {
+ throw new Error(e);
+ }
+ }
+
+ /**
+ * Builds a page list.
+ *
+ * @param pageSize the page size
+ */
+ protected AbstractSerializablePageList(int pageSize)
+ {
+ super(pageSize);
+ }
+
+ /**
+ * This constructor should not be used by subclasses, it is only for serialization
needs.
+ */
+ protected AbstractSerializablePageList()
+ {
+ super(10);
+ }
+
+ /** . */
+ private LazyList<E> lazyList;
+
+ protected abstract ListAccess<E> connect() throws Exception;
+
+ private void ensureCorrectState()
+ {
+ if (lazyList == null)
+ {
+ try
+ {
+ lazyList = new LazyList<E>(connect(), super.getPageSize());
+ }
+ catch (Exception e)
+ {
+ throw new UndeclaredThrowableException(e);
+ }
+
+ // Save temporarily the current page
+ int currentPage = currentPage_;
+
+ // Refresh state
+ super.setAvailablePage(lazyList.size());
+
+ // Put back current page that was written by previous method call
+ if (currentPage != -1)
+ {
+ currentPage_ = currentPage;
+ }
+ }
+ }
+
+ @Override
+ protected final void populateCurrentPage(int page) throws Exception
+ {
+ // Make sure we have correct state
+ ensureCorrectState();
+
+ //
+ int from = getFrom();
+ int to = getTo();
+ currentListPage_ = lazyList.subList(from, to);
+ }
+
+ @Override
+ public final List<E> getAll()
+ {
+ ensureCorrectState();
+
+ //
+ return lazyList;
+ }
+
+ // Serialization : should be used by subclasses
+
+ protected final void writeState(ObjectOutputStream out) throws IOException
+ {
+ int pageSize;
+ try
+ {
+ pageSize = pageSizeField.getInt(this);
+ }
+ catch (IllegalAccessException e)
+ {
+ InvalidObjectException ioe = new InvalidObjectException("Cannot set page
size");
+ ioe.initCause(e);
+ throw ioe;
+ }
+
+ //
+ out.writeInt(pageSize);
+ out.writeInt(currentPage_);
+ }
+
+ protected final void readState(ObjectInputStream in) throws IOException,
ClassNotFoundException
+ {
+ try
+ {
+ pageSizeField.setInt(this, in.readInt());
+ }
+ catch (IllegalAccessException e)
+ {
+ InvalidObjectException ioe = new InvalidObjectException("Cannot set page
size");
+ ioe.initCause(e);
+ throw ioe;
+ }
+ currentPage_ = in.readInt();
+ }
+
+ // Intercept all method calls
+
+ @Override
+ public final int getAvailablePage()
+ {
+ ensureCorrectState();
+ return super.getAvailablePage();
+ }
+
+ @Override
+ public final int getTo()
+ {
+ ensureCorrectState();
+ return super.getTo();
+ }
+
+ @Override
+ public final int getFrom()
+ {
+ ensureCorrectState();
+ return super.getFrom();
+ }
+
+ @Override
+ protected final void setAvailablePage(int available)
+ {
+ ensureCorrectState();
+ super.setAvailablePage(available);
+ }
+
+ @Override
+ protected final void checkAndSetPage(int page) throws Exception
+ {
+ ensureCorrectState();
+ super.checkAndSetPage(page);
+ }
+
+ @Override
+ public final List<E> getPage(int page) throws Exception
+ {
+ ensureCorrectState();
+ return super.getPage(page);
+ }
+
+ @Override
+ public final List<E> currentPage() throws Exception
+ {
+ ensureCorrectState();
+ return super.currentPage();
+ }
+
+ @Override
+ public final int getAvailable()
+ {
+ ensureCorrectState();
+ return super.getAvailable();
+ }
+
+ @Override
+ public final int getCurrentPage()
+ {
+ ensureCorrectState();
+ return super.getCurrentPage();
+ }
+
+ @Override
+ public final void setPageSize(int pageSize)
+ {
+ ensureCorrectState();
+ super.setPageSize(pageSize);
+ }
+
+ @Override
+ public final int getPageSize()
+ {
+ ensureCorrectState();
+ return super.getPageSize();
+ }
+}
Modified:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/EmptySerializablePageList.java
===================================================================
---
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/EmptySerializablePageList.java 2010-01-18
10:29:43 UTC (rev 1345)
+++
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/EmptySerializablePageList.java 2010-01-18
10:43:15 UTC (rev 1346)
@@ -20,36 +20,41 @@
package org.exoplatform.commons.utils;
import java.io.Serializable;
-import java.util.Collections;
-import java.util.List;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
-public class EmptySerializablePageList<E> extends NoArgConstructorPageList<E>
implements Serializable
+public class EmptySerializablePageList<E> extends
AbstractSerializablePageList<E> implements Serializable
{
/** . */
private static final EmptySerializablePageList instance = new
EmptySerializablePageList();
+ /** . */
+ private final ListAccess<E> listAccess = new ListAccess<E>()
+ {
+ public E[] load(int index, int length) throws Exception, IllegalArgumentException
+ {
+ throw new UnsupportedOperationException("Should not be called");
+ }
+ public int getSize() throws Exception
+ {
+ return 0;
+ }
+ };
+
public EmptySerializablePageList()
{
super(10);
}
@Override
- protected void populateCurrentPage(int page) throws Exception
+ protected ListAccess<E> connect() throws Exception
{
- currentListPage_ = Collections.emptyList();
+ return listAccess;
}
- @Override
- public List<E> getAll() throws Exception
- {
- return Collections.emptyList();
- }
-
public static <E> PageList<E> get()
{
// Cast OK
Deleted:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java
===================================================================
---
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java 2010-01-18
10:29:43 UTC (rev 1345)
+++
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/NoArgConstructorPageList.java 2010-01-18
10:43:15 UTC (rev 1346)
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software 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 software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.exoplatform.commons.utils;
-
-/**
- * This class is required for the {@link org.exoplatform.commons.utils.PageListAccess}
class to be serializable.
- *
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public abstract class NoArgConstructorPageList<E> extends PageList<E>
-{
-
- protected NoArgConstructorPageList(int pageSize)
- {
- super(pageSize);
- }
-
- protected NoArgConstructorPageList()
- {
- super(10);
- }
-}
Modified:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java
===================================================================
---
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java 2010-01-18
10:29:43 UTC (rev 1345)
+++
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/PageListAccess.java 2010-01-18
10:43:15 UTC (rev 1346)
@@ -20,38 +20,17 @@
package org.exoplatform.commons.utils;
import java.io.*;
-import java.lang.reflect.Field;
-import java.util.List;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
-public abstract class PageListAccess<E, S extends Serializable> extends
NoArgConstructorPageList<E> implements Serializable
+public abstract class PageListAccess<E, S extends Serializable> extends
AbstractSerializablePageList<E> implements Serializable
{
- /** . */
- private static final Field pageSizeField;
-
- static
- {
- try
- {
- pageSizeField = PageList.class.getDeclaredField("pageSize_");
- pageSizeField.setAccessible(true);
- }
- catch (NoSuchFieldException e)
- {
- throw new Error(e);
- }
- }
-
/** The state that recreates the list. */
private S state;
- /** . */
- private LazyList<E> lazyList;
-
protected PageListAccess(S state, int pageSize)
{
super(pageSize);
@@ -68,167 +47,27 @@
//
this.state = state;
- this.lazyList = null;
}
- private void ensureCorrectState()
- {
- if (lazyList == null)
- {
- lazyList = new LazyList<E>(create(state), super.getPageSize());
-
- // Save temporarily the current page
- int currentPage = currentPage_;
-
- // Refresh state
- super.setAvailablePage(lazyList.size());
-
- // Put back current page that was written by previous method call
- if (currentPage != -1)
- {
- currentPage_ = currentPage;
- }
- }
- }
-
-
@Override
- protected final void populateCurrentPage(int page) throws Exception
+ protected ListAccess<E> connect() throws Exception
{
- // Make sure we have correct state
- ensureCorrectState();
-
- //
- int from = getFrom();
- int to = getTo();
- currentListPage_ = lazyList.subList(from, to);
+ return create(state);
}
- @Override
- public final List<E> getAll()
- {
- ensureCorrectState();
-
- //
- return lazyList;
- }
-
protected abstract ListAccess<E> create(S state);
// Serialization
private void writeObject(ObjectOutputStream out) throws IOException
{
- int pageSize;
- try
- {
- pageSize = pageSizeField.getInt(this);
- }
- catch (IllegalAccessException e)
- {
- InvalidObjectException ioe = new InvalidObjectException("Cannot set page
size");
- ioe.initCause(e);
- throw ioe;
- }
-
- //
- out.writeInt(pageSize);
- out.writeInt(currentPage_);
+ super.writeState(out);
out.writeObject(state);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException
{
- try
- {
- pageSizeField.setInt(this, in.readInt());
- }
- catch (IllegalAccessException e)
- {
- InvalidObjectException ioe = new InvalidObjectException("Cannot set page
size");
- ioe.initCause(e);
- throw ioe;
- }
- currentPage_ = in.readInt();
+ super.readState(in);
state = (S)in.readObject();
}
-
- // Intercept all method calls
-
- @Override
- public final int getAvailablePage()
- {
- ensureCorrectState();
- return super.getAvailablePage();
- }
-
- @Override
- public final int getTo()
- {
- ensureCorrectState();
- return super.getTo();
- }
-
- @Override
- public final int getFrom()
- {
- ensureCorrectState();
- return super.getFrom();
- }
-
- @Override
- protected final void setAvailablePage(int available)
- {
- ensureCorrectState();
- super.setAvailablePage(available);
- }
-
- @Override
- protected final void checkAndSetPage(int page) throws Exception
- {
- ensureCorrectState();
- super.checkAndSetPage(page);
- }
-
- @Override
- public final List<E> getPage(int page) throws Exception
- {
- ensureCorrectState();
- return super.getPage(page);
- }
-
- @Override
- public final List<E> currentPage() throws Exception
- {
- ensureCorrectState();
- return super.currentPage();
- }
-
- @Override
- public final int getAvailable()
- {
- ensureCorrectState();
- return super.getAvailable();
- }
-
- @Override
- public final int getCurrentPage()
- {
- ensureCorrectState();
- return super.getCurrentPage();
- }
-
- @Override
- public final void setPageSize(int pageSize)
- {
- ensureCorrectState();
- super.setPageSize(pageSize);
- }
-
- @Override
- public final int getPageSize()
- {
- ensureCorrectState();
- return super.getPageSize();
- }
}
Modified:
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/StatelessPageList.java
===================================================================
---
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/StatelessPageList.java 2010-01-18
10:29:43 UTC (rev 1345)
+++
portal/trunk/component/common/src/main/java/org/exoplatform/commons/utils/StatelessPageList.java 2010-01-18
10:43:15 UTC (rev 1346)
@@ -20,217 +20,29 @@
package org.exoplatform.commons.utils;
import java.io.*;
-import java.lang.reflect.Field;
-import java.lang.reflect.UndeclaredThrowableException;
-import java.util.List;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
-public abstract class StatelessPageList<E> extends
NoArgConstructorPageList<E> implements Serializable
+public abstract class StatelessPageList<E> extends
AbstractSerializablePageList<E> implements Serializable
{
-
- /** . */
- private static final Field pageSizeField;
-
- static
- {
- try
- {
- pageSizeField = PageList.class.getDeclaredField("pageSize_");
- pageSizeField.setAccessible(true);
- }
- catch (NoSuchFieldException e)
- {
- throw new Error(e);
- }
- }
-
- /** . */
- private LazyList<E> lazyList;
-
protected StatelessPageList(int pageSize)
{
super(pageSize);
-
- //
- if (pageSize < 0)
- {
- throw new IllegalArgumentException();
- }
-
- //
- this.lazyList = null;
}
- private void ensureCorrectState()
- {
- if (lazyList == null)
- {
- ListAccess<E> listAccess;
- try
- {
- listAccess = connect();
- }
- catch (Exception e)
- {
- throw new UndeclaredThrowableException(e, "Cannot connect to list access
");
- }
-
- //
- lazyList = new LazyList<E>(listAccess, super.getPageSize());
-
- // Save temporarily the current page
- int currentPage = currentPage_;
-
- // Refresh state
- super.setAvailablePage(lazyList.size());
-
- // Put back current page that was written by previous method call
- if (currentPage != -1)
- {
- currentPage_ = currentPage;
- }
- }
- }
-
-
- @Override
- protected final void populateCurrentPage(int page) throws Exception
- {
- // Make sure we have correct state
- ensureCorrectState();
-
- //
- int from = getFrom();
- int to = getTo();
- currentListPage_ = lazyList.subList(from, to);
- }
-
- @Override
- public final List<E> getAll()
- {
- ensureCorrectState();
-
- //
- return lazyList;
- }
-
protected abstract ListAccess<E> connect() throws Exception;
// Serialization
private void writeObject(ObjectOutputStream out) throws IOException
{
- int pageSize;
- try
- {
- pageSize = pageSizeField.getInt(this);
- }
- catch (IllegalAccessException e)
- {
- InvalidObjectException ioe = new InvalidObjectException("Cannot set page
size");
- ioe.initCause(e);
- throw ioe;
- }
-
- //
- out.writeInt(pageSize);
- out.writeInt(currentPage_);
+ super.writeState(out);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException
{
- try
- {
- pageSizeField.setInt(this, in.readInt());
- }
- catch (IllegalAccessException e)
- {
- InvalidObjectException ioe = new InvalidObjectException("Cannot set page
size");
- ioe.initCause(e);
- throw ioe;
- }
- currentPage_ = in.readInt();
+ super.readState(in);
}
-
- // Intercept all method calls
-
- @Override
- public final int getAvailablePage()
- {
- ensureCorrectState();
- return super.getAvailablePage();
- }
-
- @Override
- public final int getTo()
- {
- ensureCorrectState();
- return super.getTo();
- }
-
- @Override
- public final int getFrom()
- {
- ensureCorrectState();
- return super.getFrom();
- }
-
- @Override
- protected final void setAvailablePage(int available)
- {
- ensureCorrectState();
- super.setAvailablePage(available);
- }
-
- @Override
- protected final void checkAndSetPage(int page) throws Exception
- {
- ensureCorrectState();
- super.checkAndSetPage(page);
- }
-
- @Override
- public final List<E> getPage(int page) throws Exception
- {
- ensureCorrectState();
- return super.getPage(page);
- }
-
- @Override
- public final List<E> currentPage() throws Exception
- {
- ensureCorrectState();
- return super.currentPage();
- }
-
- @Override
- public final int getAvailable()
- {
- ensureCorrectState();
- return super.getAvailable();
- }
-
- @Override
- public final int getCurrentPage()
- {
- ensureCorrectState();
- return super.getCurrentPage();
- }
-
- @Override
- public final void setPageSize(int pageSize)
- {
- ensureCorrectState();
- super.setPageSize(pageSize);
- }
-
- @Override
- public final int getPageSize()
- {
- ensureCorrectState();
- return super.getPageSize();
- }
}
\ No newline at end of file