001: package org.directwebremoting.create;
002:
003: import java.util.Properties;
004:
005: import javax.naming.Context;
006: import javax.naming.InitialContext;
007: import javax.naming.NamingException;
008:
009: import org.directwebremoting.extend.Creator;
010: import org.directwebremoting.util.LocalUtil;
011: import org.directwebremoting.util.Messages;
012:
013: /**
014: * A Creator that works against EJB3 beans
015: * @author Squishy [Squishy_I at gmx dot net]
016: * @author Joe Walker [joe at getahead dot ltd dot uk]
017: */
018: public class Ejb3Creator extends AbstractCreator implements Creator {
019: /**
020: * The common interface of the Bean.
021: * <b>If you don't have a common interface from which local and remote are
022: * derived, you have to set the bean name manually!</b>
023: * The BeanName is fetched from the part of the String behind the last '.'
024: * @param className The fully qualified class name of the Bean's interface
025: */
026: public void setInterface(String className) {
027: this .className = className;
028: this .bean = className.substring(className.lastIndexOf('.') + 1);
029: }
030:
031: /**
032: * Set the name of the bean for the JNDI lookup
033: * @param bean The JNDI name to lookup
034: */
035: public void setBean(String bean) {
036: this .bean = bean;
037: }
038:
039: /**
040: * Get local or remote interface?
041: * Defaults remote
042: * @param iface Either "local" for the local interface or anything else for the remote
043: */
044: public void setInterfaceType(String iface) {
045: remote = !iface.equalsIgnoreCase(LOCAL);
046: }
047:
048: /**
049: * Set the ending that is appended to the actual bean name fetched from the
050: * interface name (UserMgr --> UserMgrBean). Defaults to "Bean"
051: * @param beanNamePostfix The name to append to a bean before lookup
052: */
053: public void setBeanNamePostfix(String beanNamePostfix) {
054: this .beanNamePostfix = beanNamePostfix;
055: }
056:
057: /* (non-Javadoc)
058: * @see org.directwebremoting.Creator#getType()
059: */
060: public Class<?> getType() {
061: try {
062: return LocalUtil.classForName(className);
063: } catch (ClassNotFoundException ex) {
064: throw new IllegalArgumentException(Messages.getString(
065: "Creator.BeanClassNotFound", className));
066: }
067: }
068:
069: /* (non-Javadoc)
070: * @see org.directwebremoting.Creator#getInstance()
071: */
072: public Object getInstance() throws InstantiationException {
073: String type = remote ? "remote" : "local";
074:
075: Context jndi = null;
076:
077: try {
078: Properties props = new Properties();
079: props.load(getClass().getResourceAsStream(
080: "/jndi.properties"));
081: jndi = new InitialContext(props);
082:
083: return jndi.lookup(bean + beanNamePostfix + "/" + type);
084: } catch (Exception ex) {
085: throw new InstantiationException(bean + "/" + type
086: + " not bound:" + ex.getMessage());
087: } finally {
088: if (jndi != null) {
089: try {
090: jndi.close();
091: } catch (NamingException ex) {
092: // Ignore
093: }
094: }
095: }
096: }
097:
098: /**
099: * Constant for local/remote lookup
100: */
101: private static final String LOCAL = "local";
102:
103: /**
104: * Are we using a remote interface? Default == true
105: */
106: private boolean remote = true;
107:
108: /**
109: * The name of the bean
110: */
111: private String bean;
112:
113: /**
114: * The type of the bean
115: */
116: private String className;
117:
118: /**
119: * A suffix to help lookup
120: */
121: private String beanNamePostfix = "Bean";
122: }
|