KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > Configuration


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005 ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol;
38
39 import java.io.ByteArrayInputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.StringReader JavaDoc;
42 import java.util.Arrays JavaDoc;
43 import java.util.Collection JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.LinkedList JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Map JavaDoc;
49
50 import javax.management.Attribute JavaDoc;
51 import javax.management.AttributeNotFoundException JavaDoc;
52 import javax.management.InstanceNotFoundException JavaDoc;
53 import javax.management.InvalidAttributeValueException JavaDoc;
54 import javax.management.MBeanException JavaDoc;
55 import javax.management.MBeanServerConnection JavaDoc;
56 import javax.management.MalformedObjectNameException JavaDoc;
57 import javax.management.ObjectName JavaDoc;
58 import javax.management.ReflectionException JavaDoc;
59 import javax.management.remote.JMXConnector JavaDoc;
60 import javax.management.remote.JMXConnectorFactory JavaDoc;
61 import javax.management.remote.JMXServiceURL JavaDoc;
62 import javax.naming.Context JavaDoc;
63
64 import net.sourceforge.cruisecontrol.util.Util;
65
66 import org.apache.commons.lang.StringUtils;
67 import org.jdom.Document;
68 import org.jdom.Element;
69 import org.jdom.JDOMException;
70 import org.jdom.input.SAXBuilder;
71 import org.jdom.output.Format;
72 import org.jdom.output.XMLOutputter;
73
74 /**
75  * Communicates with the CruiseControl JMX server to allow CRUD operations on
76  * the CruiseControl configuration.
77  */

78 public class Configuration {
79     private MBeanServerConnection JavaDoc server;
80     private ObjectName JavaDoc ccMgr;
81     private String JavaDoc configuration;
82     private PluginDetail[] pluginDetails;
83
84     public Configuration(String JavaDoc jmxServer, int rmiPort) throws IOException JavaDoc, MalformedObjectNameException JavaDoc {
85         JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("service:jmx:rmi://" + jmxServer + ":" + rmiPort + "/jndi/jrmp");
86
87         Map JavaDoc environment = new HashMap JavaDoc();
88         environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
89         environment.put(Context.PROVIDER_URL, "rmi://" + jmxServer + ":" + rmiPort);
90
91         JMXConnector JavaDoc cntor = JMXConnectorFactory.connect(address, environment);
92         server = cntor.getMBeanServerConnection();
93         ccMgr = ObjectName.getInstance("CruiseControl Manager:id=unique");
94     }
95
96     public String JavaDoc getConfiguration() throws AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc,
97             ReflectionException JavaDoc, IOException JavaDoc, JDOMException {
98         if (configuration == null) {
99             load();
100         }
101
102         return configuration;
103     }
104
105     public Document getDocument() throws ReflectionException JavaDoc, IOException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc,
106             AttributeNotFoundException JavaDoc, JDOMException {
107         return stringToDoc(getConfiguration());
108     }
109
110     public Element getElement(String JavaDoc name) throws ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, IOException JavaDoc,
111             MBeanException JavaDoc, AttributeNotFoundException JavaDoc, JDOMException {
112         return JDOMSearcher.getElement(getDocument(), name);
113     }
114
115     public PluginDetail[] getPluginDetails() throws AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
116             MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc {
117         if (pluginDetails == null) {
118             pluginDetails = (PluginDetail[]) server.getAttribute(ccMgr, "AvailablePlugins");
119         }
120
121         return pluginDetails;
122     }
123
124     public PluginDetail[] getConfiguredBootstrappers(String JavaDoc project) throws CruiseControlException,
125             AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc,
126             JDOMException {
127         return getConfiguredPluginDetails(project, getProjectConfig(project).getBootstrappers());
128     }
129
130     public PluginDetail[] getConfiguredBuilders(String JavaDoc project) throws AttributeNotFoundException JavaDoc,
131             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, CruiseControlException,
132             JDOMException {
133         return getConfiguredPluginDetails(project, getProjectConfig(project).getSchedule().getBuilders());
134     }
135
136     public PluginDetail[] getConfiguredListeners(String JavaDoc project) throws AttributeNotFoundException JavaDoc,
137             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, CruiseControlException,
138             JDOMException {
139         return getConfiguredPluginDetails(project, getProjectConfig(project).getListeners());
140     }
141
142     public PluginDetail[] getConfiguredLoggers(String JavaDoc project) throws AttributeNotFoundException JavaDoc,
143             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, CruiseControlException,
144             JDOMException {
145         return getConfiguredPluginDetails(project, Arrays.asList(getProjectConfig(project).getLog().getLoggers()));
146     }
147
148     public PluginDetail[] getConfiguredPublishers(String JavaDoc project) throws AttributeNotFoundException JavaDoc,
149             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, CruiseControlException,
150             JDOMException {
151         return getConfiguredPluginDetails(project, getProjectConfig(project).getPublishers());
152     }
153
154     public PluginDetail[] getConfiguredSourceControls(String JavaDoc project) throws AttributeNotFoundException JavaDoc,
155             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, CruiseControlException,
156             JDOMException {
157         return getConfiguredPluginDetails(project, getProjectConfig(project).getModificationSet().getSourceControls());
158     }
159
160     public void load() throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
161             ReflectionException JavaDoc, IOException JavaDoc, JDOMException {
162         // Ensure xml is well-formed
163
configuration = docToString(stringToDoc(loadConfiguration()));
164     }
165
166     public void save() throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
167             MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc {
168         server.setAttribute(ccMgr, new Attribute("ConfigFileContents", configuration));
169     }
170
171     public void setConfiguration(String JavaDoc configuration) {
172         this.configuration = configuration;
173     }
174
175     public void setConfiguration(Document doc) {
176         setConfiguration(docToString(doc));
177     }
178
179     public void updatePluginConfiguration(PluginConfiguration pluginConfiguration) throws AttributeNotFoundException JavaDoc,
180             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, JDOMException,
181             InvalidAttributeValueException JavaDoc {
182         Element plugin = new Element(pluginConfiguration.getName());
183         for (Iterator JavaDoc i = pluginConfiguration.getDetails().entrySet().iterator(); i.hasNext();) {
184             Map.Entry JavaDoc element = (Map.Entry JavaDoc) i.next();
185             String JavaDoc key = (String JavaDoc) element.getKey();
186             String JavaDoc value = (String JavaDoc) element.getValue();
187             if (StringUtils.isNotBlank(value)) {
188                 plugin.setAttribute(key, value);
189             }
190         }
191
192         Document doc = getDocument();
193         Element parent = JDOMSearcher.getElement(doc, pluginConfiguration.getParentElementName());
194         parent.removeChild(plugin.getName());
195         parent.addContent(plugin);
196
197         setConfiguration(doc);
198     }
199
200     private static String JavaDoc docToString(Document doc) {
201         return new XMLOutputter(Format.getPrettyFormat()).outputString(doc).trim();
202     }
203
204     private static Document stringToDoc(String JavaDoc configuration) throws JDOMException, IOException JavaDoc {
205         return new SAXBuilder().build(new StringReader JavaDoc(configuration));
206     }
207
208     private PluginDetail[] getConfiguredPluginDetails(String JavaDoc project, List JavaDoc plugins) throws AttributeNotFoundException JavaDoc,
209             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, CruiseControlException {
210         Collection JavaDoc details = new LinkedList JavaDoc();
211         PluginRegistry registry = getPluginRegistry();
212
213         for (Iterator JavaDoc i = plugins.iterator(); i.hasNext();) {
214             Class JavaDoc nextClass = i.next().getClass();
215             details.add(new GenericPluginDetail(registry.getPluginName(nextClass), nextClass));
216         }
217
218         return (PluginDetail[]) details.toArray(new PluginDetail[details.size()]);
219     }
220
221     private PluginRegistry getPluginRegistry() throws AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
222             MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc {
223         return (PluginRegistry) server.getAttribute(ccMgr, "PluginRegistry");
224     }
225
226     private ProjectConfig getProjectConfig(String JavaDoc project) throws CruiseControlException, AttributeNotFoundException JavaDoc,
227             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, JDOMException {
228         CruiseControlConfig config = new CruiseControlConfig();
229         config.configure(Util.parseConfig(new ByteArrayInputStream JavaDoc(getConfiguration().getBytes())));
230         return config.getConfig(project);
231     }
232
233     private String JavaDoc loadConfiguration() throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
234             ReflectionException JavaDoc, IOException JavaDoc {
235         return (String JavaDoc) server.getAttribute(ccMgr, "ConfigFileContents");
236     }
237 }
238
Popular Tags