View Javadoc
1   /**
2    * Copyright 2014-2024 Bloomreach B.V. (<a href="http://www.bloomreach.com">http://www.bloomreach.com</a>)
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    *         <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
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  package org.onehippo.forge.exdocpicker.impl;
17  
18  import java.io.Serializable;
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import org.hippoecm.frontend.model.JcrNodeModel;
24  import org.hippoecm.frontend.plugin.IPlugin;
25  import org.hippoecm.frontend.plugin.IPluginContext;
26  import org.hippoecm.frontend.plugin.config.IPluginConfig;
27  import org.onehippo.forge.exdocpicker.api.ExternalDocumentServiceContext;
28  
29  /**
30   * Simple {@link ExternalDocumentServiceContext} impl.
31   */
32  public class SimpleExternalDocumentServiceContext implements ExternalDocumentServiceContext {
33  
34      private static final long serialVersionUID = 1L;
35  
36      private final IPlugin plugin;
37      private final IPluginConfig config;
38      private final IPluginContext context;
39      private final JcrNodeModel contextModel;
40      protected Map<String, Serializable> attributes = new LinkedHashMap<>();
41  
42      public SimpleExternalDocumentServiceContext(final IPlugin plugin, final IPluginConfig config, final IPluginContext context, final JcrNodeModel contextModel) {
43          this.plugin = plugin;
44          this.config = config;
45          this.context = context;
46          this.contextModel = contextModel;
47      }
48  
49      @Override
50      public IPlugin getPlugin() {
51          return plugin;
52      }
53  
54      @Override
55      public IPluginConfig getPluginConfig() {
56          return config;
57      }
58  
59      @Override
60      public IPluginContext getPluginContext() {
61          return context;
62      }
63  
64      @Override
65      public JcrNodeModel getContextModel() {
66          return contextModel;
67      }
68  
69      @Override
70      public void setAttribute(String name, Serializable value) {
71          if (name == null) {
72              throw new IllegalArgumentException("attribute name cannot be null.");
73          }
74  
75          if (value == null) {
76              removeAttribute(name);
77          }
78  
79          attributes.put(name, value);
80      }
81  
82      @Override
83      public Serializable getAttribute(String name) {
84          return attributes.get(name);
85      }
86  
87      @Override
88      public void removeAttribute(String name) {
89          attributes.remove(name);
90      }
91  
92      @Override
93      public Set<String> getAttributeNames() {
94          return attributes.keySet();
95      }
96  
97  }