1 16 package org.outerj.daisy.backupTool; 17 18 import java.util.Date ; 19 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 23 import org.apache.commons.httpclient.HostConfiguration; 24 import org.apache.commons.httpclient.HttpClient; 25 import org.apache.commons.httpclient.HttpMethod; 26 import org.apache.commons.httpclient.HttpState; 27 import org.apache.commons.httpclient.UsernamePasswordCredentials; 28 import org.apache.commons.httpclient.methods.GetMethod; 29 import org.w3c.dom.Document ; 30 31 public class JMXRepositoryLocker { 32 33 private final static long SUSPEND_GRACE_TIME = 60000; 35 private final static String LOCK_QUERY = "operation=lock&objectname=Daisy:name=BackupLocker&value0=" + SUSPEND_GRACE_TIME + "&type0=long"; 36 37 private final static String UNLOCK_QUERY = "operation=unlock&objectname=Daisy:name=BackupLocker"; 38 39 private final static String STATUS_QUERY = "objectname=Daisy:name=BackupLocker"; 40 41 private final static String VERSION_QUERY = "objectname=Daisy:name=SystemInfo"; 42 43 private final static String XPATH_ISLOCKED = "/MBean[@objectname = 'Daisy:name=BackupLocker']/Attribute[@name = 'Locked']/@value"; 44 45 private final static String XPATH_METHOD_STATUS = "/MBeanOperation/Operation/@result"; 46 47 private final static String XPATH_METHOD_ERRORMSG = "/MBeanOperation/Operation/@errorMsg"; 48 49 private final static String XPATH_VERSION = "/MBean[@objectname = 'Daisy:name=SystemInfo']/Attribute[@name = 'ServerVersion']/@value"; 50 51 private String username; 52 53 private String password; 54 55 private String host; 56 57 private int port; 58 59 private Date startLock; 60 61 private Date stopLock; 62 63 private DocumentBuilder documentbuilder; 64 65 public JMXRepositoryLocker(String host, int port, String username, String password) throws Exception { 66 this.host = host; 67 this.port = port; 68 this.username = username; 69 this.password = password; 70 71 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 72 documentbuilder = documentBuilderFactory.newDocumentBuilder(); 73 } 74 75 public void lock() throws Exception { 76 startLock = new Date (); 77 stopLock = null; 78 Document statusDocument = queryJmx("invoke", LOCK_QUERY); 79 if (!successfulInvocation(statusDocument)) 80 throw createException(statusDocument); 81 } 82 83 public void unlock() throws Exception { 84 stopLock = new Date (); 85 Document statusDocument = queryJmx("invoke", UNLOCK_QUERY); 86 if (!successfulInvocation(statusDocument)) 87 throw createException(statusDocument); 88 } 89 90 public boolean isLocked() throws Exception { 91 Document resultDocument = queryJmx("mbean", STATUS_QUERY); 92 String result = BackupHelper.getStringFromDom(resultDocument, XPATH_ISLOCKED); 93 return Boolean.valueOf(result).booleanValue(); 94 } 95 96 public long getLockTime() { 97 long time = -1; 98 if (stopLock != null && startLock != null) 99 time = stopLock.getTime() - startLock.getTime(); 100 return time; 101 } 102 103 public String getServerVersion() throws Exception { 104 Document result = queryJmx("mbean", VERSION_QUERY); 105 return BackupHelper.getStringFromDom(result, XPATH_VERSION); 106 } 107 108 private Exception createException(Document doc) throws Exception { 109 return new Exception (BackupHelper.getStringFromDom(doc, XPATH_METHOD_ERRORMSG)); 110 } 111 112 private Document queryJmx(String method, String query) throws Exception { 113 Document document = null; 114 115 HttpClient httpClient = new HttpClient(); 116 HttpState state = new HttpState(); 117 state.setCredentials(null, null, new UsernamePasswordCredentials(username, password)); 118 httpClient.setState(state); 119 HostConfiguration hostConfiguration = new HostConfiguration(); 120 hostConfiguration.setHost(host, port); 121 httpClient.setHostConfiguration(hostConfiguration); 122 123 HttpMethod getMethod = new GetMethod("/" + method); 124 getMethod.setQueryString(query); 125 try { 126 httpClient.executeMethod(getMethod); 127 if (getMethod.getStatusCode() != 200) 128 throw new Exception ("Failed to connect to JMX: HTTP response code : " + getMethod.getStatusCode() + " : " + getMethod.getStatusText()); 129 document = documentbuilder.parse(getMethod.getResponseBodyAsStream()); 130 } finally { 131 getMethod.releaseConnection(); 132 } 133 return document; 134 } 135 136 private boolean successfulInvocation(Document doc) throws Exception { 137 return BackupHelper.getStringFromDom(doc, XPATH_METHOD_STATUS).equals("success"); 138 } 139 140 public String getRepositoryHost() { 141 return host; 142 } 143 144 public int getRepositoryPort() { 145 return port; 146 } 147 } | Popular Tags |