• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / redis-cache / 577

04 Jun 2025 04:02PM CUT coverage: 83.163%. Remained the same
577

push

github

web-flow
Merge pull request #307 from mybatis/renovate/shogo82148-actions-setup-redis-1.x

Update shogo82148/actions-setup-redis action to v1.41.0

51 of 80 branches covered (63.75%)

163 of 196 relevant lines covered (83.16%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.59
/src/main/java/org/mybatis/caches/redis/RedisConfigurationBuilder.java
1
/*
2
 *    Copyright 2015-2023 the original author or authors.
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
 *       https://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, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.caches.redis;
17

18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.util.Arrays;
21
import java.util.Map;
22
import java.util.Properties;
23

24
import org.apache.ibatis.cache.CacheException;
25
import org.apache.ibatis.io.Resources;
26
import org.apache.ibatis.reflection.MetaObject;
27
import org.apache.ibatis.reflection.SystemMetaObject;
28

29
/**
30
 * Converter from the Config to a proper {@link RedisConfig}.
31
 *
32
 * @author Eduardo Macarron
33
 */
34
final class RedisConfigurationBuilder {
35

36
  /**
37
   * This class instance.
38
   */
39
  private static final RedisConfigurationBuilder INSTANCE = new RedisConfigurationBuilder();
1✔
40

41
  protected static final String SYSTEM_PROPERTY_REDIS_PROPERTIES_FILENAME = "redis.properties.filename";
42

43
  protected static final String REDIS_RESOURCE = "redis.properties";
44

45
  /**
46
   * Hidden constructor, this class can't be instantiated.
47
   */
48
  private RedisConfigurationBuilder() {
49
  }
50

51
  /**
52
   * Return this class instance.
53
   *
54
   * @return this class instance.
55
   */
56
  public static RedisConfigurationBuilder getInstance() {
57
    return INSTANCE;
1✔
58
  }
59

60
  /**
61
   * Parses the Config and builds a new {@link RedisConfig}.
62
   *
63
   * @return the converted {@link RedisConfig}.
64
   */
65
  public RedisConfig parseConfiguration() {
66
    return parseConfiguration(getClass().getClassLoader());
1✔
67
  }
68

69
  /**
70
   * Parses the Config and builds a new {@link RedisConfig}.
71
   *
72
   * @param the
73
   *          {@link ClassLoader} used to load the {@code memcached.properties} file in classpath.
74
   *
75
   * @return the converted {@link RedisConfig}.
76
   */
77
  public RedisConfig parseConfiguration(ClassLoader classLoader) {
78
    Properties config = new Properties();
1✔
79

80
    String redisPropertiesFilename = System.getProperty(SYSTEM_PROPERTY_REDIS_PROPERTIES_FILENAME, REDIS_RESOURCE);
1✔
81

82
    try (InputStream input = classLoader.getResourceAsStream(redisPropertiesFilename)) {
1✔
83
      if (input != null) {
1✔
84
        config.load(input);
1✔
85
      }
86
    } catch (IOException e) {
×
87
      throw new RuntimeException(
×
88
          "An error occurred while reading classpath property '" + redisPropertiesFilename + "', see nested exceptions",
89
          e);
90
    }
1✔
91

92
    RedisConfig jedisConfig = new RedisConfig();
1✔
93
    setConfigProperties(config, jedisConfig);
1✔
94
    return jedisConfig;
1✔
95
  }
96

97
  private void setConfigProperties(Properties properties, RedisConfig jedisConfig) {
98
    if (properties != null) {
1!
99
      MetaObject metaCache = SystemMetaObject.forObject(jedisConfig);
1✔
100
      for (Map.Entry<Object, Object> entry : properties.entrySet()) {
1✔
101
        String name = (String) entry.getKey();
1✔
102
        // All prefix of 'redis.' on property values
103
        if (name != null && name.startsWith("redis.")) {
1!
104
          name = name.substring(6);
1✔
105
        } else {
106
          // Skip non prefixed properties
107
          continue;
108
        }
109
        String value = (String) entry.getValue();
1✔
110
        if ("serializer".equals(name)) {
1✔
111
          if ("kryo".equalsIgnoreCase(value)) {
1✔
112
            jedisConfig.setSerializer(KryoSerializer.INSTANCE);
1✔
113
          } else if (!"jdk".equalsIgnoreCase(value)) {
1!
114
            // Custom serializer is not supported yet.
115
            throw new CacheException("Unknown serializer: '" + value + "'");
×
116
          }
117
        } else if (Arrays.asList("sslSocketFactory", "sslParameters", "hostnameVerifier").contains(name)) {
1✔
118
          setInstance(metaCache, name, value);
1✔
119
        } else if (metaCache.hasSetter(name)) {
1!
120
          Class<?> type = metaCache.getSetterType(name);
1✔
121
          if (String.class == type) {
1✔
122
            metaCache.setValue(name, value);
1✔
123
          } else if (int.class == type || Integer.class == type) {
1!
124
            metaCache.setValue(name, Integer.valueOf(value));
1✔
125
          } else if (long.class == type || Long.class == type) {
1!
126
            metaCache.setValue(name, Long.valueOf(value));
×
127
          } else if (short.class == type || Short.class == type) {
1!
128
            metaCache.setValue(name, Short.valueOf(value));
×
129
          } else if (byte.class == type || Byte.class == type) {
1!
130
            metaCache.setValue(name, Byte.valueOf(value));
×
131
          } else if (float.class == type || Float.class == type) {
1!
132
            metaCache.setValue(name, Float.valueOf(value));
×
133
          } else if (boolean.class == type || Boolean.class == type) {
1!
134
            metaCache.setValue(name, Boolean.valueOf(value));
1✔
135
          } else if (double.class == type || Double.class == type) {
×
136
            metaCache.setValue(name, Double.valueOf(value));
×
137
          } else {
138
            throw new CacheException("Unsupported property type: '" + name + "' of type " + type);
×
139
          }
140
        }
141
      }
1✔
142
    }
143
  }
1✔
144

145
  protected void setInstance(MetaObject metaCache, String name, String value) {
146
    if (value == null || value.isEmpty()) {
1!
147
      return;
×
148
    }
149
    Object instance;
150
    try {
151
      Class<?> clazz = Resources.classForName(value);
1✔
152
      instance = clazz.getDeclaredConstructor().newInstance();
1✔
153
    } catch (Exception e) {
×
154
      throw new CacheException("Could not instantiate class: '" + value + "'.", e);
×
155
    }
1✔
156
    metaCache.setValue(name, instance);
1✔
157
  }
1✔
158

159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc