1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.monitoring.snapshot; 18 19 import java.io.File; 20 import java.io.FileOutputStream; 21 import java.util.ArrayList; 22 23 import javax.xml.bind.JAXBContext; 24 import javax.xml.bind.Marshaller; 25 import javax.xml.bind.Unmarshaller; 26 27 import org.slf4j.Logger; 28 import org.slf4j.LoggerFactory; 29 import org.apache.geronimo.monitoring.MonitorConstants; 30 31 /** 32 * In charge of dealing with the XML processing of the snapshot's data. 33 */ 34 public class SnapshotConfigXMLBuilder { 35 private static final Logger log = LoggerFactory.getLogger(SnapshotConfigXMLBuilder.class); 36 37 private static final String pathToXML = 38 System.getProperty("org.apache.geronimo.home.dir") + "/var/monitoring/snapshot-config.xml"; 39 40 private static JAXBContext jc = null; 41 42 static { 43 try { 44 ObjectFactory objFactory = new ObjectFactory(); 45 ClassLoader cl = objFactory.getClass().getClassLoader(); 46 jc = JAXBContext.newInstance("org.apache.geronimo.monitoring.snapshot", cl); 47 } catch(Exception e) { 48 log.error(e.getMessage()); 49 e.printStackTrace(); 50 } 51 } 52 53 /** 54 * @return SnapshotConfig object which represents the XML document 55 */ 56 private static SnapshotConfig getSnapshotConfig() { 57 // ensure that there is a snapshot-config.xml at all times 58 try { 59 if(checkXMLExists()) { 60 // unmarshall the xml document into SnapshotConfig object 61 Unmarshaller m = jc.createUnmarshaller(); 62 return (SnapshotConfig)m.unmarshal( new File(pathToXML) ); 63 } else { 64 SnapshotConfig sc = new SnapshotConfig(); 65 sc.setMbeans( new SnapshotConfig.Mbeans() ); 66 saveDocument(sc); 67 return sc; 68 } 69 } catch(Exception e) { 70 log.error(e.getMessage()); 71 return null; // in the case of an error, return null 72 } 73 } 74 75 /** 76 * @return A list of all mbean names that have been previously saved. 77 * These mbean names are those to keep track of for per snapshot. 78 */ 79 public static ArrayList<String> getMBeanNames() { 80 if(getSnapshotConfig().getMbeans() == null) { 81 return new ArrayList<String>(); 82 } else { 83 return (ArrayList<String>)getSnapshotConfig().getMbeans().getMbean(); 84 } 85 } 86 87 /** 88 * Removes from the snapshot-config.xml a configuration element <mbean> 89 * in order to persistently keep track of all user requested statistics. 90 * If there does not exist an instance of the mbeanNAme, nothing will be done. 91 */ 92 public static boolean removeMBeanName(String mbeanName) { 93 ArrayList<String> mbeanNames = getMBeanNames(); 94 for(int i = 0 ; i < (int)mbeanNames.size(); i++) { 95 if(mbeanNames.get(i).equals(mbeanName)) { 96 // remove the mbean name by directly accessing it, because it is by reference 97 SnapshotConfig sc = getSnapshotConfig(); 98 sc.getMbeans().getMbean().remove(i); 99 // save the current state of the SnapshotConfig object 100 saveDocument( sc ); 101 return true; 102 } 103 } 104 return false; 105 } 106 107 /** 108 * Adds to the snapshot-config.xml another configuration element <mbean> 109 * in order to persistently keep track of all user requested statistics. 110 * If there is a duplicate, nothing will be done. 111 */ 112 public static boolean addMBeanName(String mbeanName) { 113 // check to see if the mbean name already exists 114 ArrayList<String> mbeanNames = getMBeanNames(); 115 for(int i = 0 ; i < (int)mbeanNames.size(); i++) { 116 if(mbeanNames.get(i).equals(mbeanName)) { 117 return false; // nothing needs to be done if it is already there 118 } 119 } 120 121 // insert the mbean name into the SnapshotConfig object 122 SnapshotConfig sc = getSnapshotConfig(); 123 sc.getMbeans().getMbean().add(mbeanName); 124 // write the object to XML 125 saveDocument(sc); 126 return true; 127 } 128 129 /** 130 * Saves the duration of the snapshot as a configuration attribute 131 * @param duration 132 */ 133 public static void saveDuration(long duration) { 134 SnapshotConfig sc = getSnapshotConfig(); 135 sc.setDuration("" + duration); 136 saveDocument(sc); 137 } 138 139 /** 140 * Saves the retention of the snapshot as a configuration attribute 141 * @param retention 142 */ 143 public static void saveRetention(int retention) { 144 SnapshotConfig sc = getSnapshotConfig(); 145 sc.setRetention("" + retention); 146 saveDocument(sc); 147 } 148 149 /** 150 * Returns the value of the configuration attribute, defined by the key 151 * @param key 152 * @return 153 * @throws Exception 154 */ 155 public static String getAttributeValue(String key) throws Exception { 156 if(key.equals( MonitorConstants.DURATION )) { 157 return getSnapshotConfig().getDuration(); 158 } else if(key.equals( MonitorConstants.RETENTION )) { 159 return getSnapshotConfig().getRetention(); 160 } else { 161 // Houston, we have a problem 162 throw new Exception("[WARNING] Attribute: " + key + " is not valid."); 163 } 164 } 165 166 /** 167 * Ensures that there is an existing XML file. Creates one if there 168 * does not exist one already. 169 */ 170 public static boolean checkXMLExists() { 171 ensureMonitorDir(); 172 File docFile = new File(pathToXML); 173 return docFile.exists(); 174 } 175 176 177 /** 178 * Write the XML document. 179 */ 180 public static void saveDocument(SnapshotConfig sc) { 181 try { 182 Marshaller m = jc.createMarshaller(); 183 m.marshal(sc, new FileOutputStream( pathToXML )); 184 } catch(Exception e) { 185 log.error(e.getMessage()); 186 } 187 } 188 189 190 /** 191 * Checks to see if the GERONIMO_HOME/var/monitoring/ directory was made. 192 * If not, the method creates it. 193 */ 194 public static void ensureMonitorDir() { 195 final String pathToDir = 196 System.getProperty("org.apache.geronimo.home.dir") + "/var/monitoring/"; 197 File dir = new File(pathToDir); 198 if(dir.exists() && dir.isDirectory()) { 199 // all good 200 return; 201 } else { 202 // make a directory 203 if(dir.mkdir()) { 204 // directory was successfully created 205 log.info("/var/monitoring directory created."); 206 return; 207 } else { 208 log.error("Could not make the directory " + pathToDir); 209 } 210 } 211 } 212 }