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

aspectran / aspectran / #4059

13 Feb 2025 08:38AM CUT coverage: 35.283% (+0.002%) from 35.281%
#4059

push

github

topframe
Update

0 of 17 new or added lines in 2 files covered. (0.0%)

2 existing lines in 2 files now uncovered.

14258 of 40410 relevant lines covered (35.28%)

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/CoreServiceHolder.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.context.ActivityContext;
19
import com.aspectran.core.context.resource.SiblingClassLoader;
20
import com.aspectran.utils.Assert;
21
import com.aspectran.utils.annotation.jsr305.Nullable;
22

23
import java.util.HashMap;
24
import java.util.Map;
25
import java.util.Set;
26
import java.util.concurrent.CopyOnWriteArraySet;
27

28
/**
29
 * <p>Created: 01/10/2019</p>
30
 */
31
public abstract class CoreServiceHolder {
×
32

NEW
33
    private static final Set<ServiceHoldingListener> serviceHoldingListeners = new CopyOnWriteArraySet<>();
×
34

UNCOV
35
    private static final Set<CoreService> allServices = new CopyOnWriteArraySet<>();
×
36

37
    private static final Map<ClassLoader, CoreService> servicesByLoader = new HashMap<>();
×
38

39
    private static final Map<Class<?>, CoreService> servicesByClass = new HashMap<>();
×
40

41
    private static volatile CoreService currentService;
42

43
    public static void addServiceHolingListener(ServiceHoldingListener listener) {
NEW
44
        Assert.notNull(listener, "listener must not be null");
×
NEW
45
        serviceHoldingListeners.add(listener);
×
NEW
46
    }
×
47

48
    public static void removeServiceHolingListener(ServiceHoldingListener listener) {
NEW
49
        Assert.notNull(listener, "listener must not be null");
×
NEW
50
        serviceHoldingListeners.remove(listener);
×
NEW
51
    }
×
52

53
    public static synchronized void hold(CoreService service) {
54
        Assert.notNull(service, "service must not be null");
×
55
        Assert.state(service.getActivityContext() != null, "No ActivityContext in service: " + service);
×
56
        Assert.state(!allServices.contains(service), "Already registered service: " + service);
×
57
        ClassLoader classLoader = service.getServiceClassLoader();
×
58
        if (classLoader != null) {
×
59
            allServices.add(service);
×
60
            if (classLoader == CoreServiceHolder.class.getClassLoader()) {
×
61
                currentService = service;
×
62
            } else {
63
                servicesByLoader.put(classLoader, service);
×
64
            }
65
            if (service.getAltClassLoader() != null) {
×
66
                hold(service.getAltClassLoader(), service);
×
67
            }
NEW
68
            for (ServiceHoldingListener listener : serviceHoldingListeners) {
×
NEW
69
                listener.afterServiceHolding(service);
×
NEW
70
            }
×
71
        }
72
    }
×
73

74
    public static synchronized void hold(ClassLoader classLoader, CoreService service) {
75
        Assert.notNull(classLoader, "classLoader must not be null");
×
76
        Assert.notNull(service, "service must not be null");
×
77
        Assert.state(allServices.contains(service), "Not a registered service: " + service);
×
78
        CoreService existing = servicesByLoader.get(classLoader);
×
79
        Assert.state(existing != service, "The classloader is already mapped to another service: " + service);
×
80
        servicesByLoader.put(classLoader, service);
×
81
    }
×
82

83
    public static synchronized void hold(Class<?> clazz, CoreService service) {
84
        Assert.notNull(clazz, "clazz must not be null");
×
85
        Assert.notNull(service, "service must not be null");
×
86
        Assert.state(allServices.contains(service), "Not a registered service: " + service);
×
87
        CoreService existing = servicesByClass.get(clazz);
×
88
        Assert.state(existing != service, "The class is already mapped to another service: " + service);
×
89
        servicesByClass.put(clazz, service);
×
90
    }
×
91

92
    public static synchronized void release(CoreService service) {
93
        Assert.notNull(service, "service must not be null");
×
94
        Assert.state(allServices.contains(service), "Not a registered service: " + service);
×
NEW
95
        for (ServiceHoldingListener listener : serviceHoldingListeners) {
×
NEW
96
            listener.beforeServiceRelease(service);
×
NEW
97
        }
×
98
        allServices.remove(service);
×
NEW
99
        if (allServices.isEmpty()) {
×
NEW
100
            serviceHoldingListeners.clear();
×
101
        }
102
        if (currentService != null && currentService == service) {
×
103
            currentService = null;
×
104
        }
105
        servicesByLoader.entrySet().removeIf(entry -> service.equals(entry.getValue()));
×
106
        servicesByClass.entrySet().removeIf(entry -> service.equals(entry.getValue()));
×
107
    }
×
108

109
    public static CoreService acquire() {
110
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
×
111
        if (classLoader != null) {
×
112
            CoreService service = servicesByLoader.get(classLoader);
×
113
            if (service == null && !(classLoader instanceof SiblingClassLoader)) {
×
114
                service = servicesByLoader.get(classLoader.getParent());
×
115
            }
116
            if (service != null) {
×
117
                return service;
×
118
            }
119
        }
120
        return currentService;
×
121
    }
122

123
    public static CoreService acquire(Class<?> clazz) {
124
        CoreService service = servicesByClass.get(clazz);
×
125
        if (service == null) {
×
126
            service = acquire();
×
127
        }
128
        return service;
×
129
    }
130

131
    @Nullable
132
    public static ActivityContext findActivityContext() {
133
        CoreService service = acquire();
×
134
        return (service != null ? service.getActivityContext() : null);
×
135
    }
136

137
    @Nullable
138
    public static ActivityContext findActivityContext(Class<?> clazz) {
139
        CoreService service = acquire(clazz);
×
140
        return (service != null ? service.getActivityContext() : null);
×
141
    }
142

143
    @Nullable
144
    public static ActivityContext findActivityContext(String contextName) {
145
        Assert.notNull(contextName, "contextName must not be null");
×
146
        for (CoreService service : allServices) {
×
147
            ActivityContext activityContext = service.getActivityContext();
×
148
            if (activityContext != null && contextName.equals(activityContext.getName())) {
×
149
                return activityContext;
×
150
            }
151
        }
×
152
        return null;
×
153
    }
154

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