1 7 package org.jboss.test.foedeployer.ejb.ql; 8 9 10 import java.sql.Date ; 11 import java.util.Collection ; 12 import java.util.HashSet ; 13 import java.util.Set ; 14 import java.util.Iterator ; 15 import java.util.ArrayList ; 16 17 import java.rmi.RemoteException ; 18 19 import javax.ejb.SessionBean ; 20 import javax.ejb.SessionContext ; 21 import javax.ejb.EJBException ; 22 import javax.ejb.CreateException ; 23 import javax.ejb.FinderException ; 24 import javax.ejb.RemoveException ; 25 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 30 import javax.rmi.PortableRemoteObject ; 31 32 import org.apache.log4j.Category; 33 34 49 public class CarCatalogBean 50 implements SessionBean 51 { 52 static Category log = Category.getInstance( CarCatalogBean.class ); 54 static String CAR_NAME = "java:comp/env/ejb/Car"; 55 private CarLocalHome carHome; 56 57 63 public void createCar( String number, String color, int year ) 64 { 65 try 66 { 67 carHome.create( number, color, year ); 68 } 69 catch( CreateException ce ) 70 { 71 log.debug( "Exception in create(): ", ce ); 72 throw new EJBException ( ce ); 73 } 74 } 75 76 81 public void removeCarIfExists( String number ) 82 { 83 try 84 { 85 CarLocal car = carHome.findByPrimaryKey( number ); 86 car.remove(); 87 } 88 catch( Exception e ) 89 { 90 log.debug( "Exception while removing car with number " 91 + number + ": ", e ); 92 } 93 } 94 95 100 public Collection getAllCarNumbers() 101 { 102 Collection allNumbers = new ArrayList (); 103 try 104 { 105 Collection allCars = carHome.findAll(); 106 for( Iterator iter = allCars.iterator(); iter.hasNext(); ) 107 { 108 CarLocal car = (CarLocal) iter.next(); 109 allNumbers.add( car.getNumber() ); 110 } 111 } 112 catch( Exception e ) 113 { 114 log.debug( "Exception in getAllCarNumbers(): ", e ); 115 throw new EJBException ( e ); 116 } 117 return allNumbers; 118 } 119 120 125 public Collection getCarsWithColor( String color ) 126 { 127 Collection result = new ArrayList (); 128 try 129 { 130 Collection cars = carHome.findByColor( color ); 131 for( Iterator iter = cars.iterator(); iter.hasNext(); ) 132 { 133 CarLocal car = (CarLocal) iter.next(); 134 result.add( car.getNumber() ); 135 } 136 } 137 catch( Exception e ) 138 { 139 log.debug( "Exception in getCarsWithColor(): ", e ); 140 throw new EJBException ( e ); 141 } 142 return result; 143 } 144 145 150 public Collection getCarsAfterYear( int year ) 151 { 152 Collection result = new ArrayList (); 153 try 154 { 155 Collection cars = carHome.findAfterYear( year ); 156 for( Iterator iter = cars.iterator(); iter.hasNext(); ) 157 { 158 CarLocal car = (CarLocal) iter.next(); 159 result.add( car.getNumber() ); 160 } 161 } 162 catch( Exception e ) 163 { 164 log.debug( "Exception in getCarsAfterYear(): ", e ); 165 throw new EJBException ( e ); 166 } 167 return result; 168 } 169 170 171 173 public void setSessionContext(SessionContext c) 174 { 175 try 176 { 177 Context ic = new InitialContext (); 178 carHome = (CarLocalHome)ic.lookup(CAR_NAME); 179 } 180 catch(NamingException ne) 181 { 182 throw new EJBException (ne); 183 } 184 } 185 186 191 public void ejbCreate() { } 192 public void ejbActivate() { } 193 public void ejbPassivate() { } 194 public void ejbRemove() { } 195 } 196 | Popular Tags |