001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Kevin Leturc <kleturc@nuxeo.com> 018 */ 019package org.nuxeo.ecm.core.schema.types.resolver; 020 021import static java.lang.Boolean.TRUE; 022 023import java.io.Serializable; 024import java.util.Collections; 025import java.util.HashMap; 026import java.util.Map; 027 028/** 029 * Basic implementation of {@link ObjectResolver} which provides {@link #parameters} and {@link #validation} support. 030 * 031 * @since 10.2 032 */ 033public abstract class AbstractObjectResolver implements ObjectResolver { 034 035 private static final long serialVersionUID = 1L; 036 037 protected Map<String, Serializable> parameters; 038 039 protected boolean validation; 040 041 @Override 042 public void configure(Map<String, String> parameters) throws IllegalArgumentException, IllegalStateException { 043 if (this.parameters != null) { 044 throw new IllegalStateException("cannot change configuration, may be already in use somewhere"); 045 } 046 this.validation = Boolean.parseBoolean(parameters.getOrDefault(VALIDATION_PARAMETER_KEY, TRUE.toString())); 047 this.parameters = new HashMap<>(); 048 this.parameters.put(VALIDATION_PARAMETER_KEY, validation); 049 } 050 051 @Override 052 public Map<String, Serializable> getParameters() { 053 checkConfig(); 054 return Collections.unmodifiableMap(parameters); 055 } 056 057 @Override 058 public boolean validate(Object value) throws IllegalStateException { 059 checkConfig(); 060 return !validation || fetch(value) != null; 061 } 062 063 @Override 064 public boolean validate(Object value, Object context) throws IllegalStateException { 065 checkConfig(); 066 return !validation || fetch(value, context) != null; 067 } 068 069 protected void checkConfig() throws IllegalStateException { 070 if (parameters == null) { 071 throw new IllegalStateException( 072 "you should call #configure(Map<String, String>) before. Please get this resolver throught ExternalReferenceService which is in charge of resolver configuration."); 073 } 074 } 075 076}