Java tutorial
/* * The MIT License * * Copyright 2014 CloudBees. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.cloudbees.mtslaves.client; import com.cloudbees.api.cr.Credential; import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.NullOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.TimeUnit; /** * Reference to a remote virtual machine. * * @author Kohsuke Kawaguchi */ public class VirtualMachineRef extends RemoteReference { public VirtualMachineRef(URL url, Credential token) { super(url, token); } public String getId() { String p = url.getPath(); if (p.endsWith("/")) p = p.substring(0, p.length() - 1); return p.substring(p.lastIndexOf('/') + 1); } /** * Throws away this VM. */ public void dispose() throws IOException, InterruptedException { HttpURLConnection con = open("dispose"); con.setRequestMethod("POST"); con.connect(); verifyResponseStatus(con); drain(con); } /** * Synchronously boot and wait until its fully booted. * * @return * The latest virtual machine state reported by the server when we confirmed a boot. */ public VirtualMachine bootSync() throws IOException, InterruptedException { HttpURLConnection con = open("boot"); con.setRequestMethod("POST"); con.setReadTimeout(BOOT_SYNC_READ_TIMEOUT_MS); con.connect(); drain(con); verifyResponseStatus(con); for (int i = 0; i < TIMEOUT; i++) { Thread.sleep(1000); VirtualMachine state = getState(); if (state.state == null) throw new IllegalStateException("Virtual machine failed to boot: " + state.json); switch (state.state.getId()) { default: throw new IllegalStateException(state.state.getId().toString()); case booting: continue; // wait some more case running: return state; case error: throw new IllegalStateException("Virtual machine failed to boot: " + state.json); } } throw new IOException("Time out: VM is taking forever to boot" + url); } /** * * @throws VirtualMachineConfigurationException * Strong-type binding of the server error indicating that the specified configurations had a problem. */ public void setup(VirtualMachineSpec p) throws IOException, InterruptedException, VirtualMachineConfigurationException { HttpURLConnection con = postJson(open("setup"), JSONObject.fromObject(p)); try { verifyResponseStatus(con); } catch (ServerException e) { if (e.getStatusCode() == 422) { throw new VirtualMachineConfigurationException(e, e.getJson()); } } } /** * Retrieves the current state of this {@link VirtualMachine} */ public VirtualMachine getState() throws IOException { return getResource(".", VirtualMachine.class); } /** * Renews the lease for the virtual machine so that it won't * be terminated by the watch dog. * * @throws IOException */ public void renew() throws IOException { HttpURLConnection con = open("renew"); con.setReadTimeout(RENEW_READ_TIMEOUT_MS); verifyResponseStatus(con); } /** * Sets the billing memo to indicate the business purpose of this Virtual Machine. * This memo field will be displayed on usage reports along with the duration * and virtual machine id. * * This could be any string, but is usually a JSON string containing details of * the builds run on this virtual machine. * @param p * @return */ public void setMemo(JSONObject memo) throws IOException { HttpURLConnection con = postJson(open("memo/"), memo); verifyResponseStatus(con); } @Override public String toString() { return super.toString() + "[url=" + url + ']'; } public static final int TIMEOUT = 5 * 60; /* secs */ /** * How long to wait to for the lease renewal request to complete. */ // we care less about seeing this complete public static final int RENEW_READ_TIMEOUT_MS = 5 * 1000; /** * How long to wait for a response when booting. * * The /boot call itself should be async, * so this shouldn't need to be too long. */ public static final int BOOT_SYNC_READ_TIMEOUT_MS = 125 * 1000; }