diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2012-08-17 13:32:54 +0300 |
---|---|---|
committer | Johannes Dahlström <johannesd@vaadin.com> | 2012-08-17 13:32:54 +0300 |
commit | 47a0326c2fb2d6d8621e7a6fbfa2f011a48e15a4 (patch) | |
tree | 416685c3912f716eb09497cc7cce071786fe2d12 /server/src/com/vaadin/util/SerializerHelper.java | |
parent | 8e3aa0a9823556896f1af00599c3e79ca2ce2e01 (diff) | |
parent | 9bdbd7efbb7fa599910dc85182968334e4dced78 (diff) | |
download | vaadin-framework-47a0326c2fb2d6d8621e7a6fbfa2f011a48e15a4.tar.gz vaadin-framework-47a0326c2fb2d6d8621e7a6fbfa2f011a48e15a4.zip |
Merge branch 'master' into root-cleanup
Conflicts:
server/src/com/vaadin/terminal/DeploymentConfiguration.java
server/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java
server/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
server/src/com/vaadin/terminal/gwt/server/AbstractDeploymentConfiguration.java
Diffstat (limited to 'server/src/com/vaadin/util/SerializerHelper.java')
-rw-r--r-- | server/src/com/vaadin/util/SerializerHelper.java | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/server/src/com/vaadin/util/SerializerHelper.java b/server/src/com/vaadin/util/SerializerHelper.java new file mode 100644 index 0000000000..30b82794f4 --- /dev/null +++ b/server/src/com/vaadin/util/SerializerHelper.java @@ -0,0 +1,157 @@ +/* + * Copyright 2011 Vaadin Ltd. + * + * Licensed 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 com.vaadin.util; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +/** + * Helper class for performing serialization. Most of the methods are here are + * workarounds for problems in Google App Engine. Used internally by Vaadin and + * should not be used by application developers. Subject to change at any time. + * + * @since 6.0 + */ +public class SerializerHelper { + + /** + * Serializes the class reference so {@link #readClass(ObjectInputStream)} + * can deserialize it. Supports null class references. + * + * @param out + * The {@link ObjectOutputStream} to serialize to. + * @param cls + * A class or null. + * @throws IOException + * Rethrows any IOExceptions from the ObjectOutputStream + */ + public static void writeClass(ObjectOutputStream out, Class<?> cls) + throws IOException { + if (cls == null) { + out.writeObject(null); + } else { + out.writeObject(cls.getName()); + } + + } + + /** + * Serializes the class references so + * {@link #readClassArray(ObjectInputStream)} can deserialize it. Supports + * null class arrays. + * + * @param out + * The {@link ObjectOutputStream} to serialize to. + * @param classes + * An array containing class references or null. + * @throws IOException + * Rethrows any IOExceptions from the ObjectOutputStream + */ + public static void writeClassArray(ObjectOutputStream out, + Class<?>[] classes) throws IOException { + if (classes == null) { + out.writeObject(null); + } else { + String[] classNames = new String[classes.length]; + for (int i = 0; i < classes.length; i++) { + classNames[i] = classes[i].getName(); + } + out.writeObject(classNames); + } + } + + /** + * Deserializes a class references serialized by + * {@link #writeClassArray(ObjectOutputStream, Class[])}. Supports null + * class arrays. + * + * @param in + * {@link ObjectInputStream} to read from. + * @return Class array with the class references or null. + * @throws ClassNotFoundException + * If one of the classes could not be resolved. + * @throws IOException + * Rethrows IOExceptions from the ObjectInputStream + */ + public static Class<?>[] readClassArray(ObjectInputStream in) + throws ClassNotFoundException, IOException { + String[] classNames = (String[]) in.readObject(); + if (classNames == null) { + return null; + } + Class<?>[] classes = new Class<?>[classNames.length]; + for (int i = 0; i < classNames.length; i++) { + classes[i] = resolveClass(classNames[i]); + } + + return classes; + } + + /** + * List of primitive classes. Google App Engine has problems + * serializing/deserializing these (#3064). + */ + private static Class<?>[] primitiveClasses = new Class<?>[] { byte.class, + short.class, int.class, long.class, float.class, double.class, + boolean.class, char.class }; + + /** + * Resolves the class given by {@code className}. + * + * @param className + * The fully qualified class name. + * @return A {@code Class} reference. + * @throws ClassNotFoundException + * If the class could not be resolved. + */ + public static Class<?> resolveClass(String className) + throws ClassNotFoundException { + for (Class<?> c : primitiveClasses) { + if (className.equals(c.getName())) { + return c; + } + } + + return Class.forName(className); + } + + /** + * Deserializes a class reference serialized by + * {@link #writeClass(ObjectOutputStream, Class)}. Supports null class + * references. + * + * @param in + * {@code ObjectInputStream} to read from. + * @return Class reference to the resolved class + * @throws ClassNotFoundException + * If the class could not be resolved. + * @throws IOException + * Rethrows IOExceptions from the ObjectInputStream + */ + public static Class<?> readClass(ObjectInputStream in) throws IOException, + ClassNotFoundException { + String className = (String) in.readObject(); + if (className == null) { + return null; + } else { + return resolveClass(className); + + } + + } + +} |