KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > RegionBean


1 package edu.rice.rubis.beans;
2
3 import java.rmi.*;
4 import javax.ejb.*;
5 import javax.rmi.PortableRemoteObject JavaDoc;
6 import javax.naming.InitialContext JavaDoc;
7
8 /**
9  * RegionBean is an entity bean with "container managed persistence".
10  * The state of an instance is stored into a relational database.
11  * The following table should exist:<p>
12  * <pre>
13  * CREATE TABLE regions (
14  * id INTEGER UNSIGNED NOT NULL UNIQUE,
15  * name VARCHAR(20),
16  * PRIMARY KEY(id)
17  * );
18  * </pre>
19  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
20  * @version 1.0
21  */

22
23 public class RegionBean implements EntityBean
24 {
25   private EntityContext entityContext;
26   private transient boolean isDirty; // used for the isModified function
27

28   /* Class member variables */
29
30   public Integer JavaDoc id;
31   public String JavaDoc name;
32
33   /**
34    * Get region's id.
35    *
36    * @return region id
37    * @exception RemoteException if an error occurs
38    */

39   public Integer JavaDoc getId() throws RemoteException
40   {
41     return id;
42   }
43
44   /**
45    * Get region name.
46    *
47    * @return region name
48    * @exception RemoteException if an error occurs
49    */

50   public String JavaDoc getName() throws RemoteException
51   {
52     return name;
53   }
54
55   /**
56    * Set region's name
57    *
58    * @param newName region name
59    * @exception RemoteException if an error occurs
60    */

61   public void setName(String JavaDoc newName) throws RemoteException
62   {
63     name = newName;
64     isDirty = true; // the bean content has been modified
65
}
66
67
68   /**
69    * This method is used to create a new Region Bean. Note that the region
70    * id is automatically generated by the database (AUTO_INCREMENT) on the
71    * primary key.
72    *
73    * @param regionName Region name
74    *
75    * @return pk primary key set to null
76    * @exception CreateException if an error occurs
77    * @exception RemoteException if an error occurs
78    * @exception RemoveException if an error occurs
79    */

80   public RegionPK ejbCreate(String JavaDoc regionName) throws CreateException, RemoteException, RemoveException
81   {
82      // Connecting to IDManager Home interface thru JNDI
83
IDManagerHome home = null;
84       IDManager idManager = null;
85       
86       try
87       {
88         InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
89         home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup(
90                "java:comp/env/ejb/IDManager"), IDManagerHome.class);
91       }
92       catch (Exception JavaDoc e)
93       {
94         throw new EJBException("Cannot lookup IDManager: " +e);
95       }
96      try
97       {
98         IDManagerPK idPK = new IDManagerPK();
99         idManager = home.findByPrimaryKey(idPK);
100         id = idManager.getNextRegionID();
101         name = regionName;
102       }
103      catch (Exception JavaDoc e)
104       {
105         throw new EJBException("Cannot create region: " +e);
106       }
107      return null;
108   }
109
110   /** This method just set an internal flag to
111       reload the id generated by the DB */

112   public void ejbPostCreate(String JavaDoc regionName)
113   {
114     isDirty = true; // the id has to be reloaded from the DB
115
}
116
117   /** Persistence is managed by the container and the bean
118       becomes up to date */

119   public void ejbLoad() throws RemoteException
120   {
121     isDirty = false;
122   }
123
124   /** Persistence is managed by the container and the bean
125       becomes up to date */

126   public void ejbStore() throws RemoteException
127   {
128     isDirty = false;
129   }
130
131   /** This method is empty because persistence is managed by the container */
132   public void ejbActivate() throws RemoteException {}
133   /** This method is empty because persistence is managed by the container */
134   public void ejbPassivate() throws RemoteException {}
135   /** This method is empty because persistence is managed by the container */
136   public void ejbRemove() throws RemoteException, RemoveException {}
137
138
139   /**
140    * Sets the associated entity context. The container invokes this method
141    * on an instance after the instance has been created.
142    *
143    * This method is called in an unspecified transaction context.
144    *
145    * @param context An EntityContext interface for the instance. The instance should
146    * store the reference to the context in an instance variable.
147    * @exception EJBException Thrown by the method to indicate a failure
148    * caused by a system-level error.
149    * @exception RemoteException - This exception is defined in the method signature
150    * to provide backward compatibility for enterprise beans
151    * written for the EJB 1.0 specification.
152    * Enterprise beans written for the EJB 1.1 and
153    * higher specification should throw the javax.ejb.EJBException
154    * instead of this exception.
155    */

156   public void setEntityContext(EntityContext context) throws RemoteException
157   {
158     entityContext = context;
159   }
160
161
162   /**
163    * Unsets the associated entity context. The container calls this method
164    * before removing the instance. This is the last method that the container
165    * invokes on the instance. The Java garbage collector will eventually invoke
166    * the finalize() method on the instance.
167    *
168    * This method is called in an unspecified transaction context.
169    *
170    * @exception EJBException Thrown by the method to indicate a failure
171    * caused by a system-level error.
172    * @exception RemoteException - This exception is defined in the method signature
173    * to provide backward compatibility for enterprise beans
174    * written for the EJB 1.0 specification.
175    * Enterprise beans written for the EJB 1.1 and
176    * higher specification should throw the javax.ejb.EJBException
177    * instead of this exception.
178    */

179   public void unsetEntityContext() throws RemoteException
180   {
181     entityContext = null;
182   }
183
184
185   /**
186    * Returns true if the beans has been modified.
187    * It prevents the EJB server from reloading a bean
188    * that has not been modified.
189    *
190    * @return a <code>boolean</code> value
191    */

192   public boolean isModified()
193   {
194     return isDirty;
195   }
196
197 }
198
Popular Tags