View Javadoc

1   /*
2    * $Id: ContainerReference.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.builders;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.impl.container.ContainerKeyPair;
15  import org.mule.umo.manager.ContainerException;
16  import org.mule.umo.manager.UMOContainerContext;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.commons.beanutils.BeanUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * <code>ContainerReference</code> maintains a container reference for the
27   * MuleXmlConfigurationBuilder that gets wired once the configuration documents have
28   * been loaded.
29   */
30  public class ContainerReference
31  {
32      /**
33       * logger used by this class
34       */
35      protected static final Log logger = LogFactory.getLog(ContainerReference.class);
36  
37      private String propertyName;
38      private String containerRef;
39      private String container;
40      private Object object;
41      private boolean required;
42  
43      public ContainerReference(String propertyName,
44                                String containerRef,
45                                Object object,
46                                boolean required,
47                                String container)
48      {
49          this.propertyName = propertyName;
50          this.containerRef = containerRef;
51          this.container = container;
52          this.object = object;
53          this.required = required;
54      }
55  
56      public void resolveReference(UMOContainerContext ctx) throws ContainerException
57      {
58          Object comp = ctx.getComponent(new ContainerKeyPair(container, containerRef, required));
59          if (comp == null)
60          {
61              return;
62          }
63  
64          try
65          {
66              if (object instanceof Map)
67              {
68                  ((Map) object).put(propertyName, comp);
69              }
70              else if (object instanceof List)
71              {
72                  ((List) object).add(comp);
73              }
74              else
75              {
76                  BeanUtils.setProperty(object, propertyName, comp);
77              }
78          }
79          catch (Exception e)
80          {
81              throw new ContainerException(
82                  CoreMessages.cannotSetPropertyOnObjectWithParamType(propertyName, 
83                      object.getClass(), comp.getClass()), e);
84          }
85      }
86  }