View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.mylasta.direction;
17  
18  import java.util.concurrent.ExecutionException;
19  
20  import org.codelibs.fess.Constants;
21  import org.dbflute.helper.jprop.ObjectiveProperties;
22  import org.lastaflute.core.direction.PropertyFilter;
23  
24  import com.google.common.cache.Cache;
25  import com.google.common.cache.CacheBuilder;
26  
27  public class FessConfigImpl extends FessConfig.SimpleImpl {
28  
29      private static final long serialVersionUID = 1L;
30  
31      private static class KeyNotFoundException extends Exception {
32          private static final long serialVersionUID = 1L;
33  
34          private KeyNotFoundException(final String key) {
35              super(key);
36          }
37      }
38  
39      @Override
40      protected ObjectiveProperties newObjectiveProperties(final String resourcePath, final PropertyFilter propertyFilter) {
41          return new ObjectiveProperties(resourcePath) { // for e.g. checking existence and filtering value
42              Cache<String, String> cache = CacheBuilder.newBuilder().build();
43  
44              @Override
45              public String get(final String propertyKey) {
46                  final String plainValue = getFromCache(propertyKey);
47                  final String filteredValue = propertyFilter.filter(propertyKey, plainValue);
48                  verifyPropertyValue(propertyKey, filteredValue); // null checked
49                  return filterPropertyAsDefault(filteredValue); // not null here
50              }
51  
52              private String getFromCache(final String propertyKey) {
53                  try {
54                      return cache.get(propertyKey, () -> {
55                          final String value = System.getProperty(Constants.FESS_CONFIG_PREFIX + propertyKey, super.get(propertyKey));
56                          if (value == null) {
57                              throw new KeyNotFoundException(propertyKey);
58                          }
59                          return value;
60                      });
61                  } catch (final ExecutionException e) {
62                      if (e.getCause() instanceof KeyNotFoundException) {
63                          return null;
64                      }
65                      return super.get(propertyKey);
66                  }
67              }
68          };
69      }
70  }