001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2005 ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol;
037:
038: import java.io.IOException;
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import java.util.Map;
042:
043: import javax.management.AttributeNotFoundException;
044: import javax.management.InstanceNotFoundException;
045: import javax.management.MBeanException;
046: import javax.management.ReflectionException;
047:
048: import org.apache.commons.lang.StringUtils;
049: import org.jdom.Element;
050: import org.jdom.JDOMException;
051:
052: /**
053: * Understands how to map parameter values to plugin attributes.
054: */
055: public class PluginConfiguration {
056: private Map details;
057: private String name;
058: private PluginType type;
059:
060: public PluginConfiguration(PluginDetail pluginDetail,
061: Configuration configuration)
062: throws AttributeNotFoundException,
063: InstanceNotFoundException, MBeanException,
064: ReflectionException, IOException, JDOMException {
065: this .name = pluginDetail.getName();
066: this .type = pluginDetail.getType();
067: this .details = createDetails(pluginDetail, configuration);
068: }
069:
070: public String getName() {
071: return this .name;
072: }
073:
074: public String getType() {
075: return this .type.getName();
076: }
077:
078: public String getParentElementName() {
079: return this .type.getParentElementName();
080: }
081:
082: public Map getDetails() {
083: return this .details;
084: }
085:
086: public void setDetail(String name, String value) {
087: Map.Entry detail = getEntryCaseInsensitive(name, details);
088: if (detail != null
089: && (StringUtils.isNotBlank((String) detail.getValue()) || StringUtils
090: .isNotBlank(value))) {
091: details.remove(detail.getKey());
092: details.put(detail.getKey(), value);
093: }
094: }
095:
096: private Map createDetails(PluginDetail pluginDetail,
097: Configuration configuration)
098: throws AttributeNotFoundException,
099: InstanceNotFoundException, MBeanException,
100: ReflectionException, IOException, JDOMException {
101: Map newDetails = new HashMap();
102: Element currentConfiguration = getElement(configuration);
103:
104: Attribute[] attributes = pluginDetail.getRequiredAttributes();
105: for (int i = 0; i < attributes.length; i++) {
106: Attribute attribute = attributes[i];
107: String key = attribute.getName();
108: String realName = key.substring(0, 1).toLowerCase()
109: + key.substring(1);
110: newDetails.put(realName, findAttributeValue(
111: currentConfiguration, realName));
112: }
113:
114: return newDetails;
115: }
116:
117: private String findAttributeValue(Element configuration,
118: String attributeName) {
119: for (Iterator i = configuration.getAttributes().iterator(); i
120: .hasNext();) {
121: String nextAttributeName = ((org.jdom.Attribute) i.next())
122: .getName();
123: if (attributeName.equalsIgnoreCase(nextAttributeName)) {
124: return configuration
125: .getAttributeValue(nextAttributeName);
126: }
127: }
128: return null;
129: }
130:
131: private static Map.Entry getEntryCaseInsensitive(String key, Map map) {
132: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
133: Map.Entry nextDetail = (Map.Entry) i.next();
134: if (key.equalsIgnoreCase((String) nextDetail.getKey())) {
135: return nextDetail;
136: }
137: }
138: return null;
139: }
140:
141: private Element getElement(Configuration configuration)
142: throws AttributeNotFoundException,
143: InstanceNotFoundException, MBeanException,
144: ReflectionException, IOException, JDOMException {
145: Element currentConfiguration = configuration.getElement(name);
146: if (currentConfiguration == null) {
147: currentConfiguration = new Element(name);
148: }
149: return currentConfiguration;
150: }
151: }
|