001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2014 ForgeRock AS. 015 */ 016 017package org.forgerock.openig.servlet; 018 019import java.io.File; 020 021import javax.servlet.ServletConfig; 022 023import org.forgerock.openig.config.Environment; 024import org.forgerock.openig.config.env.DefaultEnvironment; 025import org.forgerock.openig.config.env.EnvironmentDelegate; 026import org.forgerock.openig.config.env.PlatformEnvironment; 027 028/** 029 * Represents an {@link Environment} built from a webapp. 030 * It tries to create an environment from different sources (init-params, process-scoped values or default location). 031 * It goes from the most specific one (servlet's init-params) to the default one (default platform specific location). 032 * 033 * @since 2.2 034 */ 035public class WebEnvironment extends EnvironmentDelegate { 036 037 /** 038 * Servlet's {@literal init-param} name. 039 * <pre> 040 * <servlet> 041 * <servlet-name>GatewayServlet</servlet-name> 042 * <servlet-class>org.forgerock.openig.servlet.GatewayServlet</servlet-class> 043 * <init-param> 044 * <param-name>openig-base</param-name> 045 * <param-value>/my/openig/path</param-value> 046 * </init-param> 047 * </servlet> 048 * </pre> 049 */ 050 public static final String BASE_INIT_PARAM = "openig-base"; 051 052 /** 053 * System property name that can be specified through command line. 054 * <code> 055 * java -Dopenig.base=/my/openig/path .... 056 * </code> 057 */ 058 public static final String BASE_SYSTEM_PROPERTY = "openig.base"; 059 060 /** 061 * Environment variable name. 062 * 063 * Under UNIX: 064 * <code> 065 * export OPENIG_BASE=/my/openig/path 066 * </code> 067 * 068 * Under Windows: 069 * <code> 070 * set OPENIG_BASE=c:\my\openig\path 071 * </code> 072 */ 073 public static final String BASE_ENV_VARIABLE = "OPENIG_BASE"; 074 075 /** 076 * Delegatee. 077 */ 078 private final Environment delegate; 079 080 /** 081 * Builds a new web environment. 082 * @param config webapp's configuration 083 */ 084 public WebEnvironment(final ServletConfig config) { 085 String base = config.getInitParameter(BASE_INIT_PARAM); 086 if (base != null) { 087 delegate = new DefaultEnvironment(new File(base)); 088 return; 089 } 090 base = System.getProperty(BASE_SYSTEM_PROPERTY); 091 if (base != null) { 092 delegate = new DefaultEnvironment(new File(base)); 093 return; 094 } 095 base = System.getenv(BASE_ENV_VARIABLE); 096 if (base != null) { 097 delegate = new DefaultEnvironment(new File(base)); 098 return; 099 } 100 101 delegate = new PlatformEnvironment(); 102 103 } 104 105 @Override 106 protected Environment delegate() { 107 return delegate; 108 } 109 110}