📄 jmxaccessortask.java
字号:
public static MBeanServerConnection createJMXConnection(String url,
String host, String port, String username, String password)
throws MalformedURLException, IOException {
String urlForJMX;
if (url != null)
urlForJMX = url;
else
urlForJMX = JMX_SERVICE_PREFIX + host + ":" + port
+ JMX_SERVICE_SUFFIX;
Map environment = null;
if (username != null && password != null) {
String[] credentials = new String[2];
credentials[0] = username;
credentials[1] = password;
environment = new HashMap();
environment.put(JMXConnector.CREDENTIALS, credentials);
}
return JMXConnectorFactory.connect(new JMXServiceURL(urlForJMX),
environment).getMBeanServerConnection();
}
/**
* test the if condition
*
* @return true if there is no if condition, or the named property exists
*/
protected boolean testIfCondition() {
if (ifCondition == null || "".equals(ifCondition)) {
return true;
}
return getProperty(ifCondition) != null;
}
/**
* test the unless condition
*
* @return true if there is no unless condition, or there is a named
* property but it doesn't exist
*/
protected boolean testUnlessCondition() {
if (unlessCondition == null || "".equals(unlessCondition)) {
return true;
}
return getProperty(unlessCondition) == null;
}
/**
* Get Current Connection from <em>ref</em> parameter or create a new one!
*
* @return The server connection
* @throws MalformedURLException
* @throws IOException
*/
public static MBeanServerConnection accessJMXConnection(Project project,
String url, String host, String port, String username,
String password, String refId) throws MalformedURLException,
IOException {
MBeanServerConnection jmxServerConnection = null;
boolean isRef = project != null && refId != null && refId.length() > 0;
if (isRef) {
Object pref = project.getReference(refId);
try {
jmxServerConnection = (MBeanServerConnection) pref;
} catch (ClassCastException cce) {
if (project != null) {
project.log("wrong object reference " + refId + " - "
+ pref.getClass());
}
return null;
}
}
if (jmxServerConnection == null) {
jmxServerConnection = createJMXConnection(url, host, port,
username, password);
}
if (isRef && jmxServerConnection != null) {
project.addReference(refId, jmxServerConnection);
}
return jmxServerConnection;
}
// ------------------------------------------------------ protected Methods
/**
* get JMXConnection
*
* @return The connection
* @throws MalformedURLException
* @throws IOException
*/
protected MBeanServerConnection getJMXConnection()
throws MalformedURLException, IOException {
MBeanServerConnection jmxServerConnection = null;
if (isUseRef()) {
Object pref = null ;
if(getProject() != null) {
pref = getProject().getReference(getRef());
if (pref != null) {
try {
jmxServerConnection = (MBeanServerConnection) pref;
} catch (ClassCastException cce) {
getProject().log(
"Wrong object reference " + getRef() + " - "
+ pref.getClass());
return null;
}
}
}
if (jmxServerConnection == null) {
jmxServerConnection = accessJMXConnection(getProject(),
getUrl(), getHost(), getPort(), getUsername(),
getPassword(), getRef());
}
} else {
jmxServerConnection = accessJMXConnection(getProject(), getUrl(),
getHost(), getPort(), getUsername(), getPassword(), null);
}
return jmxServerConnection;
}
/**
* Execute the specified command, based on the configured properties. The
* input stream will be closed upon completion of this task, whether it was
* executed successfully or not.
*
* @exception Exception
* if an error occurs
*/
public String jmxExecute(MBeanServerConnection jmxServerConnection)
throws Exception {
if ((jmxServerConnection == null)) {
throw new BuildException("Must open a connection!");
} else if (isEcho()) {
handleOutput("JMX Connection ref=" + ref + " is open!");
}
return null;
}
/**
* Convert string to datatype FIXME How we can transfer values from ant
* project reference store (ref)?
*
* @param value The value
* @param valueType The type
* @return The converted object
*/
protected Object convertStringToType(String value, String valueType) {
if ("java.lang.String".equals(valueType))
return value;
Object convertValue = value;
if ("java.lang.Integer".equals(valueType) || "int".equals(valueType)) {
try {
convertValue = new Integer(value);
} catch (NumberFormatException ex) {
if (isEcho())
handleErrorOutput("Unable to convert to integer:" + value);
}
} else if ("java.lang.Long".equals(valueType)
|| "long".equals(valueType)) {
try {
convertValue = new Long(value);
} catch (NumberFormatException ex) {
if (isEcho())
handleErrorOutput("Unable to convert to long:" + value);
}
} else if ("java.lang.Boolean".equals(valueType)
|| "boolean".equals(valueType)) {
convertValue = new Boolean(value);
} else if ("java.lang.Float".equals(valueType)
|| "float".equals(valueType)) {
try {
convertValue = new Float(value);
} catch (NumberFormatException ex) {
if (isEcho())
handleErrorOutput("Unable to convert to float:" + value);
}
} else if ("java.lang.Double".equals(valueType)
|| "double".equals(valueType)) {
try {
convertValue = new Double(value);
} catch (NumberFormatException ex) {
if (isEcho())
handleErrorOutput("Unable to convert to double:" + value);
}
} else if ("javax.management.ObjectName".equals(valueType)
|| "name".equals(valueType)) {
try {
convertValue = new ObjectName(value);
} catch (MalformedObjectNameException e) {
if (isEcho())
handleErrorOutput("Unable to convert to ObjectName:"
+ value);
}
} else if ("java.net.InetAddress".equals(valueType)) {
try {
convertValue = InetAddress.getByName(value);
} catch (UnknownHostException exc) {
if (isEcho())
handleErrorOutput("Unable to resolve host name:" + value);
}
}
return convertValue;
}
/**
* @param name context of result
* @param result
*/
protected void echoResult(String name, Object result) {
if (isEcho()) {
if (result.getClass().isArray()) {
for (int i = 0; i < Array.getLength(result); i++) {
handleOutput(name + "." + i + "=" + Array.get(result, i));
}
} else
handleOutput(name + "=" + result);
}
}
/**
* create result as property with name from attribute resultproperty
*
* @param result The result
* @see #createProperty(String, Object)
*/
protected void createProperty(Object result) {
if (resultproperty != null) {
createProperty(resultproperty, result);
}
}
/**
* create result as property with name from property prefix When result is
* an array and isSeparateArrayResults is true, resultproperty used as
* prefix (<code>resultproperty.0-array.length</code> and store the
* result array length at <code>resultproperty.length</code>. Other
* option is that you delemit your result with a delimiter
* (java.util.StringTokenizer is used).
*
* @param propertyPrefix
* @param result
*/
protected void createProperty(String propertyPrefix, Object result) {
if (propertyPrefix == null)
propertyPrefix = "";
if (result instanceof CompositeDataSupport) {
CompositeDataSupport data = (CompositeDataSupport) result;
CompositeType compositeType = data.getCompositeType();
Set keys = compositeType.keySet();
for (Iterator iter = keys.iterator(); iter.hasNext();) {
String key = (String) iter.next();
Object value = data.get(key);
OpenType type = compositeType.getType(key);
if (type instanceof SimpleType) {
setProperty(propertyPrefix + "." + key, value);
} else {
createProperty(propertyPrefix + "." + key, value);
}
}
} else if (result instanceof TabularDataSupport) {
TabularDataSupport data = (TabularDataSupport) result;
for (Iterator iter = data.keySet().iterator(); iter.hasNext();) {
Object key = iter.next();
for (Iterator iter1 = ((List) key).iterator(); iter1.hasNext();) {
Object key1 = iter1.next();
CompositeData valuedata = data.get(new Object[] { key1 });
Object value = valuedata.get("value");
OpenType type = valuedata.getCompositeType().getType(
"value");
if (type instanceof SimpleType) {
setProperty(propertyPrefix + "." + key1, value);
} else {
createProperty(propertyPrefix + "." + key1, value);
}
}
}
} else if (result.getClass().isArray()) {
if (isSeparatearrayresults()) {
int size = 0;
for (int i = 0; i < Array.getLength(result); i++) {
if (setProperty(propertyPrefix + "." + size, Array.get(
result, i))) {
size++;
}
}
if (size > 0) {
setProperty(propertyPrefix + ".Length", Integer
.toString(size));
}
}
} else {
String delim = getDelimiter();
if (delim != null) {
StringTokenizer tokenizer = new StringTokenizer(result
.toString(), delim);
int size = 0;
for (; tokenizer.hasMoreTokens();) {
String token = tokenizer.nextToken();
if (setProperty(propertyPrefix + "." + size, token)) {
size++;
}
}
if (size > 0)
setProperty(propertyPrefix + ".Length", Integer
.toString(size));
} else {
setProperty(propertyPrefix, result.toString());
}
}
}
/**
* get all properties, when project is there got all project Properties
* @return properties
*/
public Map getProperties() {
Project currentProject = getProject();
if (currentProject != null) {
return currentProject.getProperties();
} else {
return properties;
}
}
/**
* get all Properties
* @param property
* @return The property
*/
public String getProperty(String property) {
Project currentProject = getProject();
if (currentProject != null) {
return currentProject.getProperty(property);
} else {
return properties.getProperty(property);
}
}
/**
* @param property The property
* @param value The value
* @return True if successful
*/
public boolean setProperty(String property, Object value) {
if (property != null) {
if (value == null)
value = "";
if (isEcho()) {
handleOutput(property + "=" + value.toString());
}
Project currentProject = getProject();
if (currentProject != null) {
currentProject.setNewProperty(property, value.toString());
} else {
properties.setProperty(property, value.toString());
}
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -