View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.catalog.api.util;
18  
19  import javax.servlet.ServletContext;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.Properties;
23  
24  /**
25   * Sets the profile for the application if it hasn't already been set.
26   */
27  public final class ProfileUtil {
28  
29      private static final String SPRING_PROFILE = "spring.profiles.active";
30  
31      private ProfileUtil() {
32      }
33  
34      /**
35       * Set the active profile if none has been specified.
36       * @param servletContext
37       */
38      public static void setActiveProfile(ServletContext servletContext) {
39          String springProfile = System.getProperty(SPRING_PROFILE);
40          if (springProfile == null) {
41              springProfile = System.getenv(SPRING_PROFILE);
42          }
43          if (springProfile == null) {
44              springProfile = servletContext.getInitParameter(SPRING_PROFILE);
45          }
46          if (springProfile == null) {
47              Properties props = loadProperties(servletContext);
48              String activeProfile = props.getProperty(SPRING_PROFILE);
49              if (activeProfile == null) {
50                  servletContext.setInitParameter(SPRING_PROFILE, "eclipseLink");
51              }
52          }
53      }
54  
55      private static Properties loadProperties(ServletContext servletContext) {
56          Properties props = new Properties();
57          String env = System.getProperty("env");
58          if (env == null) {
59              env = System.getenv("env");
60          }
61          StringBuilder sb = new StringBuilder("catalog-");
62          if (env != null) {
63              sb.append(env);
64          }
65          sb.append("config.properties");
66          InputStream is = ProfileUtil.class.getClassLoader().getResourceAsStream(sb.toString());
67          if (is != null) {
68              try {
69                  props.load(is);
70              } catch (IOException ioe) {
71                  servletContext.log("Unable to load " + sb.toString() + ": " + ioe.getMessage());
72              }
73          }
74          return props;
75      }
76  }