1 package org.munin; 2 3 import java.io.BufferedReader; 4 import java.io.FileReader; 5 import java.io.IOException; 6 import java.io.PrintStream; 7 import java.util.ArrayList; 8 import java.util.HashMap; 9 import java.util.Iterator; 10 import java.util.List; 11 import java.util.Map; 12 import java.util.Properties; 13 14 import javax.management.MalformedObjectNameException; 15 import javax.management.ObjectName; 16 17 public class Configuration { 18 private Properties graph_properties = new Properties(); 19 private Map<String, Configuration.FieldProperties> fieldMap = new HashMap(); 20 private List<Configuration.FieldProperties> fields = new ArrayList(); 21 22 public class FieldProperties extends Properties { 23 private static final long serialVersionUID = 1L; 24 private ObjectName jmxObjectName; 25 private String jmxAttributeName; 26 private String jmxAttributeKey; 27 private String fieldname; 28 private static final String JMXOBJECT = "jmxObjectName"; 29 private static final String JMXATTRIBUTE = "jmxAttributeName"; 30 private static final String JMXATTRIBUTEKEY = "jmxAttributeKey"; 31 FieldProperties(Configuration paramConfiguration, String fieldname)32 public FieldProperties(Configuration paramConfiguration, 33 String fieldname) { 34 this.fieldname = fieldname; 35 } 36 getJmxAttributeKey()37 public String getJmxAttributeKey() { 38 return jmxAttributeKey; 39 } 40 getJmxAttributeName()41 public String getJmxAttributeName() { 42 return jmxAttributeName; 43 } 44 getJmxObjectName()45 public ObjectName getJmxObjectName() { 46 return jmxObjectName; 47 } 48 toString()49 public String toString() { 50 return fieldname; 51 } 52 set(String key, String value)53 public void set(String key, String value) 54 throws MalformedObjectNameException, NullPointerException { 55 if ("jmxObjectName".equals(key)) { 56 if (jmxObjectName != null) 57 throw new IllegalStateException( 58 "jmxObjectName already set for " + this); 59 jmxObjectName = new ObjectName(value); 60 } else if ("jmxAttributeName".equals(key)) { 61 if (jmxAttributeName != null) 62 throw new IllegalStateException( 63 "jmxAttributeName already set for " + this); 64 jmxAttributeName = value; 65 } else if ("jmxAttributeKey".equals(key)) { 66 if (jmxAttributeKey != null) 67 throw new IllegalStateException( 68 "jmxAttributeKey already set for " + this); 69 jmxAttributeKey = value; 70 } else { 71 put(key, value); 72 } 73 } 74 report(PrintStream out)75 public void report(PrintStream out) { 76 for (Iterator it = entrySet().iterator(); it.hasNext();) { 77 Map.Entry entry = (Map.Entry) it.next(); 78 out.println(fieldname + '.' + entry.getKey() + " " 79 + entry.getValue()); 80 } 81 } 82 getFieldname()83 public String getFieldname() { 84 return fieldname; 85 } 86 } 87 parse(String config_file)88 public static Configuration parse(String config_file) throws IOException, 89 MalformedObjectNameException, NullPointerException { 90 BufferedReader reader = new BufferedReader(new FileReader(config_file)); 91 Configuration configuration = new Configuration(); 92 try { 93 for (;;) { 94 String s = reader.readLine(); 95 if (s == null) 96 break; 97 if ((!s.startsWith("%")) && (s.length() > 5) 98 && (!s.startsWith(" "))) { 99 configuration.parseString(s); 100 } 101 } 102 } finally { 103 reader.close(); 104 } 105 106 return configuration; 107 } 108 parseString(String s)109 private void parseString(String s) throws MalformedObjectNameException, 110 NullPointerException { 111 String[] nameval = s.split(" ", 2); 112 if (nameval[0].indexOf('.') > 0) { 113 String name = nameval[0]; 114 String fieldname = name.substring(0, name.lastIndexOf('.')); 115 if (!fieldMap.containsKey(fieldname)) { 116 Configuration.FieldProperties field = new Configuration.FieldProperties( 117 this, fieldname); 118 fieldMap.put(fieldname, field); 119 fields.add(field); 120 } 121 Configuration.FieldProperties field = (Configuration.FieldProperties) fieldMap 122 .get(fieldname); 123 String key = name.substring(name.lastIndexOf('.') + 1); 124 field.set(key, nameval[1]); 125 } else { 126 graph_properties.put(nameval[0], nameval[1]); 127 } 128 } 129 getGraphProperties()130 public Properties getGraphProperties() { 131 return graph_properties; 132 } 133 report(PrintStream out)134 public void report(PrintStream out) { 135 for (Iterator it = graph_properties.entrySet().iterator(); it.hasNext();) { 136 Map.Entry entry = (Map.Entry) it.next(); 137 out.println(entry.getKey() + " " + entry.getValue()); 138 } 139 140 for (Configuration.FieldProperties field : fields) { 141 field.report(out); 142 } 143 } 144 getFields()145 public List<Configuration.FieldProperties> getFields() { 146 return fields; 147 } 148 } 149