Java tutorial
/******************************************************************************* * Copyright (c) 2010 SOPERA GmbH. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * SOPERA GmbH - initial API and implementation *******************************************************************************/ package org.eclipse.swordfish.tools.deployer; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethodRetryHandler; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.params.HttpMethodParams; import org.eclipse.osgi.framework.console.CommandInterpreter; import org.eclipse.osgi.framework.console.CommandProvider; public class SwordfishClientCommandProvider implements CommandProvider { private static final String IU_LIST = "iu list"; private static final String IU_OPERATION = "iuOperation"; private static final String OP_INSTALL = "install"; private static final String OP_UPDATE = "update"; private static final String OP_REMOVE = "remove"; private static final String IU_TARGET_PROFILE = "iuTargetProfile"; private static final String IU_ID = "iuId."; private String repository = System.getProperty(this.getClass().getSimpleName() + ".repositoryName", null); private String servletPath = System.getProperty(this.getClass().getSimpleName() + ".servletPath", "/p2/provisioning"); private String targetHost = System.getProperty(this.getClass().getSimpleName() + ".targetHost", "http://localhost:9001"); private HttpClient httpClient = new HttpClient(); private HttpMethodRetryHandler retryhandler = new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) { return false; } }; public Object _sfsethost(CommandInterpreter intp) { try { targetHost = getMandatoryParameter(intp, "host"); } catch (IllegalArgumentException iae) { intp.println(iae.getMessage()); } return null; } public Object _sfsetrepo(CommandInterpreter intp) { try { repository = getMandatoryParameter(intp, "repository"); } catch (IllegalArgumentException iae) { intp.println(iae.getMessage()); } return null; } public Object _sfinstall(CommandInterpreter intp) { try { String ius = getMandatoryParameter(intp, IU_LIST); String profile = intp.nextArgument(); PutMethod putMethod = createPutRequest(ius, OP_INSTALL, profile); RequestEntity entity = new InputStreamRequestEntity(new FileInputStream(repository)); putMethod.setRequestEntity(entity); httpClient.executeMethod(putMethod); showResponse(intp, putMethod); } catch (Exception e) { handleException(intp, "Install", e); } return null; } public Object _sfupdate(CommandInterpreter intp) { try { String ius = getMandatoryParameter(intp, IU_LIST); String profile = intp.nextArgument(); PutMethod putMethod = createPutRequest(ius, OP_UPDATE, profile); RequestEntity entity = new InputStreamRequestEntity(new FileInputStream(repository)); putMethod.setRequestEntity(entity); httpClient.executeMethod(putMethod); showResponse(intp, putMethod); } catch (Exception e) { handleException(intp, "Update", e); } return null; } public Object _sfremove(CommandInterpreter intp) { try { String ius = getMandatoryParameter(intp, IU_LIST); String profile = intp.nextArgument(); PutMethod putMethod = createPutRequest(ius, OP_REMOVE, profile); httpClient.executeMethod(putMethod); showResponse(intp, putMethod); } catch (Exception e) { handleException(intp, "Remove", e); } return null; } public String getHelp() { StringBuilder sb = new StringBuilder(); sb.append("---Swordfish Deploy Commands---\n"); sb.append("\tsfsethost <host> - (set target host name for deployment)\n"); sb.append("\tsfsetrepo <path> - (set repository with path)\n"); sb.append("\tsfinstall <iu>[,<iu>] [profile] - (install iu's)\n"); sb.append("\tsfupdate <iu>[,<iu>] [profile] - (update currently active iu's from repository)\n"); sb.append("\tsfremove <iu>[,<iu>] [profile] - (remove iu's)\n"); return sb.toString(); } private void handleException(CommandInterpreter intp, String info, Exception ex) { intp.println(info + ": " + ex.getMessage()); if (!(ex instanceof IllegalArgumentException)) { intp.printStackTrace(ex); } } private void showResponse(CommandInterpreter intp, PutMethod pm) throws IOException { String prefix = (pm.getStatusCode() != 200) ? "Error: " : "Done: "; intp.println(prefix + pm.getStatusText()); } private PutMethod createPutRequest(String ius, String operation, String profile) { PutMethod putMethod = new PutMethod(targetHost + servletPath + "/" + repository); putMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler); putMethod.setRequestHeader(new Header(IU_OPERATION, operation)); String[] iuList = ius.split(","); for (int i = 0; i < iuList.length; i++) { putMethod.setRequestHeader(new Header(IU_ID + i, iuList[i])); } if ((profile != null) && !("".equals(profile))) { putMethod.setRequestHeader(new Header(IU_TARGET_PROFILE, profile)); } return putMethod; } private String getMandatoryParameter(CommandInterpreter intp, String name) { String parameter = intp.nextArgument(); if (parameter == null || "".equals(parameter)) { String msg = "Invalid or missing " + name + " parameter: " + parameter; throw new IllegalArgumentException(msg); } return parameter; } }