Clover icon

Magnolia REST Integration 1.1

  1. Project Clover database Thu Aug 13 2015 19:08:49 CEST
  2. Package info.magnolia.rest.registry

File EndpointDefinitionRegistry.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
40% of files have more coverage

Code metrics

4
28
7
1
136
78
12
0.43
4
7
1.71

Classes

Class Line # Actions
EndpointDefinitionRegistry 61 28 0% 12 12
0.692307769.2%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.rest.registry;
35   
36    import info.magnolia.event.EventBus;
37    import info.magnolia.event.SystemEventBus;
38    import info.magnolia.registry.RegistrationException;
39    import info.magnolia.registry.RegistryMap;
40    import info.magnolia.rest.EndpointDefinition;
41   
42    import java.util.ArrayList;
43    import java.util.Collection;
44    import java.util.List;
45    import java.util.Set;
46   
47    import javax.inject.Inject;
48    import javax.inject.Named;
49    import javax.inject.Singleton;
50   
51    import org.slf4j.Logger;
52    import org.slf4j.LoggerFactory;
53   
54    /**
55    * The central registry for {@link info.magnolia.rest.EndpointDefinition}s.
56    *
57    * @see info.magnolia.rest.EndpointDefinition
58    * @see EndpointDefinitionProvider
59    */
60    @Singleton
 
61    public class EndpointDefinitionRegistry {
62   
63    private final Logger log = LoggerFactory.getLogger(getClass());
64   
65    private EventBus systemEventBus;
66    private final RegistryMap<String, EndpointDefinitionProvider> registry = new RegistryMap<String, EndpointDefinitionProvider>() {
 
67  16 toggle @Override
68    protected String keyFromValue(EndpointDefinitionProvider value) {
69  16 return value.getName();
70    }
71    };
72   
 
73  9 toggle @Inject
74    public EndpointDefinitionRegistry(@Named(SystemEventBus.NAME) EventBus systemEventBus) {
75  9 this.systemEventBus = systemEventBus;
76    }
77   
 
78  3 toggle public EndpointDefinition getEndpointDefinition(String name) throws RegistrationException {
79  3 return getProvider(name).getEndpointDefinition();
80    }
81   
 
82  10 toggle public Set<String> unregisterAndRegister(Set<String> registeredNames, List<EndpointDefinitionProvider> providers) {
83  10 Set<String> nowRegisteredNames = registry.removeAndPutAll(registeredNames, providers);
84   
85    // Send registered events
86  10 for (EndpointDefinitionProvider provider : providers) {
87  8 String providerName = provider.getName();
88  8 EndpointDefinition endpointDefinition;
89  8 try {
90  8 endpointDefinition = provider.getEndpointDefinition();
91    } catch (RegistrationException e) {
92  0 log.error("Could not read endpoint definition [" + providerName + "]", e);
93  0 continue;
94    }
95   
96  8 if (!registeredNames.contains(providerName)) {
97  8 systemEventBus.fireEvent(new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REGISTERED, endpointDefinition));
98    } else {
99  0 systemEventBus.fireEvent(new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REREGISTERED, endpointDefinition));
100    }
101    }
102   
103    // Send unregistered events
104  10 for (String registeredName : registeredNames) {
105  1 if (!nowRegisteredNames.contains(registeredName)) {
106  1 systemEventBus.fireEvent(new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.UNREGISTERED, registeredName));
107    }
108    }
109   
110  10 return nowRegisteredNames;
111    }
112   
 
113  5 toggle public boolean isEndpointDefinitionRegistered(String name) {
114  5 return registry.get(name) != null;
115    }
116   
 
117  0 toggle public Collection<EndpointDefinition> getAllEndpointDefinitions() {
118  0 List<EndpointDefinition> endpointDefinitions = new ArrayList<EndpointDefinition>();
119  0 for (EndpointDefinitionProvider provider : registry.values()) {
120  0 try {
121  0 endpointDefinitions.add(provider.getEndpointDefinition());
122    } catch (RegistrationException e) {
123  0 log.error("Could not read endpoint definition [" + provider.getName() + "]", e);
124    }
125    }
126  0 return endpointDefinitions;
127    }
128   
 
129  3 toggle private EndpointDefinitionProvider getProvider(String name) throws RegistrationException {
130  3 try {
131  3 return registry.getRequired(name);
132    } catch (RegistrationException e) {
133  1 throw new RegistrationException("No endpoint definition registered with name: " + name, e);
134    }
135    }
136    }