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.persistence.jpa.attrvalue.validation;
20  
21  import java.util.Map;
22  import java.util.concurrent.ConcurrentHashMap;
23  import org.apache.syncope.core.persistence.api.attrvalue.validation.PlainAttrValidationManager;
24  import org.apache.syncope.core.persistence.api.attrvalue.validation.PlainAttrValueValidator;
25  import org.apache.syncope.core.persistence.api.entity.PlainAttrValue;
26  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
27  import org.apache.syncope.core.spring.implementation.ImplementationManager;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  public class DefaultPlainAttrValidationManager implements PlainAttrValidationManager {
32  
33      protected static final Logger LOG = LoggerFactory.getLogger(DefaultPlainAttrValidationManager.class);
34  
35      protected static final PlainAttrValueValidator BASIC_VALIDATOR = new BasicValidator();
36  
37      protected final Map<String, PlainAttrValueValidator> perContextValidators = new ConcurrentHashMap<>();
38  
39      @Override
40      public void validate(final PlainSchema schema, final String value, final PlainAttrValue attrValue) {
41          PlainAttrValueValidator validator = null;
42  
43          if (schema.getValidator() != null) {
44              try {
45                  validator = ImplementationManager.build(
46                          schema.getValidator(),
47                          () -> perContextValidators.get(schema.getValidator().getKey()),
48                          instance -> perContextValidators.put(schema.getValidator().getKey(), instance));
49              } catch (Exception e) {
50                  LOG.error("While building {}", schema.getValidator(), e);
51              }
52          }
53  
54          if (validator == null) {
55              validator = BASIC_VALIDATOR;
56          }
57  
58          validator.validate(schema, value, attrValue);
59      }
60  }