From: Richard G C. <pim...@us...> - 2005-02-28 10:09:26
|
Update of /cvsroot/openorb/OpenORB/src/main/org/openorb/orb/rmi In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12511/OpenORB/src/main/org/openorb/orb/rmi Modified Files: LazyInitDeserializationKernel.java DeserializationKernelFactory.java Added Files: DeserializationKernelSun15.java Log Message: - Implemented a deserialization kernel for Java 1.5 (Sun). As the reflection API has been enhanced to allow final fields to be set by setting the accessible property on the field object, the only remaining vendor specific hack is for the creation of Serializable objects where an object has to be created using the no-arg constructor of the first non Serializable super class. The current implementation uses the package local ObjectStreamClass.newInstance() method (via reflection) for this purpose. This kernel should work on all 1.5 platforms which provide this method. - Reimplemented LazyInitDeserializationKernel to use a thread safe lockless lazy singleton to remove the need to synchronize all calls. --- NEW FILE: DeserializationKernelSun15.java --- /* * Copyright (C) The Community OpenORB Project. All rights reserved. * * This software is published under the terms of The OpenORB Community Software * License version 1.0, a copy of which has been included with this distribution * in the LICENSE.txt file. */ package org.openorb.orb.rmi; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.io.ObjectStreamClass; import org.openorb.util.ExceptionTool; import org.openorb.util.JREVersion; /** * This is an implementation of the interface DeserializationKernel * for the Sun Java Runtime Environment version 1.5 onwards, which support * the package local newInstance method on the ObjectStreamClass. * * @version $Revision: 1.1 $ $Date: 2005/02/28 10:08:45 $ * @author Richard G Clark */ final class DeserializationKernelSun15 implements DeserializationKernel { private static final Method NEW_INSTANCE_METHOD; static { Method method = null; try { method = ObjectStreamClass.class.getDeclaredMethod( "newInstance", new Class[]{} ); method.setAccessible( true ); if ( !Object.class.equals( method.getReturnType() ) ) { method = null; } } catch ( final NoSuchMethodException e ) { // let method be null } NEW_INSTANCE_METHOD = method; } /** * Constructor is package protected so that it can be instantiated via the factory only. */ DeserializationKernelSun15() { if ( !isSupportedPlatform() ) { throw new Error( "Unsupported platform" ); } } static boolean isSupportedPlatform() { return JREVersion.V1_5 && ( null != NEW_INSTANCE_METHOD ); } private Field getAccessibleField( final Class c, final String n ) { try { final Field field = c.getDeclaredField( n ); field.setAccessible( true ); return field; } catch ( final NoSuchFieldException e ) { throw ExceptionTool.initCause( new Error( "Field [" + n + "] could not be found on class [" + c + "]" ), e ); } } /** * @see DeserializationKernel */ public Object allocateNewObject( final Class c, final Class base ) throws InstantiationException, IllegalAccessException { final ObjectStreamClass osc = ObjectStreamClass.lookup( c ); try { return NEW_INSTANCE_METHOD.invoke( osc, null ); } catch ( final InvocationTargetException e ) { if ( e.getCause() instanceof InstantiationException ) { throw ( InstantiationException ) e.getTargetException(); } if ( e.getCause() instanceof IllegalAccessException ) { throw ( IllegalAccessException ) e.getTargetException(); } throw ExceptionTool.initCause( new Error( "Unexpected exception (" + e.getTargetException() + ")" ), e.getCause() ); } } /** * @see DeserializationKernel */ public void setObjectField ( final Class c, final String n, final Object o, final Object v ) { final Field field = getAccessibleField( c, n ); try { field.set( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setBooleanField( final Class c, final String n, final Object o, final boolean v ) { final Field field = getAccessibleField( c, n ); try { field.setBoolean( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setByteField( final Class c, final String n, final Object o, final byte v ) { final Field field = getAccessibleField( c, n ); try { field.setByte( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setCharField( final Class c, final String n, final Object o, final char v ) { final Field field = getAccessibleField( c, n ); try { field.setChar( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setShortField( final Class c, final String n, final Object o, final short v ) { final Field field = getAccessibleField( c, n ); try { field.setShort( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setIntField( final Class c, final String n, final Object o, final int v ) { final Field field = getAccessibleField( c, n ); try { field.setInt( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setLongField( final Class c, final String n, final Object o, final long v ) { final Field field = getAccessibleField( c, n ); try { field.setLong( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setFloatField( final Class c, final String n, final Object o, final float v ) { final Field field = getAccessibleField( c, n ); try { field.setFloat( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } /** * @see DeserializationKernel */ public void setDoubleField( final Class c, final String n, final Object o, final double v ) { final Field field = getAccessibleField( c, n ); try { field.setDouble( o, v ); } catch ( final IllegalAccessException e ) { throw ExceptionTool.initCause( new Error( "Invocation not allowed! (" + e + ")" ), e ); } } } Index: LazyInitDeserializationKernel.java =================================================================== RCS file: /cvsroot/openorb/OpenORB/src/main/org/openorb/orb/rmi/LazyInitDeserializationKernel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- LazyInitDeserializationKernel.java 14 Nov 2004 21:38:35 -0000 1.1 +++ LazyInitDeserializationKernel.java 28 Feb 2005 10:08:45 -0000 1.2 @@ -11,85 +11,89 @@ /** * A DeserializationKernel that delegates all operations to another kernel, * the delegate is created on demand. + * + * @version $Revision$ $Date$ * @author lkuehne + * @author Richard G Clark */ -class LazyInitDeserializationKernel implements DeserializationKernel +final class LazyInitDeserializationKernel implements DeserializationKernel { - private DeserializationKernel m_delegate = null; - private final String m_delegateShortName; + private static String s_delegateShortName; - LazyInitDeserializationKernel( String delegateShortName ) + LazyInitDeserializationKernel() { - m_delegateShortName = delegateShortName; } - private synchronized void ensureDelegateInitialized() + static synchronized void setDelegateName( final String delegateName ) { - if ( m_delegate == null ) - { - m_delegate = - DeserializationKernelFactory.createDeserializationKernel( m_delegateShortName ); - } + s_delegateShortName = delegateName; } - public Object allocateNewObject( Class c, Class base ) + private static synchronized String getDelegateName() + { + return s_delegateShortName; + } + + private static final class SINGLETON + { + static final DeserializationKernel VALUE + = DeserializationKernelFactory.createDeserializationKernel( getDelegateName() ); + } + + private static DeserializationKernel getKernel() + { + return SINGLETON.VALUE; + } + + + public Object allocateNewObject( final Class c, final Class base ) throws InstantiationException, IllegalAccessException { - ensureDelegateInitialized(); - return m_delegate.allocateNewObject( c, base ); + return getKernel().allocateNewObject( c, base ); } - public void setObjectField( Class c, String n, Object o, Object v ) + public void setObjectField( final Class c, final String n, final Object o, final Object v ) { - ensureDelegateInitialized(); - m_delegate.setObjectField( c, n, o, v ); + getKernel().setObjectField( c, n, o, v ); } - public void setBooleanField( Class c, String n, Object o, boolean v ) + public void setBooleanField( final Class c, final String n, final Object o, final boolean v ) { - ensureDelegateInitialized(); - m_delegate.setBooleanField( c, n, o, v ); + getKernel().setBooleanField( c, n, o, v ); } - public void setByteField( Class c, String n, Object o, byte v ) + public void setByteField( final Class c, final String n, final Object o, final byte v ) { - ensureDelegateInitialized(); - m_delegate.setByteField( c, n, o, v ); + getKernel().setByteField( c, n, o, v ); } - public void setCharField( Class c, String n, Object o, char v ) + public void setCharField( final Class c, final String n, final Object o, final char v ) { - ensureDelegateInitialized(); - m_delegate.setCharField( c, n, o, v ); + getKernel().setCharField( c, n, o, v ); } - public void setShortField( Class c, String n, Object o, short v ) + public void setShortField( final Class c, final String n, final Object o, final short v ) { - ensureDelegateInitialized(); - m_delegate.setShortField( c, n, o, v ); + getKernel().setShortField( c, n, o, v ); } - public void setIntField( Class c, String n, Object o, int v ) + public void setIntField( final Class c, final String n, final Object o, final int v ) { - ensureDelegateInitialized(); - m_delegate.setIntField( c, n, o, v ); + getKernel().setIntField( c, n, o, v ); } - public void setLongField( Class c, String n, Object o, long v ) + public void setLongField( final Class c, final String n, final Object o, final long v ) { - ensureDelegateInitialized(); - m_delegate.setLongField( c, n, o, v ); + getKernel().setLongField( c, n, o, v ); } - public void setFloatField( Class c, String n, Object o, float v ) + public void setFloatField( final Class c, final String n, final Object o, final float v ) { - ensureDelegateInitialized(); - m_delegate.setFloatField( c, n, o, v ); + getKernel().setFloatField( c, n, o, v ); } - public void setDoubleField( Class c, String n, Object o, double v ) + public void setDoubleField( final Class c, final String n, final Object o, final double v ) { - ensureDelegateInitialized(); - m_delegate.setDoubleField( c, n, o, v ); + getKernel().setDoubleField( c, n, o, v ); } } Index: DeserializationKernelFactory.java =================================================================== RCS file: /cvsroot/openorb/OpenORB/src/main/org/openorb/orb/rmi/DeserializationKernelFactory.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- DeserializationKernelFactory.java 14 Nov 2004 21:38:35 -0000 1.8 +++ DeserializationKernelFactory.java 28 Feb 2005 10:08:45 -0000 1.9 @@ -30,6 +30,7 @@ * * Vendor: Sun Microsystems (http://java.sun.com) * <ul> + * <li>All, JDK 1.5.0 * <li>Windows 2000, JDK 1.3.0 * <li>Windows 2000, JDK 1.3.1 * <li>Windows 2000, JDK 1.4.0 @@ -103,8 +104,11 @@ if ( s_deserializationEngine.startsWith( LAZY_PREFIX ) && s_deserializationEngine.length() > LAZY_PREFIX.length() ) { - String delegateShortName = s_deserializationEngine.substring( LAZY_PREFIX.length() ); - s_kernel = new LazyInitDeserializationKernel( delegateShortName ); + final String delegateShortName + = s_deserializationEngine.substring( LAZY_PREFIX.length() ); + + LazyInitDeserializationKernel.setDelegateName( delegateShortName ); + s_kernel = new LazyInitDeserializationKernel(); } else { @@ -139,6 +143,11 @@ private static DeserializationKernel createAutoDeserializationKernel() { + if ( DeserializationKernelSun15.isSupportedPlatform() ) + { + return new DeserializationKernelSun15(); + } + try { Thread.currentThread().getContextClassLoader().loadClass( |