KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > hivemind > HiveMindContext


1 /*
2  * $Id: HiveMindContext.java 3167 2006-09-22 15:47:04Z holger $
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 MuleSource MPL
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 java.io.Reader JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Registry;
19 import org.apache.hivemind.impl.RegistryBuilder;
20 import org.mule.impl.container.AbstractContainerContext;
21 import org.mule.impl.container.ContainerKeyPair;
22 import org.mule.umo.lifecycle.InitialisationException;
23 import org.mule.umo.lifecycle.RecoverableException;
24 import org.mule.umo.manager.ContainerException;
25 import org.mule.umo.manager.ObjectNotFoundException;
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  * @author <a HREF="mailto:massimo@datacode.it">Massimo Lusetti</a>
32  * @version $Revision: 3167 $
33  */

34 public class HiveMindContext extends AbstractContainerContext
35 {
36     private static final Log logger = LogFactory.getLog(HiveMindContext.class);
37
38     /**
39      * the hivemind registry that manages services
40      */

41     private Registry registry;
42
43     public HiveMindContext()
44     {
45         super("hivemind");
46         logger.debug("HiveMindContext built");
47     }
48
49     protected Registry getRegistry()
50     {
51         return this.registry;
52     }
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.mule.model.UMOContainerContext#getComponent(java.lang.Object)
58      */

59     public Object JavaDoc getComponent(Object JavaDoc key) throws ObjectNotFoundException
60     {
61
62         if (registry == null)
63         {
64             throw new IllegalStateException JavaDoc("HiveMind registry has not been set");
65         }
66         if (key == null)
67         {
68             throw new ObjectNotFoundException("Component not found for null key");
69         }
70         if (key instanceof ContainerKeyPair)
71         {
72             key = ((ContainerKeyPair)key).getKey();
73         }
74         Object JavaDoc component = null;
75
76         if (key instanceof String JavaDoc)
77         {
78             try
79             {
80                 component = registry.getService((String JavaDoc)key, Object JavaDoc.class);
81                 logger.debug("Called " + key + " obtained " + component.getClass().getName());
82             }
83             catch (ApplicationRuntimeException are)
84             {
85                 throw new ObjectNotFoundException("Component not found for " + key, are);
86             }
87         }
88         else if (key instanceof Class JavaDoc)
89         {
90             try
91             {
92                 component = registry.getService((Class JavaDoc)key);
93                 logger.debug("Called " + ((Class JavaDoc)key).getName() + " obtained "
94                              + component.getClass().getName());
95             }
96             catch (ApplicationRuntimeException are)
97             {
98                 throw new ObjectNotFoundException("Component not found for " + key, are);
99             }
100         }
101
102         if (component == null)
103         {
104             logger.debug("Component not found for key" + key);
105             throw new ObjectNotFoundException("Component not found for key: " + key.toString());
106         }
107         return component;
108     }
109
110     /**
111      * Just log that we don't need any configuration fragment.
112      */

113     public void configure(Reader JavaDoc configuration) throws ContainerException
114     {
115         logger.info("HiveMindContext don't need any configuration fragment. Configuration ignored");
116     }
117
118     /**
119      * Here we build the registry from the standard deployment descriptors location.
120      */

121     public void initialise() throws InitialisationException, RecoverableException
122     {
123         if (registry == null)
124         {
125             logger.debug("About to initilise the registry...");
126             registry = RegistryBuilder.constructDefaultRegistry();
127             logger.debug(" ... registry initialized");
128         }
129         else
130         {
131             logger.debug("Registry already initialized...");
132         }
133     }
134
135     /**
136      * Shutdown the registry so to notify every
137      * {@link org.apache.hivemind.events.RegistryShutdownListener}.
138      */

139     public void dispose()
140     {
141         if (registry != null)
142         {
143             registry.shutdown();
144             logger.debug("Registry halted");
145         }
146     }
147 }
148
Popular Tags