001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package de.kumpe.hadooptimizer.hadoop;
020
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.ObjectInputStream;
024 import java.io.ObjectOutputStream;
025 import java.io.ObjectStreamClass;
026 import java.io.OutputStream;
027 import java.io.Serializable;
028
029 import org.apache.hadoop.conf.Configured;
030 import org.apache.hadoop.io.serializer.Deserializer;
031 import org.apache.hadoop.io.serializer.Serialization;
032 import org.apache.hadoop.io.serializer.Serializer;
033
034 /**
035 * THIS IS JUST A TEMPORARY SOLUTION OF BUG IN THE ORIGINAL JavaSerialization
036 * IMPLEMENTATION WHERE THE WRONG CLASSLOADER IS USED. SHOULD BE REMOVED IF THE
037 * BUT IS REMOVED IN A NEW RELEASE.
038 * <p>
039 * THE FILE IS A COPY OF THE ORIGINAL JavaSerialization IMPLEMENTATION WITH THE
040 * DIFFRENCE THAT IS USES THE CLASSLOADER OF THE CONFIGURATION TO RESOLVE
041 * SERIALIZED CLASSES.
042 * <p>
043 * An experimental {@link Serialization} for Java {@link Serializable} classes.
044 * <p>
045 * <b>This class is not part of HadoOptimizer and will be removed as soon as
046 * possible.</b>
047 *
048 * @see org.apache.hadoop.io.serializer.JavaSerialization
049 */
050 @Deprecated
051 public class JavaSerialization extends Configured implements
052 Serialization<Serializable> {
053
054 static class JavaSerializationDeserializer<T extends Serializable>
055 implements Deserializer<T> {
056 private final ClassLoader classLoader;
057 private ObjectInputStream ois;
058
059 public JavaSerializationDeserializer(final ClassLoader classLoader) {
060 this.classLoader = classLoader;
061 }
062
063 @Override
064 public void open(final InputStream in) throws IOException {
065 ois = new ObjectInputStream(in) {
066 @Override
067 protected void readStreamHeader() {
068 // no header
069 }
070
071 @Override
072 protected Class<?> resolveClass(final ObjectStreamClass desc)
073 throws IOException, ClassNotFoundException {
074 return Class.forName(desc.getName(), false, classLoader);
075 };
076 };
077 }
078
079 @Override
080 @SuppressWarnings("unchecked")
081 public T deserialize(final T object) throws IOException {
082 try {
083 // ignore passed-in object
084 return (T) ois.readObject();
085 } catch (final ClassNotFoundException e) {
086 throw new IOException(e.toString());
087 }
088 }
089
090 @Override
091 public void close() throws IOException {
092 ois.close();
093 }
094
095 }
096
097 static class JavaSerializationSerializer implements
098 Serializer<Serializable> {
099
100 private ObjectOutputStream oos;
101
102 @Override
103 public void open(final OutputStream out) throws IOException {
104 oos = new ObjectOutputStream(out) {
105 @Override
106 protected void writeStreamHeader() {
107 // no header
108 }
109 };
110 }
111
112 @Override
113 public void serialize(final Serializable object) throws IOException {
114 oos.reset(); // clear (class) back-references
115 oos.writeObject(object);
116 }
117
118 @Override
119 public void close() throws IOException {
120 oos.close();
121 }
122
123 }
124
125 @Override
126 public boolean accept(final Class<?> c) {
127 return Serializable.class.isAssignableFrom(c);
128 }
129
130 @Override
131 public Deserializer<Serializable> getDeserializer(
132 final Class<Serializable> c) {
133 return new JavaSerializationDeserializer<Serializable>(getConf()
134 .getClassLoader());
135 }
136
137 @Override
138 public Serializer<Serializable> getSerializer(final Class<Serializable> c) {
139 return new JavaSerializationSerializer();
140 }
141
142 }