/*
* $Id: AnyAbstractClass.java,v 1.11 2002/09/16 08:05:02 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core;
import anvil.script.Context;
import anvil.script.Type;
import anvil.script.Function;
import anvil.script.Module;
import anvil.server.Address;
import anvil.java.util.BindingEnumeration;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Modifier;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InvalidClassException;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.io.Writer;
/**
* class AnyAbstractClass
*
* @author Jani Lehtim�ki
* @version $Revision: 1.11 $
*/
public abstract class AnyAbstractClass extends Any
{
public int typeOf()
{
return IS_CLASS;
}
public boolean isMutable()
{
return true;
}
public boolean isClass()
{
return true;
}
public abstract anvil.script.ClassType classOf();
public Object clone()
{
return this;
}
public Any copy()
{
return this;
}
public boolean toBoolean()
{
return true;
}
public Object toObject()
{
return null;
}
public String toString()
{
return classNameOf() + '(' + toObject() + ')';
}
public AnyClass toClass()
{
return null;
}
public int hashCode()
{
return System.identityHashCode(this);
}
public void serialize(Serializer serializer) throws IOException
{
serializer.write('x');
serializer.write(getClass().getName());
doSerialize(serializer);
}
public void doSerialize(Serializer serializer) throws IOException
{
serializeObject(serializer, toObject());
}
public static Any unserialize(Unserializer unserializer)
throws UnserializationException
{
unserializer.consume('s');
String classname = unserializer.getUTF16String();
Any rv = null;
try {
Class cls = Class.forName(classname);
Method method = cls.getDeclaredMethod("unserialize", new Class[] { Unserializer.class });
if (method != null) {
int mod = method.getModifiers();
if (Modifier.isStatic(mod) && Modifier.isPublic(mod)) {
rv = (Any)method.invoke(null, new Object[] { unserializer });
}
}
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
} catch (Throwable t) {
throw new UnserializationException(t.toString());
}
if (rv == null) {
rv = Any.create(unserializeObject(unserializer));
}
if (rv == null) {
throw new UnserializationException();
}
return rv;
}
public static Object unserializeObject(Unserializer unserializer)
throws UnserializationException
{
try {
int size = (int)unserializer.getLong();
ByteArrayInputStream byteInput = new ByteArrayInputStream(unserializer._data, unserializer._pos, size);
ObjectInputStream objectInput = new ObjectInputStream(byteInput);
Object object = objectInput.readObject();
byteInput.close();
unserializer._pos += size;
byte[] data = unserializer._data;
if (unserializer._pos >= data.length+1) {
throw new UnserializationException();
}
return object;
} catch (OptionalDataException e) {
throw new UnserializationException(e.toString());
} catch (StreamCorruptedException e) {
throw new UnserializationException(e.toString());
} catch (InvalidClassException e) {
throw new UnserializationException(e.toString());
} catch (ClassNotFoundException e) {
throw new UnserializationException(e.toString());
} catch (IOException e) {
throw new UnserializationException(e.toString());
}
}
public static void serializeObject(Serializer serializer, Object object) throws IOException
{
try {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutputStream);
objectOutput.writeObject(object);
objectOutput.flush();
byteOutputStream.close();
serializer.write(byteOutputStream.size());
serializer.write(':');
byteOutputStream.writeTo(serializer.getOutput());
} catch (InvalidClassException e) {
throw new IOException("Object not serializable: "+e.getMessage());
} catch (NotSerializableException e) {
throw new IOException("Object not serializable: "+e.getMessage());
}
}
}