1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.IOException ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 44 import javax.management.AttributeNotFoundException ; 45 import javax.management.InstanceNotFoundException ; 46 import javax.management.MBeanException ; 47 import javax.management.ReflectionException ; 48 49 import org.apache.commons.lang.StringUtils; 50 import org.jdom.Element; 51 import org.jdom.JDOMException; 52 53 56 public class PluginConfiguration { 57 private Map details; 58 private String name; 59 private PluginType type; 60 61 public PluginConfiguration(PluginDetail pluginDetail, Configuration configuration) 62 throws AttributeNotFoundException , InstanceNotFoundException , MBeanException , ReflectionException , 63 IOException , JDOMException { 64 this.name = pluginDetail.getName(); 65 this.type = pluginDetail.getType(); 66 this.details = createDetails(pluginDetail, configuration); 67 } 68 69 public String getName() { 70 return this.name; 71 } 72 73 public String getType() { 74 return this.type.getName(); 75 } 76 77 public String getParentElementName() { 78 return this.type.getParentElementName(); 79 } 80 81 public Map getDetails() { 82 return this.details; 83 } 84 85 public void setDetail(String name, String value) { 86 Map.Entry detail = getEntryCaseInsensitive(name, details); 87 if (detail != null && (StringUtils.isNotBlank((String ) detail.getValue()) || StringUtils.isNotBlank(value))) { 88 details.remove(detail.getKey()); 89 details.put(detail.getKey(), value); 90 } 91 } 92 93 private Map createDetails(PluginDetail pluginDetail, Configuration configuration) 94 throws AttributeNotFoundException , InstanceNotFoundException , MBeanException , ReflectionException , 95 IOException , JDOMException { 96 Map newDetails = new HashMap (); 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 key = attribute.getName(); 103 String 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 findAttributeValue(Element configuration, String attributeName) { 111 for (Iterator i = configuration.getAttributes().iterator(); i.hasNext();) { 112 String 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 getEntryCaseInsensitive(String key, Map map) { 121 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 122 Map.Entry nextDetail = (Map.Entry ) i.next(); 123 if (key.equalsIgnoreCase((String ) nextDetail.getKey())) { 124 return nextDetail; 125 } 126 } 127 return null; 128 } 129 130 private Element getElement(Configuration configuration) throws AttributeNotFoundException , 131 InstanceNotFoundException , MBeanException , ReflectionException , IOException , 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 |