/* * ==================================================================== * * TORPEDO * A Testbed of Object Relational Products for Enterprise Distributed Objects * Copyright (c) 2004 The Middleware Company All Rights Reserved * @author Bruce Martin * @version 8.25.04 * * ==================================================================== */ package com.middleware_company.torpedo.auction; import javax.ejb.*; import java.util.Collection; import java.util.Iterator; import java.util.ArrayList; import javax.naming.*; import javax.rmi.PortableRemoteObject; import java.rmi.RemoteException; public class AuctionServiceBean implements SessionBean { SessionContext sessionContext; Persistence persistentAuctions; private static Context _context=null; public void ejbCreate() throws CreateException { try { String className = (String) PortableRemoteObject.narrow (getContext ().lookup ("java:comp/env/PersistenceClass"),String.class); System.err.println("About to load "+className); Class persistenceClass = Class.forName(className); System.err.println("About to create new instance"); persistentAuctions = (Persistence) persistenceClass.newInstance(); System.err.println("Have new instance "+persistentAuctions); } catch (Exception e) { System.err.println("Exception "+e); throw new CreateException(e.getMessage()); } } public void ejbRemove() { } public void ejbActivate() { } public void ejbPassivate() { } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } public String placeBid(String auctionID, String bidID, String userID, Float amount, Float maxAmount) throws AuctionServiceException { try { if (persistentAuctions instanceof com.middleware_company.torpedo.auction.jdo.Persistence) ((com.middleware_company.torpedo.auction.jdo.Persistence)persistentAuctions).requestAllHollow(null); User user = persistentAuctions.getUser(userID); Auction auction = persistentAuctions.getAuction(auctionID); Bid newBid = persistentAuctions.createBid(bidID,auction,user,amount,maxAmount); // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. persistentAuctions.close(); } catch (Exception e) { sessionContext.setRollbackOnly(); // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); throw new AuctionServiceException(e); } return null; } public String place2bids(String auctionID1, String auctionID2, String bidID1, String bidID2, String userID, Float amount1, Float amount2, Float maxAmount1, Float maxAmount2) throws AuctionServiceException { try { if (persistentAuctions instanceof com.middleware_company.torpedo.auction.jdo.Persistence) ((com.middleware_company.torpedo.auction.jdo.Persistence)persistentAuctions).requestAllHollow(null); User user = persistentAuctions.getUser(userID); Auction auction1 = persistentAuctions.getAuction(auctionID1); Bid newBid1 = persistentAuctions.createBid(bidID1,auction1,user,amount1,maxAmount1); Auction auction2 = persistentAuctions.getAuction(auctionID2); Bid newBid2 = persistentAuctions.createBid(bidID2,auction2,user,amount2,maxAmount2); // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. persistentAuctions.close(); } catch (Exception e) { sessionContext.setRollbackOnly(); // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); throw new AuctionServiceException(e); } return null; } public AuctionInfo listAuction(String auctionID) throws AuctionServiceException { try { Auction auction = persistentAuctions.getDetailAuction(auctionID); AuctionInfo ai = persistentAuctions.createAuctionInfo(auction,false); // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. persistentAuctions.close(); return ai; } catch (Exception e) { // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); sessionContext.setRollbackOnly(); throw new AuctionServiceException(e); } } public AuctionInfo listAuctionTwiceWithTransaction(String auctionID) throws AuctionServiceException { try { AuctionInfo ai = ((AuctionService)sessionContext.getEJBObject()).listAuction(auctionID); ai = ((AuctionService)sessionContext.getEJBObject()).listAuction(auctionID); return ai; } catch (Exception e) { // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); throw new AuctionServiceException(e); } } public AuctionInfo listAuctionTwiceWithoutTransaction(String auctionID) throws AuctionServiceException { try { AuctionInfo ai = ((AuctionService)sessionContext.getEJBObject()).listAuction(auctionID); ai = ((AuctionService)sessionContext.getEJBObject()).listAuction(auctionID); return ai; } catch (Exception e) { // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); throw new AuctionServiceException(e); } } public AuctionInfo listPartialAuction(String auctionID) throws AuctionServiceException { try { Auction auction = persistentAuctions.getPartialAuction(auctionID); AuctionInfo ai = persistentAuctions.createAuctionInfo(auction,true); // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. persistentAuctions.close(); return ai; } catch (Exception e) { // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); sessionContext.setRollbackOnly(); throw new AuctionServiceException(e); } } public CollectionWithSQL findAllAuctions() throws AuctionServiceException { try { Collection auctions = persistentAuctions.findAllAuctions(); Iterator i = auctions.iterator(); CollectionWithSQL result = new CollectionWithSQL( new ArrayList(auctions.size())); while (i.hasNext()) { result.theCollection.add(persistentAuctions.createAuctionInfo((Auction)i.next(),false)); } // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. persistentAuctions.close(); return result; } catch (Exception e) { // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); throw new AuctionServiceException(e); } } public CollectionWithSQL findHighBids(String auctionID) throws AuctionServiceException { try { Collection bids = persistentAuctions.findHighBids(auctionID); Iterator i = bids.iterator(); CollectionWithSQL result = new CollectionWithSQL( new ArrayList(bids.size())); while (i.hasNext()) { result.theCollection.add(new BidInfo((Bid)i.next())); } // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. persistentAuctions.close(); return result; } catch (Exception e) { // The next operation is for Object-Relational mapping software that needs to be explicitly cleaned up. // Those that do not, (e.g. CMP, JDO and Toplink) simply ignore this operation. if (persistentAuctions!=null) persistentAuctions.close(); throw new AuctionServiceException(e); } } private static Context getContext () throws NamingException { if (_context == null) _context = new InitialContext (); return _context; } }