View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.di.impl;
17  
18  import net.sf.jguiraffe.di.Dependency;
19  import net.sf.jguiraffe.di.DependencyProvider;
20  import net.sf.jguiraffe.di.InjectionException;
21  
22  /**
23   * <p>
24   * A special <code>Invocation</code> implementation for setting properties.
25   * </p>
26   * <p>
27   * This <code>Invocation</code> implementation is initialized with a property
28   * name and value (the latter one is provided as a {@link Dependency} object. In
29   * its <code>invoke()</code> method it will set this property on the target
30   * object.
31   * </p>
32   * <p>
33   * This is more or less a convenience class because similar results could be
34   * achieved by using {@link MethodInvocation} and specifying the name of the set
35   * method corresponding to the property and the property value as parameter
36   * dependency. Setting the property is done using the
37   * {@link net.sf.jguiraffe.di.InvocationHelper InvocationHelper} class. This
38   * ensures that required type conversions are done automatically.
39   * </p>
40   * <p>
41   * Once initialized, an instance is immutable. So it can easily be shared
42   * between multiple components and threads without having to care about
43   * synchronization issues.
44   * </p>
45   *
46   * @author Oliver Heger
47   * @version $Id: SetPropertyInvocation.java 205 2012-01-29 18:29:57Z oheger $
48   */
49  public class SetPropertyInvocation extends Invocation implements Invokable
50  {
51      /** Stores the name of the property to set. */
52      private String propertyName;
53  
54      /**
55       * Creates a new instance of <code>SetPropertyInvocation</code> and
56       * initializes it.
57       *
58       * @param propName the name of the property to set (must not be <b>null</b>)
59       * @param propValue a dependency that defines the property's value (must not
60       * be <b>null</b>)
61       * @throws IllegalArgumentException if the property name or its value is
62       * undefined
63       */
64      public SetPropertyInvocation(String propName, Dependency propValue)
65      {
66          super(null, null, propValue);
67          if (propName == null)
68          {
69              throw new IllegalArgumentException(
70                      "Property name must not be null!");
71          }
72          propertyName = propName;
73      }
74  
75      /**
76       * Returns the name of the property that will be set.
77       *
78       * @return the name of the property
79       */
80      public String getPropertyName()
81      {
82          return propertyName;
83      }
84  
85      /**
86       * Performs the invocation and sets the property on the specified target
87       * object.
88       *
89       * @param depProvider the dependency provider (must not be <b>null</b>)
90       * @param target the target instance (must not be <b>null</b>)
91       * @return the result of the invocation, which is always <b>null</b> in this
92       *         case
93       * @throws InjectionException if an error occurs
94       * @throws IllegalArgumentException if one of the parameters is <b>null</b>
95       */
96      public Object invoke(DependencyProvider depProvider, Object target)
97      {
98          if (target == null)
99          {
100             throw new InjectionException("Target object must not be null!");
101         }
102 
103         Object[] values = getResolvedParameters(depProvider);
104         assert values != null && values.length == 1 : "Wrong number of values!";
105         depProvider.getInvocationHelper().setProperty(target,
106                 getPropertyName(), values[0]);
107         return null;
108     }
109 
110     /**
111      * Outputs further information about this invocation to the specified
112      * buffer. This implementation will print the name of the affected property.
113      *
114      * @param buf the target buffer
115      */
116     @Override
117     protected void invocationInfoToString(StringBuilder buf)
118     {
119         buf.append(getPropertyName());
120     }
121 }