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

aspectran / aspectran / #3985

14 Jan 2025 10:41PM CUT coverage: 35.004% (-0.02%) from 35.021%
#3985

push

github

topframe
Bump commons-daemon:commons-daemon from 1.4.0 to 1.4.1

Bumps commons-daemon:commons-daemon from 1.4.0 to 1.4.1.

---
updated-dependencies:
- dependency-name: commons-daemon:commons-daemon
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

14186 of 40527 relevant lines covered (35.0%)

0.35 hits per line

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

82.61
/core/src/main/java/com/aspectran/core/context/builder/HybridActivityContextBuilder.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.context.builder;
17

18
import com.aspectran.core.adapter.ApplicationAdapter;
19
import com.aspectran.core.component.Component;
20
import com.aspectran.core.component.bean.BeanRuleRegistry;
21
import com.aspectran.core.context.ActivityContext;
22
import com.aspectran.core.context.env.EnvironmentProfiles;
23
import com.aspectran.core.context.rule.assistant.ActivityRuleAssistant;
24
import com.aspectran.core.context.rule.params.AspectranParameters;
25
import com.aspectran.core.context.rule.parser.ActivityContextParser;
26
import com.aspectran.core.context.rule.parser.HybridActivityContextParser;
27
import com.aspectran.core.service.CoreService;
28
import com.aspectran.utils.Assert;
29
import com.aspectran.utils.ShutdownHook;
30
import com.aspectran.utils.StringUtils;
31
import com.aspectran.utils.logging.Logger;
32
import com.aspectran.utils.logging.LoggerFactory;
33

34
import java.util.concurrent.atomic.AtomicBoolean;
35

36
public class HybridActivityContextBuilder extends AbstractActivityContextBuilder {
37

38
    private static final Logger logger = LoggerFactory.getLogger(HybridActivityContextBuilder.class);
1✔
39

40
    /** Synchronization monitor for the "build" and "destroy" */
41
    private final Object buildDestroyMonitor = new Object();
1✔
42

43
    /** Flag that indicates whether an ActivityContext is activated */
44
    private final AtomicBoolean active = new AtomicBoolean();
1✔
45

46
    private ShutdownHook.Manager shutdownHookManager;
47

48
    private ActivityContext activityContext;
49

50
    public HybridActivityContextBuilder() {
51
        this(null);
1✔
52
    }
1✔
53

54
    public HybridActivityContextBuilder(CoreService masterService) {
55
        super(masterService);
1✔
56
    }
1✔
57

58
    @Override
59
    public ActivityContext build(AspectranParameters aspectranParameters) throws ActivityContextBuilderException {
60
        synchronized (this.buildDestroyMonitor) {
×
61
            setAspectranParameters(aspectranParameters);
×
62
            return doBuild();
×
63
        }
64
    }
65

66
    @Override
67
    public ActivityContext build(String... contextRules) throws ActivityContextBuilderException {
68
        synchronized (this.buildDestroyMonitor) {
1✔
69
            setContextRules(contextRules);
1✔
70
            return doBuild();
1✔
71
        }
72
    }
73

74
    @Override
75
    public ActivityContext build() throws ActivityContextBuilderException {
76
        synchronized (this.buildDestroyMonitor) {
1✔
77
            return doBuild();
1✔
78
        }
79
    }
80

81
    private ActivityContext doBuild() throws ActivityContextBuilderException {
82
        try {
83
            Assert.state(!this.active.get(), "ActivityContext is already configured");
1✔
84

85
            String[] contextRules = getContextRules();
1✔
86
            AspectranParameters aspectranParameters = getAspectranParameters();
1✔
87

88
            if (contextRules != null) {
1✔
89
                logger.info("Building ActivityContext with context rules [" +
1✔
90
                        StringUtils.joinCommaDelimitedList(contextRules) + "]");
1✔
91
            } else if (aspectranParameters != null) {
1✔
92
                logger.info("Building ActivityContext with specified parameters");
×
93
            } else if (logger.isDebugEnabled()) {
1✔
94
                logger.debug("No context rules configured");
1✔
95
            }
96

97
            long startTime = System.currentTimeMillis();
1✔
98

99
            String contextName = (getContextConfig() != null ? getContextConfig().getName() : null);
1✔
100
            ClassLoader parentClassLoader = null;
1✔
101
            if (getMasterService() != null && getMasterService().getParentService() != null) {
1✔
102
                CoreService parentService = getMasterService().getParentService();
×
103
                if (parentService != null) {
×
104
                    parentClassLoader = parentService.getActivityContext().getClassLoader();
×
105
                }
106
            }
107

108
            ClassLoader classLoader = createSiblingClassLoader(contextName, parentClassLoader);
1✔
109

110
            ApplicationAdapter applicationAdapter = null;
1✔
111
            if (getMasterService() != null) {
1✔
112
                applicationAdapter = getMasterService().getApplicationAdapter();
×
113
            }
114
            if (applicationAdapter == null) {
1✔
115
                applicationAdapter = createApplicationAdapter();
1✔
116
            }
117

118
            EnvironmentProfiles environmentProfiles = createEnvironmentProfiles();
1✔
119

120
            ActivityRuleAssistant assistant = new ActivityRuleAssistant(
1✔
121
                    classLoader, applicationAdapter, environmentProfiles);
122
            assistant.prepare();
1✔
123

124
            if (getBasePackages() != null) {
1✔
125
                BeanRuleRegistry beanRuleRegistry = assistant.getBeanRuleRegistry();
1✔
126
                beanRuleRegistry.scanConfigurableBeans(getBasePackages());
1✔
127
            }
128

129
            if (contextRules != null || aspectranParameters != null) {
1✔
130
                ActivityContextParser parser = new HybridActivityContextParser(assistant);
1✔
131
                parser.setEncoding(getEncoding());
1✔
132
                parser.setUseXmlToApon(isUseAponToLoadXml());
1✔
133
                parser.setDebugMode(isDebugMode());
1✔
134
                if (contextRules != null) {
1✔
135
                    parser.parse(contextRules);
1✔
136
                } else {
137
                    parser.parse(aspectranParameters);
×
138
                }
139
                assistant = parser.getContextRuleAssistant();
1✔
140
                assistant.clearCurrentRuleAppender();
1✔
141
            }
142

143
            activityContext = createActivityContext(assistant);
1✔
144
            assistant.release();
1✔
145

146
            // When driven by a service
147
            if (getMasterService() != null) {
1✔
148
                // ActivityContext will be initialized in that service
149
                activityContext.setMasterService(getMasterService());
×
150
            } else {
151
                ((Component)activityContext).initialize();
1✔
152
            }
153

154
            long elapsedTime = System.currentTimeMillis() - startTime;
1✔
155

156
            logger.info("ActivityContext build completed in " + elapsedTime + " ms");
1✔
157

158
            if (getMasterService() != null) {
1✔
159
                // Timer starts only if it is driven by a service
160
                startContextReloadingTimer();
×
161
            } else {
162
                // If it is driven by a builder without a service
163
                registerDestroyTask();
1✔
164
            }
165

166
            this.active.set(true);
1✔
167

168
            return activityContext;
1✔
169
        } catch (Exception e) {
1✔
170
            throw new ActivityContextBuilderException("Failed to build ActivityContext", e);
1✔
171
        }
172
    }
173

174
    @Override
175
    public void destroy() {
176
        synchronized (this.buildDestroyMonitor) {
1✔
177
            doDestroy();
1✔
178
            removeDestroyTask();
1✔
179
        }
1✔
180
    }
1✔
181

182
    private void doDestroy() {
183
        if (this.active.get()) {
1✔
184
            stopContextReloadingTimer();
1✔
185
            if (activityContext != null) {
1✔
186
                ((Component)activityContext).destroy();
1✔
187
                activityContext = null;
1✔
188
            }
189
            this.active.set(false);
1✔
190
        }
191
    }
1✔
192

193
    private void registerDestroyTask() {
194
        shutdownHookManager = ShutdownHook.Manager.create(new ShutdownHook.Task() {
1✔
195
            @Override
196
            public void run() throws Exception {
197
                synchronized (buildDestroyMonitor) {
×
198
                    doDestroy();
×
199
                }
×
200
            }
×
201

202
            @Override
203
            public String toString() {
204
                return "Destroy " + HybridActivityContextBuilder.class.getSimpleName();
1✔
205
            }
206
        });
207
    }
1✔
208

209
    private void removeDestroyTask() {
210
        if (shutdownHookManager != null) {
1✔
211
            shutdownHookManager.remove();
1✔
212
            shutdownHookManager = null;
1✔
213
        }
214
    }
1✔
215

216
    @Override
217
    public boolean isActive() {
218
        return this.active.get();
×
219
    }
220

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