KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > foedeployer > ejb > ql > CarCatalogBean


1 /*
2 * JBoss, the OpenSource EJB server
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.foedeployer.ejb.ql;
8
9
10 import java.sql.Date JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 import java.rmi.RemoteException JavaDoc;
18
19 import javax.ejb.SessionBean JavaDoc;
20 import javax.ejb.SessionContext JavaDoc;
21 import javax.ejb.EJBException JavaDoc;
22 import javax.ejb.CreateException JavaDoc;
23 import javax.ejb.FinderException JavaDoc;
24 import javax.ejb.RemoveException JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29
30 import javax.rmi.PortableRemoteObject JavaDoc;
31
32 import org.apache.log4j.Category;
33
34 /**
35  * Car catalog session bean.
36  *
37  * @ejb.bean
38  * type="Stateless"
39  * name="CarCatalog"
40  * jndi-name="CarCatalogEJB.CarCatalogHome"
41  * generate="true"
42  *
43  * @ejb.ejb-ref
44  * ejb-name="Car"
45  * view-type="local"
46  *
47  * @ejb.transaction type="Required"
48  */

49 public class CarCatalogBean
50    implements SessionBean JavaDoc
51 {
52    // Attributes --------------------------------------------------
53
static Category log = Category.getInstance( CarCatalogBean.class );
54    static String JavaDoc CAR_NAME = "java:comp/env/ejb/Car";
55    private CarLocalHome carHome;
56
57    // Business methods ---------------------------------------------
58
/**
59     * Creates a car
60     *
61     * @ejb.interface-method
62     */

63    public void createCar( String JavaDoc number, String JavaDoc color, int year )
64    {
65       try
66       {
67          carHome.create( number, color, year );
68       }
69       catch( CreateException JavaDoc ce )
70       {
71          log.debug( "Exception in create(): ", ce );
72          throw new EJBException JavaDoc( ce );
73       }
74    }
75
76    /**
77     * Removes a car if exists
78     *
79     * @ejb.interface-method
80     */

81    public void removeCarIfExists( String JavaDoc number )
82    {
83       try
84       {
85          CarLocal car = carHome.findByPrimaryKey( number );
86          car.remove();
87       }
88       catch( Exception JavaDoc e )
89       {
90          log.debug( "Exception while removing car with number "
91             + number + ": ", e );
92       }
93    }
94
95    /**
96     * Finds all car numbers
97     *
98     * @ejb.interface-method
99     */

100    public Collection JavaDoc getAllCarNumbers()
101    {
102       Collection JavaDoc allNumbers = new ArrayList JavaDoc();
103       try
104       {
105          Collection JavaDoc allCars = carHome.findAll();
106          for( Iterator JavaDoc iter = allCars.iterator(); iter.hasNext(); )
107          {
108             CarLocal car = (CarLocal) iter.next();
109             allNumbers.add( car.getNumber() );
110          }
111       }
112       catch( Exception JavaDoc e )
113       {
114          log.debug( "Exception in getAllCarNumbers(): ", e );
115          throw new EJBException JavaDoc( e );
116       }
117       return allNumbers;
118    }
119
120    /**
121     * Finds car numbers by color
122     *
123     * @ejb.interface-method
124     */

125    public Collection JavaDoc getCarsWithColor( String JavaDoc color )
126    {
127       Collection JavaDoc result = new ArrayList JavaDoc();
128       try
129       {
130          Collection JavaDoc cars = carHome.findByColor( color );
131          for( Iterator JavaDoc iter = cars.iterator(); iter.hasNext(); )
132          {
133             CarLocal car = (CarLocal) iter.next();
134             result.add( car.getNumber() );
135          }
136       }
137       catch( Exception JavaDoc e )
138       {
139          log.debug( "Exception in getCarsWithColor(): ", e );
140          throw new EJBException JavaDoc( e );
141       }
142       return result;
143    }
144
145    /**
146     * Finds car made after year
147     *
148     * @ejb.interface-method
149     */

150    public Collection JavaDoc getCarsAfterYear( int year )
151    {
152       Collection JavaDoc result = new ArrayList JavaDoc();
153       try
154       {
155          Collection JavaDoc cars = carHome.findAfterYear( year );
156          for( Iterator JavaDoc iter = cars.iterator(); iter.hasNext(); )
157          {
158             CarLocal car = (CarLocal) iter.next();
159             result.add( car.getNumber() );
160          }
161       }
162       catch( Exception JavaDoc e )
163       {
164          log.debug( "Exception in getCarsAfterYear(): ", e );
165          throw new EJBException JavaDoc( e );
166       }
167       return result;
168    }
169
170
171    // SessionBean implementation -------------------------------------
172

173    public void setSessionContext(SessionContext JavaDoc c)
174    {
175       try
176       {
177          Context JavaDoc ic = new InitialContext JavaDoc();
178          carHome = (CarLocalHome)ic.lookup(CAR_NAME);
179       }
180       catch(NamingException JavaDoc ne)
181       {
182          throw new EJBException JavaDoc(ne);
183       }
184    }
185
186    /**
187     * create method
188     *
189     * @ejb:create-method
190     */

191    public void ejbCreate() { }
192    public void ejbActivate() { }
193    public void ejbPassivate() { }
194    public void ejbRemove() { }
195 }
196
Popular Tags