View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.config.source.yaml.dependency;
35  
36  import info.magnolia.config.converters.RawDefinitionViewToMapConverter;
37  import info.magnolia.config.registry.DefinitionProvider;
38  import info.magnolia.config.registry.DefinitionProvider.Problem;
39  import info.magnolia.config.registry.Registry;
40  
41  import java.util.Map;
42  import java.util.function.Consumer;
43  
44  /**
45   * A kind of {@link YamlConfigurationDependency} which represents a definition referenced within some YAML configuration file.
46   */
47  public class DefinitionDependency extends AbstractYamlConfigurationDependency {
48  
49      public static Builder resolve() {
50          return new Builder();
51      }
52  
53      private final Registry<?> registry;
54  
55      private final String referenceId;
56  
57      private DefinitionDependency(Registry<?> registry, String definitionReference, Consumer<Problem> problemCollector) {
58          super(problemCollector);
59          this.registry = registry;
60          this.referenceId = definitionReference;
61      }
62  
63      @Override
64      public boolean exists() {
65          try {
66              resolveDefinitionProvider();
67              return true;
68          } catch (Registry.NoSuchDefinitionException ignore) {
69              return false;
70          }
71      }
72  
73      @Override
74      public long getLastModified() {
75          return getLastModified(() -> resolveDefinitionProvider().getLastModified());
76      }
77  
78      public DefinitionProvider<?> resolveDefinitionProvider() {
79          return registry.getProvider(referenceId);
80      }
81  
82      @Override
83      public Map<String, Object> readData() {
84          return RawDefinitionViewToMapConverter.rawViewToMap(resolveDefinitionProvider().getRaw());
85      }
86  
87      public String getReferenceId() {
88          return referenceId;
89      }
90  
91      @Override
92      public String toString() {
93          return String.format("[%s] definition dependency with referenceId [%s]", this.registry.type().getName(), referenceId);
94      }
95  
96      /**
97       * Builder for {@link DefinitionDependency} class instances.
98       */
99      public static class Builder {
100 
101         private String definitionReference;
102         private Consumer<Problem> problemCollector;
103         private Registry<?> registry;
104 
105         public Builder withDefinitionReference(String definitionReference) {
106             this.definitionReference = definitionReference;
107             return this;
108         }
109 
110         public Builder withProblemCollector(Consumer<Problem> problemCollector) {
111             this.problemCollector = problemCollector;
112             return this;
113         }
114         public Builder withRegistry(Registry<?> registry) {
115             this.registry = registry;
116             return this;
117         }
118 
119         public DefinitionDependency build() {
120             return new DefinitionDependency(registry, definitionReference, problemCollector);
121         }
122     }
123 }