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
;
11 import java
.util
.logging
.Logger
;
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()}.
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
24 class EnvironmentVariableChecker
{
26 static final Logger LOGGER
= Logger
.getLogger(EnvironmentVariableChecker
.class.getName());
29 * Enum for reporting policy.
31 static enum MismatchReportingPolicy
{
32 /** Report mismatches by logging. */
34 /** Report mismatches by throwing an {@link AppEngineConfigException} */
36 /** Don't report mismatches. */
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()) {
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"
67 if (mismatchReportingPolicy
== MismatchReportingPolicy
.LOG
) {
69 } else if (mismatchReportingPolicy
== MismatchReportingPolicy
.EXCEPTION
) {
70 throw new IncorrectEnvironmentVariableException(msg
, mismatches
);
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
;
92 * @return the environmentVariableName
94 public String
getEnvironmentVariableName() {
95 return environmentVariableName
;
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
;
120 public int hashCode() {
121 final int prime
= 31;
124 prime
* result
+ ((appEngineWebXmlFile
== null) ?
0 : appEngineWebXmlFile
.hashCode());
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());
135 public boolean equals(Object obj
) {
142 if (getClass() != obj
.getClass()) {
145 Mismatch other
= (Mismatch
) obj
;
146 if (appEngineWebXmlFile
== null) {
147 if (other
.appEngineWebXmlFile
!= null) {
150 } else if (!appEngineWebXmlFile
.equals(other
.appEngineWebXmlFile
)) {
153 if (appEngineWebXmlValue
== null) {
154 if (other
.appEngineWebXmlValue
!= null) {
157 } else if (!appEngineWebXmlValue
.equals(other
.appEngineWebXmlValue
)) {
160 if (environmentVariableName
== null) {
161 if (other
.environmentVariableName
!= null) {
164 } else if (!environmentVariableName
.equals(other
.environmentVariableName
)) {
167 if (environmentVariableValue
== null) {
168 if (other
.environmentVariableValue
!= null) {
171 } else if (!environmentVariableValue
.equals(other
.environmentVariableValue
)) {
178 public String
toString() {
179 return "Mismatch environmentVariableName=" + environmentVariableName
180 + " environmentVariableValue=" + environmentVariableValue
181 + " appEngineWebXmlValue=" + appEngineWebXmlValue
182 + " appEngineWebXmlFile=" + appEngineWebXmlFile
;
187 static class IncorrectEnvironmentVariableException
extends
188 AppEngineConfigException
{
190 private final List
<Mismatch
> mismatches
;
191 private IncorrectEnvironmentVariableException(String msg
, List
<Mismatch
> mismatches
) {
193 this.mismatches
= mismatches
;
196 public List
<Mismatch
> getMismatches() {