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

aspectran / aspectran / #3971

08 Jan 2025 12:17PM CUT coverage: 35.017% (-0.009%) from 35.026%
#3971

push

github

topframe
Update

8 of 27 new or added lines in 6 files covered. (29.63%)

7 existing lines in 5 files now uncovered.

14188 of 40517 relevant lines covered (35.02%)

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/service/DefaultCoreService.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.service;
17

18
import com.aspectran.core.component.Component;
19
import com.aspectran.core.context.ActivityContext;
20
import com.aspectran.core.context.builder.ActivityContextBuilder;
21
import com.aspectran.core.context.builder.ActivityContextBuilderException;
22
import com.aspectran.core.context.builder.HybridActivityContextBuilder;
23
import com.aspectran.core.context.config.AspectranConfig;
24
import com.aspectran.core.context.config.ContextConfig;
25
import com.aspectran.core.context.config.SystemConfig;
26
import com.aspectran.utils.Assert;
27
import com.aspectran.utils.FileLocker;
28
import com.aspectran.utils.InsufficientEnvironmentException;
29
import com.aspectran.utils.ShutdownHook;
30
import com.aspectran.utils.annotation.jsr305.NonNull;
31
import com.aspectran.utils.logging.Logger;
32
import com.aspectran.utils.logging.LoggerFactory;
33

34
/**
35
 * The Class DefaultCoreService.
36
 */
37
public class DefaultCoreService extends AbstractCoreService {
38

39
    private final Logger logger = LoggerFactory.getLogger(DefaultCoreService.class);
×
40

41
    private FileLocker fileLocker;
42

43
    private ShutdownHook.Manager shutdownHookManager;
44

45
    /**
46
     * Instantiates a new DefaultCoreService.
47
     */
48
    public DefaultCoreService() {
49
        super();
×
50
    }
×
51

52
    public DefaultCoreService(CoreService parentService, boolean derived) {
53
        super(parentService, derived);
×
54
    }
×
55

56
    protected void configure(@NonNull AspectranConfig aspectranConfig) {
57
        Assert.state(!isDerived(),
×
58
            "Must not be called for derived services");
59
        Assert.state(!hasActivityContextBuilder(),
×
60
            "prepare() method can be called only once");
61

62
        try {
63
            setAspectranConfig(aspectranConfig);
×
64

65
            SystemConfig systemConfig = aspectranConfig.getSystemConfig();
×
66
            if (systemConfig != null) {
×
67
                for (String key : systemConfig.getPropertyKeys()) {
×
68
                    String value = systemConfig.getProperty(key);
×
69
                    if (value != null) {
×
70
                        System.setProperty(key, value);
×
71
                    }
72
                }
73
            }
74

75
            ContextConfig contextConfig = aspectranConfig.getContextConfig();
×
76
            if (isRootService()) {
×
77
                if (getBasePath() == null && contextConfig != null && contextConfig.hasBasePath()) {
×
78
                    setBasePath(contextConfig.getBasePath());
×
79
                }
80
            } else {
81
                setBasePath(getParentService().getBasePath());
×
82
            }
83

84
            ActivityContextBuilder activityContextBuilder = new HybridActivityContextBuilder(this);
×
85
            activityContextBuilder.configure(contextConfig);
×
86
            setActivityContextBuilder(activityContextBuilder);
×
87
            if (getBasePath() == null) {
×
88
                setBasePath(activityContextBuilder.getBasePath());
×
89
            }
90

91
            if (isRootService() && contextConfig != null && contextConfig.isSingleton()) {
×
92
                if (activityContextBuilder.hasOwnBasePath()) {
×
93
                    acquireSingletonLock();
×
94
                } else {
95
                    logger.warn("Since no base directory is explicitly specified, no singleton lock is applied");
×
96
                }
97
            }
98
        } catch (Exception e) {
×
99
            throw new CoreServiceException("Unable to prepare the service", e);
×
100
        }
×
101
    }
×
102

103
    protected void buildActivityContext() throws ActivityContextBuilderException {
104
        Assert.state(getActivityContext() == null,
×
105
            "ActivityContext is already built; " +
106
                "Must destroy the current ActivityContext before reloading");
107
        ActivityContext activityContext = getActivityContextBuilder().build();
×
108
        setActivityContext(activityContext);
×
109
        try {
110
            ((Component)activityContext).initialize();
×
111
        } catch (Exception e) {
×
NEW
112
            throw new ActivityContextBuilderException("Failed to initialize " +
×
NEW
113
                    ((Component)activityContext).getComponentName(), e);
×
114
        }
×
115
    }
×
116

117
    protected void destroyActivityContext() {
118
        if (logger.isDebugEnabled()) {
×
119
            logger.debug("Destroying all cached resources...");
×
120
        }
121
        getActivityContextBuilder().destroy();
×
122
        setActivityContext(null);
×
123
    }
×
124

125
    /**
126
     * This method is executed immediately after the ActivityContext is loaded.
127
     * @throws Exception if an error occurs
128
     */
129
    protected void afterContextLoaded() throws Exception {
130
    }
×
131

132
    /**
133
     * This method executed just before the ActivityContext is destroyed.
134
     */
135
    protected void beforeContextDestroy() {
136
    }
×
137

138
    @Override
139
    protected void doStart() throws Exception {
140
        if (!isDerived()) {
×
141
            buildActivityContext();
×
142
            buildSchedulerService();
×
143
            afterContextLoaded();
×
144
        }
145
    }
×
146

147
    @Override
148
    protected void doStop() {
149
        if (!isDerived()) {
×
150
            clearDerivedServices();
×
151
            beforeContextDestroy();
×
152
            destroyActivityContext();
×
153
        }
154
    }
×
155

156
    @Override
157
    public void start() throws Exception {
158
        if (isRootService()) {
×
159
            registerShutdownTask();
×
160
        }
161
        super.start();
×
162
    }
×
163

164
    @Override
165
    public void stop() {
166
        super.stop();
×
167
        if (isRootService()) {
×
168
            releaseSingletonLock();
×
169
            removeShutdownTask();
×
170
            getActivityContextBuilder().clear();
×
171
        }
172
    }
×
173

174
    private void acquireSingletonLock() throws Exception {
175
        Assert.state(fileLocker == null, "Singleton lock is already configured");
×
176
        fileLocker = new FileLocker(getBasePath());
×
177
        if (!fileLocker.lock()) {
×
178
            throw new InsufficientEnvironmentException("Another instance of Aspectran is already " +
×
179
                "running; Only one instance is allowed (context.singleton is set to true)");
180
        }
181
    }
×
182

183
    private void releaseSingletonLock() {
184
        if (fileLocker != null) {
×
185
            try {
186
                fileLocker.release();
×
187
                fileLocker = null;
×
188
            } catch (Exception e) {
×
189
                logger.warn("Unable to release singleton lock: " + e);
×
190
            }
×
191
        }
192
    }
×
193

194
    /**
195
     * Registers a shutdown hook with the JVM runtime, closing this context
196
     * on JVM shutdown unless it has already been closed at that time.
197
     */
198
    private void registerShutdownTask() {
199
        shutdownHookManager = ShutdownHook.Manager.create(new ShutdownHook.Task() {
×
200
            @Override
201
            public void run() throws Exception {
202
                if (isActive()) {
×
203
                    DefaultCoreService.super.stop();
×
204
                    releaseSingletonLock();
×
205
                }
206
            }
×
207

208
            @Override
209
            public String toString() {
210
                return "Stop " + getServiceName();
×
211
            }
212
        });
213
    }
×
214

215
    /**
216
     * De-registers a shutdown hook with the JVM runtime.
217
     */
218
    private void removeShutdownTask() {
219
        if (shutdownHookManager != null) {
×
220
            shutdownHookManager.remove();
×
221
            shutdownHookManager = null;
×
222
        }
223
    }
×
224

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