Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

OpenWiseSolutions / openhub-framework / 1213

7 May 2021 - 10:18 coverage increased (+0.2%) to 70.878%
1213

Pull #133

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Merge 5a3f2074f into 382999a38
Pull Request #133: Bump handlebars from 4.7.6 to 4.7.7 in /admin-console

4342 of 6126 relevant lines covered (70.88%)

0.71 hits per line

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

61.7
/core/src/main/java/org/openhubframework/openhub/core/common/camel/ApplicationContextsRegistry.java
1
/*
2
 * Copyright 2014 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
 *      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

17
package org.openhubframework.openhub.core.common.camel;
18

19
import java.util.Collections;
20
import java.util.LinkedHashMap;
21
import java.util.Map;
22
import java.util.Set;
23

24
import org.apache.camel.impl.DefaultCamelContext;
25
import org.apache.camel.spi.Registry;
26
import org.apache.camel.spring.spi.ApplicationContextRegistry;
27
import org.apache.commons.collections4.CollectionUtils;
28
import org.apache.commons.collections4.MapUtils;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31
import org.springframework.context.ApplicationContext;
32
import org.springframework.context.ApplicationListener;
33
import org.springframework.context.event.ApplicationContextEvent;
34
import org.springframework.context.event.ContextRefreshedEvent;
35
import org.springframework.context.event.ContextStartedEvent;
36
import org.springframework.core.Ordered;
37
import org.springframework.util.Assert;
38

39
/**
40
 * Implementation {@link Registry} that load beans from all {@link ApplicationContext}s.
41
 * For every {@link ApplicationContext} is created own {@link ApplicationContextRegistry}.
42
 *
43
 * @author Roman Havlicek
44
 * @see Registry
45
 * @see DefaultCamelContext#setRegistry(Registry)
46
 * @since 2.0
47
 */
48
public class ApplicationContextsRegistry implements Registry, ApplicationListener<ApplicationContextEvent>, Ordered {
1×
49

50
    private static final Logger LOG = LoggerFactory.getLogger(ApplicationContextsRegistry.class);
1×
51

52
    /**
53
     * Order of listener for application events.
54
     * It is important to receive event ContextRefreshedEvent before
55
     * {@link org.apache.camel.spring.boot.RoutesCollector} does.
56
     */
57
    public static final int ORDER = LOWEST_PRECEDENCE - 20;
1×
58

59
    /**
60
     * List of registers for all application contexts.
61
     */
62
    private final Map<ApplicationContext, Registry> applicationContextsRegistry = new LinkedHashMap<>();
1×
63

64
    @Override
65
    public Object lookupByName(String name) {
66
        for (Registry registry : applicationContextsRegistry.values()) {
1×
67
            Object result = registry.lookupByName(name);
1×
68
            if (result != null) {
1×
69
                return result;
1×
70
            }
71
        }
72
        return null;
1×
73
    }
74

75
    @Override
76
    public <T> T lookupByNameAndType(String name, Class<T> type) {
77
        for (Registry registry : applicationContextsRegistry.values()) {
1×
78
            T result = registry.lookupByNameAndType(name, type);
1×
79
            if (result != null) {
1×
80
                return result;
!
81
            }
82
        }
83
        return null;
1×
84
    }
85

86
    @Override
87
    public <T> Map<String, T> findByTypeWithName(Class<T> type) {
88
        for (Registry registry : applicationContextsRegistry.values()) {
1×
89
            Map<String, T> result = registry.findByTypeWithName(type);
1×
90
            if (!MapUtils.isEmpty(result)) {
1×
91
                return result;
!
92
            }
93
        }
94
        return Collections.emptyMap();
1×
95
    }
96

97
    @Override
98
    public <T> Set<T> findByType(Class<T> type) {
99
        for (Registry registry : applicationContextsRegistry.values()) {
1×
100
            Set<T> result = registry.findByType(type);
1×
101
            if (!CollectionUtils.isEmpty(result)) {
1×
102
                return result;
1×
103
            }
104
        }
UNCOV
105
        return Collections.emptySet();
!
106
    }
107

108
    @Override
109
    public Object lookup(String name) {
110
        for (Registry registry : applicationContextsRegistry.values()) {
!
111
            Object result = registry.lookup(name);
!
112
            if (result != null) {
!
113
                return result;
!
114
            }
115
        }
116
        return null;
!
117
    }
118

119
    @Override
120
    public <T> T lookup(String name, Class<T> type) {
121
        for (Registry registry : applicationContextsRegistry.values()) {
!
122
            T result = registry.lookup(name, type);
!
123
            if (result != null) {
!
124
                return result;
!
125
            }
126
        }
127
        return null;
!
128
    }
129

130
    @Override
131
    public <T> Map<String, T> lookupByType(Class<T> type) {
132
        for (Registry registry : applicationContextsRegistry.values()) {
!
133
            Map<String, T> result = registry.lookupByType(type);
!
134
            if (!MapUtils.isEmpty(result)) {
!
135
                return result;
!
136
            }
137
        }
138
        return Collections.emptyMap();
!
139
    }
140

141
    @Override
142
    public void onApplicationEvent(ApplicationContextEvent event) {
143
        Assert.notNull(event, "event must not be null");
1×
144

145
        ApplicationContext applicationContext = event.getApplicationContext();
1×
146

147
        //remove context from created registry
148
        applicationContextsRegistry.remove(applicationContext);
1×
149

150
        //add new found contexts
151
        if (event instanceof ContextRefreshedEvent || event instanceof ContextStartedEvent) {
1×
152
            LOG.info("Create new registry for context '{}.'", applicationContext.getDisplayName());
1×
153
            applicationContextsRegistry.put(applicationContext, new ApplicationContextRegistry(applicationContext));
1×
154
        }
155
    }
1×
156

157
    @Override
158
    public int getOrder() {
159
        return ORDER;
1×
160
    }
161
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc