Author: cziegeler Date: Mon Jan 3 10:14:32 2011 New Revision: 1054571 URL: http://svn.apache.org/viewvc?rev=1054571&view=rev Log: SLING-1913 : Implement own serialization/deserilization
Added: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java (with props) Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/PersistentResourceList.java sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java?rev=1054571&r1=1054570&r2=1054571&view=diff ============================================================================== --- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java (original) +++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java Mon Jan 3 10:14:32 2011 @@ -18,10 +18,10 @@ */ package org.apache.sling.installer.core.impl; +import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; -import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.SortedSet; @@ -36,24 +36,52 @@ import org.slf4j.LoggerFactory; */ public class EntityResourceList implements Serializable { - private static final long serialVersionUID = -1733426192525500065L; + /** Use own serial version ID as we control serialization. */ + private static final long serialVersionUID = 6L; + /** Serialization version. */ + private static final int VERSION = 1; + + /** Logger. */ private static final Logger LOGGER = LoggerFactory.getLogger(EntityResourceList.class); - private static final class ResourceComparator implements Comparator<RegisteredResource>, Serializable { - private static final long serialVersionUID = 3573107717574356088L; + /** The set of registered resources for this entity. */ + private final SortedSet<RegisteredResource> resources = new TreeSet<RegisteredResource>(); - public int compare(RegisteredResource o1, RegisteredResource o2) { - int result = o1.compareTo(o2); - if ( result == 0 ) { - result = o1.getURL().compareTo(o2.getURL()); - } - return result; + /** + * Serialize the object + * - write version id + * - serialize each entry in the resources list + * @param out Object output stream + * @throws IOException + */ + private void writeObject(final java.io.ObjectOutputStream out) + throws IOException { + out.writeInt(VERSION); + out.writeInt(resources.size()); + for(final RegisteredResource rr : this.resources) { + out.writeObject(rr); } } - private final SortedSet<RegisteredResource> resources = new TreeSet<RegisteredResource>( - new ResourceComparator()); + /** + * Deserialize the object + * - read version id + * - deserialize each entry in the resources list + */ + private void readObject(final java.io.ObjectInputStream in) + throws IOException, ClassNotFoundException { + final int version = in.readInt(); + if ( version != VERSION ) { + throw new ClassNotFoundException(this.getClass().getName()); + } + Util.setField(this, "resources", new TreeSet<RegisteredResource>()); + final int size = in.readInt(); + for(int i=0; i < size; i++) { + final RegisteredResource rr = (RegisteredResource)in.readObject(); + this.resources.add(rr); + } + } /** The resource list is empty if it contains no resources. */ public boolean isEmpty() { @@ -114,7 +142,7 @@ public class EntityResourceList implemen public void addOrUpdate(final RegisteredResource r) { LOGGER.debug("Adding new resource: {}", r); - // If an object with same sort key is already present, replace with the + // If an object with same url is already present, replace with the // new one which might have different attributes boolean first = true; for(final RegisteredResource rr : resources) { Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/PersistentResourceList.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/PersistentResourceList.java?rev=1054571&r1=1054570&r2=1054571&view=diff ============================================================================== --- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/PersistentResourceList.java (original) +++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/PersistentResourceList.java Mon Jan 3 10:14:32 2011 @@ -40,6 +40,9 @@ import org.slf4j.LoggerFactory; */ public class PersistentResourceList { + /** Serialization version. */ + private static final int VERSION = 1; + /** The logger */ private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -61,8 +64,13 @@ public class PersistentResourceList { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile))); - restoredData = (Map<String, EntityResourceList>)ois.readObject(); - logger.debug("Restored rsource list: {}", restoredData); + final int version = ois.readInt(); + if ( version == VERSION ) { + restoredData = (Map<String, EntityResourceList>)ois.readObject(); + } else { + logger.warn("Unknown version for persistent resource list: {}", version); + } + logger.debug("Restored resource list: {}", restoredData); } catch (final Exception e) { logger.warn("Unable to restore data, starting with empty list (" + e.getMessage() + ")", e); } finally { @@ -82,6 +90,7 @@ public class PersistentResourceList { try { final ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile))); try { + oos.writeInt(VERSION); oos.writeObject(data); logger.debug("Persisted resource list."); } finally { Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java?rev=1054571&r1=1054570&r2=1054571&view=diff ============================================================================== --- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java (original) +++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java Mon Jan 3 10:14:32 2011 @@ -56,34 +56,91 @@ import org.osgi.service.cm.Configuration public class RegisteredResourceImpl implements RegisteredResource, Serializable { + /** Use own serial version ID as we control serialization. */ private static final long serialVersionUID = 6L; + /** Serialization version. */ + private static final int VERSION = 1; + /** The resource url. */ private final String url; + /** The installer scheme. */ private final String urlScheme; + /** The digest for the resource. */ private final String digest; + /** The entity id. */ private final String entity; + /** The dictionary for configurations. */ private final Dictionary<String, Object> dictionary; + /** Additional attributes. */ private final Map<String, Object> attributes = new HashMap<String, Object>(); + private final File dataFile; - private final int priority; - /** Serial number to create unique file names in the data storage. */ - private static long serialNumberCounter = System.currentTimeMillis(); + private final int priority; private final String resourceType; + /** The current state of this resource. */ private State state = State.INSTALL; + /** Serial number to create unique file names in the data storage. */ + private static long serialNumberCounter = System.currentTimeMillis(); + /** Temporary attributes. */ private transient Map<String, Object> temporaryAttributes; /** + * Serialize the object + * - write version id + * - serialize each entry in the resources list + * @param out Object output stream + * @throws IOException + */ + private void writeObject(final java.io.ObjectOutputStream out) + throws IOException { + out.writeInt(VERSION); + out.writeObject(url); + out.writeObject(urlScheme); + out.writeObject(digest); + out.writeObject(entity); + out.writeObject(dictionary); + out.writeObject(attributes); + out.writeObject(dataFile); + out.writeObject(resourceType); + out.writeInt(priority); + out.writeObject(state); + } + + /** + * Deserialize the object + * - read version id + * - deserialize each entry in the resources list + */ + private void readObject(final java.io.ObjectInputStream in) + throws IOException, ClassNotFoundException { + final int version = in.readInt(); + if ( version != VERSION ) { + throw new ClassNotFoundException(this.getClass().getName()); + } + Util.setField(this, "url", in.readObject()); + Util.setField(this, "urlScheme", in.readObject()); + Util.setField(this, "digest", in.readObject()); + Util.setField(this, "entity", in.readObject()); + Util.setField(this, "dictionary", in.readObject()); + Util.setField(this, "attributes", in.readObject()); + Util.setField(this, "dataFile", in.readObject()); + Util.setField(this, "resourceType", in.readObject()); + Util.setField(this, "priority", in.readInt()); + this.state = (State) in.readObject(); + } + + /** * Try to create a registered resource. */ public static RegisteredResourceImpl create(final BundleContext ctx, @@ -243,7 +300,7 @@ public class RegisteredResourceImpl if (this.dataFile != null && this.dataFile.exists() ) { return new BufferedInputStream(new FileInputStream(this.dataFile)); } - return null; + return null; } /** @@ -402,17 +459,14 @@ public class RegisteredResourceImpl if ( ! (obj instanceof RegisteredResource) ) { return false; } - if ( compareTo((RegisteredResource)obj) != 0 ) { - return false; - } - return this.getURL().equals(((RegisteredResource)obj).getURL()); + return compareTo((RegisteredResource)obj) == 0; } /** * @see java.lang.Object#hashCode() */ public int hashCode() { - return this.entity.hashCode(); + return this.getURL().hashCode(); } /** @@ -445,6 +499,9 @@ public class RegisteredResourceImpl } } } + if ( result == 0 ) { + result = a.getURL().compareTo(b.getURL()); + } return result; } Added: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java?rev=1054571&view=auto ============================================================================== --- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java (added) +++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java Mon Jan 3 10:14:32 2011 @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. 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. + */ +package org.apache.sling.installer.core.impl; + +import java.io.IOException; +import java.lang.reflect.Field; + +public class Util { + + /** Set a (final) field during deserialization. */ + public static void setField(final Object obj, final String name, final Object value) + throws IOException { + try { + final Field field = obj.getClass().getDeclaredField(name); + if ( field == null ) { + throw new IOException("Field " + name + " not found in class " + obj.getClass()); + } + field.setAccessible(true); + field.set(obj, value); + } catch (final SecurityException e) { + throw (IOException)new IOException().initCause(e); + } catch (final NoSuchFieldException e) { + throw (IOException)new IOException().initCause(e); + } catch (final IllegalArgumentException e) { + throw (IOException)new IOException().initCause(e); + } catch (final IllegalAccessException e) { + throw (IOException)new IOException().initCause(e); + } + } +} Propchange: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/Util.java ------------------------------------------------------------------------------ svn:mime-type = text/plain