📄 jobbean.java
字号:
package data;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
public abstract class JobBean implements EntityBean {
private JobLocalHome jobHome; // reference to own home;
// should be able to get through ctx.getEJBHome(),
// but J2EE RI returns null
private CustomerLocalHome customerHome;
private CustomerLocal customerObj; // derived
public abstract void setRef(String ref);
public abstract String getRef();
public abstract void setCustomer(String customer);
public abstract String getCustomer();
public CustomerLocal getCustomerObj() {
return customerObj;
}
public abstract String getDescription();
public abstract void setDescription(String description);
public abstract LocationLocal getLocation();
public abstract void setLocation(LocationLocal location);
public abstract Collection getSkills();
public abstract void setSkills(Collection skills);
// EJB methods start here
public JobPK ejbCreate(String ref, String customer) throws CreateException {
// validate customer login is valid.
try {
customerObj = customerHome.findByPrimaryKey(customer);
} catch (FinderException ex) {
error("Invalid customer.", ex);
}
JobPK key = new JobPK(ref,customerObj.getLogin());
// for BMP, there was a workaround here, namely to call ejbFindByPrimaryKey
// under CMP, cannot call since doesn't exist.
// instead, use jobHome interface ...
try {
jobHome.findByPrimaryKey(key);
throw new CreateException("Duplicate job name: "+key);
}
catch (FinderException ex) {}
setRef(ref);
setCustomer(customer);
setDescription(null);
setLocation(null);
return key;
}
public void ejbPostCreate(String ref, String customer) { }
public void ejbHomeDeleteByCustomer(String customer) {
// an alternative might have been to define a cascade delete relationship
// from customer -> job; however, the customer / job relationship is
// bean-managed rather than container-managed.
try {
// ctx.getEJBHome() returns null
// Collection col = ((JobLocalHome)ctx.getEJBHome()).findByCustomer(customer);
// work-around; access own home through JNDI
Collection col = this.jobHome.findByCustomer(customer);
for (Iterator iter = col.iterator(); iter.hasNext(); ) {
JobLocal job = (JobLocal)iter.next();
// remove job from collection
iter.remove();
// remove job itself
job.remove();
}
}
catch (FinderException e) {
error("Error removing all jobs for " + customer, e);
}
// needed because of the explicit job.remove()
catch (RemoveException e) {
error("Error explicitly removing job for " + customer, e);
}
System.out.println("JobBean: ejbHomeDeleteByCustomer() - end");
}
public void ejbLoad() {
JobPK key = (JobPK)ctx.getPrimaryKey();
try {
this.customerObj = customerHome.findByPrimaryKey(getCustomer()); // derived
}
catch (FinderException e) {
error("Error in ejbLoad (invalid customer) for " + key, e);
}
}
public void ejbStore() { }
public void ejbPassivate() {
setRef(null);
setCustomer(null);
customerObj = null;
setDescription(null);
setLocation(null);
}
public void ejbActivate() { }
public void ejbRemove() { }
private EntityContext ctx;
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
InitialContext ic = null;
try {
ic = new InitialContext();
customerHome = (CustomerLocalHome)ic.lookup("java:comp/env/ejb/CustomerLocal");
jobHome = (JobLocalHome)ic.lookup("java:comp/env/ejb/JobLocal");
}
catch (NamingException ex) {
error("Error looking up depended EJB or resource", ex);
return;
}
}
public void unsetEntityContext() {
this.ctx = null;
customerHome = null;
jobHome = null;
}
private void error(String msg, Exception ex) {
String s = "JobBean: " + msg + "\n" + ex;
System.out.println(s);
throw new EJBException(s, ex);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -