KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > model > ComponentFactory


1 /*
2  * $Id: ComponentFactory.java 3798 2006-11-04 04:07:14Z 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.impl.model;
12
13 import org.mule.MuleManager;
14 import org.mule.config.i18n.Message;
15 import org.mule.config.i18n.Messages;
16 import org.mule.impl.MuleDescriptor;
17 import org.mule.impl.container.ContainerKeyPair;
18 import org.mule.umo.UMOException;
19 import org.mule.umo.UMODescriptor;
20 import org.mule.umo.lifecycle.InitialisationException;
21 import org.mule.umo.manager.UMOManager;
22 import org.mule.util.BeanUtils;
23
24 /**
25  * Reusable methods for working with UMOComponents.
26  */

27 public class ComponentFactory
28 {
29
30     /**
31      * Creates a component based on its descriptor.
32      *
33      * @param descriptor the descriptor to create the component from
34      * @return The newly created component
35      * @throws UMOException
36      */

37     public static Object JavaDoc createComponent(UMODescriptor descriptor) throws UMOException
38     {
39         UMOManager manager = MuleManager.getInstance();
40         Object JavaDoc impl = descriptor.getImplementation();
41         Object JavaDoc component;
42
43         if (impl instanceof String JavaDoc)
44         {
45             impl = new ContainerKeyPair(null, impl);
46         }
47         if (impl instanceof ContainerKeyPair)
48         {
49             component = manager.getContainerContext().getComponent(impl);
50
51             if (descriptor.isSingleton())
52             {
53                 descriptor.setImplementation(component);
54             }
55         }
56         else
57         {
58             component = impl;
59         }
60
61         try
62         {
63             BeanUtils.populate(component, descriptor.getProperties());
64         }
65         catch (Exception JavaDoc e)
66         {
67             throw new InitialisationException(new Message(Messages.FAILED_TO_SET_PROPERTIES_ON_X,
68                 "Component '" + descriptor.getName() + "'"), e, descriptor);
69         }
70
71         // Call any custom initialisers
72
if (descriptor instanceof MuleDescriptor)
73         {
74             ((MuleDescriptor)descriptor).fireInitialisationCallbacks(component);
75         }
76
77         return component;
78     }
79 }
80
Popular Tags