View Javadoc

1   /*
2    * $Id: ShowBeansAction.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.Map;
25  import java.util.Set;
26  import java.util.TreeMap;
27  import java.util.TreeSet;
28  
29  import org.apache.struts2.StrutsConstants;
30  import org.apache.struts2.components.UrlRenderer;
31  import org.apache.struts2.dispatcher.mapper.ActionMapper;
32  import org.apache.struts2.dispatcher.multipart.MultiPartRequest;
33  import org.apache.struts2.views.freemarker.FreemarkerManager;
34  import org.apache.struts2.views.velocity.VelocityManager;
35  
36  import com.opensymphony.xwork2.ActionProxyFactory;
37  import com.opensymphony.xwork2.ObjectFactory;
38  import com.opensymphony.xwork2.TextProvider;
39  import com.opensymphony.xwork2.conversion.ObjectTypeDeterminer;
40  import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
41  import com.opensymphony.xwork2.inject.Container;
42  import com.opensymphony.xwork2.inject.Inject;
43  
44  /***
45   * Shows the beans loaded by the internal Guice container.  Only shows beans that are recognized by Struts as official
46   * plugin extension points.
47   */
48  public class ShowBeansAction extends ActionNamesAction {
49  
50      Map<String,Set<Binding>> bindings;
51  
52      @Inject
53      public void setContainer(Container container) {
54          bindings = new TreeMap<String,Set<Binding>>();
55          bindings.put(ObjectFactory.class.getName(), addBindings(container, ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY));
56          bindings.put(XWorkConverter.class.getName(), addBindings(container, XWorkConverter.class, StrutsConstants.STRUTS_XWORKCONVERTER));
57          bindings.put(TextProvider.class.getName(), addBindings(container, TextProvider.class, StrutsConstants.STRUTS_XWORKTEXTPROVIDER));
58          bindings.put(ActionProxyFactory.class.getName(), addBindings(container, ActionProxyFactory.class, StrutsConstants.STRUTS_ACTIONPROXYFACTORY));
59          bindings.put(ObjectTypeDeterminer.class.getName(), addBindings(container, ObjectTypeDeterminer.class, StrutsConstants.STRUTS_OBJECTTYPEDETERMINER));
60          bindings.put(ActionMapper.class.getName(), addBindings(container, ActionMapper.class, StrutsConstants.STRUTS_MAPPER_CLASS));
61          bindings.put(MultiPartRequest.class.getName(), addBindings(container, MultiPartRequest.class, StrutsConstants.STRUTS_MULTIPART_PARSER));
62          bindings.put(FreemarkerManager.class.getName(), addBindings(container, FreemarkerManager.class, StrutsConstants.STRUTS_FREEMARKER_MANAGER_CLASSNAME));
63          bindings.put(VelocityManager.class.getName(), addBindings(container, VelocityManager.class, StrutsConstants.STRUTS_VELOCITY_MANAGER_CLASSNAME));
64          bindings.put(UrlRenderer.class.getName(), addBindings(container, UrlRenderer.class, StrutsConstants.STRUTS_URL_RENDERER));
65      }
66      
67      public Map<String, Set<Binding>> getBeans()
68      {
69          return bindings;
70      }
71      
72      protected Set<Binding> addBindings(Container container, Class type, String constName) {
73          Set<Binding> bindings = new TreeSet<Binding>();
74          String chosenName = container.getInstance(String.class, constName);
75          if (chosenName == null) {
76              chosenName = "struts";
77          }
78          Set<String> names = container.getInstanceNames(type);
79          if (!names.contains(chosenName)) {
80              bindings.add(new Binding(getInstanceClassName(container, type, "default"), chosenName, constName, true));
81          }
82          for (String name : names) {
83              if (!"default".equals(name)) {
84                  bindings.add(new Binding(getInstanceClassName(container, type, name), name, constName, name.equals(chosenName)));
85              }
86          }
87          return bindings;
88      }
89  
90      String getInstanceClassName(Container container, Class type, String name) {
91          String instName = "Class unable to be loaded";
92          try {
93              Object inst = container.getInstance(type, name);
94              instName = inst.getClass().getName();
95          } catch (Exception ex) {
96              // Ignoring beans unable to be loaded
97          }
98          return instName;
99      }
100     
101     public class Binding implements Comparable<Binding> {
102         private String impl;
103         private String alias;
104         private String constant;
105         private boolean isDefault;
106         
107         public Binding(String impl, String alias, String constant, boolean def) {
108             this.impl = impl;
109             this.alias = alias;
110             this.constant = constant;
111             this.isDefault = def;
112         }
113         public String getImpl() {
114             return impl;
115         }
116         public String getAlias() {
117             return alias;
118         }
119         public String getConstant() {
120             return constant;
121         }
122         
123         public boolean isDefault() {
124             return isDefault;
125         }
126         
127         public int compareTo(Binding b2) {
128             int ret = 0;
129             if (isDefault) {
130                 ret = -1;
131             } else if (b2.isDefault()) {
132                 ret = 1;
133             } else {
134                 ret = alias.compareTo(b2.getAlias());
135             }
136             return ret;
137         }
138     }
139 }