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