View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
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    *      http://www.apache.org/licenses/LICENSE-2.0
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.apache.logging.log4j.catalog.api.plugins;
17  
18  import java.io.IOException;
19  
20  import com.fasterxml.jackson.core.JsonParser;
21  import com.fasterxml.jackson.core.JsonProcessingException;
22  import com.fasterxml.jackson.databind.DeserializationContext;
23  import com.fasterxml.jackson.databind.JsonNode;
24  import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
25  import org.apache.logging.log4j.catalog.api.ConstraintType;
26  import org.apache.logging.log4j.catalog.api.exception.NameNotFoundException;
27  
28  /**
29   *
30   */
31  public class ConstraintTypeDeserializer extends StdDeserializer<ConstraintType> {
32  
33      ConstraintPlugins plugins = ConstraintPlugins.getInstance();
34  
35      public ConstraintTypeDeserializer() {
36          this(null);
37      }
38  
39      public ConstraintTypeDeserializer(Class<?> vc) {
40          super(vc);
41      }
42  
43      @Override
44      public ConstraintType deserialize(JsonParser jp, DeserializationContext ctxt)
45              throws IOException, JsonProcessingException {
46          JsonNode node = jp.getCodec().readTree(jp);
47          String name = node.get("name").textValue();
48          ConstraintType type = plugins.findByName(name);
49          if (type == null) {
50              throw new NameNotFoundException("Unable to locate plugin for constraint type " + name);
51          }
52          return type;
53      }
54  }