Author: dain
Date: Mon Apr 2 17:59:10 2007
New Revision: 524985
URL: http://svn.apache.org/viewvc?view=rev&rev=524985
Log:
GERONIMO-2972: Bind JavaMail Session to Global JNDI
Thanks Christopher M. Cardona
Modified:
geronimo/server/trunk/configs/client/src/plan/plan.xml
geronimo/server/trunk/configs/javamail/src/plan/plan.xml
geronimo/server/trunk/configs/rmi-naming/src/plan/plan.xml
geronimo/server/trunk/modules/geronimo-mail/src/main/java/org/apache/geronimo/mail/MailGBean.java
geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/gjndi/KernelContextGBean.java
Modified: geronimo/server/trunk/configs/client/src/plan/plan.xml
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/configs/client/src/plan/plan.xml?view=diff&rev=524985&r1=524984&r2=524985
==============================================================================
--- geronimo/server/trunk/configs/client/src/plan/plan.xml (original)
+++ geronimo/server/trunk/configs/client/src/plan/plan.xml Mon Apr 2 17:59:10 2007
@@ -35,6 +35,9 @@
<attribute name="nameInNamespace">java:</attribute>
</gbean>
+ <gbean name="GeronimoContext" class="org.apache.geronimo.gjndi.WritableContextGBean">
+ <attribute name="nameInNamespace">ger:</attribute>
+ </gbean>
<gbean name="DefaultThreadPool" class="org.apache.geronimo.pool.ThreadPool">
<attribute name="keepAliveTime">5000</attribute>
Modified: geronimo/server/trunk/configs/javamail/src/plan/plan.xml
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/configs/javamail/src/plan/plan.xml?view=diff&rev=524985&r1=524984&r2=524985
==============================================================================
--- geronimo/server/trunk/configs/javamail/src/plan/plan.xml (original)
+++ geronimo/server/trunk/configs/javamail/src/plan/plan.xml Mon Apr 2 17:59:10 2007
@@ -22,6 +22,7 @@
<gbean name="mail/MailSession" class="org.apache.geronimo.mail.MailGBean">
<attribute name="transportProtocol">smtp</attribute>
+ <attribute name="jndiName">ger:/MailSession</attribute>
<reference name="Protocols"/>
</gbean>
Modified: geronimo/server/trunk/configs/rmi-naming/src/plan/plan.xml
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/configs/rmi-naming/src/plan/plan.xml?view=diff&rev=524985&r1=524984&r2=524985
==============================================================================
--- geronimo/server/trunk/configs/rmi-naming/src/plan/plan.xml (original)
+++ geronimo/server/trunk/configs/rmi-naming/src/plan/plan.xml Mon Apr 2 17:59:10 2007
@@ -38,6 +38,10 @@
<attribute name="nameInNamespace">java:</attribute>
</gbean>
+ <gbean name="GeronimoContext" class="org.apache.geronimo.gjndi.WritableContextGBean">
+ <attribute name="nameInNamespace">ger:</attribute>
+ </gbean>
+
<gbean name="MBeanServerReference" class="org.apache.geronimo.system.jmx.RealMBeanServerReference"/>
<!-- MBean server service -->
Modified: geronimo/server/trunk/modules/geronimo-mail/src/main/java/org/apache/geronimo/mail/MailGBean.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/modules/geronimo-mail/src/main/java/org/apache/geronimo/mail/MailGBean.java?view=diff&rev=524985&r1=524984&r2=524985
==============================================================================
--- geronimo/server/trunk/modules/geronimo-mail/src/main/java/org/apache/geronimo/mail/MailGBean.java
(original)
+++ geronimo/server/trunk/modules/geronimo-mail/src/main/java/org/apache/geronimo/mail/MailGBean.java
Mon Apr 2 17:59:10 2007
@@ -18,6 +18,10 @@
import javax.mail.Authenticator;
import javax.mail.Session;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.naming.Name;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
@@ -59,6 +63,7 @@
private String host;
private String user;
private Boolean debug;
+ private String jndiName;
/**
@@ -76,9 +81,10 @@
* @param host the default Mail server
* @param user the username to provide when connecting to a Mail server
* @param debug the debug setting for Sessions created from this GBean
+ * @param jndiName the JNDI name to which the mail Session should be bound
*/
public MailGBean(String objectName, Collection protocols, Boolean useDefault, Properties
properties, Authenticator authenticator,
- String storeProtocol, String transportProtocol, String host, String
user, Boolean debug) {
+ String storeProtocol, String transportProtocol, String host, String
user, Boolean debug, String jndiName) {
this.objectName = objectName;
this.protocols = protocols;
setUseDefault(useDefault);
@@ -89,7 +95,7 @@
setHost(host);
setUser(user);
setDebug(debug);
-
+ setJndiName(jndiName);
}
/**
@@ -290,6 +296,22 @@
this.debug = debug;
}
+ /**
+ * Gets the JNDI name to which the mail Session should be bound
+ * @return the JNDI name to which the mail Session should be bound
+ */
+ public String getJndiName() {
+ return jndiName;
+ }
+
+ /**
+ * Sets the JNDI name to which the mail Session should be bound
+ * @param jndiName the JNDI name to which the mail Session should be bound
+ */
+ public void setJndiName(String jndiName) {
+ this.jndiName = jndiName;
+ }
+
public Object $getResource() {
Properties props = new Properties(properties);
@@ -330,14 +352,59 @@
+ " JavaMail Session "
+ (authenticator == null ? "without" : "with")
+ " authenticator");
+
+ String jndiName = getJndiName();
+ if (jndiName != null && jndiName.length() > 0) {
+ // first get the resource incase there are exceptions
+ Object value = $getResource();
+
+ // get the initial context
+ Context context = new InitialContext();
+ Name parsedName = context.getNameParser("").parse(jndiName);
+
+ // create intermediate contexts
+ for (int i = 1; i < parsedName.size(); i++) {
+ Name contextName = parsedName.getPrefix(i);
+ if (!bindingExists(context, contextName)) {
+ context.createSubcontext(contextName);
+ }
+ }
+
+ // bind
+ context.bind(jndiName, value);
+ log.info("JavaMail session bound to " + jndiName);
+ }
}
public void doStop() throws Exception {
log.debug("Stopped " + objectName);
+ stop();
}
public void doFail() {
log.warn("Failed " + objectName);
+ stop();
+ }
+
+ private void stop() {
+ String jndiName = getJndiName();
+ if (jndiName != null && jndiName.length() > 0) {
+ try {
+ Context context = new InitialContext();
+ context.unbind(jndiName);
+ log.info("JavaMail session unbound from " + jndiName);
+ } catch (NamingException e) {
+ // we tried... this is a common error which occurs during shutdown due to
ordering
+ }
+ }
+ }
+
+ private static boolean bindingExists(Context context, Name contextName) {
+ try {
+ return context.lookup(contextName) != null;
+ } catch (NamingException e) {
+ }
+ return false;
}
/**
@@ -374,6 +441,7 @@
infoFactory.addAttribute("host", String.class, true);
infoFactory.addAttribute("user", String.class, true);
infoFactory.addAttribute("debug", Boolean.class, true);
+ infoFactory.addAttribute("jndiName", String.class, true);
infoFactory.addOperation("$getResource");
infoFactory.addOperation("getProtocols");
infoFactory.addInterface(JavaMailResource.class);
@@ -387,7 +455,8 @@
"transportProtocol",
"host",
"user",
- "debug"});
+ "debug",
+ "jndiName"});
GBEAN_INFO = infoFactory.getBeanInfo();
}
Modified: geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/gjndi/KernelContextGBean.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/gjndi/KernelContextGBean.java?view=diff&rev=524985&r1=524984&r2=524985
==============================================================================
--- geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/gjndi/KernelContextGBean.java
(original)
+++ geronimo/server/trunk/modules/geronimo-naming/src/main/java/org/apache/geronimo/gjndi/KernelContextGBean.java
Mon Apr 2 17:59:10 2007
@@ -52,7 +52,7 @@
private final Map bindingsByAbstractName = new HashMap();
public KernelContextGBean(String nameInNamespace, AbstractNameQuery abstractNameQuery,
Kernel kernel) throws NamingException {
- super(nameInNamespace, Collections.EMPTY_MAP, ContextAccess.UNMODIFIABLE, false);
+ super(nameInNamespace, Collections.EMPTY_MAP, ContextAccess.MODIFIABLE, false);
this.abstractNameQuery = abstractNameQuery;
this.kernel = kernel;
}
|