1 /* 2 * Copyright 2009-2013 smartics, Kronseder & Reiner GmbH 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 de.smartics.validation.constraints.impl; 17 18 import java.net.InetAddress; 19 20 import javax.validation.ConstraintValidator; 21 import javax.validation.ConstraintValidatorContext; 22 23 import de.smartics.validation.constraints.Available; 24 25 /** 26 * Checks that the annotated resource is availability. 27 */ 28 public class InetAdressAvailabilityValidator implements 29 ConstraintValidator<Available, InetAddress> 30 { 31 // ******************************** Fields ******************************** 32 33 // --- constants ---------------------------------------------------------- 34 35 // --- members ------------------------------------------------------------ 36 37 /** 38 * The connection timeout in milliseconds. A timeout of zero is interpreted as 39 * an infinite timeout. 40 */ 41 private int timeoutInMs; 42 43 // ***************************** Initializer ****************************** 44 45 // ***************************** Constructors ***************************** 46 47 // ***************************** Inner Classes **************************** 48 49 // ******************************** Methods ******************************* 50 51 // --- init --------------------------------------------------------------- 52 53 @Override 54 public void initialize(final Available constraintAnnotation) 55 { 56 timeoutInMs = constraintAnnotation.timeout(); 57 } 58 59 // --- get&set ------------------------------------------------------------ 60 61 // --- business ----------------------------------------------------------- 62 63 @Override 64 public boolean isValid(final InetAddress value, 65 final ConstraintValidatorContext constraintValidatorContext) 66 { 67 try 68 { 69 final boolean valid = value.isReachable(timeoutInMs); 70 return valid; 71 } 72 catch (final Exception e) 73 { 74 return false; 75 } 76 } 77 78 // --- object basics ------------------------------------------------------ 79 80 }