View Javadoc

1   /*
2    * $Id: ComponentFactory.java 7963 2007-08-21 08:53:15Z 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.impl.model;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.impl.MuleDescriptor;
16  import org.mule.impl.container.ContainerKeyPair;
17  import org.mule.umo.UMODescriptor;
18  import org.mule.umo.UMOException;
19  import org.mule.umo.lifecycle.InitialisationException;
20  import org.mule.umo.manager.UMOManager;
21  import org.mule.util.BeanUtils;
22  
23  /**
24   * Reusable methods for working with UMOComponents.
25   */
26  public final class ComponentFactory
27  {
28  
29      /** Do not instanciate. */
30      private ComponentFactory ()
31      {
32          // no-op
33      }
34  
35      /**
36       * Creates a component based on its descriptor.
37       * 
38       * @param descriptor the descriptor to create the component from
39       * @return The newly created component
40       * @throws UMOException
41       */
42      public static Object createComponent(UMODescriptor descriptor) throws UMOException
43      {
44          UMOManager manager = MuleManager.getInstance();
45          Object impl = descriptor.getImplementation();
46          Object component;
47  
48          if (impl instanceof String)
49          {
50              impl = new ContainerKeyPair(null, impl);
51          }
52          if (impl instanceof ContainerKeyPair)
53          {
54              component = manager.getContainerContext().getComponent(impl);
55  
56              if (descriptor.isSingleton())
57              {
58                  descriptor.setImplementation(component);
59              }
60          }
61          else
62          {
63              component = impl;
64          }
65  
66          try
67          {
68              BeanUtils.populate(component, descriptor.getProperties());
69          }
70          catch (Exception e)
71          {
72              throw new InitialisationException(
73                  CoreMessages.failedToSetPropertiesOn("Component '" + descriptor.getName() + "'"), 
74                  e, descriptor);
75          }
76  
77          // Call any custom initialisers
78          if (descriptor instanceof MuleDescriptor)
79          {
80              ((MuleDescriptor) descriptor).fireInitialisationCallbacks(component);
81          }
82  
83          return component;
84      }
85  }