View Javadoc

1   /*
2    * $Id: ListValidatorsAction.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.config_browser;
19  
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.struts2.util.ClassLoaderUtils;
24  
25  import com.opensymphony.xwork2.ActionContext;
26  import com.opensymphony.xwork2.ActionSupport;
27  import com.opensymphony.xwork2.validator.ActionValidatorManagerFactory;
28  
29  /***
30   * ListValidatorsAction loads the validations for a given class and context
31   *
32   */
33  public class ListValidatorsAction extends ActionSupport {
34  	
35  	private static final long serialVersionUID = 1L;
36  	
37  	private String clazz;
38      private String context;
39      List validators = Collections.EMPTY_LIST;
40  
41      public String getClazz() {
42          return clazz;
43      }
44  
45      public void setClazz(String clazz) {
46          this.clazz = clazz;
47      }
48  
49      public String stripPackage(Class clazz) {
50          return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
51      }
52  
53      public String stripPackage(String clazz) {
54          return clazz.substring(clazz.lastIndexOf('.') + 1);
55      }
56  
57      public String getContext() {
58          return context;
59      }
60  
61      public void setContext(String context) {
62          this.context = context;
63      }
64  
65      public List getValidators() {
66          return validators;
67      }
68  
69      public String execute() throws Exception {
70          loadValidators();
71          return super.execute();
72      }
73  
74      protected void loadValidators() {
75          Class value = getClassInstance();
76          if ( value != null ) {
77              validators = ActionValidatorManagerFactory.getInstance().getValidators(value, context);
78          }
79      }
80  
81      private Class getClassInstance() {
82          try {
83              return ClassLoaderUtils.loadClass(clazz, ActionContext.getContext().getClass());
84          } catch (Exception e) {
85              LOG.error("Class '" + clazz + "' not found...",e);
86          }
87          return null;
88      }
89  }