View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.keymaster.internal;
20  
21  import com.fasterxml.jackson.databind.JsonNode;
22  import com.fasterxml.jackson.databind.json.JsonMapper;
23  import java.io.IOException;
24  import java.util.Map;
25  import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.transaction.annotation.Transactional;
29  
30  public class SelfKeymasterInternalConfParamOps implements ConfParamOps {
31  
32      protected static final Logger LOG = LoggerFactory.getLogger(ConfParamOps.class);
33  
34      protected static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
35  
36      protected final InternalConfParamHelper helper;
37  
38      public SelfKeymasterInternalConfParamOps(final InternalConfParamHelper helper) {
39          this.helper = helper;
40      }
41  
42      @Transactional(readOnly = true)
43      @Override
44      public Map<String, Object> list(final String domain) {
45          return helper.list();
46      }
47  
48      @Transactional(readOnly = true)
49      @Override
50      public <T> T get(final String domain, final String key, final T defaultValue, final Class<T> reference) {
51          JsonNode valueNode = helper.get(key);
52          if (valueNode == null) {
53              return defaultValue;
54          }
55  
56          try {
57              return MAPPER.treeToValue(valueNode, reference);
58          } catch (IOException e) {
59              LOG.error("Could not deserialize {}", valueNode, e);
60              return defaultValue;
61          }
62      }
63  
64      @Transactional
65      @Override
66      public <T> void set(final String domain, final String key, final T value) {
67          if (value == null) {
68              remove(domain, key);
69          } else {
70              helper.set(key, MAPPER.valueToTree(value));
71          }
72      }
73  
74      @Transactional
75      @Override
76      public void remove(final String domain, final String key) {
77          helper.remove(key);
78      }
79  }