View Javadoc

1   /*
2    * $Id: CommonsPoolProxyFactory.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.pool;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.impl.MuleDescriptor;
15  import org.mule.umo.lifecycle.InitialisationException;
16  import org.mule.umo.model.UMOModel;
17  
18  import org.apache.commons.beanutils.BeanUtils;
19  import org.apache.commons.pool.PoolableObjectFactory;
20  
21  /**
22   * <code>CommonsPoolProxyFactory</code> is used to create MuleProxies for use in a
23   * proxy pool. This is a Jakarta commons-pool implementation.
24   */
25  public class CommonsPoolProxyFactory extends AbstractProxyFactory implements PoolableObjectFactory
26  {
27      /**
28       * Creates a pool factory using the descriptor as the basis for creating its
29       * objects
30       * 
31       * @param descriptor the descriptor to use to construct a MuleProxy
32       * @see MuleDescriptor
33       */
34      public CommonsPoolProxyFactory(MuleDescriptor descriptor, UMOModel model)
35      {
36          super(descriptor, model);
37      }
38  
39      public void activateObject(Object arg0) throws Exception
40      {
41          // nothing to do
42      }
43  
44      public void destroyObject(Object object) throws Exception
45      {
46          pool.onRemove(object);
47      }
48  
49      public Object makeObject() throws Exception
50      {
51          Object object = create();
52          pool.onAdd(object);
53          return object;
54      }
55  
56      public void passivateObject(Object arg0) throws Exception
57      {
58          // nothing to do
59      }
60  
61      public boolean validateObject(Object arg0)
62      {
63          return true;
64      }
65  
66      protected void afterComponentCreate(Object component) throws InitialisationException
67      {
68          try
69          {
70              BeanUtils.populate(component, descriptor.getProperties());
71          }
72          catch (Exception e)
73          {
74              throw new InitialisationException(
75                  CoreMessages.failedToSetPropertiesOn("Component '" + descriptor.getName() + "'"), 
76                  e, descriptor);
77          }
78      }
79  
80  }