View Javadoc

1   /*
2    * $Id: MuleObjectNameProcessor.java 8098 2007-08-29 01:51:36Z 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 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.extras.spring.config;
12  
13  import org.mule.impl.endpoint.MuleEndpoint;
14  import org.mule.umo.UMODescriptor;
15  import org.mule.umo.endpoint.UMOEndpoint;
16  import org.mule.umo.manager.UMOAgent;
17  import org.mule.umo.model.UMOModel;
18  import org.mule.umo.provider.UMOConnector;
19  import org.mule.umo.transformer.UMOTransformer;
20  import org.mule.util.MuleObjectHelper;
21  
22  import org.springframework.beans.BeansException;
23  import org.springframework.beans.factory.config.BeanPostProcessor;
24  
25  /**
26   * <code>MuleObjectNameProcessor</code> is used to set spring ids to Mule object
27   * names so the the bean id and name property on the object don't both have to be
28   * set.
29   */
30  
31  public class MuleObjectNameProcessor implements BeanPostProcessor
32  {
33      private boolean overwrite = false;
34  
35      public Object postProcessBeforeInitialization(Object o, String s) throws BeansException
36      {
37          if (!MuleObjectHelper.class.getName().equals(s))
38          {
39              if (o instanceof UMOConnector)
40              {
41                  if (((UMOConnector)o).getName() == null || overwrite)
42                  {
43                      ((UMOConnector)o).setName(s);
44                  }
45              }                                  
46              else if (o instanceof UMOTransformer)
47              {
48                  ((UMOTransformer)o).setName(s);
49              }
50              else if (o instanceof UMOEndpoint)
51              {
52                  // spring uses the class name of the object as the name if no other
53                  // id is set; this is no good for endpoints
54                  if ((((UMOEndpoint)o).getName() == null || overwrite)
55                      && s.indexOf(MuleEndpoint.class.getName()) == -1)
56                  {
57                      ((UMOEndpoint)o).setName(s);
58                  }
59              }
60              else if (o instanceof UMODescriptor)
61              {
62                  if (((UMODescriptor)o).getName() == null || overwrite)
63                  {
64                      ((UMODescriptor)o).setName(s);
65                  }
66              }
67              else if (o instanceof UMOModel)
68              {
69                  if (((UMOModel)o).getName() == null || overwrite)
70                  {
71                      ((UMOModel)o).setName(s);
72                  }
73              }
74              else if (o instanceof UMOAgent)
75              {
76                  ((UMOAgent)o).setName(s);
77              }
78          }
79          return o;
80      }
81  
82      public Object postProcessAfterInitialization(Object o, String s) throws BeansException
83      {
84          return o;
85      }
86  
87      public boolean isOverwrite()
88      {
89          return overwrite;
90      }
91  
92      public void setOverwrite(boolean overwrite)
93      {
94          this.overwrite = overwrite;
95      }
96  
97  }