PageRenderTime 199ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jboss-5.1.0/deployment/src/main/org/jboss/deployment/spi/JMXTarget.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 384 lines | 267 code | 33 blank | 84 comment | 28 complexity | 664f53e6c611783d9d588556b737988b MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.deployment.spi;
  23. import java.net.InetAddress;
  24. import java.net.URI;
  25. import java.net.URL;
  26. import java.net.URLDecoder;
  27. import java.net.UnknownHostException;
  28. import java.util.ArrayList;
  29. import java.util.Hashtable;
  30. import java.util.List;
  31. import java.util.Properties;
  32. import java.util.StringTokenizer;
  33. import javax.enterprise.deploy.shared.ModuleType;
  34. import javax.enterprise.deploy.spi.TargetModuleID;
  35. import javax.enterprise.deploy.spi.exceptions.TargetException;
  36. import javax.management.MBeanServerConnection;
  37. import javax.management.ObjectName;
  38. import javax.naming.Context;
  39. import javax.naming.InitialContext;
  40. import javax.naming.NamingException;
  41. import org.jboss.logging.Logger;
  42. /**
  43. * A Target that deploys using the JMX adaptor to communicate with the
  44. * MainDeployer using file URLs to the deployments. This target is selected
  45. * by including a targetType=jmx param in the DeploymentManager deployURI.
  46. *
  47. * @author Thomas.Diesler@jboss.org
  48. * @author Scott.Stark@jboss.com
  49. * @version $Revision: 81011 $
  50. */
  51. public class JMXTarget implements JBossTarget
  52. {
  53. private static final Logger log = Logger.getLogger(JMXTarget.class);
  54. public static final String DESCRIPTION = "JBoss JMX deployment target";
  55. /**
  56. * The default RMIAdaptor JNDI location
  57. */
  58. private static final String DEFAULT_ADAPTOR_PATH = "/jmx/invoker/RMIAdaptor";
  59. /** The JSR88 deployment manager default and uri option name */
  60. private static final String JSR88_MBEAN = "jboss.management.local:type=JSR88DeploymentManager,name=DefaultManager";
  61. private static final String JSR88_MBEAN_OPT = "jsr88MBean";
  62. /** The deployment target uri */
  63. private URI deployURI;
  64. /** The JNDI properties used to locate the MBeanServer */
  65. private Properties jndiEnv;
  66. private ObjectName jsr88MBean;
  67. /**
  68. * @todo merge the query parameter parsing for jndi and mbean names and
  69. * test proper escaping of unsafe url chars.
  70. *
  71. * @param deployURI
  72. */
  73. public JMXTarget(URI deployURI)
  74. {
  75. log.debug("new JMXTarget: " + deployURI);
  76. try
  77. {
  78. String localHostName = InetAddress.getLocalHost().getHostName();
  79. String scheme = deployURI.getScheme();
  80. String host = deployURI.getHost();
  81. int port = deployURI.getPort();
  82. String path = deployURI.getPath();
  83. String query = deployURI.getRawQuery();
  84. String uri = deployURI.toASCIIString();
  85. if (uri.startsWith(DeploymentManagerImpl.DEPLOYER_URI))
  86. {
  87. // Using JNDI defaults
  88. scheme = "jnp";
  89. host = localHostName;
  90. port = 1099;
  91. path = DEFAULT_ADAPTOR_PATH;
  92. try
  93. {
  94. InitialContext iniCtx = new InitialContext();
  95. Hashtable env = iniCtx.getEnvironment();
  96. String providerURL = (String)env.get(InitialContext.PROVIDER_URL);
  97. if (providerURL != null)
  98. {
  99. // In case there is no schema returned
  100. if (providerURL.indexOf("://") < 0)
  101. providerURL = "jnp://" + providerURL;
  102. URI providerURI = new URI(providerURL);
  103. scheme = providerURI.getScheme();
  104. host = providerURI.getHost();
  105. port = providerURI.getPort();
  106. }
  107. }
  108. catch (NamingException e)
  109. {
  110. log.error(e);
  111. }
  112. }
  113. StringBuffer tmp = new StringBuffer(scheme + "://");
  114. tmp.append(host != null ? host : localHostName);
  115. tmp.append(port > 0 ? ":" + port : "");
  116. tmp.append(path != null && path.length() > 0 ? path : DEFAULT_ADAPTOR_PATH);
  117. tmp.append(query != null ? "?" + query : "");
  118. deployURI = new URI(tmp.toString());
  119. log.debug("URI changed to: " + deployURI);
  120. this.deployURI = deployURI;
  121. // Parse the query for options
  122. parseQuery();
  123. }
  124. catch (Exception e)
  125. {
  126. log.error(e);
  127. }
  128. }
  129. /**
  130. * Get the target's description
  131. * @return the description
  132. */
  133. public String getDescription()
  134. {
  135. return DESCRIPTION;
  136. }
  137. /**
  138. * Get the target's name
  139. * @return the name
  140. */
  141. public String getName()
  142. {
  143. return deployURI.toString();
  144. }
  145. /**
  146. * Get the target's host name
  147. */
  148. public String getHostName()
  149. {
  150. return deployURI.getHost();
  151. }
  152. /**
  153. * Deploy a given module
  154. */
  155. public void deploy(TargetModuleID targetModuleID) throws Exception
  156. {
  157. TargetModuleIDImpl moduleID = (TargetModuleIDImpl)targetModuleID;
  158. SerializableTargetModuleID smoduleID = new SerializableTargetModuleID(moduleID);
  159. MBeanServerConnection server = getMBeanServerConnection();
  160. String url = targetModuleID.getModuleID();
  161. Object[] args = { smoduleID };
  162. String[] sig = { smoduleID.getClass().getName() };
  163. log.info("Begin deploy: " + url);
  164. server.invoke(jsr88MBean, "deploy", args, sig);
  165. log.info("End deploy");
  166. }
  167. /**
  168. * Start a given module
  169. */
  170. public void start(TargetModuleID targetModuleID) throws Exception
  171. {
  172. MBeanServerConnection server = getMBeanServerConnection();
  173. URL url = new URL(targetModuleID.getModuleID());
  174. Object[] args = { url };
  175. String[] sig = { URL.class.getName() };
  176. log.debug("Start: " + url);
  177. args = new Object[] { url.toExternalForm() };
  178. sig = new String[] { String.class.getName() };
  179. log.info("Begin start: " + url);
  180. server.invoke(jsr88MBean, "start", args, sig);
  181. log.info("End start");
  182. }
  183. /**
  184. * Stop a given module
  185. */
  186. public void stop(TargetModuleID targetModuleID) throws Exception
  187. {
  188. MBeanServerConnection server = getMBeanServerConnection();
  189. URL url = new URL(targetModuleID.getModuleID());
  190. Object[] args = { url };
  191. String[] sig = { URL.class.getName() };
  192. log.debug("Stop: " + url);
  193. args = new Object[] { url.toExternalForm() };
  194. sig = new String[] { String.class.getName() };
  195. log.info("Begin stop: " + url);
  196. server.invoke(jsr88MBean, "stop", args, sig);
  197. log.info("End stop");
  198. }
  199. /**
  200. * Undeploy a given module
  201. */
  202. public void undeploy(TargetModuleID targetModuleID) throws Exception
  203. {
  204. MBeanServerConnection server = getMBeanServerConnection();
  205. String url = targetModuleID.getModuleID();
  206. Object[] args = { url };
  207. String[] sig = { String.class.getName() };
  208. log.info("Begin undeploy: " + url);
  209. server.invoke(jsr88MBean, "undeploy", args, sig);
  210. log.info("End undeploy");
  211. }
  212. /**
  213. * Retrieve the list of all J2EE application modules running or not running
  214. * on the identified targets.
  215. */
  216. public TargetModuleID[] getAvailableModules(ModuleType moduleType) throws TargetException
  217. {
  218. try
  219. {
  220. List list = new ArrayList();
  221. MBeanServerConnection server = getMBeanServerConnection();
  222. Object[] args = { new Integer(moduleType.getValue()) };
  223. String[] sig = { int.class.getName() };
  224. SerializableTargetModuleID[] modules = (SerializableTargetModuleID[])server.invoke(jsr88MBean, "getAvailableModules", args, sig);
  225. for (int n = 0; n < modules.length; n++)
  226. {
  227. SerializableTargetModuleID id = modules[n];
  228. String moduleID = id.getModuleID();
  229. boolean isRunning = id.isRunning();
  230. ModuleType type = ModuleType.getModuleType(id.getModuleType());
  231. TargetModuleIDImpl tmid = new TargetModuleIDImpl(this, moduleID, null, isRunning, type);
  232. convertChildren(tmid, id);
  233. list.add(tmid);
  234. }
  235. TargetModuleID[] targetModuleIDs = new TargetModuleID[list.size()];
  236. list.toArray(targetModuleIDs);
  237. return targetModuleIDs;
  238. }
  239. catch (Exception e)
  240. {
  241. TargetException tex = new TargetException("Failed to get available modules");
  242. tex.initCause(e);
  243. throw tex;
  244. }
  245. }
  246. private void convertChildren(TargetModuleIDImpl parent, SerializableTargetModuleID parentID)
  247. {
  248. SerializableTargetModuleID[] children = parentID.getChildModuleIDs();
  249. int length = children != null ? children.length : 0;
  250. for (int n = 0; n < length; n++)
  251. {
  252. SerializableTargetModuleID id = children[n];
  253. String moduleID = id.getModuleID();
  254. boolean isRunning = id.isRunning();
  255. ModuleType type = ModuleType.getModuleType(id.getModuleType());
  256. TargetModuleIDImpl child = new TargetModuleIDImpl(this, moduleID, parent, isRunning, type);
  257. parent.addChildTargetModuleID(child);
  258. convertChildren(child, id);
  259. }
  260. }
  261. // Get MBeanServerConnection
  262. private MBeanServerConnection getMBeanServerConnection() throws NamingException
  263. {
  264. Properties env = buildJNDIEnv();
  265. String lookupPath = deployURI.getPath();
  266. log.debug("JNDI lookup: " + lookupPath);
  267. log.trace("Creating InitialContext with env: " + env);
  268. InitialContext ctx = new InitialContext(env);
  269. MBeanServerConnection server = (MBeanServerConnection)ctx.lookup(lookupPath);
  270. return server;
  271. }
  272. /**
  273. * Parse the query portion of the deployment URI to look for options:
  274. * jsr88MBean : specifies the JSR88 mbean service that provides the
  275. * deployment manager deploy/start/stop/undeploy operations.
  276. */
  277. private void parseQuery() throws Exception
  278. {
  279. String query = deployURI.getRawQuery();
  280. log.debug("DeployURI.rawQuery: " + query);
  281. Properties params = new Properties();
  282. if (query != null)
  283. {
  284. /* Break the raw query into the name=value pairs. This processing is
  285. too fragile to how the URI was built as it expects that only
  286. the name/value portions of the query were encoded.
  287. */
  288. String[] options = query.split("[&=]");
  289. for (int n = 0; n < options.length; n += 2)
  290. {
  291. String name = URLDecoder.decode(options[n], "UTF-8");
  292. String value = URLDecoder.decode(options[n + 1], "UTF-8");
  293. params.setProperty(name, value);
  294. }
  295. }
  296. String name = params.getProperty(JSR88_MBEAN_OPT, JSR88_MBEAN);
  297. jsr88MBean = new ObjectName(name);
  298. }
  299. private Properties buildJNDIEnv()
  300. {
  301. if (jndiEnv == null)
  302. {
  303. jndiEnv = new Properties();
  304. // Parse the query string for name=value pairs to put into the env
  305. String query = deployURI.getQuery();
  306. if (query != null)
  307. {
  308. log.debug("Parsing query string: " + query);
  309. StringTokenizer tokenizer = new StringTokenizer(query, "=&");
  310. while (tokenizer.hasMoreTokens())
  311. {
  312. String name = tokenizer.nextToken();
  313. String value = tokenizer.nextToken();
  314. jndiEnv.setProperty(name, value);
  315. }
  316. }
  317. // Set defaults for missing properties
  318. if (jndiEnv.getProperty(Context.INITIAL_CONTEXT_FACTORY) == null)
  319. {
  320. jndiEnv.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
  321. }
  322. if (jndiEnv.getProperty(Context.PROVIDER_URL) == null)
  323. {
  324. String host = deployURI.getHost();
  325. if (host == null)
  326. {
  327. try
  328. {
  329. host = InetAddress.getLocalHost().getHostName();
  330. }
  331. catch (UnknownHostException e)
  332. {
  333. host = "localhost";
  334. }
  335. }
  336. int port = deployURI.getPort();
  337. if (port <= 0)
  338. {
  339. port = 1099;
  340. }
  341. String jnpURL = "jnp://" + host + ':' + port;
  342. jndiEnv.setProperty(Context.PROVIDER_URL, jnpURL);
  343. }
  344. if (jndiEnv.getProperty(Context.OBJECT_FACTORIES) == null)
  345. {
  346. jndiEnv.setProperty(Context.OBJECT_FACTORIES, "org.jboss.naming");
  347. }
  348. }
  349. return jndiEnv;
  350. }
  351. }