![]() |
![]() |
![]() |
![]() |
![]() |
||||
![]() |
Home | Download | Lists | Issues |
![]() |
||||||
![]() |
![]() |
![]() |
![]() |
![]() |
||||
OverviewIn EJB 3.0, your bean's interfaces are not required to extend any specific set of interfaces and generally have no requirements on them at all. These are referred to in EJB spec lingo as Business Interfaces. In EJB 2.1 and prior, however, there were some pretty strict requirements on interfaces. Namely, you had to have a "home" interface that extended javax.ejb.EJBHome or javax.ejb.EJBLocalHome. This interface served as a sort of factory to create your bean's interface which itself had to extend either javax.ejb.EJBObject or javax.ejb.EJBLocalObject. These legacy styles of interfaces in EJB spec lingo are referred to as Component Interfaces. In this example we see how one can support both business interfaces and component interfaces on the same bean. You may choose to do this for backwards compatibility with older clients or to maintain an older EJB 2.x API still being used. The advantage is that you can turn EJB 2.1 beans into EJB 3.0 beans without having to update any code consuming that EJB. The source for this example is in the "component-interfaces" directory located in the openejb-examples.zip available on the download page. The CodeAnnotated Bean Class// EJB 3.0 Style business interfaces // Each of these interfaces are already annotated in the classes // themselves with @Remote and @Local, so annotating them here // in the bean class again is not really required. @Remote({FriendlyPersonRemote.class}) @Local({FriendlyPersonLocal.class}) // EJB 2.1 Style component interfaces // These interfaces, however, must be annotated here in the bean class. // Use of @RemoteHome in the FriendlyPersonEjbHome class itself is not allowed. // Use of @LocalHome in the FriendlyPersonEjbLocalHome class itself is also not allowed. @RemoteHome(FriendlyPersonEjbHome.class) @LocalHome(FriendlyPersonEjbLocalHome.class) @Stateful(name="FriendlyPerson") public class FriendlyPersonImpl implements FriendlyPersonLocal, FriendlyPersonRemote { private final HashMap<String, MessageFormat> greetings; private final Properties languagePreferences; private String defaultLanguage; public FriendlyPersonImpl() { greetings = new HashMap(); languagePreferences = new Properties(); defaultLanguage = Locale.getDefault().getLanguage(); addGreeting("en", "Hello {0}!"); addGreeting("es", "Hola {0}!"); addGreeting("fr", "Bonjour {0}!"); addGreeting("pl", "Witaj {0}!"); } /** * This method corresponds to the FriendlyPersonEjbHome.create() method * and the FriendlyPersonEjbLocalHome.create() * * If you do not have an EJBHome or EJBLocalHome interface, this method * can be deleted. */ @Init public void create(){} /** * This method corresponds to the following methods: * - EJBObject.remove() * - EJBHome.remove(ejbObject) * - EJBLocalObject.remove() * - EJBLocalHome.remove(ejbObject) * * If you do not have an EJBHome or EJBLocalHome interface, this method * can be deleted. */ @Remove public void remove(){} public String greet(String friend) { String language = languagePreferences.getProperty(friend, defaultLanguage); return greet(language, friend); } public String greet(String language, String friend) { MessageFormat greeting = greetings.get(language); if (greeting == null) { Locale locale = new Locale(language); return "Sorry, I don't speak " + locale.getDisplayLanguage() + "."; } return greeting.format(new Object[]{friend}); } public void addGreeting(String language, String message) { greetings.put(language, new MessageFormat(message)); } public void setLanguagePreferences(String friend, String language) { languagePreferences.put(friend, language); } public String getDefaultLanguage() { return defaultLanguage; } public void setDefaultLanguage(String defaultLanguage) { this.defaultLanguage = defaultLanguage; } } EJB 2.1 Home And Remote viewsimport javax.ejb.CreateException; import javax.ejb.EJBHome; import java.rmi.RemoteException; public interface FriendlyPersonEjbHome extends EJBHome { FriendlyPersonEjbObject create() throws CreateException, RemoteException; } import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface FriendlyPersonEjbObject extends EJBObject { String greet(String friend) throws RemoteException; String greet(String language, String friend) throws RemoteException; void addGreeting(String language, String message) throws RemoteException; void setLanguagePreferences(String friend, String language) throws RemoteException; String getDefaultLanguage() throws RemoteException; void setDefaultLanguage(String defaultLanguage) throws RemoteException; } Test Casepublic void testEjbHomeAndEjbObject() throws Exception { Object object = initialContext.lookup("FriendlyPersonRemoteHome"); FriendlyPersonEjbHome home = (FriendlyPersonEjbHome) object; FriendlyPersonEjbObject friendlyPerson = home.create(); friendlyPerson.setDefaultLanguage("en"); assertEquals("Hello David!", friendlyPerson.greet("David")); assertEquals("Hello Amelia!", friendlyPerson.greet("Amelia")); friendlyPerson.setLanguagePreferences("Amelia", "es"); assertEquals("Hello David!", friendlyPerson.greet("David")); assertEquals("Hola Amelia!", friendlyPerson.greet("Amelia")); // Amelia took some French, let's see if she remembers assertEquals("Bonjour Amelia!", friendlyPerson.greet("fr", "Amelia")); // Dave should take some Polish and if he had, he could say Hi in Polish assertEquals("Witaj Dave!", friendlyPerson.greet("pl", "Dave")); // Let's see if I speak Portuguese assertEquals("Sorry, I don't speak " + new Locale("pt").getDisplayLanguage() + ".", friendlyPerson.greet("pt", "David")); // Ok, well I've been meaning to learn, so... friendlyPerson.addGreeting("pt", "Ola {0}!"); assertEquals("Ola David!", friendlyPerson.greet("pt", "David")); } Running itRunning the example is fairly simple. In the "component-interfaces" directory of the examples zip, just run:
Which should create output like the following. ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.superbiz.FriendlyPersonTest Apache OpenEJB 3.0 build: 20080408-04:13 http://openejb.apache.org/ INFO - openejb.home = /Users/dblevins/work/openejb-3.0/examples/component-interfaces INFO - openejb.base = /Users/dblevins/work/openejb-3.0/examples/component-interfaces INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) INFO - Configuring Service(id=Default JDK 1.3 ProxyFactory, type=ProxyFactory, provider-id=Default JDK 1.3 ProxyFactory) INFO - Found EjbModule in classpath: /Users/dblevins/work/openejb-3.0/examples/component-interfaces/target/classes INFO - Configuring app: /Users/dblevins/work/openejb-3.0/examples/component-interfaces/target/classes INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container) INFO - Auto-creating a container for bean FriendlyPerson: Container(type=STATEFUL, id=Default Stateful Container) INFO - Loaded Module: /Users/dblevins/work/openejb-3.0/examples/component-interfaces/target/classes INFO - Assembling app: /Users/dblevins/work/openejb-3.0/examples/component-interfaces/target/classes INFO - Jndi(name=FriendlyPersonRemoteHome) --> Ejb(deployment-id=FriendlyPerson) INFO - Jndi(name=FriendlyPersonLocalHome) --> Ejb(deployment-id=FriendlyPerson) INFO - Jndi(name=FriendlyPersonLocal) --> Ejb(deployment-id=FriendlyPerson) INFO - Jndi(name=FriendlyPersonRemote) --> Ejb(deployment-id=FriendlyPerson) INFO - Created Ejb(deployment-id=FriendlyPerson, ejb-name=FriendlyPerson, container=Default Stateful Container) INFO - Deployed Application(path=/Users/dblevins/work/openejb-3.0/examples/component-interfaces/target/classes) Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.741 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 |
![]() |
|||||||
|
![]() |