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.http;
018
019import java.io.File;
020
021import org.forgerock.openig.config.Environment;
022import org.forgerock.openig.config.env.DefaultEnvironment;
023import org.forgerock.openig.config.env.EnvironmentDelegate;
024import org.forgerock.openig.config.env.PlatformEnvironment;
025
026/**
027 * Represents an {@link Environment} built from a webapp.
028 * It tries to create an environment from different sources (init-params, process-scoped values or default location).
029 * It goes from the most specific one (servlet's init-params) to the default one (default platform specific location).
030 *
031 * @since 2.2
032 */
033public class GatewayEnvironment extends EnvironmentDelegate {
034
035    // @Checkstyle:off
036    /**
037     * Servlet's {@literal init-param} name.
038     * <pre>{@code
039     * <servlet>
040     *   <servlet-name>GatewayServlet</servlet-name>
041     *   <servlet-class>org.forgerock.openig.servlet.GatewayServlet</servlet-class>
042     *   <init-param>
043     *     <param-name>openig-base</param-name>
044     *     <param-value>/my/openig/path</param-value>
045     *   </init-param>
046     * </servlet>
047     * }</pre>
048     */
049    // @Checkstyle:on
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     */
083    public GatewayEnvironment() {
084        String base = System.getProperty(BASE_SYSTEM_PROPERTY);
085        if (base != null) {
086            delegate = new DefaultEnvironment(new File(base));
087            return;
088        }
089        base = System.getenv(BASE_ENV_VARIABLE);
090        if (base != null) {
091            delegate = new DefaultEnvironment(new File(base));
092            return;
093        }
094
095        delegate = new PlatformEnvironment();
096
097    }
098
099    @Override
100    protected Environment delegate() {
101        return delegate;
102    }
103
104}