delegate to store the reference (Affordable web hosting) in the ServletContext
delegate to store the reference in the ServletContext after doing the required JNDI lookup is a potential solution. This would prevent any additional lookups, but it would require us to make the ServletContext available to our delegate through its constructor. This one change would ripple out to our service factory, because it currently instantiates an IStorefrontService implementation using its no-argument constructor. It would be preferable to choose a solution without such a strong tie to HTTP constructs. A more flexible approach is to apply the EJBHomeFactory pattern as a way to cache the references we need. 13.2.2.1 Implementing an EJBHomeFactory The EJBHomeFactory pattern is defined in EJBDesignPatterns by Floyd Marinescu (Wiley & Sons). Implementing this pattern allows you to create and cache any EJB home reference needed by your application. Because it’s not dependent on the ServletContext, you can reuse this technique in non-web applications. Example 13-8 shows the implementation of this pattern that we’ll use for the Storefront application. Example 13-8. An EJBHomeFactory implementation package com.oreilly.struts.storefront.framework.ejb; import java.io.InputStream; import java.io.IOException; import java.util.*; import javax.ejb.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; /** * This class implements the EJBHomeFactory pattern. Itperforms JNDI * lookups to locate EJB homes and caches the results forsubsequent calls. */ public class EJBHomeFactory { private Map homes; private static EJBHomeFactory singleton; private Context ctx; private EJBHomeFactory( ) throws NamingException { homes = Collections.synchronizedMap(new HashMap( )); try { // Load the properties file from the classpath rootInputStream inputStream = getClass( ).getResourceAsStream( “/jndi.properties” ); if ( inputStream != null) { Properties jndiParams = new Properties( ); jndiParams.load( inputStream ); Hashtable props = new Hashtable( );
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.