PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/controller-client/src/main/java/org/jboss/as/controller/client/helpers/domain/impl/DeploymentPlanBuilderImpl.java

#
Java | 273 lines | 211 code | 36 blank | 26 comment | 15 complexity | cbd85fa1ba54a7c76ce0bb24e5667f7f MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, Red Hat, Inc., 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.as.controller.client.helpers.domain.impl;
  23. import static org.jboss.as.controller.client.ControllerClientMessages.MESSAGES;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.net.URISyntaxException;
  28. import java.net.URL;
  29. import java.net.URLConnection;
  30. import org.jboss.as.controller.client.helpers.domain.AddDeploymentPlanBuilder;
  31. import org.jboss.as.controller.client.helpers.domain.DeployDeploymentPlanBuilder;
  32. import org.jboss.as.controller.client.helpers.domain.DeploymentPlanBuilder;
  33. import org.jboss.as.controller.client.helpers.domain.DuplicateDeploymentNameException;
  34. import org.jboss.as.controller.client.helpers.domain.RemoveDeploymentPlanBuilder;
  35. import org.jboss.as.controller.client.helpers.domain.ReplaceDeploymentPlanBuilder;
  36. import org.jboss.as.controller.client.helpers.domain.ServerGroupDeploymentPlan;
  37. import org.jboss.as.controller.client.helpers.domain.ServerGroupDeploymentPlanBuilder;
  38. import org.jboss.as.controller.client.helpers.domain.UndeployDeploymentPlanBuilder;
  39. /**
  40. * Builder capable of creating a {@link DeploymentPlanImpl}.
  41. *
  42. * @author Brian Stansberry
  43. */
  44. class DeploymentPlanBuilderImpl extends AbstractDeploymentPlanBuilder implements DeploymentPlanBuilder {
  45. private final DeploymentContentDistributor deploymentDistributor;
  46. DeploymentPlanBuilderImpl(DeploymentContentDistributor deploymentDistributor) {
  47. super();
  48. if (deploymentDistributor == null)
  49. throw MESSAGES.nullVar("deploymentDistributor");
  50. this.deploymentDistributor = deploymentDistributor;
  51. }
  52. DeploymentPlanBuilderImpl(DeploymentPlanBuilderImpl existing, boolean globalRollback) {
  53. super(existing, globalRollback);
  54. this.deploymentDistributor = existing.deploymentDistributor;
  55. }
  56. DeploymentPlanBuilderImpl(DeploymentPlanBuilderImpl existing, DeploymentSetPlanImpl setPlan) {
  57. super(existing, setPlan);
  58. this.deploymentDistributor = existing.deploymentDistributor;
  59. }
  60. @Override
  61. public AddDeploymentPlanBuilder add(File file) throws IOException, DuplicateDeploymentNameException {
  62. String name = file.getName();
  63. return add(name, name, file.toURI().toURL());
  64. }
  65. @Override
  66. public AddDeploymentPlanBuilder add(URL url) throws IOException, DuplicateDeploymentNameException {
  67. String name = getName(url);
  68. return add(name, name, url);
  69. }
  70. @Override
  71. public AddDeploymentPlanBuilder add(String name, File file) throws IOException, DuplicateDeploymentNameException {
  72. return add(name, name, file.toURI().toURL());
  73. }
  74. @Override
  75. public AddDeploymentPlanBuilder add(String name, URL url) throws IOException, DuplicateDeploymentNameException {
  76. return add(name, name, url);
  77. }
  78. @Override
  79. public AddDeploymentPlanBuilder add(String name, InputStream stream) throws IOException, DuplicateDeploymentNameException {
  80. return add(name, name, stream);
  81. }
  82. @Override
  83. public AddDeploymentPlanBuilder add(String name, String commonName,
  84. InputStream stream) throws IOException, DuplicateDeploymentNameException {
  85. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  86. if (currentSet.hasServerGroupPlans()) {
  87. throw MESSAGES.cannotAddDeploymentAction();
  88. }
  89. byte[] hash = deploymentDistributor.distributeDeploymentContent(name, commonName, stream);
  90. DeploymentActionImpl mod = DeploymentActionImpl.getAddAction(name, commonName, hash);
  91. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  92. return new AddDeploymentPlanBuilderImpl(this, newSet);
  93. }
  94. @Override
  95. public AddDeploymentPlanBuilder add(String name) throws IOException {
  96. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  97. if (currentSet.hasServerGroupPlans()) {
  98. throw MESSAGES.cannotAddDeploymentAction();
  99. }
  100. DeploymentActionImpl mod = DeploymentActionImpl.getAddAction(name, null, null);
  101. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  102. return new AddDeploymentPlanBuilderImpl(this, newSet);
  103. }
  104. @Override
  105. public DeployDeploymentPlanBuilder deploy(String key) {
  106. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  107. if (currentSet.hasServerGroupPlans()) {
  108. throw MESSAGES.cannotAddDeploymentAction();
  109. }
  110. DeploymentActionImpl mod = DeploymentActionImpl.getDeployAction(key);
  111. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  112. return new DeployDeploymentPlanBuilderImpl(this, newSet);
  113. }
  114. @Override
  115. public UndeployDeploymentPlanBuilder undeploy(String key) {
  116. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  117. if (currentSet.hasServerGroupPlans()) {
  118. throw MESSAGES.cannotAddDeploymentAction();
  119. }
  120. DeploymentActionImpl mod = DeploymentActionImpl.getUndeployAction(key);
  121. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  122. return new UndeployDeploymentPlanBuilderImpl(this, newSet);
  123. }
  124. @Override
  125. public DeploymentPlanBuilder redeploy(String deploymentName) {
  126. DeploymentActionImpl mod = DeploymentActionImpl.getRedeployAction(deploymentName);
  127. return getNewBuilder(mod);
  128. }
  129. @Override
  130. public ReplaceDeploymentPlanBuilder replace(String replacement, String toReplace) {
  131. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  132. if (currentSet.hasServerGroupPlans()) {
  133. throw MESSAGES.cannotAddDeploymentAction();
  134. }
  135. DeploymentActionImpl mod = DeploymentActionImpl.getReplaceAction(replacement, toReplace);
  136. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  137. return new ReplaceDeploymentPlanBuilderImpl(this, newSet);
  138. }
  139. @Override
  140. public RemoveDeploymentPlanBuilder replace(File file) throws IOException {
  141. String name = file.getName();
  142. return replace(name, name, file.toURI().toURL());
  143. }
  144. @Override
  145. public RemoveDeploymentPlanBuilder replace(URL url) throws IOException {
  146. String name = getName(url);
  147. return replace(name, name, url);
  148. }
  149. @Override
  150. public RemoveDeploymentPlanBuilder replace(String name, File file) throws IOException {
  151. return replace(name, name, file.toURI().toURL());
  152. }
  153. @Override
  154. public RemoveDeploymentPlanBuilder replace(String name, URL url) throws IOException {
  155. return replace(name, name, url);
  156. }
  157. @Override
  158. public RemoveDeploymentPlanBuilder replace(String name, InputStream stream) throws IOException {
  159. return replace(name, name, stream);
  160. }
  161. @Override
  162. public RemoveDeploymentPlanBuilder replace(String name, String commonName, InputStream stream) throws IOException {
  163. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  164. if (currentSet.hasServerGroupPlans()) {
  165. throw MESSAGES.cannotAddDeploymentAction();
  166. }
  167. byte[] hash = deploymentDistributor.distributeReplacementDeploymentContent(name, commonName, stream);
  168. DeploymentActionImpl mod = DeploymentActionImpl.getFullReplaceAction(name, commonName, hash);
  169. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  170. return new RemoveDeploymentPlanBuilderImpl(this, newSet);
  171. }
  172. @Override
  173. public RemoveDeploymentPlanBuilder remove(String key) {
  174. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  175. if (currentSet.hasServerGroupPlans()) {
  176. throw MESSAGES.cannotAddDeploymentAction();
  177. }
  178. DeploymentActionImpl mod = DeploymentActionImpl.getRemoveAction(key);
  179. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  180. return new RemoveDeploymentPlanBuilderImpl(this, newSet);
  181. }
  182. ServerGroupDeploymentPlanBuilder toServerGroup(final String serverGroupName) {
  183. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  184. DeploymentSetPlanImpl newSet = currentSet.storeServerGroup(new ServerGroupDeploymentPlan(serverGroupName));
  185. return new ServerGroupDeploymentPlanBuilderImpl(this, newSet);
  186. }
  187. private AddDeploymentPlanBuilder add(String name, String commonName, URL url) throws IOException, DuplicateDeploymentNameException {
  188. URLConnection conn = url.openConnection();
  189. conn.connect();
  190. InputStream stream = conn.getInputStream();
  191. try {
  192. return add(name, commonName, stream);
  193. }
  194. finally {
  195. try { stream.close(); } catch (Exception ignored) {}
  196. }
  197. }
  198. private RemoveDeploymentPlanBuilder replace(String name, String commonName, URL url) throws IOException {
  199. URLConnection conn = url.openConnection();
  200. conn.connect();
  201. InputStream stream = conn.getInputStream();
  202. try {
  203. return replace(name, commonName, stream);
  204. }
  205. finally {
  206. try { stream.close(); } catch (Exception ignored) {}
  207. }
  208. }
  209. DeploymentPlanBuilderImpl getNewBuilder(DeploymentActionImpl mod) {
  210. DeploymentSetPlanImpl currentSet = getCurrentDeploymentSetPlan();
  211. if (currentSet.hasServerGroupPlans()) {
  212. throw MESSAGES.cannotAddDeploymentAction();
  213. }
  214. DeploymentSetPlanImpl newSet = currentSet.addAction(mod);
  215. return new DeploymentPlanBuilderImpl(this, newSet);
  216. }
  217. private static String getName(URL url) {
  218. if ("file".equals(url.getProtocol())) {
  219. try {
  220. File f = new File(url.toURI());
  221. return f.getName();
  222. } catch (URISyntaxException e) {
  223. throw MESSAGES.invalidUri(e, url);
  224. }
  225. }
  226. String path = url.getPath();
  227. int idx = path.lastIndexOf('/');
  228. while (idx == path.length() - 1) {
  229. path = path.substring(0, idx);
  230. idx = path.lastIndexOf('/');
  231. }
  232. if (idx == -1) {
  233. throw MESSAGES.cannotDeriveDeploymentName(url);
  234. }
  235. return path.substring(idx + 1);
  236. }
  237. }