Use the “Service Locator” Design Pattern with the J2EE Architechture

Use the “Service Locator” Design Pattern with the J2EE Architechture

This tip is an extension of a Design Pattern called “Service Locator” for the J2EE architecture. Generally, the “Service Locator” pattern returns a home object by merely accepting a string, usually a mapping for a JNDI name.

This tip creates a pure factory for stateless session beans (a method which accepts a string?in JNDI, the name of the stateless session bean) and returns the appropriate remote objects (called EJBObject) without actually type-casting the appropriate home object (called EJBHome) or executing the create method. The following code demonstrates:

Accounts valAccounts= ((AccountsHome)home).create();

This easiest way to call a subclass method without actually type-casting it is by using reflection:

Method m = homeBase.getMethod("create", null);remoteObject = (EJBObject) m.invoke(home, null);

Here’s the sample code:

/* Sample code */public class ResourceLocator{   public static EJBObect getResourceInstance(String jndiName)   {      Context ctx = new InitialContext();      Object o = ctx.lookup(jndiName);      //Need not type cast home object to appropriate Home Interface      home = (EJBHome) PortableRemoteObject.narrow(o, Class.forName("javax.ejb.EJBHome"));    //Need not type cast home object to appropriate Remote Interface    Class homeBase = home.getClass();    Method m = homeBase.getMethod("create", null);    remoteObject = (EJBObject) m.invoke(home, null);    return remoteObject;   }}

Whichever JNDI name is provided, the above code generalizes and achieves a perfect factory pattern for a stateless session bean.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several