BrightSide Workbench Full Report + Source Code
ElephantProperties.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.context;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FilenameFilter;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.Properties;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import java.util.stream.Collectors;
30 import org.amic.util.file.FileUtil;
31 
36 public class ElephantProperties {
37 
38  private static Properties _elephantContext;
39  private static final String ELEPHANT_CONTEXT_FOLDER = "/WEB-INF/elephant/conf";
40 
41  public static void resetProperties() {
42  _elephantContext = null;
43  }
44 
45  public static boolean containsContextProperty(String context, String property) {
46  checkProperties();
47  return _elephantContext != null ? _elephantContext.containsKey(context + "_" + property) : false;
48  }
49 
50  public static String getContextProperty(String context, String property) {
51  return getContextProperty(context, property, (String) null);
52  }
53 
54  public static String getContextProperty(String context, String property, String defaultValue) {
55  checkProperties();
56  return _elephantContext != null ? _elephantContext.getProperty(context + "_" + property, defaultValue) : null;
57  }
58 
59  public static Boolean getContextProperty(String context, String property, Boolean defaultValue) {
60  return Boolean.valueOf(getContextProperty(context, property, defaultValue.toString()));
61  }
62 
63  public static List<String> getContextPrefixedProperties(String context, String prefix) {
64  checkProperties();
65  return _elephantContext.keySet().stream()
66  .filter(k -> ((String) k).startsWith(context + "_" + prefix))
67  .map(k -> (String) _elephantContext.get(k))
68  .collect(Collectors.toList());
69  }
70 
74  private static void checkProperties() {
75  if(_elephantContext == null) {
76  _elephantContext = new Properties();
77  File propFiles[] = new File(ElephantContext.getRealPath(ELEPHANT_CONTEXT_FOLDER)).listFiles(new FilenameFilter() {
78  @Override
79  public boolean accept(File dir, String name) {
80  return name.endsWith(".properties") && !name.startsWith("formula-context");
81  }
82  });
83  for(File propFile : propFiles) {
84  String prefix = FileUtil.getBaseName(propFile);
85  try(FileInputStream fis = new FileInputStream(propFile)) {
86  Properties properties = new Properties();
87  properties.load(fis);
88  for(String key : properties.stringPropertyNames()) {
89  _elephantContext.put(prefix + "_" + key, properties.get(key));
90  }
91  } catch (IOException ex) {
92  Logger.getLogger(ElephantContext.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
93  }
94  }
95  }
96  }
97 
98 }
static String getContextProperty(String context, String property, String defaultValue)
static List< String > getContextPrefixedProperties(String context, String prefix)
static boolean containsContextProperty(String context, String property)
static Boolean getContextProperty(String context, String property, Boolean defaultValue)
static String getContextProperty(String context, String property)