KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import javax.management.AttributeNotFoundException JavaDoc;
45 import javax.management.InstanceNotFoundException JavaDoc;
46 import javax.management.MBeanException JavaDoc;
47 import javax.management.ReflectionException JavaDoc;
48
49 import org.apache.commons.lang.StringUtils;
50 import org.jdom.Element;
51 import org.jdom.JDOMException;
52
53 /**
54  * Understands how to map parameter values to plugin attributes.
55  */

56 public class PluginConfiguration {
57     private Map JavaDoc details;
58     private String JavaDoc name;
59     private PluginType type;
60
61     public PluginConfiguration(PluginDetail pluginDetail, Configuration configuration)
62             throws AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc,
63             IOException JavaDoc, JDOMException {
64         this.name = pluginDetail.getName();
65         this.type = pluginDetail.getType();
66         this.details = createDetails(pluginDetail, configuration);
67     }
68
69     public String JavaDoc getName() {
70         return this.name;
71     }
72
73     public String JavaDoc getType() {
74         return this.type.getName();
75     }
76
77     public String JavaDoc getParentElementName() {
78         return this.type.getParentElementName();
79     }
80
81     public Map JavaDoc getDetails() {
82         return this.details;
83     }
84
85     public void setDetail(String JavaDoc name, String JavaDoc value) {
86         Map.Entry JavaDoc detail = getEntryCaseInsensitive(name, details);
87         if (detail != null && (StringUtils.isNotBlank((String JavaDoc) detail.getValue()) || StringUtils.isNotBlank(value))) {
88             details.remove(detail.getKey());
89             details.put(detail.getKey(), value);
90         }
91     }
92
93     private Map JavaDoc createDetails(PluginDetail pluginDetail, Configuration configuration)
94             throws AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc,
95             IOException JavaDoc, JDOMException {
96         Map JavaDoc newDetails = new HashMap JavaDoc();
97         Element currentConfiguration = getElement(configuration);
98
99         Attribute[] attributes = pluginDetail.getRequiredAttributes();
100         for (int i = 0; i < attributes.length; i++) {
101             Attribute attribute = attributes[i];
102             String JavaDoc key = attribute.getName();
103             String JavaDoc realName = key.substring(0, 1).toLowerCase() + key.substring(1);
104             newDetails.put(realName, findAttributeValue(currentConfiguration, realName));
105         }
106
107         return newDetails;
108     }
109
110     private String JavaDoc findAttributeValue(Element configuration, String JavaDoc attributeName) {
111         for (Iterator JavaDoc i = configuration.getAttributes().iterator(); i.hasNext();) {
112             String JavaDoc nextAttributeName = ((org.jdom.Attribute) i.next()).getName();
113             if (attributeName.equalsIgnoreCase(nextAttributeName)) {
114                 return configuration.getAttributeValue(nextAttributeName);
115             }
116         }
117         return null;
118     }
119
120     private static Map.Entry JavaDoc getEntryCaseInsensitive(String JavaDoc key, Map JavaDoc map) {
121         for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
122             Map.Entry JavaDoc nextDetail = (Map.Entry JavaDoc) i.next();
123             if (key.equalsIgnoreCase((String JavaDoc) nextDetail.getKey())) {
124                 return nextDetail;
125             }
126         }
127         return null;
128     }
129
130     private Element getElement(Configuration configuration) throws AttributeNotFoundException JavaDoc,
131             InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, IOException JavaDoc, JDOMException {
132         Element currentConfiguration = configuration.getElement(name);
133         if (currentConfiguration == null) {
134             currentConfiguration = new Element(name);
135         }
136         return currentConfiguration;
137     }
138 }
139
Popular Tags