January 10th, 2008
PortableRemoteObject.narrow(ref, CabinRemote.class); if(cabin_1.isIdentical(cabin_2)) // this will always be true. At first glance, the Handle and the primary key appear to do the same thing, but in truth they are very different. Using the primary key requires you to have the correct remote EJB home if you no longer have a reference to the EJB remote home, you must look up the container using JNDI and get a new home. Only then can you call findByPrimaryKey() to locate the actual enterprise bean. The following code shows how this might work: // Obtain the primary key from an input stream. Integer primaryKey = (Integer)inStream.readObject(); // The JNDI API is used to get a root directory or initial context. javax.naming.Context ctx = new javax.naming.InitialContext(); // Using the initial context, obtain the EJBHome for the Cabin bean. Object ref = ctx.lookup(”CabinHome”); CabinHomeRemote home = (CabinHomeRemote) PortableRemoteObject.narrow(ref,CabinHomeRemote.class); // Obtain a reference to an EJB object that represents the entity instance. CabinRemote cabin_2 = CabinHome.findByPrimaryKey(primaryKey); The Handle object is easier to use because it encapsulates the details of doing a JNDI lookup on the container. With a Handle, the correct EJB object can be obtained in one method call, Handle.getEJBObject(), rather than using the three method calls required to look up the context, get the home, and find the actual bean. Furthermore, while the primary key can be used to obtain remote references to unique entity beans, it is not available for session beans; a handle can be used with either type of enterprise bean. This makes using a handle more consistent across bean types. Consis tency is, of course, good in its own right, but it isn t the whole story. Normally, we think of session beans as not having identifiable instances because they exist for only the life of the client session, but this is not exactly true. We have mentioned (but not yet shown) stateful session beans, which retain state information between method invocations. With stateful session beans, two instances are not equivalent. A handle allows you to work with a stateful session bean, deactivate the bean, and then reactivate it at a later time using the handle. A client could, for example, be using a stateful session bean to process an order when the process needs to be interrupted for some reason. Instead of losing all the work performed in the session, a handle can be obtained from the EJB object and the client application can be closed down. When the user is ready to continue the order, the handle can be used to obtain a reference to the stateful Copyright (c) 2001 O’Reilly & Associates 23
Would you like to be a member of headache free web site owner’s community. Now you can. We focus in IX Web Hosting, go go go!
Posted in Java Server Pages Blog | No Comments »
January 9th, 2008
The enterprise bean handle The EJBObject.getHandle() method returns a javax.ejb.Handle object. The Handle is a serializable reference to the remote EJB object. This means that the client can save the Handle object using Java serialization and then deserialize it to reobtain a reference to the same remote EJB object. This is similar to serializing and reusing the primary key. The Handle allows us to recreate a remote EJB object reference that points to the same type of session bean or the same unique entity bean that the handle came from. Here is the interface definition of the Handle: public interface javax.ejb.Handle { public abstract EJBObject getEJBObject() throws RemoteException; } The Handle interface specifies only one method, getEJBObject(). Calling this method returns the remote EJB object from which the handle was created. Once you ve gotten the object back, you can narrow it to the appropriate remote interface type. The following code shows how to serialize and deserialize the EJB Handle on a client: // Obtain cabin 100. Context jndiContext = getInitialContext(); Object ref = jndiContext.lookup(”CabinHome”); CabinHomeRemote home = (CabinHomeRemote) PortableRemoteObject.narrow(ref,CabinHomeRemote.class); Integer pk_1 = new Integer(101); CabinRemote cabin_1 = home.findByPrimaryKey(pk_1); // Serialize the Handle for cabin 100 to a file. Handle handle = cabin_1.getHandle(); FileOutputStream fos = new FileOutputStream(”handle100.ser”); ObjectOutputStream outStream = new ObjectOutputStream(fos); outStream.writeObject(handle); outStream.flush(); fos.close(); handle = null; // Deserialize the Handle for cabin 100. FileInputStream fis = new FileInputStream(”handle100.ser”); ObjectInputStream inStream = new ObjectInputStream(fis); handle = (Handle)inStream.readObject(); fis.close(); // Reobtain a remote reference to cabin 100 and read its name. ref = handle.getEJBObject(); CabinRemote cabin_2 = (CabinRemote) Copyright (c) 2001 O’Reilly & Associates
Get account with us and you will get completely access to our free web templates database with over 10.000 templates in it to build your website.Don’t wait, go and check free web templates.
Posted in Java Server Pages Blog | No Comments »
January 8th, 2008
Context ctx = getInitialContext(); Object ref = ctx.lookup(”TravelAgentHomeRemote”); TravelAgentHomeRemote agentHome =(TravelAgentHomeRemote) PortableRemoteObject.narrow(ref, TravelAgentHomeRemote.class); TravelAgentRemote agent_1 = agentHome.create(); TravelAgentRemote agent_2 = agentHome.create(); boolean x = agent_1.isIdentical(agent_2); // x will equal true; the two EJB objects are equal. ref = ctx.lookup(”CabinHomeRemote”); CabinHomeRemote c_home = (CabinHomeRemote) PortableRemoteObject.narrow(ref, CabinHomeRemote.class); Integer pk_1 = new Integer(101); Integer pk_2 = new Integer(101); Cabin cabin_1 = c_home.findByPrimaryKey(pk_1); Cabin cabin_2 = c_home.findByPrimaryKey(pk_2); x = cabin_1.isIdentical(cabin_2); // x will equal true; the two EJB objects are equal. The Integer primary key used in the Cabin bean is simple. More complex custom defined primary keys require us to override Object.equals() and Object.hashCode() in order for the EJBObject.isIdentical() method to work. Chapter 9 discusses this the development of more complex custom primary keys, which are called compound primary keys. Removing beans The EJBObject.remove() method is used to remove the session or entity bean. The impact of this method is the same as the EJBHome.remove() method discussed previously. For session beans, remove() causes the session to be released and the remote EJB object reference to become invalid. For entity beans, the actual entity data is deleted from the database and the remote reference becomes invalid. The following code shows the EJBObject.remove() method in use: Context jndiContext = getInitialContext(); Object ref = jndiContext.lookup(”CabinHome”); CabinHomeRemote c_home = (CabinHomeRemote) PortableRemoteObject.narrow(ref,CabinHomeRemote.class); Integer pk = new Integer(101); CabinRemote cabin = c_home.findByPrimaryKey(pk); cabin.remove(); The remove() method throws a RemoveException if for some reason the reference can t be deleted. Copyright (c) 2001 O’Reilly & Associates
Do you have a godaddy domain name? If you do, you have found the right web hosting provider for you.Our Godaddy Web Hosting packages are the best match to well known, affordable godaddy domain names.
Posted in Java Server Pages Blog | No Comments »
January 7th, 2008
Object ref = jndiContext.lookup(”CabinHome”); CabinHomeRemote home = (CabinHomeRemote) PortableRemoteObject.narrow(ref, CabinHomeRemote.class); Integer pk_1 = new Integer(101); Cabin cabin_1 = home.findByPrimaryKey(pk_1); cabin_1.setName(”Presidential Suite”); // Serialize the primary key for cabin 101 to a file. FileOutputStream fos = new FileOutputStream(”pk101.ser”); ObjectOutputStream outStream = new ObjectOutputStream(fos); outStream.writeObject(pk_1); outStream.flush(); outStream.close(); pk_1 = null; // Deserialize the primary key for cabin 101. FileInputStream fis = new FileInputStream(”pk101.ser”); ObjectInputStream inStream = new ObjectInputStream(fis); Integer pk_2 = (Integer)inStream.readObject(); inStream.close(); // Re-obtain a remote reference to cabin 101 and read its name. Cabin cabin_2 = home.findByPrimaryKey(pk_2); System.out.println(cabin_2.getName()); Comparing beans for identity The EJBObject.isIdentical() method compares two EJB object remote references. It s worth considering why Object.equals() isn t sufficient for comparing EJB objects. An EJB object is a distributed object stub and therefore contains a lot of networking and other state. As a result, references to two EJB objects may be unequal, even if they both represent the same unique bean. The EJBObject.isIdentical() method returns true if two EJB object references represent the same bean, even if the EJB object stubs are different object instances. The following code shows how this might work. It starts by creating two remote references to the TravelAgent EJB. These remote EJB objects both refer to the same type of enterprise bean; comparing them with isIdentical() returns true. The two TravelAgent EJBs were created separately, but because they are stateless they are considered to be equivalent. If TravelAgent EJB had been a stateful bean (which it becomes in Chapter 12) the outcome would have been very different. Comparing two stateful beans in this manner will result in false because stateful beans have conversational state, which makes them unique. When we use CabinHome.findByPrimaryKey() to locate two EJB objects that refer to the same Cabin entity bean, we know the entity beans are identical, because we used the same primary key. In this case, isIdentical() also returns true because both remote EJB object references point to the same entity. Copyright (c) 2001 O’Reilly & Associates
Are you a Mac user in a search of Mac web hosting provider? We are. once upon a time, we were just like you, trying to find somebody who knows. Unfortunately, we had no luck, so we decided to help many others like you, and us. our team developed Mac friendly web hosting plans, feature packed, constantly monitored, and more than affordable. With us, you are home. Sign up.
Posted in Java Server Pages Blog | No Comments »
January 6th, 2008
client s perspective, the primary key object can be used to identify a unique entity bean. Understanding the context of a primary key s uniqueness is important, as the following code shows: Context jndiContext = getInitialContext() Object ref = jndiContext.lookup(”CabinHomeRemote”); CabinHomeRemote home = (CabinHomeRemote) PortableRemoteObject.narrow(ref,CabinHomeRemote.class); Cabin cabin_1 = home.create(101); Integer pk = (Integer)cabin_1.getPrimaryKey(); Cabin cabin_2 = home.findByPrimaryKey(pk); In this code, the client creates a Cabin EJB, retrieves its primary key and then uses the key to get a new reference to the same Cabin EJB. Thus, we have two variables, cabin_1 and cabin_2, which are remote references to EJB objects. These both reference the same Cabin bean, with the same underlying data, because they have the same primary key. The primary key must be used for the correct bean in the correct container. While this seems fairly obvious, the primary key s relationship to a specific container and home interface is important. The primary key can only be guaranteed to return the same entity if it is used within the container that produced the key. As an example, imagine that a third-party vendor sells the Cabin EJB as a product. The vendor sells the Cabin EJB to both Titan and to a competitor. Both companies deploy the entity bean using their own relational databases with their own data. An Integer primary key with value of 20 in Titan s EJB system will not map to the same Cabin entity in the competitor s EJB system. Both cruise companies have a Cabin bean with a primary key equal to 20, but they represent different cabins for different ships. The Cabin EJBs come from different EJB containers, so their primary keys are not equivalent. Every entity EJB object has a unique identity with its EJB home. If two EJB objects have the same home and same primary key, they are considered identical. A primary key must implement the java.io.Serializable interface. This means that the primary key, regardless of its form, can be obtained from an EJB object, stored on the client using the Java serialization mechanism, and deserialized when needed. When a primary key is deserialized, it can be used to obtain a remote reference to that entity using findByPrimaryKey(), provided that the key is used on the right remote home interface and container. Preserving the primary key using serialization might be useful if the client application needs to access specific entity beans at a later date. The following code shows a primary key that is serialized and then deserialized to reobtain a remote reference to the same bean: // Obtain cabin 101 and set its name. Context jndiContext = getInitialContext(); Copyright (c) 2001 O’Reilly & Associates
Our facility is located in Orlando, Florida. Our Orlando web hosting data center gives you assurance that your website will work smoothly . Check more in Orlando Web Hosting.
Posted in Java Server Pages Blog | No Comments »
January 5th, 2008
try { Context jndiContext = getInitialContext(); Object ref = jndiContext.lookup(”TravelAgentHomeRemote”); TravelAgentHomeRemote home = (TravelAgentHomeRemote) PortableRemoteObject.narrow(ref,TravelAgentHomeRemote.class); // Get a remote reference to the bean (EJB object). TravelAgentRemote agent = home.create(); // Pass the remote reference to some method. getTheEJBHome(agent); } catch (java.rmi.RemoteException re){re.printStackTrace();} catch (Throwable t){t.printStackTrace();} } public static void getTheEJBHome(TravelAgentRemote agent) throws RemoteException { // The home interface is out of scope in this method, // so it must be obtained from the EJB object. // EJB 1.0: Use native cast instead of narrow() Object ref = agent.getEJBHome(); TravelAgentHomeRemote home = (TravelAgentHomeRemote) PortableRemoteObject.narrow(ref,TravelAgentHomeRemote.class); // Do something useful with the home interface. } Primary key EJBObject.getPrimaryKey() returns the primary key for an entity bean. This method is only supported by EJB objects that represent entity beans. Entity beans represent specific data that can be identified using this primary key. Session beans represent tasks or processes, not data, so a primary key would be meaningless. To better understand the nature of a primary key, we need to look beyond the boundaries of the client s view into the EJB container s layer, which was introduced in Chapters 2 and 3. The EJB container is responsible for persistence of the entity beans, but the exact mechanism for persistence is up to the vendor. In order to locate an instance of a bean in a persistent store, the data that makes up the entity must be mapped to some kind of unique key. In relational databases, data is uniquely identified by one or more column values that can be combined to form a primary key. In an object-oriented database, the key wraps an object ID (OID) or some kind of database pointer. Regardless of the mechanism which isn t really relevant from the client s perspective the unique key for an entity bean s data is encapsulated by the primary key, which is returned by the EJBObject.getPrimaryKey() method. The primary key can be used to obtain remote references to entity beans using the findByPrimaryKey() method on the remote home interface. From the Copyright (c) 2001 O’Reilly & Associates
Get account with us and you will get completely access to our free web templates database with over 10.000 templates in it to build your website.Don’t wait, go and check free web templates.
Posted in Java Server Pages Blog | No Comments »
January 4th, 2008
errors in executing the business method. You will learn more about defining custom exceptions in Chapters 12 and 14. Exercise 5.1, The remote component interfaces EJBObject, Handle, and Primary Key All remote interfaces extend the javax.ejb.EJBObject interface, which provides a set of utility methods and return types. These methods and return types are valuable in managing the client s interactions with beans. Here is the definition for the EJBObject interface: public interface javax.ejb.EJBObject extends java.rmi.Remote { public abstract EJBHome getEJBHome() throws RemoteException; public abstract Handle getHandle() throws RemoteException; public abstract Object getPrimaryKey() throws RemoteException; public abstract boolean isIdentical(EJBObject obj) throws RemoteException; public abstract void remove() throws RemoteException, RemoveException; } When the client obtains a reference to the remote interface, it is actually obtaining a remote reference to an EJB object. The EJB object implements the remote interface by delegating business method calls to the bean class; it provides its own implementations for the EJBObject methods. These methods return information about the corresponding bean instance on the server. As discussed in Chapter 2, the EJB object is automatically generated when deploying the bean in the EJB server, so the bean developer doesn t need to write an EJBObject implementation. Getting the EJBHome The EJBObject.getEJBHome() method returns a remote reference to the EJB home for the bean. The remote reference is returned as a javax.ejb.EJBHome object, which can be narrowed to the specific enterprise bean s remote home interface. This method is useful when an EJB object has left the scope of the remote EJB home that manufactured it. Because remote references can be passed as references and returned from methods, like any other Java object on the remote client, a remote reference can quickly find itself in a completely different part of the application from its remote home. The following code is contrived, but it illustrates how a remote reference can move out of the scope of its home and how getEJBHome() can be used to get a new reference to the EJB home at any time: public static void main(String [] args) { Copyright (c) 2001 O’Reilly & Associates
If you are in search for clan hosting account you just came in to right place. We host many clan websites in almost all popular games like Counterstrike, Call Of Duty and other. More details about our offer you can find inside clan web hosting section.
Posted in Java Server Pages Blog | No Comments »
January 4th, 2008
While the use of a suffix in the create method names in EJB 2.0 is allowed it is not required. EJB 1.1 doesn t support the use of suffixes in create method names. The create and find methods defined in the remote home interfaces are straightforward and can be easily employed by the client. The create methods on the home interface have to match the ejbCreate() methods on the bean class. create() and ejbCreate() match when they have the same parameters, when the arguments are of same type and in the same order, and when their method names are the same. This way, when a client calls the create method on the home interface, the call can be delegated to the corresponding ejbCreate() method on the bean instance. The find methods in the home interface work similarly for bean- managed entities in EJB 2.0 and 1.1. Every find() method in the home interface must correspond to an ejbFind() method in the bean itself. Container-managed entities do not implement ejbFind() methods in the bean class; the EJB container supports find methods automatically. You will discover more about how to implement the ebjCreate() and ejbFind() methods in the bean in Chapters 6 and 8. The Remote Interface The business methods of an enterprise bean can be defined by the remote interface provided by the enterprise bean developer. The javax.ejb.EJBObject interface, which extends the java.rmi.Remote interface, is the base class for all remote interfaces. The following code is the remote interface for the TravelAgent bean that we developed in Chapter 4: public interface TravelAgentRemote extends javax.ejb.EJBObject { public String [] listCabins(int shipID, int bedCount) throws RemoteException; } Figure 5-7 shows the TravelAgentRemoteinterface s inheritance hierarchy. [FIGURE see modified figure 5-4] Figure 5-4: Enterprise bean interface inheritance hierarchy Remote interfaces are focused on the business problem and do not include methods for system-level operations such as persistence, security, concurrency, or transactions. System-level operations are handled by the EJB server, which relieves the client developer of many responsibilities. All remote interface methods for beans must throw, at the very least, a java.rmi.RemoteException, which identifies problems with distributed communications. In addition, methods in the remote interface can throw as many custom exceptions as needed to indicate abnormal business-related conditions or Copyright (c) 2001 O’Reilly & Associates
Have you tried other web hosting companies and found out that their focus and your focus are in clash?Check our filemaker web hosting services.
Posted in Java Server Pages Blog | No Comments »
January 3rd, 2008
public interface CabinHomeRemote extends javax.ejb.EJBHome { public CabinRemote create(Integer id) throws CreateException, RemoteException; public CabinRemote findByPrimaryKey(Integer pk) throws FinderException, RemoteException; } Create methods throw a CreateException if something goes wrong during the creation process; find methods throw a FinderException if the requested bean can t be located. Since these methods are defined in an interface that subclasses Remote, they must also declare that they throw RemoteException. The create and find methods are specific to the enterprise bean, so it is up to the bean developer to define the appropriate create and find methods in the remote home interface. CabinHomeRemote currently has only one create method that creates a cabin with a specified ID and a find method that looks up an enterprise bean given its primary key, but it s easy to imagine methods that would create and find a cabin with particular properties for example, a cabin with three beds, or a deluxe cabin with blue wallpaper. Only entity beans have find methods; session beans do not. Entity beans represent unique identifiable data within a database and therefore can be found. Session beans, on the other hand, do not represent data: they are created to serve a client application and are not persistent, so there is nothing to find. A find method for a session bean would be meaningless. In EJB 2.0, the create methods were expanded so that a method name could be used as suffix. In other words, all create methods may take the form create(). For example, the Customer EJB might define a remote home interface with several create methods, each of which take a different String type parameters, but have different methods names. public interface CustomerHome extends javax.ejb.EJBHome { public CustomerRemote createWithSSN(Integer id, String socialSecurityNumber) throws CreateException, RemoteException; public CustomerRemote createWithPIN(Integer personalIdNubmer) throws CreateException, RemoteException; public CustomerRemote createWithBLN(Integer id, String businessLicenseNumber) throws CreateException, RemoteException; public Customer findByPrimaryKey(Integer id) throws FinderException, RemoteException; } Copyright (c) 2001 O’Reilly & Associates
Welcome to web hosting for all of you from India.Our recommendation is Web Hosting India.
Posted in Java Server Pages Blog | No Comments »
January 2nd, 2008
EJBMetaData meta = c_home.getEJBMetaData(); System.out.println(meta.getHomeInterfaceClass().getName()); System.out.println(meta.getRemoteInterfaceClass().getName()); System.out.println(meta.getPrimaryKeyClass().getName()); System.out.println(meta.isSession()); This application creates output like the following: com.titan.cabin.CabinHome com.titan.cabin.Cabin com.titan.cabin.CabinPK false In addition to providing the class types of the enterprise bean, the EJBMetaData also makes available the remote EJB home for the bean. By obtaining the remote EJB home from the EJBMetaData, we can obtain references to the remote EJB object and perform other functions. In the following code, we use the EJBMetaData to get the primary key class, create a key instance, obtain the remote EJB home, and from it, get a remote reference to the EJB object for a specific cabin entity: Class primKeyType = meta.getPrimaryKeyClass(); If(primKetType instanceof java.lang.Integer){ Integer pk = new Integer(1); Object ref = meta.getEJBHome(); CabinHomeRemote c_home2 = (CabinHomeRemote) PortableRemoteObject.narrow(ref,CabinHomeRemote.class); CabinRemote cabin = c_home2.findByPrimaryKey(pk); System.out.println(cabin.getName()); } The HomeHandle EJB 1.1 provides a new object called a HomeHandle, which is accessed by calling the EJBHome.getHomeHandle() method. This method returns a javax.ejb.HomeHandle object that provides a serializable reference to an enterprise bean s remote home. The HomeHandle allows a remote home reference to be stored and used later. It is similar to the javax.ejb.Handle and is discussed in more detail a little later. Creating and finding beans In addition to the standard javax.ejb.EJBHome methods that all remote home interfaces inherit, remote home interfaces also include special create and find methods for the bean. We have already talked about create and find methods, but a little review will solidify your understanding of the remote home interface s role in the Remote Client API. The following code shows the remote home interface defined for the Cabin EJB: Copyright (c) 2001 O’Reilly & Associates
Find the right website host for you, offering a directory of website hosts, free domain name registration tool, hosting reviews, web hosting ratings and articles for beginners, and tons of other resources, check web hosting ratings for more information.
Posted in Java Server Pages Blog | No Comments »