EASC and enum types

at 2007-07-05 in EASC by friebe (0 comments)

While developing a client for a stateless session bean deployed on an enabled application server for a corporate project, I stumbled upon the need to pass a Java 5 enum. It was part of the signature of a remote method which was something along the lines of:
public CaseId createCase(FaxId faxId, Provider provider, String url);

The Provider is an enumeration type, declared as follows:
public enum Provider { FAXTOOL, ORDERING, TELECOM }

Because PHP does not support enums, we will have to find a workaround.

Analysis
My initial idea was to pass the ordinal value for the enum type, but this could not work - how would the Java side know which enum's ordinal value this was? After a quick apidoc lookup I found the method Enum.valueOf() method, which, when given a Class object and the enum member's name, would return the correct enum value.

New version
This required a small change to the EASC Serializer. Because the changes also affect Java -> PHP serialization changes, I bumped EASC's version number to 1.2. Thus, in order to be able to work with enum types using the XP framework's remote API, you will need to upgrade your EASC MBean (undeploy any older version first!)

The client side code
With this change in place, the task is now on the client side to create a serialized representation in such a manner that the Java side will deserialize it to an enum. This can be accomplished by creating a class with a single public member called name, for example:

<?php 
// Declare as follows:
class Provider extends Object {
public
$name= '';

public
function __construct($name= '') {
$this->name= $name;
}
}

// Instantiate:
$provider= new Provider('FAXTOOL');

// And pass it to the method (JAAS authentication showcased, too:))
$facade= Remote::forName('xp://user:pass@casetool01')
->lookup
('businesscase/CaseFacade/remote')
->create
()
;
$caseId= $facade->createCase($faxId, $provider, $url);
?>


The Java side will now correctly deserialize the Provider instance to the Provider.FAXTOOL enum value and pass it to the createCase() method.

Mission accomplished?
For the moment, this will work just fine. But I don't like halfbaked solutions, and would like to have something more explicitely stating a class is an enum in PHP.

This is what RFC #0132 is for:)



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases

Related

Find related articles by a search for «EASC».