View Javadoc

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