001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: CarBean.java 6240 2005-02-07 17:48:55Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.cascade;
027:
028: import java.rmi.RemoteException;
029: import java.util.Collection;
030: import java.util.Vector;
031: import javax.ejb.CreateException;
032: import javax.ejb.EJBException;
033: import javax.ejb.EntityBean;
034: import javax.ejb.EntityContext;
035: import javax.ejb.RemoveException;
036: import javax.naming.InitialContext;
037: import javax.naming.NamingException;
038:
039: import org.objectweb.jonas.common.Log;
040: import org.objectweb.util.monolog.api.BasicLevel;
041: import org.objectweb.util.monolog.api.Logger;
042:
043: /**
044: * Implementation for bean Car
045: * @author Ph Durieux
046: */
047: public abstract class CarBean implements EntityBean {
048:
049: static protected Logger logger = null;
050: protected EntityContext ejbContext = null;
051: protected InsuranceHL insuranceHL = null;
052: protected InvoiceHL invoiceHL = null;
053:
054: public Object ejbCreate(String number, byte type, String name)
055: throws CreateException {
056: logger.log(BasicLevel.DEBUG, "");
057: setNumber(number);
058: setType(type);
059: setName(name);
060: return null;
061: }
062:
063: public void ejbPostCreate(String number, byte type, String name)
064: throws CreateException {
065: logger.log(BasicLevel.DEBUG, "");
066: InsuranceL ins = insuranceHL.create("000" + number);
067: setInsurance(ins);
068: }
069:
070: // persistent fields
071: public abstract Integer getId();
072:
073: public abstract void setId(Integer id);
074:
075: public abstract String getNumber();
076:
077: public abstract void setNumber(String number);
078:
079: public abstract byte getType();
080:
081: public abstract void setType(byte type);
082:
083: public abstract String getName();
084:
085: public abstract void setName(String name);
086:
087: // persistent relationships
088: public abstract InsuranceL getInsurance();
089:
090: public abstract void setInsurance(InsuranceL i);
091:
092: public abstract CustomerL getCustomer();
093:
094: public abstract void setCustomer(CustomerL c);
095:
096: public abstract java.util.Collection getInvoices();
097:
098: public abstract void setInvoices(java.util.Collection invoices);
099:
100: public void addInvoice(String number) throws NamingException,
101: CreateException {
102: logger.log(BasicLevel.DEBUG, "");
103: InvoiceL invoice = invoiceHL.create(number);
104: Collection invoices = getInvoices();
105: invoices.add(invoice);
106: }
107:
108: public void setEntityContext(EntityContext ec) {
109: if (logger == null)
110: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
111: logger.log(BasicLevel.DEBUG, "");
112: ejbContext = ec;
113: try {
114: InitialContext cntx = new InitialContext();
115: insuranceHL = (InsuranceHL) cntx
116: .lookup("java:comp/env/ejb/InsuranceHomeLocal");
117: invoiceHL = (InvoiceHL) cntx
118: .lookup("java:comp/env/ejb/InvoiceHomeLocal");
119: } catch (Exception e) {
120: throw new javax.ejb.EJBException(e);
121: }
122: }
123:
124: public void unsetEntityContext() {
125: logger.log(BasicLevel.DEBUG, "");
126: ejbContext = null;
127: }
128:
129: public void ejbLoad() {
130: logger.log(BasicLevel.DEBUG, "");
131: checkCustomerAccess();
132: }
133:
134: public void ejbStore() {
135: logger.log(BasicLevel.DEBUG, "");
136: checkCustomerAccess();
137: }
138:
139: public void ejbActivate() {
140: logger.log(BasicLevel.DEBUG, "");
141: // This cannot be done: See EJB specs
142: // A bean must not attempt to access its state here.
143: //checkCustomerAccess();
144: }
145:
146: public void ejbPassivate() {
147: logger.log(BasicLevel.DEBUG, "");
148: // This cannot be done: See EJB specs
149: // A bean must not attempt to access its state here.
150: //checkCustomerAccess();
151: }
152:
153: /**
154: * this instance is being removed.
155: * we must be able to access bean fields here, including the CMRs
156: */
157: public void ejbRemove() throws javax.ejb.RemoveException {
158: logger.log(BasicLevel.DEBUG, "");
159: InsuranceL myins = getInsurance();
160: if (myins == null) {
161: logger.log(BasicLevel.ERROR, "CMR field Insurance is null");
162: throw new RemoveException(
163: "Cannot access CMR field Insurance inside ejbRemove");
164: }
165: String insnumber = myins.getNumber();
166: String expect = "000" + getNumber();
167: if (!expect.equals(insnumber)) {
168: throw new RemoveException("Bad insurance number:"
169: + insnumber);
170: }
171: checkCustomerAccess();
172: }
173:
174: protected void checkCustomerAccess() {
175: logger.log(BasicLevel.DEBUG, "");
176: CustomerL mycust = getCustomer();
177: if (mycust == null) {
178: logger.log(BasicLevel.ERROR, "CMR field Customer is null");
179: throw new EJBException("CMR field Customer is null");
180: }
181: Name n = mycust.getName();
182: if (n == null || n.getLastName().length() == 0) {
183: logger.log(BasicLevel.ERROR, "Cannot get customer name");
184: throw new EJBException("Cannot get customer name");
185: }
186: }
187: }
|