View Javadoc

1   /*
2    * $Id: Bean.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.components;
23  
24  import java.io.Writer;
25  
26  import org.apache.struts2.views.annotations.StrutsTag;
27  import org.apache.struts2.views.annotations.StrutsTagAttribute;
28  
29  import com.opensymphony.xwork2.ObjectFactory;
30  import com.opensymphony.xwork2.inject.Inject;
31  import com.opensymphony.xwork2.util.ClassLoaderUtil;
32  import com.opensymphony.xwork2.util.ValueStack;
33  import com.opensymphony.xwork2.util.logging.Logger;
34  import com.opensymphony.xwork2.util.logging.LoggerFactory;
35  import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
36  
37  /***
38   * <!-- START SNIPPET: javadoc -->
39   * <p>Instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain
40   * a number of {@link Param} elements to set any mutator methods on that class.</p>
41   * <p/>
42   * <p>If the var attribute is set on the BeanTag, it will place the instantiated bean into the
43   * stack's Context.</p>
44   * <p/>
45   * <!-- END SNIPPET: javadoc -->
46   *
47   *
48   * <!-- START SNIPPET: params -->
49   * <ul>
50   *      <li>var - the stack's context name (if supplied) that the created bean will be store under</li>
51   *      <li>name* - the class name of the bean to be instantiated (must respect JavaBean specification)</li>
52   * </ul>
53   * <!-- END SNIPPET: params -->
54   *
55   *
56   * <p>Examples:</p>
57   * <p/>
58   * <pre>
59   * <!-- START SNIPPET: examples -->
60   * &lt;-- in freemarker form --&gt;
61   * [@s.bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter"]
62   *   [s:param name="foo" value="BAR"/]
63   *   The value of foo is : [s:property value="foo"/], when inside the bean tag.<br />
64   * [/s:bean]
65   *
66   * &lt;-- in jsp form --&gt;
67   * &lt;s:bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter"&gt;
68   *   &lt;s:param name="foo" value="BAR" /&gt;
69   *   The value of foot is : &lt;s:property value="foo"/&gt;, when inside the bean tag &lt;br /&gt;
70   * &lt;/s:bean&gt;
71   * <!-- END SNIPPET: examples -->
72   * </pre>
73   * <p/>
74   *
75   * <!-- START SNIPPET: examplesdescription -->
76   * <p>This example instantiates a bean called SimpleCounter and sets the foo property (setFoo('BAR')). The
77   * SimpleCounter object is then pushed onto the Valuestack, which means that we can call its accessor methods (getFoo())
78   * with the Property tag and get their values.</p>
79   * <p/>
80   * <p>In the above example, the id has been set to a value of <i>counter</i>. This means that the SimpleCounter class
81   * will be placed into the stack's context. You can access the SimpleCounter class using a Struts tag:</p>
82   * <p/>
83   * <pre>
84   * &lt;-- jsp form --&gt;
85   * &lt;s:property value="#counter" /&gt;
86   *
87   * &lt;-- freemarker form --&gt;
88   * [s:property value="#counter.foo"/]
89   * </pre>
90   * <p/>
91   * <p>In the property tag example, the <i>#</i> tells Ognl to search the context for the SimpleCounter class which has
92   * an id(key) of <i>counter</i></p>
93   * <!-- END SNIPPET: examplesdescription -->
94   *
95   * @see Param
96   */
97  @StrutsTag(name="bean", tldTagClass="org.apache.struts2.views.jsp.BeanTag",
98          description="Instantiate a JavaBean and place it in the context")
99  public class Bean extends ContextBean {
100     protected static Logger LOG = LoggerFactory.getLogger(Bean.class);
101 
102     protected Object bean;
103     protected String name;
104     protected ObjectFactory objectFactory;
105     protected ReflectionProvider reflectionProvider;
106 
107     public Bean(ValueStack stack) {
108         super(stack);
109     }
110     
111     @Inject
112     public void setObjectFactory(ObjectFactory objectFactory) {
113         this.objectFactory = objectFactory;
114     }
115     
116     @Inject
117     public void setReflectionProvider(ReflectionProvider prov) {
118         this.reflectionProvider = prov;
119     }
120 
121     public boolean start(Writer writer) {
122         boolean result = super.start(writer);
123 
124         ValueStack stack = getStack();
125 
126         try {
127             String beanName = findString(name, "name", "Bean name is required. Example: com.acme.FooBean");
128             bean = objectFactory.buildBean(ClassLoaderUtil.loadClass(beanName, getClass()), stack.getContext());
129         } catch (Exception e) {
130             LOG.error("Could not instantiate bean", e);
131 
132             return false;
133         }
134 
135         // push bean on stack
136         stack.push(bean);
137 
138         // store for reference later
139         putInContext(bean);
140 
141         return result;
142     }
143 
144     public boolean end(Writer writer, String body) {
145         ValueStack stack = getStack();
146         stack.pop();
147 
148         return super.end(writer, body);
149     }
150 
151     public void addParameter(String key, Object value) {
152         reflectionProvider.setProperty(key, value, bean, getStack().getContext());
153     }
154 
155     @StrutsTagAttribute(description="The class name of the bean to be instantiated (must respect JavaBean specification)",
156                         required=true)
157     public void setName(String name) {
158         this.name = name;
159     }
160 }