Coverage Report - org.mule.registry.impl.AbstractRegistryComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractRegistryComponent
0%
0/132
0%
0/19
2.394
 
 1  
 /*
 2  
  * $Id: AbstractRegistryComponent.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.registry.ClassLoaderFactory;
 14  
 import org.mule.registry.ComponentType;
 15  
 import org.mule.registry.Library;
 16  
 import org.mule.registry.Registry;
 17  
 import org.mule.registry.RegistryComponent;
 18  
 import org.mule.registry.RegistryDescriptor;
 19  
 import org.mule.registry.RegistryException;
 20  
 import org.mule.registry.Unit;
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Collection;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 
 28  
 import javax.management.ObjectName;
 29  
 
 30  
 /**
 31  
  * todo document
 32  
  * 
 33  
  * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
 34  
  * @version $Revision: 7976 $
 35  
  */
 36  
 public abstract class AbstractRegistryComponent extends AbstractEntry implements RegistryComponent
 37  
 {
 38  
 
 39  
     protected ComponentType type;
 40  
     protected String name;
 41  
     protected transient ObjectName objectName;
 42  
     protected List units;
 43  
     protected List libraries;
 44  
     protected String workspaceRoot;
 45  
     protected List classPathElements;
 46  
     protected String componentClassName;
 47  
     protected boolean isClassLoaderParentFirst;
 48  
     protected boolean isTransient;
 49  
     protected Object component;
 50  
     protected RegistryDescriptor descriptor;
 51  
 
 52  
     protected AbstractRegistryComponent(String name, ComponentType type, Registry registry)
 53  
     {
 54  0
         super(registry);
 55  0
         this.type = type;
 56  0
         this.name = name;
 57  0
         this.units = new ArrayList();
 58  0
         this.libraries = new ArrayList();
 59  0
     }
 60  
 
 61  
     public ComponentType getType()
 62  
     {
 63  0
         return type;
 64  
     }
 65  
 
 66  
     protected void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
 67  
     {
 68  0
         super.readObject(in);
 69  0
         in.defaultReadObject();
 70  0
     }
 71  
 
 72  
     /*
 73  
      * (non-Javadoc)
 74  
      * 
 75  
      * @see org.mule.jbi.registry.Component#getLibraries()
 76  
      */
 77  
     public Library[] getLibraries()
 78  
     {
 79  0
         Collection c = new ArrayList();
 80  0
         for (Iterator it = this.libraries.iterator(); it.hasNext();)
 81  
         {
 82  0
             String ref = (String)it.next();
 83  0
             Library library = getRegistry().getLibrary(ref);
 84  0
             c.add(library);
 85  
         }
 86  0
         return (Library[])c.toArray(new Library[c.size()]);
 87  
     }
 88  
 
 89  
     /*
 90  
      * (non-Javadoc)
 91  
      * 
 92  
      * @see org.mule.jbi.registry.Component#getUnits()
 93  
      */
 94  
     public Unit[] getUnits()
 95  
     {
 96  0
         Collection c = new ArrayList();
 97  0
         for (Iterator it = this.units.iterator(); it.hasNext();)
 98  
         {
 99  0
             String ref = (String)it.next();
 100  0
             String[] refs = ref.split("/");
 101  0
             if (refs.length != 2)
 102  
             {
 103  0
                 throw new IllegalStateException("Malformed unit ref");
 104  
             }
 105  0
             Unit unit = getRegistry().getAssembly(refs[0]).getUnit(refs[1]);
 106  0
             c.add(unit);
 107  
         }
 108  0
         return (Unit[])c.toArray(new Unit[c.size()]);
 109  
     }
 110  
 
 111  
     public void addUnit(Unit unit)
 112  
     {
 113  0
         this.units.add(unit.getAssembly().getName() + "/" + unit.getName());
 114  0
     }
 115  
 
 116  
     public void removeUnit(Unit unit)
 117  
     {
 118  0
         this.units.remove(unit.getAssembly().getName() + "/" + unit.getName());
 119  0
     }
 120  
 
 121  
     /*
 122  
      * (non-Javadoc)
 123  
      * 
 124  
      * @see org.mule.jbi.registry.Component#getObjectName()
 125  
      */
 126  
     public ObjectName getObjectName()
 127  
     {
 128  0
         return this.objectName;
 129  
     }
 130  
 
 131  
     /*
 132  
      * (non-Javadoc)
 133  
      * 
 134  
      * @see org.mule.jbi.registry.mule.AbstractEntry#checkDescriptor()
 135  
      */
 136  
     protected void checkDescriptor() throws RegistryException
 137  
     {
 138  0
         super.checkDescriptor();
 139  
         // Check that it is a service assembly
 140  0
         if (!getDescriptor().isComponent())
 141  
         {
 142  0
             throw new RegistryException("component should be set");
 143  
         }
 144  0
     }
 145  
 
 146  
     protected void createComponent() throws RegistryException
 147  
     {
 148  
         try
 149  
         {
 150  0
             ClassLoader loader = ClassLoaderFactory.getInstance().createComponentClassLoader(this);
 151  0
             Class cl = Class.forName(this.componentClassName, true, loader);
 152  0
             this.component = cl.newInstance();
 153  
         }
 154  0
         catch (Exception e)
 155  
         {
 156  0
             throw new RegistryException(e);
 157  0
         }
 158  0
     }
 159  
 
 160  
     /*
 161  
      * (non-Javadoc)
 162  
      * 
 163  
      * @see org.mule.jbi.registry.Component#install()
 164  
      */
 165  
     public synchronized void install() throws RegistryException
 166  
     {
 167  0
         if (!getCurrentState().equals(UNKNOWN))
 168  
         {
 169  0
             throw new RegistryException("Illegal status: " + getCurrentState());
 170  
         }
 171  0
         if (isTransient)
 172  
         {
 173  0
             return;
 174  
         }
 175  
 
 176  
         try
 177  
         {
 178  0
             doInstall();
 179  
         }
 180  0
         catch (Exception e)
 181  
         {
 182  0
             throw new RegistryException(e);
 183  0
         }
 184  
 
 185  
         // Create component
 186  0
         createComponent();
 187  
         try
 188  
         {
 189  0
             objectName = initComponent();
 190  
         }
 191  0
         catch (Exception e)
 192  
         {
 193  0
             throw new RegistryException(e);
 194  0
         }
 195  0
     }
 196  
 
 197  
     protected abstract void doInstall() throws Exception;
 198  
 
 199  
     /*
 200  
      * (non-Javadoc)
 201  
      * 
 202  
      * @see org.mule.jbi.registry.Component#restoreState(org.mule.jbi.JbiContainer)
 203  
      */
 204  
     public final synchronized void restoreState() throws RegistryException
 205  
     {
 206  0
         if (!getCurrentState().equals(UNKNOWN) && !getCurrentState().equals(INITIALIZED))
 207  
         {
 208  0
             throw new RegistryException("Illegal status: " + getCurrentState());
 209  
         }
 210  
         try
 211  
         {
 212  0
             if (!isTransient)
 213  
             {
 214  0
                 createComponent();
 215  0
                 initComponent();
 216  
             }
 217  0
             if (getStateAtShutdown().equals(RUNNING))
 218  
             {
 219  0
                 start();
 220  
             }
 221  0
             doRestoreState();
 222  
         }
 223  0
         catch (Exception e)
 224  
         {
 225  0
             throw new RegistryException(e);
 226  0
         }
 227  0
     }
 228  
 
 229  
     protected abstract void doRestoreState() throws Exception;
 230  
 
 231  
     /*
 232  
      * (non-Javadoc)
 233  
      * 
 234  
      * @see org.mule.jbi.registry.Component#saveAndShutdown()
 235  
      */
 236  
     public synchronized void saveAndShutdown() throws RegistryException
 237  
     {
 238  0
         setStateAtShutdown(getCurrentState());
 239  0
         Unit[] units = getUnits();
 240  0
         for (int i = 0; i < units.length; i++)
 241  
         {
 242  0
             units[i].setStateAtShutdown(units[i].getCurrentState());
 243  
         }
 244  0
         shutDown();
 245  0
     }
 246  
 
 247  
     /*
 248  
      * (non-Javadoc)
 249  
      * 
 250  
      * @see org.mule.jbi.registry.Component#start()
 251  
      */
 252  
     public final synchronized void start() throws RegistryException
 253  
     {
 254  0
         if (getCurrentState().equals(UNKNOWN))
 255  
         {
 256  0
             throw new RegistryException("Illegal status: " + getCurrentState());
 257  
         }
 258  0
         if (getCurrentState().equals(RUNNING))
 259  
         {
 260  0
             return;
 261  
         }
 262  
         try
 263  
         {
 264  0
             doStart();
 265  
         }
 266  0
         catch (Exception e)
 267  
         {
 268  0
             throw new RegistryException(e);
 269  0
         }
 270  0
         setCurrentState(RUNNING);
 271  0
     }
 272  
 
 273  
     protected abstract void doStart() throws Exception;
 274  
 
 275  
     /*
 276  
      * (non-Javadoc)
 277  
      * 
 278  
      * @see org.mule.jbi.registry.Component#stop()
 279  
      */
 280  
     public final synchronized void stop() throws RegistryException
 281  
     {
 282  0
         if (getCurrentState().equals(UNKNOWN) || getCurrentState().equals(SHUTDOWN))
 283  
         {
 284  0
             throw new RegistryException("Illegal status: " + getCurrentState());
 285  
         }
 286  0
         if (getCurrentState().equals(STOPPED))
 287  
         {
 288  0
             return;
 289  
         }
 290  
         try
 291  
         {
 292  0
             doStop();
 293  
         }
 294  0
         catch (Exception e)
 295  
         {
 296  0
             throw new RegistryException(e);
 297  0
         }
 298  0
         setCurrentState(STOPPED);
 299  0
     }
 300  
 
 301  
     protected abstract void doStop() throws Exception;
 302  
 
 303  
     /*
 304  
      * (non-Javadoc)
 305  
      * 
 306  
      * @see org.mule.jbi.registry.Component#shutDown()
 307  
      */
 308  
     public final synchronized void shutDown() throws RegistryException
 309  
     {
 310  0
         if (getCurrentState().equals(UNKNOWN))
 311  
         {
 312  0
             throw new RegistryException("Illegal status: " + getCurrentState());
 313  
         }
 314  0
         if (getCurrentState().equals(SHUTDOWN))
 315  
         {
 316  0
             return;
 317  
         }
 318  0
         stop();
 319  
         // TODO: unregister mbean
 320  
         try
 321  
         {
 322  0
             doShutDown();
 323  
         }
 324  0
         catch (Exception e)
 325  
         {
 326  0
             throw new RegistryException(e);
 327  0
         }
 328  0
         setCurrentState(SHUTDOWN);
 329  0
     }
 330  
 
 331  
     protected abstract void doShutDown() throws Exception;
 332  
 
 333  
     /*
 334  
      * (non-Javadoc)
 335  
      * 
 336  
      * @see org.mule.jbi.registry.Component#uninstall()
 337  
      */
 338  
     public synchronized void uninstall() throws RegistryException
 339  
     {
 340  0
         if (!getCurrentState().equals(SHUTDOWN) && !getCurrentState().equals(UNKNOWN))
 341  
         {
 342  0
             throw new RegistryException("Illegal status: " + getCurrentState());
 343  
         }
 344  0
         if (this.units.size() > 0)
 345  
         {
 346  0
             throw new RegistryException("Component has service units deployed");
 347  
         }
 348  0
         Library[] libraries = getLibraries();
 349  0
         for (int i = 0; i < libraries.length; i++)
 350  
         {
 351  0
             libraries[i].removeComponent(this);
 352  
         }
 353  
         // Remove directories
 354  0
         registry.getManagementContext().deleteDir(getInstallRoot());
 355  0
         registry.getManagementContext().deleteDir(getWorkspaceRoot());
 356  
         // Remove component from registry
 357  0
         getRegistry().removeComponent(this);
 358  0
         setCurrentState(UNKNOWN);
 359  0
     }
 360  
 
 361  
     /*
 362  
      * (non-Javadoc)
 363  
      * 
 364  
      * @see org.mule.jbi.registry.Component#getWorkspaceRoot()
 365  
      */
 366  
     public String getWorkspaceRoot()
 367  
     {
 368  0
         return this.workspaceRoot;
 369  
     }
 370  
 
 371  
     /*
 372  
      * (non-Javadoc)
 373  
      * 
 374  
      * @see org.mule.jbi.registry.Component#setWorkspaceRoot(java.lang.String)
 375  
      */
 376  
     public void setWorkspaceRoot(String workspaceRoot)
 377  
     {
 378  0
         this.workspaceRoot = workspaceRoot;
 379  0
     }
 380  
 
 381  
     /*
 382  
      * (non-Javadoc)
 383  
      * 
 384  
      * @see org.mule.jbi.registry.Component#getClassPathElements()
 385  
      */
 386  
     public List getClassPathElements()
 387  
     {
 388  0
         return this.classPathElements;
 389  
     }
 390  
 
 391  
     /*
 392  
      * (non-Javadoc)
 393  
      * 
 394  
      * @see org.mule.jbi.registry.Component#setClassPathElements(java.util.List)
 395  
      */
 396  
     public void setClassPathElements(List classPathElements)
 397  
     {
 398  0
         this.classPathElements = classPathElements;
 399  0
     }
 400  
 
 401  
     /*
 402  
      * (non-Javadoc)
 403  
      * 
 404  
      * @see org.mule.jbi.registry.Component#isClassLoaderParentFirst()
 405  
      */
 406  
     public boolean isClassLoaderParentFirst()
 407  
     {
 408  0
         return this.isClassLoaderParentFirst;
 409  
     }
 410  
 
 411  
     public void setComponent(Object component)
 412  
     {
 413  0
         this.component = component;
 414  0
     }
 415  
 
 416  
     public boolean isTransient()
 417  
     {
 418  0
         return isTransient;
 419  
     }
 420  
 
 421  
     public void setTransient(boolean isTransient)
 422  
     {
 423  0
         this.isTransient = isTransient;
 424  0
     }
 425  
 
 426  
     /**
 427  
      * Return the component implementation.
 428  
      * 
 429  
      * @return
 430  
      */
 431  
     public Object getComponent()
 432  
     {
 433  0
         return component;
 434  
     }
 435  
 
 436  
     /**
 437  
      * Return the descriptor for this component.
 438  
      * 
 439  
      * @return
 440  
      */
 441  
     public RegistryDescriptor getDescriptor() throws RegistryException
 442  
     {
 443  0
         return descriptor;
 444  
     }
 445  
 
 446  
     public void setDescriptor(RegistryDescriptor descriptor) throws RegistryException
 447  
     {
 448  0
         this.descriptor = descriptor;
 449  0
     }
 450  
 }