KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > ant > managers > ComponentAntTaskAbstract


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: AntTaskComponent.java 09:08:21 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.tools.ant.managers;
23
24 import javax.management.MBeanServerConnection JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.management.remote.JMXConnector JavaDoc;
27
28 import org.apache.tools.ant.BuildException;
29 import org.objectweb.petals.tools.ant.util.JBIJMXConnectorUtil;
30
31 /**
32  * This class is used to gathers the operation on the components
33  *
34  * @author ddesjardins - eBMWebsourcing
35  */

36 public abstract class ComponentAntTaskAbstract extends JBIAntTaskAbstract {
37
38     /**
39      * Action : shutdown the component
40      */

41     protected static final String JavaDoc SHUTDOWN = "shutDown";
42
43     /**
44      * Action : start the component
45      */

46     protected static final String JavaDoc START = "start";
47
48     /**
49      * Action : stop the component
50      */

51     protected static final String JavaDoc STOP = "stop";
52
53     /**
54      * Name of the JBI component in the JBI environment
55      */

56     protected String JavaDoc name;
57
58     /**
59      * Perform an action
60      *
61      * @param action
62      * action to perform
63      */

64     protected void performAction(String JavaDoc action) {
65         if (name == null) {
66             throw new BuildException("Required attribute : name");
67         }
68         try {
69             JMXConnector JavaDoc connector = JBIJMXConnectorUtil.getConnection(host,
70                     port, username, password);
71             MBeanServerConnection JavaDoc connection = connector
72                     .getMBeanServerConnection();
73             Object JavaDoc[] objects = new Object JavaDoc[0];
74             String JavaDoc[] strings = new String JavaDoc[0];
75             ObjectName JavaDoc objectName = JBIJMXConnectorUtil.getComponentMBeanName(
76                     connection, name);
77             if (objectName == null) {
78                 throw new BuildException("The component is not installed");
79             }
80             connection.invoke(objectName, action, objects, strings);
81             try {
82                 log("Shutdown component name : " + name);
83             } catch (NullPointerException JavaDoc e) {
84                 // Exception thrown when execute outisde of the ant context
85
}
86             connector.close();
87         } catch (Exception JavaDoc e) {
88             if (Boolean.parseBoolean(failOnError)) {
89                 throw new BuildException(e.getMessage(), e.getCause());
90             }
91         }
92     }
93
94     /**
95      * Perform an uninstall on the component
96      *
97      */

98     protected void performUninstall() {
99         if (name == null) {
100             throw new BuildException("Required attribute : name");
101         }
102         try {
103             JMXConnector JavaDoc connector = JBIJMXConnectorUtil.getConnection(host,
104                     port, username, password);
105             MBeanServerConnection JavaDoc connection = connector
106                     .getMBeanServerConnection();
107             Object JavaDoc[] objects = new Object JavaDoc[2];
108             objects[0] = name;
109             objects[1] = true;
110             String JavaDoc[] strings = new String JavaDoc[2];
111             strings[0] = "java.lang.String";
112             strings[1] = "boolean";
113             // Unload the installer
114
connection.invoke(JBIJMXConnectorUtil
115                     .getInstallationServiceMBeanName(connection),
116                     "unloadInstaller", objects, strings);
117             try {
118                 log("Uninstalled component name : " + name);
119             } catch (NullPointerException JavaDoc e) {
120                 // Exception thrown when execute outisde of the ant context
121
}
122         } catch (Exception JavaDoc e) {
123             if (Boolean.parseBoolean(failOnError)) {
124                 throw new BuildException(e.getMessage(), e.getCause());
125             }
126         }
127     }
128
129     public void setName(String JavaDoc name) {
130         this.name = name;
131     }
132
133 }
134
Popular Tags