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  
20  package org.apache.myfaces.cdi.validator;
21  
22  import java.lang.reflect.Type;
23  
24  import javax.enterprise.inject.spi.BeanManager;
25  import javax.enterprise.util.TypeLiteral;
26  import javax.faces.FacesWrapper;
27  import javax.faces.component.PartialStateHolder;
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.FacesContext;
30  import javax.faces.validator.Validator;
31  import javax.faces.validator.ValidatorException;
32  
33  import org.apache.myfaces.cdi.util.CDIUtils;
34  
35  /**
36   *
37   */
38  public class FacesValidatorCDIWrapper implements PartialStateHolder, Validator, FacesWrapper<Validator>
39  {
40      private transient Validator delegate;
41      
42      private String validatorId;
43      private boolean _transient;
44      private static final Type VALIDATOR_TYPE = new TypeLiteral<Validator<?>>() 
45      { 
46          private static final long serialVersionUID = 1L; 
47      }.getType();
48  
49      public FacesValidatorCDIWrapper()
50      {
51      }
52  
53      public FacesValidatorCDIWrapper(Class<? extends Validator> validatorClass, String validatorId)
54      {
55          this.validatorId = validatorId;
56      }
57  
58      @Override
59      public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
60      {
61          getWrapped().validate(context, component, value);
62      }
63  
64      @Override
65      public Validator getWrapped()
66      {
67          if (delegate == null)
68          {
69              BeanManager  beanManager = CDIUtils.getBeanManager(FacesContext.getCurrentInstance().getExternalContext());
70              FacesValidatorAnnotationLiteral qualifier = new FacesValidatorAnnotationLiteral(validatorId, false, true);
71  
72              delegate = (Validator) CDIUtils.getInstance(beanManager,VALIDATOR_TYPE, true, qualifier);
73   
74              if(delegate == null)
75              {
76                  delegate = (Validator) CDIUtils.getInstance(beanManager, Validator.class, true, qualifier);
77              }
78          }
79          return delegate;
80      }
81      
82      @Override
83      public Object saveState(FacesContext context)
84      {
85          if (!initialStateMarked())
86          {
87              Object values[] = new Object[1];
88              values[0] = validatorId;
89              return values;
90          }
91          return null;
92      }
93  
94      @Override
95      public void restoreState(FacesContext context, Object state)
96      {
97          if (state != null)
98          {
99              Object values[] = (Object[])state;
100             validatorId = (String)values[0];
101         }
102     }
103 
104     @Override
105     public boolean isTransient()
106     {
107         return _transient;
108     }
109 
110     @Override
111     public void setTransient(boolean newTransientValue)
112     {
113         _transient = newTransientValue;
114     }
115 
116     private boolean _initialStateMarked = false;
117 
118     public void clearInitialState()
119     {
120         _initialStateMarked = false;
121     }
122 
123     public boolean initialStateMarked()
124     {
125         return _initialStateMarked;
126     }
127 
128     public void markInitialState()
129     {
130         _initialStateMarked = true;
131     }
132 
133 }