KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import javax.management.JMException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 /**
32  * Root class of the JBoss JSR-77 implementation of
33  * {@link javax.management.j2ee.URLResource URLResource}.
34  *
35  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
36  * @version $Revision: 40550 $
37  */

38 public class URLResource extends J2EEResource
39    implements URLResourceMBean
40 {
41    // Constants -----------------------------------------------------
42

43    // Attributes ----------------------------------------------------
44
private static Logger log = Logger.getLogger(URLResource.class);
45
46    private StateManagement mState;
47    private ObjectName JavaDoc mService;
48
49    // Static --------------------------------------------------------
50

51    public static ObjectName JavaDoc create(MBeanServer JavaDoc pServer, String JavaDoc pName, ObjectName JavaDoc pService)
52    {
53       ObjectName JavaDoc lServer = null;
54       try
55       {
56          lServer = (ObjectName JavaDoc) pServer.queryNames(new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
57                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEServer + "," +
58                  "*"),
59                  null).iterator().next();
60       }
61       catch (Exception JavaDoc e)
62       {
63          log.error("Could not create JSR-77 URLResource: " + pName, e);
64          return null;
65       }
66       try
67       {
68          // Now create the URLResource Representant
69
return pServer.createMBean("org.jboss.management.j2ee.URLResource",
70                  null,
71                  new Object JavaDoc[]{
72                     pName,
73                     lServer,
74                     pService
75                  },
76                  new String JavaDoc[]{
77                     String JavaDoc.class.getName(),
78                     ObjectName JavaDoc.class.getName(),
79                     ObjectName JavaDoc.class.getName()
80                  }).getObjectName();
81       }
82       catch (Exception JavaDoc e)
83       {
84          log.error("Could not create JSR-77 URLResource: " + pName, e);
85          return null;
86       }
87    }
88
89    public static void destroy(MBeanServer JavaDoc pServer, String JavaDoc pName)
90    {
91       try
92       {
93          J2EEManagedObject.removeObject(pServer,
94                  J2EEDomain.getDomainName() + ":" +
95                  J2EEManagedObject.TYPE + "=" + J2EETypeConstants.URLResource + "," +
96                  "name=" + pName + "," +
97                  "*");
98       }
99       catch (Exception JavaDoc e)
100       {
101          log.error("Could not destroy JSR-77 URLResource: " + pName, e);
102       }
103    }
104
105    // Constructors --------------------------------------------------
106

107    /**
108     * @param pName Name of the URLResource
109     * @throws InvalidParameterException If list of nodes or ports was null or empty
110     */

111    public URLResource(String JavaDoc pName, ObjectName JavaDoc pServer, ObjectName JavaDoc pService)
112            throws
113            MalformedObjectNameException JavaDoc,
114            InvalidParentException
115    {
116       super(J2EETypeConstants.URLResource, pName, pServer);
117       if (log.isDebugEnabled())
118          log.debug("Service name: " + pService);
119       mService = pService;
120       mState = new StateManagement(this);
121    }
122
123    // javax.managment.j2ee.EventProvider implementation -------------
124

125    public String JavaDoc[] getEventTypes()
126    {
127       return StateManagement.stateTypes;
128    }
129
130    public String JavaDoc getEventType(int pIndex)
131    {
132       if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
133       {
134          return StateManagement.stateTypes[pIndex];
135       }
136       else
137       {
138          return null;
139       }
140    }
141
142    // javax.management.j2ee.StateManageable implementation ----------
143

144    public long getStartTime()
145    {
146       return mState.getStartTime();
147    }
148
149    public int getState()
150    {
151       return mState.getState();
152    }
153    public String JavaDoc getStateString()
154    {
155       return mState.getStateString();
156    }
157
158    public void mejbStart()
159    {
160       try
161       {
162          server.invoke(mService,
163                  "start",
164                  new Object JavaDoc[]{},
165                  new String JavaDoc[]{});
166       }
167       catch (Exception JavaDoc e)
168       {
169          log.error("Start of URL Resource failed", e);
170       }
171    }
172
173    public void mejbStartRecursive()
174    {
175       // No recursive start here
176
mejbStart();
177    }
178
179    public void mejbStop()
180    {
181       try
182       {
183          server.invoke(mService,
184                  "stop",
185                  new Object JavaDoc[]{},
186                  new String JavaDoc[]{});
187       }
188       catch (Exception JavaDoc e)
189       {
190          log.error("Stop of URL Resource failed", e);
191       }
192    }
193
194    public void postCreation()
195    {
196       try
197       {
198          server.addNotificationListener(mService, mState, null, null);
199       }
200       catch (JMException JavaDoc e)
201       {
202          log.debug("Failed to add notification listener", e);
203       }
204       sendNotification(NotificationConstants.OBJECT_CREATED, "URL Resource created");
205    }
206
207    public void preDestruction()
208    {
209       sendNotification(NotificationConstants.OBJECT_DELETED, "URL Resource deleted");
210       // Remove the listener of the target MBean
211
try
212       {
213          server.removeNotificationListener(mService, mState);
214       }
215       catch (JMException JavaDoc jme)
216       {
217          // When the service is not available anymore then just ignore the exception
218
}
219    }
220
221    // java.lang.Object overrides ------------------------------------
222

223    public String JavaDoc toString()
224    {
225       return "URLResource { " + super.toString() + " } [ " +
226               " ]";
227    }
228
229    // Package protected ---------------------------------------------
230

231    // Protected -----------------------------------------------------
232

233    // Private -------------------------------------------------------
234

235    // Inner classes -------------------------------------------------
236
}
237
Popular Tags