KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > JavaMailResource


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.management.j2ee;
23
24 import org.jboss.logging.Logger;
25 import org.w3c.dom.Document JavaDoc;
26
27 import javax.management.Attribute JavaDoc;
28 import javax.management.JMException JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MalformedObjectNameException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.InputStream JavaDoc;
36
37 /**
38  * Root class of the JBoss JSR-77 implementation of JavaMailResource.
39  *
40  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
41  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
42  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
43  * @version $Revision: 40550 $
44  */

45 public class JavaMailResource extends J2EEResource
46    implements JavaMailResourceMBean
47 {
48    // Constants -----------------------------------------------------
49
private static Logger log = Logger.getLogger(JavaMailResource.class);
50
51    // Attributes ----------------------------------------------------
52

53    private StateManagement mState;
54    private ObjectName JavaDoc mailServiceName;
55
56    // Static --------------------------------------------------------
57

58    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, String JavaDoc resName,
59                                    ObjectName JavaDoc mailServiceName)
60    {
61       ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
62       ObjectName JavaDoc jsr77Name = null;
63       try
64       {
65          JavaMailResource mailRes = new JavaMailResource(resName, j2eeServerName, mailServiceName);
66          jsr77Name = mailRes.getObjectName();
67          mbeanServer.registerMBean(mailRes, jsr77Name);
68          log.debug("Created JSR-77 JavaMailResource: " + resName);
69       }
70       catch (Exception JavaDoc e)
71       {
72          log.debug("Could not create JSR-77 JavaMailResource: " + resName, e);
73       }
74       return jsr77Name;
75    }
76
77    public static void destroy(MBeanServer JavaDoc mbeanServer, String JavaDoc resName)
78    {
79       try
80       {
81          J2EEManagedObject.removeObject(mbeanServer,
82                  J2EEDomain.getDomainName() + ":" +
83                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JavaMailResource + "," +
84                  "name=" + resName + "," +
85                  "*");
86       }
87       catch (Exception JavaDoc e)
88       {
89          log.debug("Could not destroy JSR-77 JNDIResource: " + resName, e);
90       }
91    }
92
93    // Constructors --------------------------------------------------
94

95    /**
96     * @param resName Name of the JavaMailResource
97     */

98    public JavaMailResource(String JavaDoc resName, ObjectName JavaDoc j2eeServerName,
99                            ObjectName JavaDoc mailServiceName)
100            throws MalformedObjectNameException JavaDoc,
101            InvalidParentException
102    {
103       super(J2EETypeConstants.JavaMailResource, resName, j2eeServerName);
104       this.mailServiceName = mailServiceName;
105       mState = new StateManagement(this);
106    }
107
108    // Public --------------------------------------------------------
109

110    /**
111     * @jmx:managed-attribute
112     */

113    public String JavaDoc getuserName()
114            throws Exception JavaDoc
115    {
116       return (String JavaDoc) server.getAttribute(mailServiceName, "User");
117    }
118
119    /**
120     * @jmx:managed-attribute
121     */

122    public void setuserName(String JavaDoc pName)
123            throws Exception JavaDoc
124    {
125       server.setAttribute(mailServiceName, new Attribute JavaDoc("User", pName));
126    }
127
128    /**
129     * @jmx:managed-attribute
130     */

131    public void setpassword(String JavaDoc pPassword)
132            throws Exception JavaDoc
133    {
134       server.setAttribute(mailServiceName, new Attribute JavaDoc("Password", pPassword));
135    }
136
137    /**
138     * @jmx:managed-attribute
139     */

140    public String JavaDoc getjndiName()
141            throws Exception JavaDoc
142    {
143       return (String JavaDoc) server.getAttribute(mailServiceName, "JNDIName");
144    }
145
146    /**
147     * @jmx:managed-attribute
148     */

149    public void setjndiName(String JavaDoc pName)
150            throws Exception JavaDoc
151    {
152       server.setAttribute(mailServiceName, new Attribute JavaDoc("JNDIName", pName));
153    }
154
155    /**
156     * @jmx:managed-attribute
157     */

158    public String JavaDoc getconfiguration()
159            throws Exception JavaDoc
160    {
161       return server.getAttribute(mailServiceName, "Configuration") + "";
162    }
163
164    /**
165     * @jmx:managed-attribute
166     */

167    public void setconfiguration(String JavaDoc pConfigurationElement)
168            throws Exception JavaDoc
169    {
170       if (pConfigurationElement == null || pConfigurationElement.length() == 0)
171       {
172          pConfigurationElement = "<configuration/>";
173       }
174       DocumentBuilder JavaDoc lParser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
175       InputStream JavaDoc lInput = new ByteArrayInputStream JavaDoc(pConfigurationElement.getBytes());
176       Document JavaDoc lDocument = lParser.parse(lInput);
177       server.setAttribute(mailServiceName, new Attribute JavaDoc("Configuration", lDocument.getDocumentElement()));
178    }
179
180    // javax.managment.j2ee.EventProvider implementation -------------
181

182    public String JavaDoc[] getEventTypes()
183    {
184       return StateManagement.stateTypes;
185    }
186
187    public String JavaDoc getEventType(int pIndex)
188    {
189       if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
190       {
191          return StateManagement.stateTypes[pIndex];
192       }
193       else
194       {
195          return null;
196       }
197    }
198
199    // javax.management.j2ee.StateManageable implementation ----------
200

201    public long getStartTime()
202    {
203       return mState.getStartTime();
204    }
205
206    public int getState()
207    {
208       return mState.getState();
209    }
210    public String JavaDoc getStateString()
211    {
212       return mState.getStateString();
213    }
214
215    public void mejbStart()
216    {
217       try
218       {
219          server.invoke(mailServiceName,
220                  "start",
221                  new Object JavaDoc[]{},
222                  new String JavaDoc[]{});
223       }
224       catch (Exception JavaDoc e)
225       {
226          log.error("start failed", e);
227       }
228    }
229
230    public void mejbStartRecursive()
231    {
232       // No recursive start here
233
try
234       {
235          mejbStart();
236       }
237       catch (Exception JavaDoc e)
238       {
239          log.error("start failed", e);
240       }
241    }
242
243    public void mejbStop()
244    {
245       try
246       {
247          server.invoke(mailServiceName,
248                  "stop",
249                  new Object JavaDoc[]{},
250                  new String JavaDoc[]{});
251       }
252       catch (Exception JavaDoc e)
253       {
254          log.error("Stop of JavaMailResource failed", e);
255       }
256    }
257
258    public void postCreation()
259    {
260       try
261       {
262          server.addNotificationListener(mailServiceName, mState, null, null);
263       }
264       catch (JMException JavaDoc e)
265       {
266          log.debug("Failed to add notification listener", e);
267       }
268       sendNotification(NotificationConstants.OBJECT_CREATED, "Java Mail Resource created");
269    }
270
271    public void preDestruction()
272    {
273       sendNotification(NotificationConstants.OBJECT_DELETED, "Java Mail Resource deleted");
274       // Remove the listener of the target MBean
275
try
276       {
277          server.removeNotificationListener(mailServiceName, mState);
278       }
279       catch (JMException JavaDoc jme)
280       {
281          // When the service is not available anymore then just ignore the exception
282
}
283    }
284
285    // java.lang.Object overrides ------------------------------------
286

287    public String JavaDoc toString()
288    {
289       return "JavaMailResource { " + super.toString() + " } [ " +
290               " ]";
291    }
292 }
293
Popular Tags