1 22 package org.jboss.test.cmp2.perf.ejb; 23 24 import java.util.Iterator ; 25 import java.util.Calendar ; 26 import java.util.Collection ; 27 import javax.ejb.CreateException ; 28 import javax.ejb.EJBException ; 29 import javax.ejb.FinderException ; 30 import javax.ejb.SessionBean ; 31 import javax.ejb.SessionContext ; 32 import javax.naming.InitialContext ; 33 import javax.naming.Context ; 34 35 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBook; 36 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookEntry; 37 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookHome; 38 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookEntryHome; 39 import org.jboss.logging.Logger; 40 41 45 public class CheckBookMgrBean implements SessionBean 46 { 47 private static Logger log = Logger.getLogger(CheckBookMgrBean.class); 48 private LocalCheckBook checkBook; 49 50 public CheckBookMgrBean() 51 { 52 } 53 54 public void ejbCreate(String account, double balance) throws CreateException 55 { 56 try 57 { 58 InitialContext ctx = new InitialContext (); 59 Context enc = (Context ) ctx.lookup("java:comp/env"); 60 LocalCheckBookHome home = (LocalCheckBookHome) enc.lookup("ejb/LocalCheckBookHome"); 61 try 62 { 63 checkBook = home.findByPrimaryKey(account); 64 } 65 catch(FinderException e) 66 { 67 log.info("Failed to find CheckBook for: "+account); 68 checkBook = home.create(account, balance); 69 LocalCheckBookEntryHome home2 = (LocalCheckBookEntryHome) enc.lookup("ejb/LocalCheckBookEntryHome"); 71 populateCheckBook(home2); 72 } 73 } 74 catch(Exception e) 75 { 76 log.error("Failed to setup CheckBookMgrBean", e); 77 throw new CreateException ("Failed to setup CheckBookMgrBean: "+e.getMessage()); 78 } 79 } 80 81 public void ejbActivate() throws EJBException 82 { 83 } 84 85 public void ejbPassivate() throws EJBException 86 { 87 } 88 89 public void ejbRemove() throws EJBException 90 { 91 } 92 93 public void setSessionContext(SessionContext ctx) throws EJBException 94 { 95 } 96 97 public int getEntryCount() 98 { 99 log.info("Begin getEntryCount"); 100 Collection entries = checkBook.getCheckBookEntries(); 101 int size = entries.size(); 102 log.info("End getEntryCount"); 103 return size; 104 } 105 public double getBalance() 106 { 107 log.info("Begin getBalance"); 108 double total = checkBook.getBalance(); 109 110 Iterator entries = checkBook.getCheckBookEntries().iterator(); 111 while (entries.hasNext()) 112 { 113 LocalCheckBookEntry entry = (LocalCheckBookEntry) entries.next(); 114 total -= entry.getAmount(); 115 } 116 log.info("End getBalance"); 117 118 return total; 119 } 120 public double entryTotalByLogger(String category) 121 { 122 double total = 0; 123 return total; 124 } 125 public double[] entryTotalByMonth(int year) 126 { 127 double[] months = new double[12]; 128 return months; 129 } 130 131 public StringBuffer createAnnualReport(int year) 132 { 133 StringBuffer report = new StringBuffer (); 134 return report; 135 } 136 137 private void populateCheckBook(LocalCheckBookEntryHome home) 138 throws CreateException 139 { 140 Calendar cal = Calendar.getInstance(); 141 Collection entries = checkBook.getCheckBookEntries(); 142 String [] categories = {"Business", "Personal", "Travel", "Expenses", "Misc"}; 143 int entryNo = 0; 144 for(int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month ++) 145 { 146 cal.set(2003, month, 1); 147 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 148 for(int day = 2; day < lastDay; day ++) 149 { 150 long timestamp = cal.getTime().getTime(); 151 for(int n = 0; n < categories.length; n ++) 152 { 153 LocalCheckBookEntry entry = home.create(new Integer (entryNo)); 154 entryNo ++; 155 entry.setAmount(1); 156 entry.setTimestamp(timestamp); 157 entry.setLogger(categories[n]); 158 entries.add(entry); 159 timestamp += 3600 * 1000; 160 } 161 cal.set(2003, month, day); 162 } 163 } 164 } 165 } 166 | Popular Tags |