You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Paul Galbraith-TM <Pa...@telus.com> on 2005/07/15 21:42:24 UTC

[lang] improving usefullness of enums for remoting

I have an enum class that defines several error codes, and is passed between server and client applications.  

I want to be able to update the enum class to define new error objects on the server, without having to redeploy the new enum class to all the clients, which likely won't directly reference the new error code enum objects directly.

>From looking at the code, the readResolve() method in the current enum implementation doesn't support this...

I think this code, in the enum class, would allow me to optionally override the remoteExtension() method to do what I want...any thoughts?


    /**
     * <p>Handle the deserialization of the class to ensure that multiple
     * copies are not wastefully created, or illegal enum types created.</p>
     *
     * @return the resolved object
     */
    protected Object readResolve() {
        Entry entry = (Entry) cEnumClasses.get(getEnumClass());
        if (entry == null) {
            return remoteExtension(this);
        }
        Enum resolved = (Enum) entry.map.get(getName());
        return resolved != null ?  resolved : remoteExtension(this);
    }

    /**
     * <p>If you want to allow new enum values to be created
     * when deserializing enum objects, override this method with
     * the appropriate code to add the new enum to the collection.</p>
     * 
     * <pre>
     * // sample for deserialization of most simple enums
     * protected Enum remoteExtension(Enum newEnum) {
     *     return new Enum(newEnum.getName());
     * }
     * </pre>
     * 
     * @return the newly resolved object
     */
    protected Enum remoteExtension(Enum newEnum) {
        return null;
    }


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org