You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by ca...@apache.org on 2008/07/30 17:41:02 UTC
svn commit: r681083 - in /incubator/abdera/java/trunk/core/src:
main/java/org/apache/abdera/util/filter/ test/java/org/apache/abdera/util/
test/java/org/apache/abdera/util/filter/
Author: calavera
Date: Wed Jul 30 08:41:00 2008
New Revision: 681083
URL: http://svn.apache.org/viewvc?rev=681083&view=rev
Log:
[ABDERA-190]: serialization and concurrency updates to AbstractListParseFilter and AbstractParseFilter. Thanks to Nathan Beyer:
Serialization adjustments
* declare explicit serialVersionUID fields
* make Collection-based fields transient and implement custom read/write methods
Concurrency adjustments
* remove the Collections.synchronizedXXX types
* use synchronized blocks to wrap all access to the fields; this will increase the atomicity to the complete interactions with the fields, instead just each method call
Added:
incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/util/
incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/util/filter/
incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/util/filter/AbstractListParseFilterTest.java
Modified:
incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java
incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java
Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java?rev=681083&r1=681082&r2=681083&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractListParseFilter.java Wed Jul 30 08:41:00 2008
@@ -17,8 +17,10 @@
*/
package org.apache.abdera.util.filter;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -36,43 +38,103 @@
extends AbstractParseFilter
implements Cloneable, ListParseFilter {
- private final List<QName> qnames = Collections.synchronizedList(new ArrayList<QName>());
- private final Map<QName,List<QName>> attributes = Collections.synchronizedMap(new HashMap<QName,List<QName>>());
+ private static final long serialVersionUID = -758691949740569208L;
+
+ private transient List<QName> qnames = new ArrayList<QName>();
+ private transient Map<QName,List<QName>> attributes = new HashMap<QName,List<QName>>();
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public ListParseFilter add(QName qname) {
- if (!contains(qname)) qnames.add(qname);
+ synchronized (qnames) {
+ if (!contains(qname)) qnames.add(qname);
+ }
return this;
}
public boolean contains(QName qname) {
- return qnames.contains(qname);
+ synchronized (qnames) {
+ return qnames.contains(qname);
+ }
}
public ListParseFilter add(QName parent, QName attribute) {
- if (attributes.containsKey(parent)) {
- List<QName> attrs = attributes.get(parent);
- if (!attrs.contains(attribute)) attrs.add(attribute);
- } else {
- List<QName> attrs = new ArrayList<QName>();
- attrs.add(attribute);
- attributes.put(parent, attrs);
+ synchronized (attributes) {
+ if (attributes.containsKey(parent)) {
+ List<QName> attrs = attributes.get(parent);
+ if (!attrs.contains(attribute))
+ attrs.add(attribute);
+ } else {
+ List<QName> attrs = new ArrayList<QName>();
+ attrs.add(attribute);
+ attributes.put(parent, attrs);
+ }
}
return this;
}
public boolean contains(QName qname, QName attribute) {
- if (attributes.containsKey(qname)) {
- List<QName> attrs = attributes.get(qname);
- return attrs.contains(attribute);
- } else {
- return false;
+ synchronized (attributes) {
+ if (attributes.containsKey(qname)) {
+ List<QName> attrs = attributes.get(qname);
+ return attrs.contains(attribute);
+ } else {
+ return false;
+ }
}
}
public abstract boolean acceptable(QName qname);
public abstract boolean acceptable(QName qname, QName attribute);
+
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ out.defaultWriteObject();
+
+ // qnames field
+ assert qnames != null;
+ out.writeInt(qnames.size());
+ for (QName q : qnames) {
+ out.writeObject(q);
+ }
+
+ // attributes field
+ assert attributes != null;
+ out.writeInt(attributes.size());
+ for (Map.Entry<QName, List<QName>> e : attributes.entrySet()) {
+ out.writeObject(e.getKey());
+ final List<QName> v = e.getValue();
+ assert v != null;
+ out.writeInt(v.size());
+ for (QName q : v) {
+ out.writeObject(q);
+ }
+ }
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException,
+ ClassNotFoundException {
+ in.defaultReadObject();
+
+ // qnames field
+ final int qnamesSize = in.readInt();
+ qnames = new ArrayList<QName>(qnamesSize);
+ for (int i = 0; i < qnamesSize; i++) {
+ qnames.add((QName) in.readObject());
+ }
+
+ // attributes field
+ final int attributesSize = in.readInt();
+ attributes = new HashMap<QName, List<QName>>(attributesSize);
+ for (int i = 0; i < attributesSize; i++) {
+ final QName k = (QName) in.readObject();
+ final int vSize = in.readInt();
+ final List<QName> v = new ArrayList<QName>(vSize);
+ for (int j = 0; j < vSize; j++) {
+ v.add((QName) in.readObject());
+ }
+ attributes.put(k, v);
+ }
+ }
}
Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java?rev=681083&r1=681082&r2=681083&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/filter/AbstractParseFilter.java Wed Jul 30 08:41:00 2008
@@ -22,6 +22,8 @@
public abstract class AbstractParseFilter
implements ParseFilter {
+ private static final long serialVersionUID = -1866308276050148524L;
+
private static final byte COMMENTS = 1;
private static final byte WHITESPACE = 2;
private static final byte PI = 4;
Added: incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/util/filter/AbstractListParseFilterTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/util/filter/AbstractListParseFilterTest.java?rev=681083&view=auto
==============================================================================
--- incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/util/filter/AbstractListParseFilterTest.java (added)
+++ incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/util/filter/AbstractListParseFilterTest.java Wed Jul 30 08:41:00 2008
@@ -0,0 +1,66 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License. For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.util.filter;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import javax.xml.namespace.QName;
+
+import org.junit.Test;
+
+
+public class AbstractListParseFilterTest {
+
+ private static final class SerializedImpl extends AbstractListParseFilter {
+
+ private static final long serialVersionUID = -1695184231548373283L;
+
+ @Override
+ public boolean acceptable(QName qname, QName attribute) {
+ return true;
+ }
+
+ @Override
+ public boolean acceptable(QName qname) {
+ return true;
+ }
+
+ }
+
+ @Test
+ public void testSerialization() throws Exception {
+ SerializedImpl si = new SerializedImpl();
+ si.add(new QName("lp0"));
+ si.add(new QName("parentLp0"), new QName("lp1"));
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(si);
+ oos.flush();
+ ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
+ si = (SerializedImpl)ois.readObject();
+ assertTrue(si.contains(new QName("lp0")));
+ assertTrue(si.contains(new QName("parentLp0"), new QName("lp1")));
+ }
+
+
+}