View Javadoc

1   /*
2    * $Id: AbstractUnit.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.Assembly;
14  import org.mule.registry.Registry;
15  import org.mule.registry.RegistryComponent;
16  import org.mule.registry.RegistryException;
17  import org.mule.registry.Unit;
18  
19  /**
20   * @author <a href="mailto:gnt@codehaus.org">Guillaume Nodet</a>
21   */
22  public abstract class AbstractUnit extends AbstractEntry implements Unit
23  {
24  
25      private String assembly;
26  
27      protected AbstractUnit(Registry registry)
28      {
29          super(registry);
30      }
31  
32      /*
33       * (non-Javadoc)
34       * 
35       * @see org.mule.jbi.registry.Unit#getAssemblies()
36       */
37      public Assembly getAssembly()
38      {
39          return getRegistry().getAssembly(this.assembly);
40      }
41  
42      public void setAssembly(Assembly assembly)
43      {
44          this.assembly = assembly.getName();
45      }
46  
47      /*
48       * (non-Javadoc)
49       * 
50       * @see org.mule.jbi.registry.Unit#deploy()
51       */
52      public final synchronized String deploy() throws RegistryException
53      {
54          if (!getCurrentState().equals(UNKNOWN))
55          {
56              throw new RegistryException("Illegal status: " + getCurrentState());
57          }
58          String result = null;
59          try
60          {
61              result = doDeploy();
62          }
63          catch (Exception e)
64          {
65              throw new RegistryException(e);
66          }
67          // TODO: analyse result
68          getRegistryComponent().addUnit(this);
69          ((AbstractAssembly)getAssembly()).addUnit(this);
70          setCurrentState(STOPPED);
71          return result;
72      }
73  
74      public abstract String doDeploy() throws Exception;
75  
76      /*
77       * (non-Javadoc)
78       * 
79       * @see org.mule.jbi.registry.Unit#init()
80       */
81      public final synchronized void init() throws RegistryException
82      {
83          if (!getCurrentState().equals(UNKNOWN))
84          {
85              throw new RegistryException("Illegal status: " + getCurrentState());
86          }
87          try
88          {
89              doInit();
90          }
91          catch (Exception e)
92          {
93              throw new RegistryException(e);
94          }
95          setCurrentState(STOPPED);
96      }
97  
98      protected abstract void doInit() throws Exception;
99  
100     /*
101      * (non-Javadoc)
102      * 
103      * @see org.mule.jbi.registry.Unit#start()
104      */
105     public final synchronized void start() throws RegistryException
106     {
107         if (getCurrentState().equals(UNKNOWN))
108         {
109             throw new RegistryException("Illegal status: " + getCurrentState());
110         }
111         if (!getCurrentState().equals(RUNNING))
112         {
113             try
114             {
115                 doStart();
116             }
117             catch (Exception e)
118             {
119                 throw new RegistryException(e);
120             }
121             setCurrentState(RUNNING);
122         }
123     }
124 
125     protected abstract void doStart() throws Exception;
126 
127     /*
128      * (non-Javadoc)
129      * 
130      * @see org.mule.jbi.registry.Unit#stop()
131      */
132     public final synchronized void stop() throws RegistryException
133     {
134         if (getCurrentState().equals(UNKNOWN) || getCurrentState().equals(SHUTDOWN))
135         {
136             throw new RegistryException("Illegal status: " + getCurrentState());
137         }
138         if (!getCurrentState().equals(STOPPED))
139         {
140             try
141             {
142                 doStop();
143             }
144             catch (Exception e)
145             {
146                 throw new RegistryException(e);
147             }
148             setCurrentState(STOPPED);
149         }
150     }
151 
152     protected abstract void doStop() throws Exception;
153 
154     /*
155      * (non-Javadoc)
156      * 
157      * @see org.mule.jbi.registry.Unit#shutDown()
158      */
159     public final synchronized void shutDown() throws RegistryException
160     {
161         if (getCurrentState().equals(UNKNOWN))
162         {
163             throw new RegistryException("Illegal status: " + getCurrentState());
164         }
165         if (!getCurrentState().equals(SHUTDOWN))
166         {
167             stop();
168             try
169             {
170                 doShutDown();
171             }
172             catch (Exception e)
173             {
174                 throw new RegistryException(e);
175             }
176             setCurrentState(SHUTDOWN);
177         }
178     }
179 
180     protected abstract void doShutDown() throws Exception;
181 
182     /*
183      * (non-Javadoc)
184      * 
185      * @see org.mule.jbi.registry.Unit#undeploy()
186      */
187     public synchronized String undeploy() throws RegistryException
188     {
189         if (!getCurrentState().equals(SHUTDOWN))
190         {
191             throw new RegistryException("Illegal status: " + getCurrentState());
192         }
193         String result = null;
194         try
195         {
196             result = doUndeploy();
197         }
198         catch (Exception e)
199         {
200             throw new RegistryException(e);
201         }
202         // TODO: analyse result
203         getRegistryComponent().removeUnit(this);
204         ((AbstractAssembly)getAssembly()).removeUnit(this);
205         setCurrentState(UNKNOWN);
206         return result;
207     }
208 
209     protected abstract String doUndeploy() throws Exception;
210 
211     public void setRegistryComponent(RegistryComponent component)
212     {
213         // nothing to do (yet?)
214     }
215 
216 }