View Javadoc
1 package com.maxiq.tools.jboss; 2 3 import java.util.Vector; 4 5 import java.net.InetAddress; 6 import java.net.UnknownHostException; 7 8 /*** 9 * Main generic bean for JMX access. 10 * <br>Note that the system property <code>com.maxiq.tools.jboss.home"</code> 11 * should be properly set to the home of a running JBoss installation. 12 * 13 * @author <a href="mailto:fvancea@maxiq.com">Florin Vancea</a> 14 */ 15 public class JMXBean { 16 17 /*** The name of the server as known to the JMX system. 18 * Normally the host name. Defaults to plain local host name. 19 */ 20 private String serverName = null; 21 /*** URL of target JNDI system */ 22 private String jndiURL = null; 23 /*** JNDI name of target RMI adaptor */ 24 private String rmiJndiName = null; 25 /*** Name of the target MBean */ 26 private String mbean = null; 27 /*** Name of the target method */ 28 private String method = null; 29 /*** Current set of arguments */ 30 private Vector argSet = new Vector(); 31 /*** Current set of argument types */ 32 private Vector argTypeSet = new Vector(); 33 34 /*** Last value returned after a (successful) call to execute */ 35 private Object lastReturnValue = null; 36 37 /*** The current proxy to use for calls */ 38 private JMXProxy jmxProxy = null; 39 40 /*** 41 * Plain bean constructor. 42 */ 43 public JMXBean() { 44 if (serverName == null) { 45 try { 46 serverName = InetAddress.getLocalHost().getHostName(); 47 } catch (UnknownHostException uhex) { 48 ; 49 } 50 } 51 } 52 53 /*** 54 * Gets the server name. 55 * @return The current server name. 56 */ 57 public String getServerName() { 58 return serverName; 59 } 60 61 /*** 62 * Sets the server name. If set to null, it tries to fall back to 63 * local host name (may not succeed). 64 * No additional checks are made. 65 * @param newServerName New server name. 66 */ 67 public void setServerName(String newServerName) { 68 if (newServerName == null) { 69 jmxProxy = null; // to force a new JMXProxy on first request 70 try { 71 serverName = InetAddress.getLocalHost().getHostName(); 72 } catch (UnknownHostException uhex) { 73 ; 74 } 75 } else { 76 if (!newServerName.equals(serverName)) { 77 jmxProxy = null; // to force a new JMXProxy on first request 78 serverName = newServerName; 79 } 80 } 81 } 82 83 /*** 84 * Gets the JNDI URL of target server. 85 * @return JNDI URL of target server. 86 */ 87 public String getJndiurl() { 88 return jndiURL; 89 } 90 91 /*** 92 * Sets the JNDI URL of target server. 93 */ 94 public void setJndiurl(String aJndiUrl) { 95 jndiURL = aJndiUrl; 96 jmxProxy = null; // force a re-get of JMXProxy 97 } 98 99 /*** 100 * Gets the JNDI name of RMI adapter. 101 * @return JNDI name of RMI adapter. 102 */ 103 public String getRmijndiname() { 104 return rmiJndiName; 105 } 106 107 /*** 108 * Sets the JNDI URL of target server. 109 */ 110 public void setetRmijndiname(String aRmiJndiName) { 111 rmiJndiName = aRmiJndiName; 112 } 113 114 /*** 115 * Gets the target MBean name. 116 * @return The target MBean name. 117 */ 118 public String getMbean() { 119 return mbean; 120 } 121 122 /*** 123 * Sets the target MBean name. 124 * @param newMbean New target MBean name. 125 */ 126 public void setMbean(String newMbean) { 127 mbean = newMbean; 128 } 129 130 /*** 131 * Gets the target method name. 132 * @return The target method name. 133 */ 134 public String getMethod() { 135 return method; 136 } 137 138 /*** 139 * Sets the target method name. 140 * @param newMethod New target method name. 141 */ 142 public void setMethod(String newMethod) { 143 method = newMethod; 144 } 145 146 /*** 147 * Gets the JBoss home as set in system property "com.maxiq.tools.jboss.home". 148 * @return The JBoss home. 149 */ 150 public String getJbhome() { 151 return System.getProperty("com.maxiq.tools.jboss.home"); 152 } 153 154 /*** 155 * Gets the JBoss home directly into 156 * system property "com.maxiq.tools.jboss.home". 157 * @param newMbean New JBoss home. 158 */ 159 public void setJbhome(String newJBHome) { 160 System.setProperty("com.maxiq.tools.jboss.home", newJBHome); 161 } 162 163 /*** 164 * Reset/clears the argument list. 165 */ 166 public void clearArguments() { 167 argSet = new Vector(); 168 argTypeSet = new Vector(); 169 } 170 171 /*** 172 * Adds a new argument. Argument type is auto-determined. If adding null, 173 * the type is supposed to be "java.lang.String" 174 * @param arg The argument to add. 175 */ 176 public void addArgument(Object arg) { 177 if (arg == null) { 178 addArgument(arg, "java.lang.String"); 179 } else { 180 addArgument(arg, arg.getClass().getName()); 181 } 182 } 183 184 /*** 185 * Adds a new typed argument. 186 * @param arg The argument value 187 * @param typename The argument type (class name) as a String 188 */ 189 public void addArgument(Object arg, String typename) { 190 argSet.add(arg); 191 argTypeSet.add(typename); 192 } 193 194 /*** 195 * Get the returned value (if any) from the last successful call to 196 * execute(). 197 * @return The value as Object 198 */ 199 public Object getLastReturnValue() { 200 return lastReturnValue; 201 } 202 203 /*** 204 * "execute" method allows using this as Ant task via Ant's proxy. 205 * For this plain class, execute allows only calls with no return value 206 * (or discarded return value) and no params, because there is no 207 * simple mechanism to add arguments from proxied Ant task-beans. 208 * @throws JBossNotAvailable when it seems there's no JBoss to talk with 209 */ 210 public void execute() throws JBossNotAvailable { 211 doExecute(); 212 } 213 214 /*** 215 * Finds out if the server is responding. Used in bean mode. 216 * @return <code>true</code> if the server seems to be available. 217 */ 218 public boolean isServerAlive() { 219 try { 220 getJMXProxy(); 221 return true; 222 } catch (JBossNotAvailable jbna) { 223 return false; 224 } 225 } 226 227 /*** 228 * Internal method to perform the actual call 229 * @throws JBossNotAvailable when it seems there's no JBoss to talk with 230 */ 231 protected void doExecute() throws JBossNotAvailable { 232 // clear as we may throw exceptions but old value is not relevant any more 233 lastReturnValue = null; 234 if (mbean == null) { 235 throw new IllegalArgumentException( 236 "The mbean attribute cannot be null"); 237 } 238 if (method == null) { 239 throw new IllegalArgumentException( 240 "The method attribute cannot be null"); 241 } 242 lastReturnValue = getJMXProxy().invoke( 243 mbean, method, 244 argSet.toArray(), 245 (String[]) argTypeSet.toArray(new String[] {}) 246 ); 247 } 248 249 /*** 250 * Creates (if necessary) and returns a working JMXProxy 251 * @return Current JMXProxy 252 * @throws JBossNotAvailable when we suspect JBoss is not running. 253 */ 254 JMXProxy getJMXProxy() throws JBossNotAvailable { 255 if (jmxProxy == null) { 256 if (jndiURL != null && !"".equals(jndiURL.trim())) { 257 String rmiName= rmiJndiName; 258 if (rmiName == null || "".equals(rmiName.trim())) { 259 rmiName = "jmx:" + serverName + ":rmi"; 260 } 261 jmxProxy = new JMXProxy(jndiURL, rmiName); 262 } else { 263 jmxProxy = new JMXProxy(serverName); 264 } 265 } 266 return jmxProxy; 267 } 268 269 }

This page was automatically generated by Maven