Coverage Report - org.mule.registry.impl.AbstractRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractRegistry
0%
0/149
0%
0/19
2.214
 
 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  0
     private transient boolean started = false;
 52  
 
 53  
     public AbstractRegistry(RegistryStore store, ManagementContext context)
 54  0
     {
 55  0
         this.store = store;
 56  0
         this.context = context;
 57  0
         registry = new HashMap();
 58  0
         for (int i = 0; i < ComponentType.COMPONENT_TYPES.length; i++)
 59  
         {
 60  0
             ComponentType componentType = ComponentType.COMPONENT_TYPES[i];
 61  0
             registry.put(componentType.getName() + ".list", new ArrayList());
 62  0
             registry.put(componentType.getName() + ".map", new HashMap());
 63  
         }
 64  
 
 65  0
         this.components = new ArrayList();
 66  0
         this.libraries = new ArrayList();
 67  0
         this.assemblies = new ArrayList();
 68  0
         this.librariesMap = new HashMap();
 69  0
         this.assembliesMap = new HashMap();
 70  0
         this.componentsMap = new HashMap();
 71  
 
 72  0
         workingDirectory = FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory(), REGISTRY_DIRECTORY);
 73  0
     }
 74  
 
 75  
     public File getWorkingDirectory()
 76  
     {
 77  0
         return workingDirectory;
 78  
     }
 79  
 
 80  
     public void setWorkingDirectory(File workingDirectory)
 81  
     {
 82  0
         this.workingDirectory = workingDirectory;
 83  0
     }
 84  
 
 85  
     public String getStoreLocation()
 86  
     {
 87  0
         return this.storeLocation;
 88  
     }
 89  
 
 90  
     public void setStoreLocation(String storeLocation)
 91  
     {
 92  0
         this.storeLocation = storeLocation;
 93  0
     }
 94  
 
 95  
     /*
 96  
      * (non-Javadoc)
 97  
      * 
 98  
      * @see org.mule.jbi.registry.Registry#getComponents()
 99  
      */
 100  
     public synchronized RegistryComponent[] getComponents()
 101  
     {
 102  0
         return (RegistryComponent[])components.toArray(new RegistryComponent[components.size()]);
 103  
     }
 104  
 
 105  
     public synchronized RegistryComponent[] getComponents(ComponentType type)
 106  
     {
 107  0
         RegistryComponent[] components = new RegistryComponent[]{};
 108  0
         List list = (List)registry.get(type + ".list");
 109  0
         if (list != null)
 110  
         {
 111  0
             components = (RegistryComponent[])list.toArray(new RegistryComponent[list.size()]);
 112  
         }
 113  0
         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  0
         return (RegistryComponent)this.componentsMap.get(name);
 124  
     }
 125  
 
 126  
     public synchronized void removeComponent(RegistryComponent component)
 127  
     {
 128  0
         this.componentsMap.remove(component.getName());
 129  0
         List list = (List)registry.get(component.getType() + ".list");
 130  0
         if (list != null)
 131  
         {
 132  0
             list.remove(component);
 133  
         }
 134  0
         Map map = (Map)registry.get(component.getType() + ".map");
 135  0
         if (map != null)
 136  
         {
 137  0
             map.remove(component);
 138  
         }
 139  0
     }
 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  0
         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  0
         if (getComponent(name) != null)
 160  
         {
 161  0
             throw new RegistryException("Component already registered: " + name);
 162  
         }
 163  0
         RegistryComponent rc = createComponent(name, type);
 164  0
         this.componentsMap.put(name, rc);
 165  0
         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  0
         RegistryComponent rc = getComponent(name);
 181  0
         if (rc == null)
 182  
         {
 183  0
             rc = createComponent(name, type);
 184  0
             rc.setTransient(true);
 185  0
             rc.setComponent(component);
 186  0
             rc.setStateAtShutdown(RegistryComponent.RUNNING);
 187  
             try
 188  
             {
 189  0
                 rc.setWorkspaceRoot(context.getComponentWorkspaceDir(getWorkingDirectory(), name)
 190  
                     .getAbsoluteFile()
 191  
                     .getCanonicalPath());
 192  
             }
 193  0
             catch (IOException e)
 194  
             {
 195  0
                 throw new RegistryException(e);
 196  0
             }
 197  0
             this.componentsMap.put(name, rc);
 198  0
             components.add(rc);
 199  0
             if (bootstrap != null)
 200  
             {
 201  
                 try
 202  
                 {
 203  0
                     bootstrapComponent(rc, bootstrap);
 204  
                 }
 205  0
                 catch (Exception e)
 206  
                 {
 207  0
                     throw new RegistryException(e);
 208  0
                 }
 209  
             }
 210  
         }
 211  
         else
 212  
         {
 213  0
             if (!rc.isTransient())
 214  
             {
 215  0
                 throw new RegistryException("A non-transient component is already registered: " + name);
 216  
             }
 217  0
             rc.setComponent(component);
 218  
         }
 219  
         try
 220  
         {
 221  0
             rc.initComponent();
 222  
         }
 223  0
         catch (Exception e)
 224  
         {
 225  0
             throw new RegistryException(e);
 226  0
         }
 227  0
         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  0
         Collection c = this.libraries;
 241  0
         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  0
         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  0
         if (getLibrary(name) != null)
 262  
         {
 263  0
             throw new RegistryException("Library already registered: " + name);
 264  
         }
 265  0
         Library l = createLibrary(name);
 266  0
         this.libraries.add(l);
 267  0
         this.librariesMap.put(name, l);
 268  0
         return l;
 269  
     }
 270  
 
 271  
     public synchronized void removeLibrary(Library library)
 272  
     {
 273  0
         this.librariesMap.remove(library.getName());
 274  0
         this.libraries.remove(library);
 275  0
     }
 276  
 
 277  
     /*
 278  
      * (non-Javadoc)
 279  
      * 
 280  
      * @see org.mule.jbi.registry.Registry#getAssemblies()
 281  
      */
 282  
     public synchronized Assembly[] getAssemblies()
 283  
     {
 284  0
         Collection c = this.assemblies;
 285  0
         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  0
         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  0
         if (getAssembly(name) != null)
 306  
         {
 307  0
             return null;
 308  
         }
 309  0
         Assembly a = createAssembly(name);
 310  0
         this.assemblies.add(a);
 311  0
         this.assembliesMap.put(name, a);
 312  0
         return a;
 313  
     }
 314  
 
 315  
     public synchronized void removeAssembly(Assembly assembly)
 316  
     {
 317  0
         this.assembliesMap.remove(assembly.getName());
 318  0
         this.assemblies.remove(assembly);
 319  0
     }
 320  
 
 321  
     public void initialize()
 322  
     {
 323  0
         this.componentsMap = new HashMap();
 324  0
         for (Iterator it = this.components.iterator(); it.hasNext();)
 325  
         {
 326  0
             RegistryComponent e = (RegistryComponent)it.next();
 327  0
             this.componentsMap.put(e.getName(), e);
 328  
         }
 329  0
         this.librariesMap = new HashMap();
 330  0
         for (Iterator it = this.libraries.iterator(); it.hasNext();)
 331  
         {
 332  0
             AbstractLibrary l = (AbstractLibrary)it.next();
 333  0
             this.librariesMap.put(l.getName(), l);
 334  
         }
 335  0
         this.assembliesMap = new HashMap();
 336  0
         for (Iterator it = this.assemblies.iterator(); it.hasNext();)
 337  
         {
 338  0
             AbstractAssembly a = (AbstractAssembly)it.next();
 339  0
             this.assembliesMap.put(a.getName(), a);
 340  
         }
 341  0
     }
 342  
 
 343  
     /*
 344  
      * (non-Javadoc)
 345  
      * 
 346  
      * @see org.mule.jbi.registry.Registry#start()
 347  
      */
 348  
     public synchronized void start() throws RegistryException
 349  
     {
 350  0
         started = true;
 351  
         try
 352  
         {
 353  0
             RegistryComponent[] components = getComponents();
 354  0
             for (int i = 0; i < components.length; i++)
 355  
             {
 356  0
                 if (components[i].isTransient() && components[i].getComponent() == null)
 357  
                 {
 358  
                     // a transient component was removed from config, so remove it
 359  
                     // from registry
 360  0
                     removeComponent(components[i]);
 361  
                 }
 362  
                 else
 363  
                 {
 364  0
                     components[i].restoreState();
 365  
                 }
 366  
             }
 367  0
             Assembly[] assemblies = getAssemblies();
 368  0
             for (int i = 0; i < assemblies.length; i++)
 369  
             {
 370  0
                 assemblies[i].restoreState();
 371  
             }
 372  
         }
 373  0
         catch (Exception e)
 374  
         {
 375  0
             throw new RegistryException(e);
 376  0
         }
 377  0
     }
 378  
 
 379  
     /*
 380  
      * (non-Javadoc)
 381  
      * 
 382  
      * @see org.mule.jbi.registry.Registry#shutDown()
 383  
      */
 384  
     public synchronized void shutDown() throws RegistryException
 385  
     {
 386  0
         RegistryComponent[] components = getComponents();
 387  0
         for (int i = 0; i < components.length; i++)
 388  
         {
 389  0
             components[i].saveAndShutdown();
 390  
         }
 391  0
         store.save(this);
 392  0
     }
 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  0
         Assembly a = getAssembly(suName);
 404  0
         if (a == null)
 405  
         {
 406  0
             Assembly assembly = createAssembly(suName);
 407  0
             assembly.setTransient(true);
 408  0
             assembly.setStateAtShutdown(Assembly.RUNNING);
 409  0
             Unit unit = createUnit(suName);
 410  0
             unit.setName(suName);
 411  0
             unit.setAssembly(assembly);
 412  0
             unit.setRegistryComponent(component);
 413  
             try
 414  
             {
 415  0
                 unit.setInstallRoot(FileUtils.newFile(installDir).getAbsoluteFile().getCanonicalPath());
 416  
             }
 417  0
             catch (IOException e)
 418  
             {
 419  0
                 throw new RegistryException(e);
 420  0
             }
 421  0
             this.assemblies.add(assembly);
 422  0
             this.assembliesMap.put(suName, assembly);
 423  0
             unit.deploy();
 424  0
             unit.start();
 425  0
             assembly.setCurrentState(Assembly.RUNNING);
 426  
         }
 427  
         else
 428  
         {
 429  0
             if (!a.isTransient())
 430  
             {
 431  0
                 throw new RegistryException("A non-transient or assembly is already deployed: " + suName);
 432  
             }
 433  
         }
 434  0
     }
 435  
 
 436  
     public void save() throws RegistryException
 437  
     {
 438  0
         store.save(this);
 439  0
     }
 440  
 
 441  
     public boolean isStarted()
 442  
     {
 443  0
         return started;
 444  
     }
 445  
 
 446  
     public ManagementContext getManagementContext()
 447  
     {
 448  0
         return context;
 449  
     }
 450  
 }