Version 1.7.4
[gae.git] / java / src / main / com / google / appengine / tools / development / EnvironmentVariableChecker.java
blob18e70eab108c47a27423f066fb64d9b864e1bfd8
1 package com.google.appengine.tools.development;
3 import com.google.apphosting.utils.config.AppEngineConfigException;
4 import com.google.apphosting.utils.config.AppEngineWebXml;
5 import com.google.common.annotations.VisibleForTesting;
6 import com.google.common.collect.ImmutableList;
8 import java.io.File;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.logging.Logger;
13 /**
14 * Checker for reporting differences between environment variables specified
15 * in an application's appengine-web.xml files and values from
16 * {@link System#getenv()}.
17 * <p>
18 * Users can specify values for environment variables in an application's
19 * WEB-INF/appengine-web.xml file. It is not possible to set environment
20 * variables after JVM startup so we report mismatches based on the current
21 * reporting policy.
24 class EnvironmentVariableChecker {
25 @VisibleForTesting
26 static final Logger LOGGER = Logger.getLogger(EnvironmentVariableChecker.class.getName());
28 /**
29 * Enum for reporting policy.
31 static enum MismatchReportingPolicy {
32 /** Report mismatches by logging. */
33 LOG,
34 /** Report mismatches by throwing an {@link AppEngineConfigException} */
35 EXCEPTION,
36 /** Don't report mismatches. */
37 NONE
40 private final MismatchReportingPolicy mismatchReportingPolicy;
41 private final ImmutableList.Builder<Mismatch> mismatchListBuilder = ImmutableList.builder();
43 EnvironmentVariableChecker(MismatchReportingPolicy mismatchReportingPolicy) {
44 this.mismatchReportingPolicy = mismatchReportingPolicy;
47 void add(AppEngineWebXml appEngineWebXml, File appEngineWebXmlFile) {
48 for (Map.Entry<String, String> entry : appEngineWebXml.getEnvironmentVariables().entrySet()) {
49 if (!entry.getValue().equals(System.getenv(entry.getKey()))) {
50 mismatchListBuilder.add(new Mismatch(entry.getKey(), System.getenv(entry.getKey()),
51 entry.getValue(), appEngineWebXmlFile));
56 void check() throws AppEngineConfigException {
57 List<Mismatch> mismatches = mismatchListBuilder.build();
58 if (!mismatches.isEmpty()) {
59 String msg =
60 "One or more environment variables have been configured in appengine-web.xml that have "
61 + "missing or different values in your local environment. We recommend you use system "
62 + "properties instead, but if you are interacting with legacy code that requires "
63 + "specific environment variables to have specific values, please set these environment "
64 + "variables in your environment before running.\n"
65 + mismatches;
67 if (mismatchReportingPolicy == MismatchReportingPolicy.LOG) {
68 LOGGER.warning(msg);
69 } else if (mismatchReportingPolicy == MismatchReportingPolicy.EXCEPTION) {
70 throw new IncorrectEnvironmentVariableException(msg, mismatches);
76 @VisibleForTesting
77 static class Mismatch {
78 private final String environmentVariableName;
79 private final String environmentVariableValue;
80 private final String appEngineWebXmlValue;
81 private final File appEngineWebXmlFile;
83 Mismatch(String environmentVariableName, String environmentVariableValue,
84 String appEngineWebXmlValue, File appEngineWebXmlFile) {
85 this.environmentVariableName = environmentVariableName;
86 this.environmentVariableValue = environmentVariableValue;
87 this.appEngineWebXmlValue = appEngineWebXmlValue;
88 this.appEngineWebXmlFile = appEngineWebXmlFile;
91 /**
92 * @return the environmentVariableName
94 public String getEnvironmentVariableName() {
95 return environmentVariableName;
98 /**
99 * @return the environmentVariableValue
101 public String getEnvironmentVariableValue() {
102 return environmentVariableValue;
106 * @return the appEngineWebXmlValue
108 public String getAppEngineWebXmlValue() {
109 return appEngineWebXmlValue;
113 * @return the appEngineWebXmlFile
115 public File getAppEngineWebXmlFile() {
116 return appEngineWebXmlFile;
119 @Override
120 public int hashCode() {
121 final int prime = 31;
122 int result = 1;
123 result =
124 prime * result + ((appEngineWebXmlFile == null) ? 0 : appEngineWebXmlFile.hashCode());
125 result =
126 prime * result + ((appEngineWebXmlValue == null) ? 0 : appEngineWebXmlValue.hashCode());
127 result = prime * result
128 + ((environmentVariableName == null) ? 0 : environmentVariableName.hashCode());
129 result = prime * result
130 + ((environmentVariableValue == null) ? 0 : environmentVariableValue.hashCode());
131 return result;
134 @Override
135 public boolean equals(Object obj) {
136 if (this == obj) {
137 return true;
139 if (obj == null) {
140 return false;
142 if (getClass() != obj.getClass()) {
143 return false;
145 Mismatch other = (Mismatch) obj;
146 if (appEngineWebXmlFile == null) {
147 if (other.appEngineWebXmlFile != null) {
148 return false;
150 } else if (!appEngineWebXmlFile.equals(other.appEngineWebXmlFile)) {
151 return false;
153 if (appEngineWebXmlValue == null) {
154 if (other.appEngineWebXmlValue != null) {
155 return false;
157 } else if (!appEngineWebXmlValue.equals(other.appEngineWebXmlValue)) {
158 return false;
160 if (environmentVariableName == null) {
161 if (other.environmentVariableName != null) {
162 return false;
164 } else if (!environmentVariableName.equals(other.environmentVariableName)) {
165 return false;
167 if (environmentVariableValue == null) {
168 if (other.environmentVariableValue != null) {
169 return false;
171 } else if (!environmentVariableValue.equals(other.environmentVariableValue)) {
172 return false;
174 return true;
177 @Override
178 public String toString() {
179 return "Mismatch environmentVariableName=" + environmentVariableName
180 + " environmentVariableValue=" + environmentVariableValue
181 + " appEngineWebXmlValue=" + appEngineWebXmlValue
182 + " appEngineWebXmlFile=" + appEngineWebXmlFile;
186 @VisibleForTesting
187 static class IncorrectEnvironmentVariableException extends
188 AppEngineConfigException {
190 private final List<Mismatch> mismatches;
191 private IncorrectEnvironmentVariableException(String msg, List<Mismatch> mismatches) {
192 super(msg);
193 this.mismatches = mismatches;
196 public List<Mismatch> getMismatches() {
197 return mismatches;