Archive for October, 2007

Web design course - props.put(Context.INITIAL_CONTEXT_FACTORY, jndiParams.get(Context.INITIAL_CONTEXT_FACTORY)); props.put(Context.PROVIDER_URL, jndiParams.get(Context.PROVIDER_URL)); ctx = new InitialContext(props);

Saturday, October 13th, 2007

props.put(Context.INITIAL_CONTEXT_FACTORY, jndiParams.get(Context.INITIAL_CONTEXT_FACTORY)); props.put(Context.PROVIDER_URL, jndiParams.get(Context.PROVIDER_URL)); ctx = new InitialContext(props); } else { // Use default provider ctx = new InitialContext( ); } } catch( IOException ex ){ // Use default provider ctx = new InitialContext( ); } } /** * Get the Singleton instance of the class. */ public static EJBHomeFactory getInstance( ) throwsNamingException { if (singleton == null) { singleton = new EJBHomeFactory( ); } return singleton; } /** * Specify the JNDI name and class for the desired homeinterface. */ public EJBHome lookupHome(String jndiName, Class homeClass) throws NamingException { EJBHome home = (EJBHome)homes.get(homeClass); if (home == null) { home = (EJBHome)PortableRemoteObject.narrow(ctx.lookup( jndiName), homeClass); // Cache the home for repeated use homes.put(homeClass, home); } return home; } } The getInstance method of EJBHomeFactory differs from most Singleton implementations in that it isn’t declared to be synchronized. As discussed in EJBDesignPatterns, using a synchronized method here would degrade performance without providing any significant benefit in return. If multiple instances of EJBHomeFactory are instantiated due to simultaneous calls to getInstance during initialization, some redundant
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

delegate to store the reference (Affordable web hosting) in the ServletContext

Friday, October 12th, 2007

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.

Email web hosting - the type of error that occurred than using

Thursday, October 11th, 2007

the type of error that occurred than using a DatastoreException. Furthermore, adding this new exception to our IStorefrontService declarations still wouldn’t expose the fact that the implementation is based on EJB. 13.2.1.2 Swapping the implementation All that’s left to do is to swap the current Storefront service implementation with the delegate we have created. The framework put into place with the StorefrontServiceFactoryin Chapter 6 makes this easy to do. We simply need to change the class specified for our service implementation in the web.xml file to the following:
storefront-service-class com.oreilly.struts.storefront.service.StorefrontEJBDelegate
With this change made, an action will be creating a delegate instance whenever it calls the getStorefrontService( )method implemented in the StorefrontBaseAction. This method should be called only once during a request, to avoid the unnecessary overhead of creating additional remote references. However, even taking care to use the same delegate throughout a request leaves us with an implementation that isn’t very efficient. The next section covers some ways to improve our use of JNDI and home interfaces. Don’t forget that you’ll need to copy the JBoss client JARs to the lib directory for your web application before using your delegate. You’ll also need the home and remote interface class files for the Storefrontsession bean in the classes directory. 13.2.2 Managing EJB Home and Remote References Implementing a business delegate clearly isolates and minimizes the dependencies between the web and application tiers. We were able to implement our Storefront session bean using a business interface that isn’t tied to any particular client type. We also were able to leave our Struts action classes untouched when switching to this implementation of our model. We do have a couple of problems to address, though, to turn this into a solution you would want to use in a real application. Most importantly, we need to improve how we’re obtaining our home interface references. We also should get rid of the hardcoded parameters used by our JNDI lookup. Performing a JNDI lookup to obtain a home interface reference is an expensive (slow) operation. We couldn’t do much about this overhead if we actually needed a new home reference for each request, but that’s not the case. An EJB home is a factory object that is valid throughout the lifetime of the client application. There is no state in this object that prevents it from being used across requests or client threads. Our delegate would be significantly improved if the home reference it needed were cached within the web tier after being requested the first time. As with any design problem, there is more than one technique we should consider for caching our home reference. We’re basically talking about application-scope data in the web tier, so modifying the
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

public void destroy( ) { // Do nothing (Java web server)

Wednesday, October 10th, 2007

public void destroy( ) { // Do nothing for this example} } When an instance of the StorefrontEJBDelegate class is created, its init( ) method is called to obtain a remote reference to the Storefront session bean. This method performs the required JNDI lookup using the naming service implementation provided by JBoss. As written, the delegate assumes that the naming service is running on the local machine. Later, we’ll look at how to externalize the details of the JNDI lookup that must be performed by a delegate. Once a remote reference is obtained, the delegate holds it as part of its state. This field is declared to be of the business interface type because we need it only for accessing business methods. Even though the storefront field isn’t declared to be of the session bean’s remote interface type, the required handling of RemoteExceptionmakes it clear that our delegate is accessing a remote object. Other than what is required to obtain a remote reference, most of the code in our delegate does nothing more than relay business method calls to the session-bean implementation. The logout( )and destroy( )methods have no counterparts in the application tier, so those implementations don’t include session-bean calls. If we needed to do something in these methods, that code could either be implemented directly in the StorefrontEJBDelegatemethods or in another web-tier component that could be called by the delegate. 13.2.1.1 Exception handling The exception handling found in this implementation of the StorefrontEJBDelegate class is worth noting. In addition to hiding the details of JNDI lookups, a business delegate used with a session bean should also hide the EJB-specific exceptions that come with being a remote client. In the business methods of the delegate, any RemoteException that gets thrown from a session-bean call is caught and reported to the client using a DatastoreException. Hiding the remote nature of the model implementation addresses the mismatch in declared exceptions between our business interface and the IStorefrontService declarations. If the inclusion of RemoteException in our business interface had been the only difference between this and the service interface, it might have been tempting to simply add this exception to IStorefrontServiceand continue forward. However, this would have unnecessarily cluttered the contract for whatever service implementation might be used with implementation details. The only reason our delegate uses a DatastoreException to respond to a RemoteException is to leave the service interface unaffected by the implementation approach. If this self-imposed constraint were relaxed so that changes to IStorefrontServicewere acceptable, a better approach would be to declare an exception class whose sole purpose is to report exceptions from a delegate in a generic fashion. For example, if we were to declare an application exception named ServiceDelegateException, we could throw that when a RemoteException occurred. Instead of throwing a RuntimeException to report a failure in obtaining a remote reference, the init( )method could also be updated to make use of ServiceDelegateException. This new exception would be a more accurate indication of
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

StorefrontHome sfHome = (StorefrontHome) PortableRemoteObject.narrow(home, StorefrontHome.class); storefront =

Tuesday, October 9th, 2007

StorefrontHome sfHome = (StorefrontHome) PortableRemoteObject.narrow(home, StorefrontHome.class); storefront = sfHome.create( ); } catch (NamingException e) { throw new RuntimeException(e.getMessage( )); } catch (CreateException e) { throw new RuntimeException(e.getMessage( )); } catch (RemoteException e) { throw new RuntimeException(e.getMessage( )); } } public UserView authenticate( String email, String password ) throws InvalidLoginException, ExpiredPasswordException, AccountLockedException, DatastoreException { try { return storefront.authenticate(email, password); } catch (RemoteException e) { throw DatastoreException.datastoreError(e); } } public List getFeaturedItems( ) throws DatastoreException { try { return storefront.getFeaturedItems( ); } catch (RemoteException e) { throw DatastoreException.datastoreError(e); } } public ItemDetailView getItemDetailView( String itemId ) throws DatastoreException { try { return storefront.getItemDetailView(itemId); } catch (RemoteException e) { throw DatastoreException.datastoreError(e); } } public void logout( String email ) { // Do nothing for this example }
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Web hosting solutions - RemoteException in their throws clauses. We’ll address these

Monday, October 8th, 2007

RemoteException in their throws clauses. We’ll address these differences by going back to the Business Delegate pattern introduced in Chapter 6. Recall that the purpose of this pattern is to hide the business-service implementation from the client application. We’ll start out with a fairly straightforward Business Delegate implementation and then cover some specific ways to improve it. An initial implementation is shown in Example 13-7. Example 13-7. A business delegate for the Storefront session bean package com.oreilly.struts.storefront.service; import java.rmi.RemoteException; import java.util.Hashtable; import java.util.List; import javax.ejb.CreateException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import com.oreilly.struts.storefront.catalog.view.ItemDetailView; import com.oreilly.struts.storefront.customer.view.UserView; import com.oreilly.struts.storefront.framework.exceptions.*; /** * This class is a business delegate that supports theimplementation of the * IStorefrontService interface using the Storefront sessionbean. */ public class StorefrontEJBDelegate implementsIStorefrontService { private IStorefront storefront; public StorefrontEJBDelegate( ) { init( ); } private void init( ) { try { Hashtable props = new Hashtable( ); props.put(Context.INITIAL_CONTEXT_FACTORY, “org.jnp.interfaces.NamingContextFactory”); props.put(Context.PROVIDER_URL, “localhost”); InitialContext ic = new InitialContext(props); Object home = ic.lookup(”com.oreilly.struts.storefront.service.Storefront”);
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

In addition to the (Web hosting top) ejb-jar.xml file, most containers

Sunday, October 7th, 2007

In addition to the ejb-jar.xml file, most containers require one or more vendor-specific descriptors as part of a bean’s deployment information. In this case, all we need to do is associate a JNDI name with our bean. Example 13-6 shows how this is done with JBoss. The fully qualified name of the bean’s remote interface was chosen as the JNDI name. It’s also common to use the home interface name. Example 13-6. The J Boss deployment descriptor for the Storefront session bean Storefront com.oreilly.struts.storefront.service.Storefront Deployment of an EJB requires packaging it into a Java archive (JAR) file. The deployment JAR file for our session bean needs to include the following files: The home and remote interface class files The bean implementation class file The two deployment descriptors (these files must be placed in a META-INF directory) The OJB.properties file and the various repository XML files used by the ORM framework The business object and DTO class files referenced by the Storefront bean Once you’ve created this JAR file, you can deploy the bean by copying the file to the server/default/deploy directory underneath your JBoss installation. You can place the JAR files for your JDBC driver and the OJB classes in the server/default/lib directory. At this point, you can start JBoss and verify that you have everything in place to execute the application tier. 13.2 Interfacing Struts to EJB It’s now time to turn our attention back to the client side of our session fa ade. In this section, we’ll first cover how to satisfy the requirements of our service interface with our session-bean implementation. We’ll then look at how to better manage the JNDI lookups and home and remote interface management inherent in being a remote client to an EJB. 13.2.1 Using a Business Delegate As you saw when we defined the business interface for the Storefront session bean, we still have some work to do to match it up to the Storefront service interface. Our business interface doesn’t include all the methods of IStorefrontService, and the methods that are declared include
You want to have a cheap webhost for your apache application, then check apache web hosting services.

With our (Web hosting domain) minimal implementation, we don’t need anything

Saturday, October 6th, 2007

With our minimal implementation, we don’t need anything complicated as far as deployment information for our session bean. Example 13-5 shows the standard ejb-jar.xml descriptor for our bean. For the most part, this file simply identifies the home and remote interfaces and the implementation class. It also declares that all of the business methods are nontransactional (because they’re read-only methods). Example 13-5. The ejb-jar.xml deployment descriptor for the Storefront session bean Generic deployment information for the Storefront session bean Storefront Session Bean Storefront com.oreilly.struts.storefront.service.StorefrontHome com.oreilly.struts.storefront.service.Storefront com.oreilly.struts.storefront.service.StorefrontBean Stateless
Container
Storefront *
NotSupported

In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Web hosting ratings - } } In our StorefrontBean class, the business

Friday, October 5th, 2007

} } In our StorefrontBean class, the business method implementations are unchanged from the StorefrontServiceImpl versions. Only the management of the database connection needed to be modified. Whenever the EJB container creates a new instance of this bean, the ejbCreate( ) callback method is invoked and a database connection is established. This connection is closed in the corresponding ejbRemove( )method that is called prior to the instance being destroyed. The container never passivates stateless session beans, so do-nothing implementations are supplied for the ejbPassivate( )and ejbActivate( ) methods of the SessionBeaninterface. If we needed more than one session bean in our example, we’d move these two methods into an adapter class and extend all our concrete implementation classes from it. A more correct EJB approach would be to open the database connection using a javax.sql.DataSource connection factory obtained from a JNDI lookup. This allows the container to manage connection pooling and transaction enlistment for you automatically. Again, this doesn’t affect our interface, so we can continue to use this simple implementation. Our session bean now has a remote interface and an implementation class. That leaves the home interface, which is always simple in the case of a stateless session bean. All we need is a single create( )method, as shown in Example 13-4. Example 13-4. The home interface for the Storefront session bean package com.oreilly.struts.storefront.service; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; /** * The home interface for the Storefront session bean. */ public interface StorefrontHome extends EJBHome { public Storefront create( ) throws CreateException, RemoteException; } 13.1.5 JBoss Deployment We need to select an EJB container and create the required XML deployment descriptors before we can deploy and use our session bean. The open source JBoss application server fits our requirements perfectly. This full-featured J2EE implementation, complete with EJB 2.0 support, is a favorite among open source developers. You can download the software for free from http://www.jboss.org.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Web host server - view.setUnitPrice( itemBO.getBasePrice( ) ); view.setTimeCreated( new Timestamp(System.currentTimeMillis( )

Thursday, October 4th, 2007

view.setUnitPrice( itemBO.getBasePrice( ) ); view.setTimeCreated( new Timestamp(System.currentTimeMillis( ) )); view.setModelNumber( itemBO.getModelNumber( ) ); return view; } /** * Opens the database and prepares it for transactions. */ private void init( ) throws DatastoreException { // Get odmg facade instance odmg = OJB.getInstance( ); db = odmg.newDatabase( ); // Open database try{ db.open(”repository.xml”, Database.OPEN_READ_WRITE); }catch( ODMGException ex ){ throw DatastoreException.datastoreError(ex); } } public void ejbCreate( ) throws CreateException { try { init( ); }catch ( DatastoreException e ) { throw new CreateException(e.getMessage( )); } } public void ejbRemove( ) { try { if (db != null) { db.close( ); } }catch ( ODMGException e ) {} } public void setSessionContext( SessionContextassignedContext ) { ctx = assignedContext; } public void ejbActivate( ) { // Nothing to do for a stateless bean } public void ejbPassivate( ) { // Nothing to do for a stateless bean
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.