KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > create > Ejb3Creator


1 package org.directwebremoting.create;
2
3 import java.util.Properties JavaDoc;
4
5 import javax.naming.Context JavaDoc;
6 import javax.naming.InitialContext JavaDoc;
7
8 import org.directwebremoting.extend.Creator;
9 import org.directwebremoting.util.LocalUtil;
10 import org.directwebremoting.util.Messages;
11
12 /**
13  * A Creator that works against EJB3 beans
14  * @author Squishy [Squishy_I at gmx dot net]
15  * @author Joe Walker [joe at getahead dot ltd dot uk]
16  */

17 public class Ejb3Creator extends AbstractCreator implements Creator
18 {
19     /**
20      * The common interface of the Bean.
21      * <b>If you don't have a common interface from which local and remote are
22      * derived, you have to set the bean name manually!</b>
23      * The BeanName is fetched from the part of the String behind the last '.'
24      * @param className The fully qualified class name of the Bean's interface
25      */

26     public void setInterface(String JavaDoc className)
27     {
28         this.className = className;
29         this.bean = className.substring(className.lastIndexOf('.') + 1);
30     }
31
32     /**
33      * Set the name of the bean for the JNDI lookup
34      * @param bean
35      */

36     public void setBean(String JavaDoc bean)
37     {
38         this.bean = bean;
39     }
40
41     /**
42      * Get local or remote interface?
43      * Defaults remote
44      * @param iface Either "local" for the local interface or anything else for the remote
45      */

46     public void setInterfaceType(String JavaDoc iface)
47     {
48         remote = !iface.equalsIgnoreCase(LOCAL);
49     }
50
51     /**
52      * Set the ending that is appended to the actual bean name fetched from the
53      * interface name (UserMgr --> UserMgrBean). Defaults to "Bean"
54      * @param beanNamePostfix The name to append to a bean before lookup
55      */

56     public void setBeanNamePostfix(String JavaDoc beanNamePostfix)
57     {
58         this.beanNamePostfix = beanNamePostfix;
59     }
60
61     /* (non-Javadoc)
62      * @see org.directwebremoting.Creator#getType()
63      */

64     public Class JavaDoc getType()
65     {
66         try
67         {
68             return LocalUtil.classForName(className);
69         }
70         catch (ClassNotFoundException JavaDoc ex)
71         {
72             throw new IllegalArgumentException JavaDoc(Messages.getString("Creator.BeanClassNotFound", className));
73         }
74     }
75
76     /* (non-Javadoc)
77      * @see org.directwebremoting.Creator#getInstance()
78      */

79     public Object JavaDoc getInstance() throws InstantiationException JavaDoc
80     {
81         String JavaDoc type = remote ? "remote" : "local";
82
83         try
84         {
85             Properties JavaDoc props = new Properties JavaDoc();
86             props.load(getClass().getResourceAsStream("/jndi.properties"));
87             Context JavaDoc jndi = new InitialContext JavaDoc(props);
88
89             return jndi.lookup(bean + beanNamePostfix + "/" + type);
90         }
91         catch (Exception JavaDoc ex)
92         {
93             throw new InstantiationException JavaDoc(bean + "/" + type + " not bound:" + ex.getMessage());
94         }
95     }
96
97     /**
98      * Constant for local/remote lookup
99      */

100     private final static String JavaDoc LOCAL = "local";
101
102     /**
103      * Are we using a remote interface? Default == true
104      */

105     private boolean remote = true;
106
107     /**
108      * The name of the bean
109      */

110     private String JavaDoc bean;
111
112     /**
113      * The type of the bean
114      */

115     private String JavaDoc className;
116
117     /**
118      * A suffix to help lookup
119      */

120     private String JavaDoc beanNamePostfix = "Bean";
121 }
122
Popular Tags