View Javadoc

1   /*
2    * $Id: ListValidatorsAction.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.config_browser;
23  
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.struts2.util.ClassLoaderUtils;
28  
29  import com.opensymphony.xwork2.ActionContext;
30  import com.opensymphony.xwork2.ActionSupport;
31  import com.opensymphony.xwork2.inject.Inject;
32  import com.opensymphony.xwork2.validator.ActionValidatorManager;
33  
34  /***
35   * ListValidatorsAction loads the validations for a given class and context
36   *
37   */
38  public class ListValidatorsAction extends ActionSupport {
39  
40      private static final long serialVersionUID = 1L;
41  
42      private String clazz;
43      private String context;
44      List validators = Collections.EMPTY_LIST;
45      private ActionValidatorManager actionValidatorManager;
46  
47      
48      @Inject
49      public void setActionValidatorManager(ActionValidatorManager mgr) {
50          this.actionValidatorManager = mgr;
51      }
52      
53      public String getClazz() {
54          return clazz;
55      }
56  
57      public void setClazz(String clazz) {
58          this.clazz = clazz;
59      }
60  
61      public String stripPackage(Class clazz) {
62          return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
63      }
64  
65      public String stripPackage(String clazz) {
66          return clazz.substring(clazz.lastIndexOf('.') + 1);
67      }
68  
69      public String getContext() {
70          return context;
71      }
72  
73      public void setContext(String context) {
74          this.context = context;
75      }
76  
77      public List getValidators() {
78          return validators;
79      }
80  
81      public String execute() throws Exception {
82          loadValidators();
83          return super.execute();
84      }
85  
86      protected void loadValidators() {
87          Class value = getClassInstance();
88          if ( value != null ) {
89              validators = actionValidatorManager.getValidators(value, context);
90          }
91      }
92  
93      private Class getClassInstance() {
94          try {
95              return ClassLoaderUtils.loadClass(clazz, ActionContext.getContext().getClass());
96          } catch (Exception e) {
97              LOG.error("Class '" + clazz + "' not found...",e);
98          }
99          return null;
100     }
101 }