001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * *** 008 * 009 * Community License: GPL 3.0 010 * 011 * This file is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as published 013 * by the Free Software Foundation, either version 3 of the License, 014 * or (at your option) any later version. 015 * 016 * This file is distributed in the hope that it will be useful, but 017 * WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * 024 * *** 025 * 026 * Available Commercial License: GraniteDS SLA 1.0 027 * 028 * This is the appropriate option if you are creating proprietary 029 * applications and you are not prepared to distribute and share the 030 * source code of your application under the GPL v3 license. 031 * 032 * Please visit http://www.granitedataservices.com/license for more 033 * details. 034 */ 035package org.granite.client.validation; 036 037import java.io.InputStream; 038 039import javax.validation.Configuration; 040import javax.validation.ConstraintValidatorFactory; 041import javax.validation.MessageInterpolator; 042import javax.validation.TraversableResolver; 043import javax.validation.Validation; 044import javax.validation.ValidationProviderResolver; 045import javax.validation.ValidatorFactory; 046import javax.validation.bootstrap.GenericBootstrap; 047import javax.validation.bootstrap.ProviderSpecificBootstrap; 048import javax.validation.spi.ValidationProvider; 049 050/** 051 * @author William DRAI 052 */ 053public class NotifyingValidation { 054 055 public static NotifyingValidatorFactory buildDefaultValidatorFactory() { 056 return new DefaultNotifyingValidatorFactory(Validation.buildDefaultValidatorFactory()); 057 } 058 059 public static GenericBootstrap byDefaultProvider() { 060 return new GenericBootstrapWrapper(Validation.byDefaultProvider()); 061 } 062 063 public static <T extends Configuration<T>, U extends ValidationProvider<T>> ProviderSpecificBootstrap<T> byProvider(Class<U> providerType) { 064 ProviderSpecificBootstrap<T> bootstrap = Validation.byProvider(providerType); 065 return new ProviderSpecificBootstrapWrapper<T>(bootstrap); 066 } 067 068 public static class GenericBootstrapWrapper implements GenericBootstrap { 069 070 private GenericBootstrap bootstrap; 071 072 private GenericBootstrapWrapper(GenericBootstrap bootstrap) { 073 this.bootstrap = bootstrap; 074 } 075 076 @Override 077 public GenericBootstrap providerResolver(ValidationProviderResolver resolver) { 078 this.bootstrap = bootstrap.providerResolver(resolver); 079 return this; 080 } 081 082 @SuppressWarnings({"unchecked", "rawtypes"}) 083 @Override 084 public Configuration<?> configure() { 085 return new ConfigurationWrapper(bootstrap.configure()); 086 } 087 } 088 089 public static class ProviderSpecificBootstrapWrapper<T extends Configuration<T>> implements ProviderSpecificBootstrap<T> { 090 091 private ProviderSpecificBootstrap<T> bootstrap; 092 093 private ProviderSpecificBootstrapWrapper(ProviderSpecificBootstrap<T> bootstrap) { 094 this.bootstrap = bootstrap; 095 } 096 097 @Override 098 public ProviderSpecificBootstrap<T> providerResolver(ValidationProviderResolver resolver) { 099 this.bootstrap = bootstrap.providerResolver(resolver); 100 return this; 101 } 102 103 @SuppressWarnings("unchecked") 104 @Override 105 public T configure() { 106 Configuration<T> configuration = new ConfigurationWrapper<T>(bootstrap.configure()); 107 return (T)configuration; 108 } 109 } 110 111 public static class ConfigurationWrapper<T extends Configuration<T>> implements Configuration<T> { 112 113 private Configuration<T> configuration; 114 115 public ConfigurationWrapper(Configuration<T> configuration) { 116 this.configuration = configuration; 117 } 118 119 @SuppressWarnings("unchecked") 120 @Override 121 public T ignoreXmlConfiguration() { 122 configuration = configuration.ignoreXmlConfiguration(); 123 return (T)this; 124 } 125 126 @SuppressWarnings("unchecked") 127 @Override 128 public T messageInterpolator(MessageInterpolator interpolator) { 129 configuration = configuration.messageInterpolator(interpolator); 130 return (T)this; 131 } 132 133 @SuppressWarnings("unchecked") 134 @Override 135 public T traversableResolver(TraversableResolver resolver) { 136 configuration = configuration.traversableResolver(resolver); 137 return (T)this; 138 } 139 140 @SuppressWarnings("unchecked") 141 @Override 142 public T constraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory) { 143 configuration = configuration.constraintValidatorFactory(constraintValidatorFactory); 144 return (T)this; 145 } 146 147 @SuppressWarnings("unchecked") 148 @Override 149 public T addMapping(InputStream stream) { 150 configuration = configuration.addMapping(stream); 151 return (T)this; 152 } 153 154 @SuppressWarnings("unchecked") 155 @Override 156 public T addProperty(String name, String value) { 157 configuration = configuration.addProperty(name, value); 158 return (T)this; 159 } 160 161 public Configuration<T> providerSpecific() { 162 return configuration; 163 } 164 165 @Override 166 public MessageInterpolator getDefaultMessageInterpolator() { 167 return configuration.getDefaultMessageInterpolator(); 168 } 169 170 @Override 171 public TraversableResolver getDefaultTraversableResolver() { 172 return configuration.getDefaultTraversableResolver(); 173 } 174 175 @Override 176 public ConstraintValidatorFactory getDefaultConstraintValidatorFactory() { 177 return configuration.getDefaultConstraintValidatorFactory(); 178 } 179 180 @Override 181 public NotifyingValidatorFactory buildValidatorFactory() { 182 ValidatorFactory validatorFactory = configuration.buildValidatorFactory(); 183 return new DefaultNotifyingValidatorFactory(validatorFactory); 184 } 185 } 186}