View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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      public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, DefinitionProvider<EndpointDefinition> endpointDefinitionProvider) {
61          this.type = type;
62          this.endpointDefinitionProvider = endpointDefinitionProvider;
63      }
64  
65      public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, String endpointName) {
66          this.type = type;
67          this.endpointDefinitionProvider = new FixedDefinitionProvider(endpointName);
68      }
69  
70      /**
71       * @deprecated since 2.0, use {@link #EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType, DefinitionProvider)}
72       */
73      @Deprecated
74      public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, EndpointDefinition endpointDefinition) {
75          this.type = type;
76          this.endpointDefinitionProvider = new FixedDefinitionProvider(endpointDefinition);
77      }
78  
79      public EndpointDefinitionRegistryEventType getType() {
80          return type;
81      }
82  
83      public String getEndpointName() {
84          return endpointDefinitionProvider.getMetadata().getReferenceId();
85      }
86  
87      public DefinitionProvider<EndpointDefinition> getEndpointDefinitionProvider() {
88          return endpointDefinitionProvider;
89      }
90  
91      /**
92       * @deprecated since 2.0, use {@link #getEndpointDefinitionProvider()}
93       */
94      @Deprecated
95      public EndpointDefinition getEndpointDefinition() {
96          return endpointDefinitionProvider.get();
97      }
98  
99      @Override
100     public void dispatch(EndpointDefinitionRegistryEventHandler handler) {
101         switch(type) {
102         case REGISTERED:
103             handler.onEndpointRegistered(this);
104             break;
105         case REREGISTERED:
106             handler.onEndpointReregistered(this);
107             break;
108         case UNREGISTERED:
109             handler.onEndpointUnregistered(this);
110             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         public FixedDefinitionProvider(EndpointDefinition endpointDefinition) {
122             this.endpointDefinition = endpointDefinition;
123             this.metadata = DefinitionMetadataBuilder.usingNameAsId()
124                     .name(endpointDefinition.getName())
125                     .build();
126         }
127 
128         public FixedDefinitionProvider(String endpointName) {
129             this.endpointDefinition = null;
130             this.metadata = DefinitionMetadataBuilder.usingNameAsId()
131                     .name(endpointName)
132                     .build();
133         }
134 
135         @Override
136         public List<DefinitionDecorator<EndpointDefinition>> getDecorators() {
137             return emptyList();
138         }
139 
140         @Override
141         public DefinitionMetadata getMetadata() {
142             return metadata;
143         }
144 
145         @Override
146         public EndpointDefinition get() throws Registry.InvalidDefinitionException {
147             return endpointDefinition;
148         }
149 
150         @Override
151         public DefinitionRawView getRaw() {
152             return null;
153         }
154 
155         @Override
156         public boolean isValid() {
157             return endpointDefinition != null;
158         }
159 
160         @Override
161         public long getLastModified() {
162             return 0;
163         }
164     }
165 }