View Javadoc

1   /*
2    * $Id: HiveMindContext.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.extras.hivemind;
12  
13  import org.mule.impl.container.AbstractContainerContext;
14  import org.mule.impl.container.ContainerKeyPair;
15  import org.mule.umo.lifecycle.InitialisationException;
16  import org.mule.umo.manager.ContainerException;
17  import org.mule.umo.manager.ObjectNotFoundException;
18  
19  import java.io.Reader;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hivemind.ApplicationRuntimeException;
24  import org.apache.hivemind.Registry;
25  import org.apache.hivemind.impl.RegistryBuilder;
26  
27  /**
28   * <code>HiveMindContext</code> is a HiveMind Context that can expose HiveMind
29   * managed services for use in the Mule framework.
30   */
31  public class HiveMindContext extends AbstractContainerContext
32  {
33      private static final Log logger = LogFactory.getLog(HiveMindContext.class);
34  
35      /**
36       * the hivemind registry that manages services
37       */
38      private Registry registry;
39  
40      public HiveMindContext()
41      {
42          super("hivemind");
43          logger.debug("HiveMindContext built");
44      }
45  
46      protected Registry getRegistry()
47      {
48          return this.registry;
49      }
50  
51      /*
52       * (non-Javadoc)
53       * 
54       * @see org.mule.model.UMOContainerContext#getComponent(java.lang.Object)
55       */
56      public Object getComponent(Object key) throws ObjectNotFoundException
57      {
58  
59          if (registry == null)
60          {
61              throw new IllegalStateException("HiveMind registry has not been set");
62          }
63          if (key == null)
64          {
65              throw new ObjectNotFoundException("Component not found for null key");
66          }
67          if (key instanceof ContainerKeyPair)
68          {
69              key = ((ContainerKeyPair)key).getKey();
70          }
71          Object component = null;
72  
73          if (key instanceof String)
74          {
75              try
76              {
77                  component = registry.getService((String)key, Object.class);
78                  logger.debug("Called " + key + " obtained  " + component.getClass().getName());
79              }
80              catch (ApplicationRuntimeException are)
81              {
82                  throw new ObjectNotFoundException("Component not found for " + key, are);
83              }
84          }
85          else if (key instanceof Class)
86          {
87              try
88              {
89                  component = registry.getService((Class)key);
90                  logger.debug("Called " + ((Class)key).getName() + " obtained  "
91                               + component.getClass().getName());
92              }
93              catch (ApplicationRuntimeException are)
94              {
95                  throw new ObjectNotFoundException("Component not found for " + key, are);
96              }
97          }
98  
99          if (component == null)
100         {
101             logger.debug("Component not found for key" + key);
102             throw new ObjectNotFoundException("Component not found for key: " + key.toString());
103         }
104         return component;
105     }
106 
107     /**
108      * Just log that we don't need any configuration fragment.
109      */
110     public void configure(Reader configuration) throws ContainerException
111     {
112         logger.info("HiveMindContext don't need any configuration fragment. Configuration ignored");
113     }
114 
115     /**
116      * Here we build the registry from the standard deployment descriptors location.
117      */
118     public void initialise() throws InitialisationException {
119         if (registry == null)
120         {
121             logger.debug("About to initilise the registry...");
122             registry = RegistryBuilder.constructDefaultRegistry();
123             logger.debug(" ... registry initialized");
124         }
125         else
126         {
127             logger.debug("Registry already initialized...");
128         }
129     }
130 
131     /**
132      * Shutdown the registry so to notify every
133      * {@link org.apache.hivemind.events.RegistryShutdownListener}.
134      */
135     public void dispose()
136     {
137         if (registry != null)
138         {
139             registry.shutdown();
140             logger.debug("Registry halted");
141         }
142     }
143 }