View Javadoc

1   /*
2    * $Id: AbstractServiceDescriptor.java 12269 2008-07-10 04:19:03Z dfeist $
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.api.registry;
12  
13  import org.mule.util.ClassUtils;
14  import org.mule.util.StringUtils;
15  
16  import java.util.Map;
17  import java.util.Properties;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  public abstract class AbstractServiceDescriptor implements ServiceDescriptor
23  {
24  
25      /**
26       * logger used by this class
27       */
28      protected final Log logger = LogFactory.getLog(getClass());
29  
30      protected String service;
31  
32      public AbstractServiceDescriptor(String service)
33      {
34          this.service = service;
35      }
36  
37      public String getService()
38      {
39          return service;
40      }
41  
42      protected String removeProperty(String name, Properties properties)
43      {
44          String temp = (String)properties.remove(name);
45          if (StringUtils.isEmpty(StringUtils.trim(temp)))
46          {
47              return null;
48          }
49          else
50          {
51              return temp;
52          }
53      }
54  
55      protected Class removeClassProperty(String name, Properties properties) throws ClassNotFoundException
56      {
57          String clazz = removeProperty(name, properties);
58          if (clazz == null)
59          {
60              return null;
61          }
62          else
63          {
64              return ClassUtils.loadClass(clazz, getClass());
65          }
66      }
67  
68  
69  
70      /**
71       * Unique key used to cache the service descriptors.  This uses the service and the
72       * overrides, but since it is generated externally by the factory that instantiates
73       * the service descriptor we do not need to keep overrides or properties anywhere else.
74       */
75      public static class Key
76      {
77          
78          private final Map overrides;
79          private final String service;
80  
81          public Key(String service, Map overrides)
82          {
83              this.overrides = overrides;
84              this.service = service;
85          }
86  
87          //@Override
88          public boolean equals(Object o)
89          {
90              if (this == o)
91              {
92                  return true;
93              }
94              if (!(o instanceof Key))
95              {
96                  return false;
97              }
98  
99              final Key key = (Key)o;
100 
101             if (overrides != null ? !overrides.equals(key.overrides) : key.overrides != null)
102             {
103                 return false;
104             }
105             if (!service.equals(key.service))
106             {
107                 return false;
108             }
109 
110             return true;
111         }
112 
113         //@Override
114         public int hashCode()
115         {
116             return 29 * (overrides != null ? overrides.hashCode() : 0) + (service != null ? service.hashCode(): 0);
117         }
118 
119         public String getKey()
120         {
121             return service + ":" + Integer.toString(hashCode()); 
122         }
123 
124     }
125 
126 }
127 
128