Clover icon

Magnolia REST Integration 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:27:09 CET
  2. Package info.magnolia.rest.registry

File EndpointDefinitionRegistryEvent.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
50% of files have more coverage

Code metrics

0
21
7
2
165
95
10
0.48
3
3.5
1.43
39.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
EndpointDefinitionRegistryEvent 55 16 28.6% 7 14
0.330%
EndpointDefinitionRegistryEvent.FixedDefinitionProvider 117 5 55.6% 3 5
0.37537.5%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2017 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 static java.util.Collections.emptyList;
37   
38    import info.magnolia.config.registry.DefinitionMetadata;
39    import info.magnolia.config.registry.DefinitionMetadataBuilder;
40    import info.magnolia.config.registry.DefinitionProvider;
41    import info.magnolia.config.registry.DefinitionRawView;
42    import info.magnolia.config.registry.Registry;
43    import info.magnolia.config.registry.decoration.DefinitionDecorator;
44    import info.magnolia.event.Event;
45    import info.magnolia.rest.EndpointDefinition;
46   
47    import java.util.List;
48   
49    /**
50    * Event fired by {@link EndpointDefinitionRegistry} when endpoint definitions are added, removed or changed.
51    *
52    * @see EndpointDefinitionRegistryEventHandler
53    * @see EndpointDefinitionRegistry
54    */
 
55    public class EndpointDefinitionRegistryEvent implements Event<EndpointDefinitionRegistryEventHandler> {
56   
57    private final EndpointDefinitionRegistryEventType type;
58    private final DefinitionProvider<EndpointDefinition> endpointDefinitionProvider;
59   
 
60  30 toggle public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, DefinitionProvider<EndpointDefinition> endpointDefinitionProvider) {
61  30 this.type = type;
62  30 this.endpointDefinitionProvider = endpointDefinitionProvider;
63    }
64   
 
65  4 toggle public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, String endpointName) {
66  4 this.type = type;
67  4 this.endpointDefinitionProvider = new FixedDefinitionProvider(endpointName);
68    }
69   
70    /**
71    * @deprecated since 2.0, use {@link #EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType, DefinitionProvider)}
72    */
 
73  0 toggle @Deprecated
74    public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, EndpointDefinition endpointDefinition) {
75  0 this.type = type;
76  0 this.endpointDefinitionProvider = new FixedDefinitionProvider(endpointDefinition);
77    }
78   
 
79    toggle public EndpointDefinitionRegistryEventType getType() {
80    return type;
81    }
82   
 
83    toggle public String getEndpointName() {
84    return endpointDefinitionProvider.getMetadata().getReferenceId();
85    }
86   
 
87    toggle public DefinitionProvider<EndpointDefinition> getEndpointDefinitionProvider() {
88    return endpointDefinitionProvider;
89    }
90   
91    /**
92    * @deprecated since 2.0, use {@link #getEndpointDefinitionProvider()}
93    */
 
94    toggle @Deprecated
95    public EndpointDefinition getEndpointDefinition() {
96    return endpointDefinitionProvider.get();
97    }
98   
 
99  0 toggle @Override
100    public void dispatch(EndpointDefinitionRegistryEventHandler handler) {
101  0 switch(type) {
102  0 case REGISTERED:
103  0 handler.onEndpointRegistered(this);
104  0 break;
105  0 case REREGISTERED:
106  0 handler.onEndpointReregistered(this);
107  0 break;
108  0 case UNREGISTERED:
109  0 handler.onEndpointUnregistered(this);
110  0 break;
111    }
112    }
113   
114    /**
115    * Compatibility definition provider, should events be fired with explicit definition or name.
116    */
 
117    private static class FixedDefinitionProvider implements DefinitionProvider<EndpointDefinition> {
118    private final DefinitionMetadata metadata;
119    private final EndpointDefinition endpointDefinition;
120   
 
121  0 toggle public FixedDefinitionProvider(EndpointDefinition endpointDefinition) {
122  0 this.endpointDefinition = endpointDefinition;
123  0 this.metadata = DefinitionMetadataBuilder.usingNameAsId()
124    .name(endpointDefinition.getName())
125    .build();
126    }
127   
 
128  4 toggle public FixedDefinitionProvider(String endpointName) {
129  4 this.endpointDefinition = null;
130  4 this.metadata = DefinitionMetadataBuilder.usingNameAsId()
131    .name(endpointName)
132    .build();
133    }
134   
 
135    toggle @Override
136    public List<DefinitionDecorator<EndpointDefinition>> getDecorators() {
137    return emptyList();
138    }
139   
 
140    toggle @Override
141    public DefinitionMetadata getMetadata() {
142    return metadata;
143    }
144   
 
145  0 toggle @Override
146    public EndpointDefinition get() throws Registry.InvalidDefinitionException {
147  0 return endpointDefinition;
148    }
149   
 
150    toggle @Override
151    public DefinitionRawView getRaw() {
152    return null;
153    }
154   
 
155    toggle @Override
156    public boolean isValid() {
157    return endpointDefinition != null;
158    }
159   
 
160    toggle @Override
161    public long getLastModified() {
162    return 0;
163    }
164    }
165    }