001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.foedeployer.ejb.ql;
023:
024: import java.sql.Date;
025: import java.util.Collection;
026: import java.util.HashSet;
027: import java.util.Set;
028: import java.util.Iterator;
029: import java.util.ArrayList;
030:
031: import java.rmi.RemoteException;
032:
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035: import javax.ejb.EJBException;
036: import javax.ejb.CreateException;
037: import javax.ejb.FinderException;
038: import javax.ejb.RemoveException;
039:
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043:
044: import javax.rmi.PortableRemoteObject;
045:
046: import org.apache.log4j.Category;
047:
048: /**
049: * Car catalog session bean.
050: *
051: * @ejb.bean
052: * type="Stateless"
053: * name="CarCatalog"
054: * jndi-name="CarCatalogEJB.CarCatalogHome"
055: * generate="true"
056: *
057: * @ejb.ejb-ref
058: * ejb-name="Car"
059: * view-type="local"
060: *
061: * @ejb.transaction type="Required"
062: */
063: public class CarCatalogBean implements SessionBean {
064: // Attributes --------------------------------------------------
065: static Category log = Category.getInstance(CarCatalogBean.class);
066: static String CAR_NAME = "java:comp/env/ejb/Car";
067: private CarLocalHome carHome;
068:
069: // Business methods ---------------------------------------------
070: /**
071: * Creates a car
072: *
073: * @ejb.interface-method
074: */
075: public void createCar(String number, String color, int year) {
076: try {
077: carHome.create(number, color, year);
078: } catch (CreateException ce) {
079: log.debug("Exception in create(): ", ce);
080: throw new EJBException(ce);
081: }
082: }
083:
084: /**
085: * Removes a car if exists
086: *
087: * @ejb.interface-method
088: */
089: public void removeCarIfExists(String number) {
090: try {
091: CarLocal car = carHome.findByPrimaryKey(number);
092: car.remove();
093: } catch (Exception e) {
094: log.debug("Exception while removing car with number "
095: + number + ": ", e);
096: }
097: }
098:
099: /**
100: * Finds all car numbers
101: *
102: * @ejb.interface-method
103: */
104: public Collection getAllCarNumbers() {
105: Collection allNumbers = new ArrayList();
106: try {
107: Collection allCars = carHome.findAll();
108: for (Iterator iter = allCars.iterator(); iter.hasNext();) {
109: CarLocal car = (CarLocal) iter.next();
110: allNumbers.add(car.getNumber());
111: }
112: } catch (Exception e) {
113: log.debug("Exception in getAllCarNumbers(): ", e);
114: throw new EJBException(e);
115: }
116: return allNumbers;
117: }
118:
119: /**
120: * Finds car numbers by color
121: *
122: * @ejb.interface-method
123: */
124: public Collection getCarsWithColor(String color) {
125: Collection result = new ArrayList();
126: try {
127: Collection cars = carHome.findByColor(color);
128: for (Iterator iter = cars.iterator(); iter.hasNext();) {
129: CarLocal car = (CarLocal) iter.next();
130: result.add(car.getNumber());
131: }
132: } catch (Exception e) {
133: log.debug("Exception in getCarsWithColor(): ", e);
134: throw new EJBException(e);
135: }
136: return result;
137: }
138:
139: /**
140: * Finds car made after year
141: *
142: * @ejb.interface-method
143: */
144: public Collection getCarsAfterYear(int year) {
145: Collection result = new ArrayList();
146: try {
147: Collection cars = carHome.findAfterYear(year);
148: for (Iterator iter = cars.iterator(); iter.hasNext();) {
149: CarLocal car = (CarLocal) iter.next();
150: result.add(car.getNumber());
151: }
152: } catch (Exception e) {
153: log.debug("Exception in getCarsAfterYear(): ", e);
154: throw new EJBException(e);
155: }
156: return result;
157: }
158:
159: // SessionBean implementation -------------------------------------
160:
161: public void setSessionContext(SessionContext c) {
162: try {
163: Context ic = new InitialContext();
164: carHome = (CarLocalHome) ic.lookup(CAR_NAME);
165: } catch (NamingException ne) {
166: throw new EJBException(ne);
167: }
168: }
169:
170: /**
171: * create method
172: *
173: * @ejb:create-method
174: */
175: public void ejbCreate() {
176: }
177:
178: public void ejbActivate() {
179: }
180:
181: public void ejbPassivate() {
182: }
183:
184: public void ejbRemove() {
185: }
186: }
|