View Javadoc

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 javax.mail.Session;
19  import javax.mail.Transport;
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   * <p>
28   * The timout parameter is not used by this implementation.
29   * </p>
30   */
31  public class MailServerAvailabilityValidator implements
32      ConstraintValidator<Available, Session>
33  {
34    // ******************************** Fields ********************************
35  
36    // --- constants ----------------------------------------------------------
37  
38    // --- members ------------------------------------------------------------
39  
40    // ***************************** Initializer ******************************
41  
42    // ***************************** Constructors *****************************
43  
44    // ***************************** Inner Classes ****************************
45  
46    // ******************************** Methods *******************************
47  
48    // --- init ---------------------------------------------------------------
49  
50    @Override
51    public void initialize(final Available constraintAnnotation)
52    {
53    }
54  
55    // --- get&set ------------------------------------------------------------
56  
57    // --- business -----------------------------------------------------------
58  
59    @Override
60    public boolean isValid(final Session value,
61        final ConstraintValidatorContext constraintValidatorContext)
62    {
63      try
64      {
65        final Transport transport = value.getTransport("smtp");
66        final boolean valid = transport.isConnected();
67        return valid;
68      }
69      catch (final Exception e)
70      {
71        return false;
72      }
73    }
74  
75    // --- object basics ------------------------------------------------------
76  
77  }