package org.jboss.management.j2ee;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import javax.management.Attribute;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class JavaMailResource
extends J2EEResource
implements JavaMailResourceMBean
{
private static Logger log = Logger.getLogger(JavaMailResource.class);
private StateManagement mState;
private ObjectName mailServiceName;
public static ObjectName create(MBeanServer mbeanServer, String resName,
ObjectName mailServiceName)
{
ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
ObjectName jsr77Name = null;
try
{
JavaMailResource mailRes = new JavaMailResource(resName, j2eeServerName, mailServiceName);
jsr77Name = mailRes.getObjectName();
mbeanServer.registerMBean(mailRes, jsr77Name);
log.debug("Created JSR-77 JavaMailResource: " + resName);
}
catch (Exception e)
{
log.debug("Could not create JSR-77 JavaMailResource: " + resName, e);
}
return jsr77Name;
}
public static void destroy(MBeanServer mbeanServer, String resName)
{
try
{
J2EEManagedObject.removeObject(mbeanServer,
J2EEDomain.getDomainName() + ":" +
J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JavaMailResource + "," +
"name=" + resName + "," +
"*");
}
catch (Exception e)
{
log.debug("Could not destroy JSR-77 JNDIResource: " + resName, e);
}
}
public JavaMailResource(String resName, ObjectName j2eeServerName,
ObjectName mailServiceName)
throws MalformedObjectNameException,
InvalidParentException
{
super(J2EETypeConstants.JavaMailResource, resName, j2eeServerName);
this.mailServiceName = mailServiceName;
mState = new StateManagement(this);
}
public String getuserName()
throws Exception
{
return (String) server.getAttribute(mailServiceName, "User");
}
public void setuserName(String pName)
throws Exception
{
server.setAttribute(mailServiceName, new Attribute("User", pName));
}
public void setpassword(String pPassword)
throws Exception
{
server.setAttribute(mailServiceName, new Attribute("Password", pPassword));
}
public String getjndiName()
throws Exception
{
return (String) server.getAttribute(mailServiceName, "JNDIName");
}
public void setjndiName(String pName)
throws Exception
{
server.setAttribute(mailServiceName, new Attribute("JNDIName", pName));
}
public String getconfiguration()
throws Exception
{
return server.getAttribute(mailServiceName, "Configuration") + "";
}
public void setconfiguration(String pConfigurationElement)
throws Exception
{
if (pConfigurationElement == null || pConfigurationElement.length() == 0)
{
pConfigurationElement = "<configuration/>";
}
DocumentBuilder lParser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream lInput = new ByteArrayInputStream(pConfigurationElement.getBytes());
Document lDocument = lParser.parse(lInput);
server.setAttribute(mailServiceName, new Attribute("Configuration", lDocument.getDocumentElement()));
}
public String[] getEventTypes()
{
return StateManagement.stateTypes;
}
public String getEventType(int pIndex)
{
if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
{
return StateManagement.stateTypes[pIndex];
}
else
{
return null;
}
}
public long getStartTime()
{
return mState.getStartTime();
}
public int getState()
{
return mState.getState();
}
public void mejbStart()
{
try
{
server.invoke(mailServiceName,
"start",
new Object[]{},
new String[]{});
}
catch (Exception e)
{
log.error("start failed", e);
}
}
public void mejbStartRecursive()
{
try
{
mejbStart();
}
catch (Exception e)
{
log.error("start failed", e);
}
}
public void mejbStop()
{
try
{
server.invoke(mailServiceName,
"stop",
new Object[]{},
new String[]{});
}
catch (Exception e)
{
log.error("Stop of JavaMailResource failed", e);
}
}
public void postCreation()
{
try
{
server.addNotificationListener(mailServiceName, mState, null, null);
}
catch (JMException e)
{
log.debug("Failed to add notification listener", e);
}
sendNotification(NotificationConstants.OBJECT_CREATED, "Java Mail Resource created");
}
public void preDestruction()
{
sendNotification(NotificationConstants.OBJECT_DELETED, "Java Mail Resource deleted");
try
{
server.removeNotificationListener(mailServiceName, mState);
}
catch (JMException jme)
{
}
}
public String toString()
{
return "JavaMailResource { " + super.toString() + " } [ " +
" ]";
}
}