001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.spring.env.model; 017 018import java.util.Properties; 019 020import org.kuali.common.util.Assert; 021import org.kuali.common.util.Mode; 022import org.kuali.common.util.spring.env.EnvUtils; 023import org.kuali.common.util.spring.env.PropertiesEnvironment; 024import org.springframework.core.env.Environment; 025 026public final class EnvironmentServiceContext { 027 028 public Environment getEnv() { 029 return env; 030 } 031 032 public boolean isCheckEnvironmentVariables() { 033 return checkEnvironmentVariables; 034 } 035 036 public boolean isResolveStrings() { 037 return resolveStrings; 038 } 039 040 public Mode getMissingPropertyMode() { 041 return missingPropertyMode; 042 } 043 044 private final Environment env; 045 private final boolean checkEnvironmentVariables; 046 private final boolean resolveStrings; 047 private final Mode missingPropertyMode; 048 049 private EnvironmentServiceContext(Builder builder) { 050 this.env = builder.env; 051 this.checkEnvironmentVariables = builder.checkEnvironmentVariables; 052 this.resolveStrings = builder.resolveStrings; 053 this.missingPropertyMode = builder.missingPropertyMode; 054 } 055 056 public static class Builder { 057 058 private Environment env = EnvUtils.getDefaultEnvironment(); 059 private boolean checkEnvironmentVariables = true; 060 private boolean resolveStrings = true; 061 private Mode missingPropertyMode = Mode.ERROR; 062 063 private static final String CHECK_ENVIRONMENT_VARIABLES_KEY = "env.checkEnvironmentVariables"; 064 private static final String RESOLVE_STRINGS_KEY = "env.resolveStrings"; 065 private static final String MISSING_PROPERTY_MODE_KEY = "env.missingPropertyMode"; 066 067 public Builder env(Properties properties) { 068 return env(new PropertiesEnvironment(properties)); 069 } 070 071 public Builder env(Environment env) { 072 this.env = env; 073 return this; 074 } 075 076 public Builder checkEnvironmentVariables(boolean checkEnvironmentVariables) { 077 this.checkEnvironmentVariables = checkEnvironmentVariables; 078 return this; 079 } 080 081 public Builder resolveStrings(boolean resolveStrings) { 082 this.resolveStrings = resolveStrings; 083 return this; 084 } 085 086 public Builder missingPropertyMode(Mode missingPropertyMode) { 087 this.missingPropertyMode = missingPropertyMode; 088 return this; 089 } 090 091 private void override() { 092 Assert.noNulls(env); 093 checkEnvironmentVariables(env.getProperty(CHECK_ENVIRONMENT_VARIABLES_KEY, Boolean.class, checkEnvironmentVariables)); 094 resolveStrings(env.getProperty(RESOLVE_STRINGS_KEY, Boolean.class, resolveStrings)); 095 missingPropertyMode(env.getProperty(MISSING_PROPERTY_MODE_KEY, Mode.class, missingPropertyMode)); 096 } 097 098 protected void validate(EnvironmentServiceContext ctx) { 099 Assert.notNull(ctx.getEnv(), "'env' cannot be null"); 100 Assert.notNull(ctx.getMissingPropertyMode(), "'missingPropertyMode' cannot be null"); 101 } 102 103 protected EnvironmentServiceContext getInstance() { 104 return new EnvironmentServiceContext(this); 105 } 106 107 public EnvironmentServiceContext build() { 108 override(); 109 EnvironmentServiceContext ctx = new EnvironmentServiceContext(this); 110 validate(ctx); 111 return ctx; 112 } 113 114 } 115 116}