View Javadoc

1   /*
2    * $Id: AbstractRegistry.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.registry.impl;
12  
13  import org.mule.ManagementContext;
14  import org.mule.MuleManager;
15  import org.mule.registry.Assembly;
16  import org.mule.registry.ComponentType;
17  import org.mule.registry.Library;
18  import org.mule.registry.Registry;
19  import org.mule.registry.RegistryComponent;
20  import org.mule.registry.RegistryException;
21  import org.mule.registry.RegistryStore;
22  import org.mule.registry.Unit;
23  import org.mule.util.FileUtils;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  public abstract class AbstractRegistry implements Registry
35  {
36  
37      public static final String REGISTRY_DIRECTORY = "registry";
38  
39      private List libraries;
40      private List assemblies;
41      private List components;
42      private transient Map registry;
43      private transient Map librariesMap;
44      private transient Map assembliesMap;
45      private transient Map componentsMap;
46      private transient String storeLocation;
47      private File workingDirectory;
48      protected transient RegistryStore store;
49      protected transient ManagementContext context;
50  
51      private transient boolean started = false;
52  
53      public AbstractRegistry(RegistryStore store, ManagementContext context)
54      {
55          this.store = store;
56          this.context = context;
57          registry = new HashMap();
58          for (int i = 0; i < ComponentType.COMPONENT_TYPES.length; i++)
59          {
60              ComponentType componentType = ComponentType.COMPONENT_TYPES[i];
61              registry.put(componentType.getName() + ".list", new ArrayList());
62              registry.put(componentType.getName() + ".map", new HashMap());
63          }
64  
65          this.components = new ArrayList();
66          this.libraries = new ArrayList();
67          this.assemblies = new ArrayList();
68          this.librariesMap = new HashMap();
69          this.assembliesMap = new HashMap();
70          this.componentsMap = new HashMap();
71  
72          workingDirectory = FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory(), REGISTRY_DIRECTORY);
73      }
74  
75      public File getWorkingDirectory()
76      {
77          return workingDirectory;
78      }
79  
80      public void setWorkingDirectory(File workingDirectory)
81      {
82          this.workingDirectory = workingDirectory;
83      }
84  
85      public String getStoreLocation()
86      {
87          return this.storeLocation;
88      }
89  
90      public void setStoreLocation(String storeLocation)
91      {
92          this.storeLocation = storeLocation;
93      }
94  
95      /*
96       * (non-Javadoc)
97       * 
98       * @see org.mule.jbi.registry.Registry#getComponents()
99       */
100     public synchronized RegistryComponent[] getComponents()
101     {
102         return (RegistryComponent[])components.toArray(new RegistryComponent[components.size()]);
103     }
104 
105     public synchronized RegistryComponent[] getComponents(ComponentType type)
106     {
107         RegistryComponent[] components = new RegistryComponent[]{};
108         List list = (List)registry.get(type + ".list");
109         if (list != null)
110         {
111             components = (RegistryComponent[])list.toArray(new RegistryComponent[list.size()]);
112         }
113         return components;
114     }
115 
116     /*
117      * (non-Javadoc)
118      * 
119      * @see org.mule.jbi.registry.Registry#getRegistryComponent(java.lang.String)
120      */
121     public synchronized RegistryComponent getComponent(String name, ComponentType type)
122     {
123         return (RegistryComponent)this.componentsMap.get(name);
124     }
125 
126     public synchronized void removeComponent(RegistryComponent component)
127     {
128         this.componentsMap.remove(component.getName());
129         List list = (List)registry.get(component.getType() + ".list");
130         if (list != null)
131         {
132             list.remove(component);
133         }
134         Map map = (Map)registry.get(component.getType() + ".map");
135         if (map != null)
136         {
137             map.remove(component);
138         }
139     }
140 
141     /*
142      * (non-Javadoc)
143      * 
144      * @see org.mule.jbi.registry.Registry#getRegistryComponent(java.lang.String)
145      */
146     public synchronized RegistryComponent getComponent(String name)
147     {
148         return (RegistryComponent)this.componentsMap.get(name);
149     }
150 
151     /*
152      * (non-Javadoc)
153      * 
154      * @see org.mule.jbi.registry.Registry#addEngine(java.lang.String)
155      */
156     public synchronized RegistryComponent addComponent(String name, ComponentType type)
157         throws RegistryException
158     {
159         if (getComponent(name) != null)
160         {
161             throw new RegistryException("Component already registered: " + name);
162         }
163         RegistryComponent rc = createComponent(name, type);
164         this.componentsMap.put(name, rc);
165         return rc;
166     }
167 
168     /*
169      * (non-Javadoc)
170      * 
171      * @see org.mule.jbi.registry.Registry#addTransientEngine(java.lang.String,
172      *      javax.jbi.component.Component)
173      */
174     public synchronized RegistryComponent addTransientComponent(String name,
175                                                                 ComponentType type,
176                                                                 Object component,
177                                                                 Object bootstrap) throws RegistryException
178     {
179 
180         RegistryComponent rc = getComponent(name);
181         if (rc == null)
182         {
183             rc = createComponent(name, type);
184             rc.setTransient(true);
185             rc.setComponent(component);
186             rc.setStateAtShutdown(RegistryComponent.RUNNING);
187             try
188             {
189                 rc.setWorkspaceRoot(context.getComponentWorkspaceDir(getWorkingDirectory(), name)
190                     .getAbsoluteFile()
191                     .getCanonicalPath());
192             }
193             catch (IOException e)
194             {
195                 throw new RegistryException(e);
196             }
197             this.componentsMap.put(name, rc);
198             components.add(rc);
199             if (bootstrap != null)
200             {
201                 try
202                 {
203                     bootstrapComponent(rc, bootstrap);
204                 }
205                 catch (Exception e)
206                 {
207                     throw new RegistryException(e);
208                 }
209             }
210         }
211         else
212         {
213             if (!rc.isTransient())
214             {
215                 throw new RegistryException("A non-transient component is already registered: " + name);
216             }
217             rc.setComponent(component);
218         }
219         try
220         {
221             rc.initComponent();
222         }
223         catch (Exception e)
224         {
225             throw new RegistryException(e);
226         }
227         return rc;
228     }
229 
230     protected abstract void bootstrapComponent(RegistryComponent component, Object bootstrap)
231         throws Exception;
232 
233     /*
234      * (non-Javadoc)
235      * 
236      * @see org.mule.jbi.registry.Registry#getLibraries()
237      */
238     public synchronized Library[] getLibraries()
239     {
240         Collection c = this.libraries;
241         return (Library[])c.toArray(new Library[c.size()]);
242     }
243 
244     /*
245      * (non-Javadoc)
246      * 
247      * @see org.mule.jbi.registry.Registry#getLibrary(java.lang.String)
248      */
249     public synchronized Library getLibrary(String name)
250     {
251         return (Library)this.librariesMap.get(name);
252     }
253 
254     /*
255      * (non-Javadoc)
256      * 
257      * @see org.mule.jbi.registry.Registry#addLibrary(java.lang.String)
258      */
259     public synchronized Library addLibrary(String name) throws RegistryException
260     {
261         if (getLibrary(name) != null)
262         {
263             throw new RegistryException("Library already registered: " + name);
264         }
265         Library l = createLibrary(name);
266         this.libraries.add(l);
267         this.librariesMap.put(name, l);
268         return l;
269     }
270 
271     public synchronized void removeLibrary(Library library)
272     {
273         this.librariesMap.remove(library.getName());
274         this.libraries.remove(library);
275     }
276 
277     /*
278      * (non-Javadoc)
279      * 
280      * @see org.mule.jbi.registry.Registry#getAssemblies()
281      */
282     public synchronized Assembly[] getAssemblies()
283     {
284         Collection c = this.assemblies;
285         return (Assembly[])c.toArray(new Assembly[c.size()]);
286     }
287 
288     /*
289      * (non-Javadoc)
290      * 
291      * @see org.mule.jbi.registry.Registry#getAssembly(java.lang.String)
292      */
293     public synchronized Assembly getAssembly(String name)
294     {
295         return (Assembly)this.assembliesMap.get(name);
296     }
297 
298     /*
299      * (non-Javadoc)
300      * 
301      * @see org.mule.jbi.registry.Registry#addAssembly(java.lang.String)
302      */
303     public synchronized Assembly addAssembly(String name)
304     {
305         if (getAssembly(name) != null)
306         {
307             return null;
308         }
309         Assembly a = createAssembly(name);
310         this.assemblies.add(a);
311         this.assembliesMap.put(name, a);
312         return a;
313     }
314 
315     public synchronized void removeAssembly(Assembly assembly)
316     {
317         this.assembliesMap.remove(assembly.getName());
318         this.assemblies.remove(assembly);
319     }
320 
321     public void initialize()
322     {
323         this.componentsMap = new HashMap();
324         for (Iterator it = this.components.iterator(); it.hasNext();)
325         {
326             RegistryComponent e = (RegistryComponent)it.next();
327             this.componentsMap.put(e.getName(), e);
328         }
329         this.librariesMap = new HashMap();
330         for (Iterator it = this.libraries.iterator(); it.hasNext();)
331         {
332             AbstractLibrary l = (AbstractLibrary)it.next();
333             this.librariesMap.put(l.getName(), l);
334         }
335         this.assembliesMap = new HashMap();
336         for (Iterator it = this.assemblies.iterator(); it.hasNext();)
337         {
338             AbstractAssembly a = (AbstractAssembly)it.next();
339             this.assembliesMap.put(a.getName(), a);
340         }
341     }
342 
343     /*
344      * (non-Javadoc)
345      * 
346      * @see org.mule.jbi.registry.Registry#start()
347      */
348     public synchronized void start() throws RegistryException
349     {
350         started = true;
351         try
352         {
353             RegistryComponent[] components = getComponents();
354             for (int i = 0; i < components.length; i++)
355             {
356                 if (components[i].isTransient() && components[i].getComponent() == null)
357                 {
358                     // a transient component was removed from config, so remove it
359                     // from registry
360                     removeComponent(components[i]);
361                 }
362                 else
363                 {
364                     components[i].restoreState();
365                 }
366             }
367             Assembly[] assemblies = getAssemblies();
368             for (int i = 0; i < assemblies.length; i++)
369             {
370                 assemblies[i].restoreState();
371             }
372         }
373         catch (Exception e)
374         {
375             throw new RegistryException(e);
376         }
377     }
378 
379     /*
380      * (non-Javadoc)
381      * 
382      * @see org.mule.jbi.registry.Registry#shutDown()
383      */
384     public synchronized void shutDown() throws RegistryException
385     {
386         RegistryComponent[] components = getComponents();
387         for (int i = 0; i < components.length; i++)
388         {
389             components[i].saveAndShutdown();
390         }
391         store.save(this);
392     }
393 
394     /*
395      * (non-Javadoc)
396      * 
397      * @see org.mule.jbi.registry.Registry#addTransientUnit(java.lang.String,
398      *      java.lang.String, java.lang.String)
399      */
400     public synchronized void addTransientUnit(String suName, RegistryComponent component, String installDir)
401         throws RegistryException
402     {
403         Assembly a = getAssembly(suName);
404         if (a == null)
405         {
406             Assembly assembly = createAssembly(suName);
407             assembly.setTransient(true);
408             assembly.setStateAtShutdown(Assembly.RUNNING);
409             Unit unit = createUnit(suName);
410             unit.setName(suName);
411             unit.setAssembly(assembly);
412             unit.setRegistryComponent(component);
413             try
414             {
415                 unit.setInstallRoot(FileUtils.newFile(installDir).getAbsoluteFile().getCanonicalPath());
416             }
417             catch (IOException e)
418             {
419                 throw new RegistryException(e);
420             }
421             this.assemblies.add(assembly);
422             this.assembliesMap.put(suName, assembly);
423             unit.deploy();
424             unit.start();
425             assembly.setCurrentState(Assembly.RUNNING);
426         }
427         else
428         {
429             if (!a.isTransient())
430             {
431                 throw new RegistryException("A non-transient or assembly is already deployed: " + suName);
432             }
433         }
434     }
435 
436     public void save() throws RegistryException
437     {
438         store.save(this);
439     }
440 
441     public boolean isStarted()
442     {
443         return started;
444     }
445 
446     public ManagementContext getManagementContext()
447     {
448         return context;
449     }
450 }