Java tutorial
/* 1: */ package com.newrelic.agent; /* 2: */ /* 3: */ import com.newrelic.agent.config.AgentConfig; /* 4: */ import com.newrelic.agent.config.ConfigService; /* 5: */ import com.newrelic.agent.config.ConfigServiceFactory; /* 6: */ import com.newrelic.agent.deps.org.apache.commons.cli.CommandLine; /* 7: */ import com.newrelic.agent.util.Streams; /* 8: */ import java.io.ByteArrayOutputStream; /* 9: */ import java.io.IOException; /* 10: */ import java.io.InputStream; /* 11: */ import java.io.OutputStream; /* 12: */ import java.io.PrintStream; /* 13: */ import java.net.HttpURLConnection; /* 14: */ import java.net.URL; /* 15: */ import java.net.URLEncoder; /* 16: */ import java.text.MessageFormat; /* 17: */ /* 18: */ public class Deployments /* 19: */ { /* 20: */ static final String REVISION_OPTION = "revision"; /* 21: */ static final String CHANGE_LOG_OPTION = "changes"; /* 22: */ static final String APP_NAME_OPTION = "appname"; /* 23: */ static final String USER_OPTION = "user"; /* 24: */ static final String ENVIRONMENT_OPTION = "environment"; /* 25: */ /* 26: */ static int recordDeployment(CommandLine cmd)/* 27: */ throws Exception /* 28: */ { /* 29: 27 */ if (cmd.hasOption("environment")) { /* 30: 28 */ System.setProperty("newrelic.environment", cmd.getOptionValue("environment")); /* 31: */ } /* 32: 30 */ AgentConfig config = ConfigServiceFactory.createConfigService().getDefaultAgentConfig(); /* 33: 31 */ return recordDeployment(cmd, config); /* 34: */ } /* 35: */ /* 36: */ static int recordDeployment(CommandLine cmd, AgentConfig config)/* 37: */ throws Exception /* 38: */ { /* 39: 35 */ String appName = config.getApplicationName(); /* 40: 36 */ if (cmd.hasOption("appname")) { /* 41: 37 */ appName = cmd.getOptionValue("appname"); /* 42: */ } /* 43: 39 */ if (appName == null) { /* 44: 40 */ throw new IllegalArgumentException( "A deployment must be associated with an application. Set app_name in newrelic.yml or specify the application name with the -appname switch."); /* 45: */ } /* 46: 43 */ System.out.println("Recording a deployment for application " + appName); /* 47: */ /* 48: 45 */ String uri = "/deployments.xml"; /* 49: 46 */ String payload = getDeploymentPayload(appName, cmd); /* 50: 47 */ String protocol = "http" + (config.isSSL() ? "s" : ""); /* 51: 48 */ URL url = new URL(protocol, config.getApiHost(), config.getApiPort(), uri); /* 52: */ /* 53: 50 */ System.out.println(MessageFormat.format("Opening connection to {0}:{1}", new Object[] { config.getApiHost(), Integer.toString(config.getApiPort()) })); /* 54: */ /* 55: */ /* 56: 53 */ HttpURLConnection conn = (HttpURLConnection) url.openConnection(); /* 57: 54 */ conn.setRequestProperty("x-license-key", config.getLicenseKey()); /* 58: */ /* 59: 56 */ conn.setRequestMethod("POST"); /* 60: 57 */ conn.setConnectTimeout(10000); /* 61: 58 */ conn.setReadTimeout(10000); /* 62: 59 */ conn.setDoOutput(true); /* 63: 60 */ conn.setDoInput(true); /* 64: */ /* 65: 62 */ conn.setRequestProperty("Content-Length", Integer.toString(payload.length())); /* 66: 63 */ conn.setFixedLengthStreamingMode(payload.length()); /* 67: 64 */ conn.getOutputStream().write(payload.getBytes()); /* 68: */ /* 69: 66 */ int responseCode = conn.getResponseCode(); /* 70: 67 */ if (responseCode < 300) /* 71: */ { /* 72: 68 */ System.out.println("Deployment successfully recorded"); /* 73: */ } /* 74: 69 */ else if (responseCode == 401) /* 75: */ { /* 76: 70 */ System.out.println( "Unable to notify New Relic of the deployment because of an authorization error. Check your license key."); /* 77: 71 */ System.out.println("Response message: " + conn.getResponseMessage()); /* 78: */ } /* 79: */ else /* 80: */ { /* 81: 73 */ System.out.println("Unable to notify New Relic of the deployment"); /* 82: 74 */ System.out.println("Response message: " + conn.getResponseMessage()); /* 83: */ } /* 84: 76 */ boolean isError = responseCode >= 300; /* 85: 77 */ if ((isError) || (config.isDebugEnabled())) /* 86: */ { /* 87: 78 */ System.out.println("Response code: " + responseCode); /* 88: 79 */ InputStream inStream = isError ? conn.getErrorStream() : conn.getInputStream(); /* 89: 81 */ if (inStream != null) /* 90: */ { /* 91: 82 */ ByteArrayOutputStream output = new ByteArrayOutputStream(); /* 92: 83 */ Streams.copy(inStream, output); /* 93: */ /* 94: 85 */ PrintStream out = isError ? System.err : System.out; /* 95: */ /* 96: 87 */ out.println(output); /* 97: */ } /* 98: */ } /* 99: 90 */ return responseCode; /* 100: */ } /* 101: */ /* 102: */ private static String getDeploymentPayload(String appName, CommandLine cmd) /* 103: */ throws IOException /* 104: */ { /* 105: 98 */ StringBuilder builder = new StringBuilder(); /* 106: 99 */ builder.append("deployment[timestamp]=").append(System.currentTimeMillis()); /* 107: */ /* 108:101 */ builder.append("&deployment[appname]=").append(URLEncoder.encode(appName, "UTF-8")); /* 109:102 */ if (cmd.getArgs().length > 1) { /* 110:103 */ builder.append("&deployment[description]=") .append(URLEncoder.encode(cmd.getArgs()[1], "UTF-8")); /* 111: */ } /* 112:105 */ if (cmd.hasOption("user")) { /* 113:106 */ builder.append("&deployment[user]=") .append(URLEncoder.encode(cmd.getOptionValue("user"), "UTF-8")); /* 114: */ } /* 115:108 */ if (cmd.hasOption("revision")) { /* 116:109 */ builder.append("&deployment[revision]=") .append(URLEncoder.encode(cmd.getOptionValue("revision"), "UTF-8")); /* 117: */ } /* 118:112 */ if (cmd.hasOption("changes")) /* 119: */ { /* 120:113 */ System.out.println("Reading the change log from standard input..."); /* 121: */ try /* 122: */ { /* 123:115 */ ByteArrayOutputStream output = new ByteArrayOutputStream(); /* 124:116 */ Streams.copy(System.in, output); /* 125: */ /* 126:118 */ builder.append("&deployment[changelog]=") .append(URLEncoder.encode(output.toString(), "UTF-8")); /* 127: */ } /* 128: */ catch (IOException ex) /* 129: */ { /* 130:120 */ throw new IOException("An error occurred reading the change log from standard input", ex); /* 131: */ } /* 132: */ } /* 133:124 */ return builder.toString(); /* 134: */ } /* 135: */ } /* Location: E:\tmp\newrelic\newrelic.jar * Qualified Name: com.newrelic.agent.Deployments * JD-Core Version: 0.7.0.1 */