1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.ByteArrayInputStream ; 40 import java.io.IOException ; 41 import java.io.StringReader ; 42 import java.util.Arrays ; 43 import java.util.Collection ; 44 import java.util.HashMap ; 45 import java.util.Iterator ; 46 import java.util.LinkedList ; 47 import java.util.List ; 48 import java.util.Map ; 49 50 import javax.management.Attribute ; 51 import javax.management.AttributeNotFoundException ; 52 import javax.management.InstanceNotFoundException ; 53 import javax.management.InvalidAttributeValueException ; 54 import javax.management.MBeanException ; 55 import javax.management.MBeanServerConnection ; 56 import javax.management.MalformedObjectNameException ; 57 import javax.management.ObjectName ; 58 import javax.management.ReflectionException ; 59 import javax.management.remote.JMXConnector ; 60 import javax.management.remote.JMXConnectorFactory ; 61 import javax.management.remote.JMXServiceURL ; 62 import javax.naming.Context ; 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 78 public class Configuration { 79 private MBeanServerConnection server; 80 private ObjectName ccMgr; 81 private String configuration; 82 private PluginDetail[] pluginDetails; 83 84 public Configuration(String jmxServer, int rmiPort) throws IOException , MalformedObjectNameException { 85 JMXServiceURL address = new JMXServiceURL ("service:jmx:rmi://" + jmxServer + ":" + rmiPort + "/jndi/jrmp"); 86 87 Map environment = new HashMap (); 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 cntor = JMXConnectorFactory.connect(address, environment); 92 server = cntor.getMBeanServerConnection(); 93 ccMgr = ObjectName.getInstance("CruiseControl Manager:id=unique"); 94 } 95 96 public String getConfiguration() throws AttributeNotFoundException , InstanceNotFoundException , MBeanException , 97 ReflectionException , IOException , JDOMException { 98 if (configuration == null) { 99 load(); 100 } 101 102 return configuration; 103 } 104 105 public Document getDocument() throws ReflectionException , IOException , InstanceNotFoundException , MBeanException , 106 AttributeNotFoundException , JDOMException { 107 return stringToDoc(getConfiguration()); 108 } 109 110 public Element getElement(String name) throws ReflectionException , InstanceNotFoundException , IOException , 111 MBeanException , AttributeNotFoundException , JDOMException { 112 return JDOMSearcher.getElement(getDocument(), name); 113 } 114 115 public PluginDetail[] getPluginDetails() throws AttributeNotFoundException , InstanceNotFoundException , 116 MBeanException , ReflectionException , IOException { 117 if (pluginDetails == null) { 118 pluginDetails = (PluginDetail[]) server.getAttribute(ccMgr, "AvailablePlugins"); 119 } 120 121 return pluginDetails; 122 } 123 124 public PluginDetail[] getConfiguredBootstrappers(String project) throws CruiseControlException, 125 AttributeNotFoundException , InstanceNotFoundException , MBeanException , ReflectionException , IOException , 126 JDOMException { 127 return getConfiguredPluginDetails(project, getProjectConfig(project).getBootstrappers()); 128 } 129 130 public PluginDetail[] getConfiguredBuilders(String project) throws AttributeNotFoundException , 131 InstanceNotFoundException , MBeanException , ReflectionException , IOException , CruiseControlException, 132 JDOMException { 133 return getConfiguredPluginDetails(project, getProjectConfig(project).getSchedule().getBuilders()); 134 } 135 136 public PluginDetail[] getConfiguredListeners(String project) throws AttributeNotFoundException , 137 InstanceNotFoundException , MBeanException , ReflectionException , IOException , CruiseControlException, 138 JDOMException { 139 return getConfiguredPluginDetails(project, getProjectConfig(project).getListeners()); 140 } 141 142 public PluginDetail[] getConfiguredLoggers(String project) throws AttributeNotFoundException , 143 InstanceNotFoundException , MBeanException , ReflectionException , IOException , CruiseControlException, 144 JDOMException { 145 return getConfiguredPluginDetails(project, Arrays.asList(getProjectConfig(project).getLog().getLoggers())); 146 } 147 148 public PluginDetail[] getConfiguredPublishers(String project) throws AttributeNotFoundException , 149 InstanceNotFoundException , MBeanException , ReflectionException , IOException , CruiseControlException, 150 JDOMException { 151 return getConfiguredPluginDetails(project, getProjectConfig(project).getPublishers()); 152 } 153 154 public PluginDetail[] getConfiguredSourceControls(String project) throws AttributeNotFoundException , 155 InstanceNotFoundException , MBeanException , ReflectionException , IOException , CruiseControlException, 156 JDOMException { 157 return getConfiguredPluginDetails(project, getProjectConfig(project).getModificationSet().getSourceControls()); 158 } 159 160 public void load() throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , 161 ReflectionException , IOException , JDOMException { 162 configuration = docToString(stringToDoc(loadConfiguration())); 164 } 165 166 public void save() throws InstanceNotFoundException , AttributeNotFoundException , InvalidAttributeValueException , 167 MBeanException , ReflectionException , IOException { 168 server.setAttribute(ccMgr, new Attribute("ConfigFileContents", configuration)); 169 } 170 171 public void setConfiguration(String 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 , 180 InstanceNotFoundException , MBeanException , ReflectionException , IOException , JDOMException, 181 InvalidAttributeValueException { 182 Element plugin = new Element(pluginConfiguration.getName()); 183 for (Iterator i = pluginConfiguration.getDetails().entrySet().iterator(); i.hasNext();) { 184 Map.Entry element = (Map.Entry ) i.next(); 185 String key = (String ) element.getKey(); 186 String value = (String ) 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 docToString(Document doc) { 201 return new XMLOutputter(Format.getPrettyFormat()).outputString(doc).trim(); 202 } 203 204 private static Document stringToDoc(String configuration) throws JDOMException, IOException { 205 return new SAXBuilder().build(new StringReader (configuration)); 206 } 207 208 private PluginDetail[] getConfiguredPluginDetails(String project, List plugins) throws AttributeNotFoundException , 209 InstanceNotFoundException , MBeanException , ReflectionException , IOException , CruiseControlException { 210 Collection details = new LinkedList (); 211 PluginRegistry registry = getPluginRegistry(); 212 213 for (Iterator i = plugins.iterator(); i.hasNext();) { 214 Class 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 , InstanceNotFoundException , 222 MBeanException , ReflectionException , IOException { 223 return (PluginRegistry) server.getAttribute(ccMgr, "PluginRegistry"); 224 } 225 226 private ProjectConfig getProjectConfig(String project) throws CruiseControlException, AttributeNotFoundException , 227 InstanceNotFoundException , MBeanException , ReflectionException , IOException , JDOMException { 228 CruiseControlConfig config = new CruiseControlConfig(); 229 config.configure(Util.parseConfig(new ByteArrayInputStream (getConfiguration().getBytes()))); 230 return config.getConfig(project); 231 } 232 233 private String loadConfiguration() throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , 234 ReflectionException , IOException { 235 return (String ) server.getAttribute(ccMgr, "ConfigFileContents"); 236 } 237 } 238 | Popular Tags |