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

aspectran / aspectran / #4075

19 Feb 2025 12:58PM CUT coverage: 35.181% (-0.004%) from 35.185%
#4075

push

github

topframe
Update

20 of 155 new or added lines in 9 files covered. (12.9%)

5 existing lines in 3 files now uncovered.

14252 of 40510 relevant lines covered (35.18%)

0.35 hits per line

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

0.0
/core/src/main/java/com/aspectran/core/support/PropertiesFactoryBean.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
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, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.core.support;
17

18
import com.aspectran.core.component.bean.ablility.InitializableFactoryBean;
19
import com.aspectran.core.component.bean.annotation.AvoidAdvice;
20
import com.aspectran.core.component.bean.aware.ActivityContextAware;
21
import com.aspectran.core.context.ActivityContext;
22
import com.aspectran.core.context.asel.token.Token;
23
import com.aspectran.core.context.asel.token.TokenEvaluator;
24
import com.aspectran.core.context.asel.token.TokenParser;
25
import com.aspectran.core.context.rule.type.TokenType;
26
import com.aspectran.utils.Assert;
27
import com.aspectran.utils.PropertiesLoaderSupport;
28
import com.aspectran.utils.ResourceUtils;
29

30
import java.io.FileInputStream;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.util.Properties;
34
import java.util.Set;
35

36
import static com.aspectran.utils.ResourceUtils.CLASSPATH_URL_PREFIX;
37

38
/**
39
 * Allows for making a properties file from a classpath location available
40
 * as Properties instance in a bean factory. Can be used to populate
41
 * any bean property of type Properties via a bean reference.
42
 * Supports loading from a properties file and/or setting local properties
43
 * on this FactoryBean. The created Properties instance will be merged from
44
 * loaded and local values.
45
 *
46
 * <p>Created: 2025. 2. 18.</p>
47
 */
48
@AvoidAdvice
49
public class PropertiesFactoryBean extends PropertiesLoaderSupport
×
50
        implements ActivityContextAware, InitializableFactoryBean<Properties> {
51

52
    private ActivityContext context;
53

54
    private Properties properties;
55

56
    @Override
57
    public void setActivityContext(ActivityContext context) {
58
        this.context = context;
×
59
    }
×
60

61
    @Override
62
    protected InputStream getResourceAsStream(String location) throws IOException {
63
        Assert.notNull(location, "location must not be null");
×
64
        InputStream is;
65
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
×
66
            is = ResourceUtils.getResourceAsStream(location.substring(CLASSPATH_URL_PREFIX.length()));
×
67
        } else {
68
            Assert.state(context != null, "No ActivityContext injected");
×
69
            is = new FileInputStream(context.getApplicationAdapter().getRealPath(location).toFile());
×
70
        }
71
        return is;
×
72
    }
73

74
    @Override
75
    public void initialize() throws Exception {
76
        if (properties == null) {
×
77
            Properties properties = mergeProperties();
×
78
            if (context != null) {
×
79
                TokenEvaluator evaluator = context.getAvailableActivity().getTokenEvaluator();
×
80
                Set<String> propertyNames = properties.stringPropertyNames();
×
81
                for (String name : propertyNames) {
×
82
                    String value = properties.getProperty(name);
×
83
                    if (value != null && Token.hasToken(value)) {
×
84
                        Token[] tokens = TokenParser.parse(value);
×
85
                        for (int i = 0; i < tokens.length; i++) {
×
86
                            Token token = tokens[i];
×
87
                            if (token.getType() == TokenType.PROPERTY &&
×
88
                                    token.getGetterName() == null &&
×
89
                                    token.getDirectiveType() == null &&
×
NEW
90
                                    token.getValueProvider() == null) {
×
91
                                String tokenName = token.getName();
×
92
                                String defaultValue = token.getDefaultValue();
×
93
                                if (tokenName != null && !tokenName.equals(name) && propertyNames.contains(tokenName)) {
×
94
                                    tokens[i] = new Token(properties.getProperty(tokenName, defaultValue));
×
95
                                }
96
                            }
97
                        }
98
                        String evaluated = (String)evaluator.evaluate(tokens);
×
99
                        if (!value.equals(evaluated)) {
×
100
                            properties.setProperty(name, evaluated);
×
101
                        }
102
                    }
103
                }
×
104
            }
105
            this.properties = properties;
×
106
        }
107
    }
×
108

109
    @Override
110
    public Properties getObject() throws Exception {
111
        return properties;
×
112
    }
113

114
}
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