KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > builders > ContainerReference


1 /*
2  * $Id: ContainerReference.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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.apache.commons.beanutils.BeanUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.mule.config.i18n.Message;
17 import org.mule.config.i18n.Messages;
18 import org.mule.impl.container.ContainerKeyPair;
19 import org.mule.umo.manager.ContainerException;
20 import org.mule.umo.manager.UMOContainerContext;
21
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
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 JavaDoc propertyName;
38     private String JavaDoc containerRef;
39     private String JavaDoc container;
40     private Object JavaDoc object;
41     private boolean required;
42
43     public ContainerReference(String JavaDoc propertyName,
44                               String JavaDoc containerRef,
45                               Object JavaDoc object,
46                               boolean required,
47                               String JavaDoc 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 JavaDoc comp = ctx.getComponent(new ContainerKeyPair(container, containerRef, required));
59         if (comp == null) return;
60
61         try
62         {
63             if (object instanceof Map JavaDoc)
64             {
65                 ((Map JavaDoc)object).put(propertyName, comp);
66             }
67             else if (object instanceof List JavaDoc)
68             {
69                 ((List JavaDoc)object).add(comp);
70             }
71             else
72             {
73                 BeanUtils.setProperty(object, propertyName, comp);
74             }
75         }
76         catch (Exception JavaDoc e)
77         {
78             throw new ContainerException(new Message(Messages.CANT_SET_PROP_X_ON_X_OF_TYPE_X, propertyName,
79                 object.getClass().getName(), comp.getClass().getName()));
80         }
81     }
82 }
83
Popular Tags