KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > backupTool > JMXRepositoryLocker


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.backupTool;
17
18 import java.util.Date JavaDoc;
19
20 import javax.xml.parsers.DocumentBuilder JavaDoc;
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 JavaDoc;
30
31 public class JMXRepositoryLocker {
32
33     private final static long SUSPEND_GRACE_TIME = 60000; // one minute
34

35     private final static String JavaDoc LOCK_QUERY = "operation=lock&objectname=Daisy:name=BackupLocker&value0=" + SUSPEND_GRACE_TIME + "&type0=long";
36
37     private final static String JavaDoc UNLOCK_QUERY = "operation=unlock&objectname=Daisy:name=BackupLocker";
38
39     private final static String JavaDoc STATUS_QUERY = "objectname=Daisy:name=BackupLocker";
40     
41     private final static String JavaDoc VERSION_QUERY = "objectname=Daisy:name=SystemInfo";
42
43     private final static String JavaDoc XPATH_ISLOCKED = "/MBean[@objectname = 'Daisy:name=BackupLocker']/Attribute[@name = 'Locked']/@value";
44
45     private final static String JavaDoc XPATH_METHOD_STATUS = "/MBeanOperation/Operation/@result";
46
47     private final static String JavaDoc XPATH_METHOD_ERRORMSG = "/MBeanOperation/Operation/@errorMsg";
48     
49     private final static String JavaDoc XPATH_VERSION = "/MBean[@objectname = 'Daisy:name=SystemInfo']/Attribute[@name = 'ServerVersion']/@value";
50
51     private String JavaDoc username;
52
53     private String JavaDoc password;
54
55     private String JavaDoc host;
56
57     private int port;
58
59     private Date JavaDoc startLock;
60
61     private Date JavaDoc stopLock;
62
63     private DocumentBuilder JavaDoc documentbuilder;
64
65     public JMXRepositoryLocker(String JavaDoc host, int port, String JavaDoc username, String JavaDoc password) throws Exception JavaDoc {
66         this.host = host;
67         this.port = port;
68         this.username = username;
69         this.password = password;
70
71         DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
72         documentbuilder = documentBuilderFactory.newDocumentBuilder();
73     }
74
75     public void lock() throws Exception JavaDoc {
76         startLock = new Date JavaDoc();
77         stopLock = null;
78         Document JavaDoc statusDocument = queryJmx("invoke", LOCK_QUERY);
79         if (!successfulInvocation(statusDocument))
80             throw createException(statusDocument);
81     }
82
83     public void unlock() throws Exception JavaDoc {
84         stopLock = new Date JavaDoc();
85         Document JavaDoc statusDocument = queryJmx("invoke", UNLOCK_QUERY);
86         if (!successfulInvocation(statusDocument))
87             throw createException(statusDocument);
88     }
89
90     public boolean isLocked() throws Exception JavaDoc {
91         Document JavaDoc resultDocument = queryJmx("mbean", STATUS_QUERY);
92         String JavaDoc 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 JavaDoc getServerVersion() throws Exception JavaDoc{
104         Document JavaDoc result = queryJmx("mbean", VERSION_QUERY);
105         return BackupHelper.getStringFromDom(result, XPATH_VERSION);
106     }
107
108     private Exception JavaDoc createException(Document JavaDoc doc) throws Exception JavaDoc {
109         return new Exception JavaDoc(BackupHelper.getStringFromDom(doc, XPATH_METHOD_ERRORMSG));
110     }
111
112     private Document JavaDoc queryJmx(String JavaDoc method, String JavaDoc query) throws Exception JavaDoc {
113         Document JavaDoc 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 JavaDoc("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 JavaDoc doc) throws Exception JavaDoc {
137         return BackupHelper.getStringFromDom(doc, XPATH_METHOD_STATUS).equals("success");
138     }
139
140     public String JavaDoc getRepositoryHost() {
141         return host;
142     }
143
144     public int getRepositoryPort() {
145         return port;
146     }
147 }
Popular Tags