View Javadoc

1   /*
2    * $Id: AbstractAssembly.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.RegistryDescriptor;
16  import org.mule.registry.RegistryException;
17  import org.mule.registry.Unit;
18  import org.mule.util.FileUtils;
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  public abstract class AbstractAssembly extends AbstractEntry implements Assembly
26  {
27  
28      protected List units;
29      protected boolean isTransient;
30      protected RegistryDescriptor descriptor;
31  
32      protected AbstractAssembly(Registry registry)
33      {
34          super(registry);
35          this.units = new ArrayList();
36      }
37  
38      public Unit getUnit(String name)
39      {
40          for (Iterator it = this.units.iterator(); it.hasNext();)
41          {
42              AbstractUnit u = (AbstractUnit)it.next();
43              if (u.getName().equals(name))
44              {
45                  return u;
46              }
47          }
48          return null;
49      }
50  
51      public void addUnit(Unit unit)
52      {
53          this.units.add(unit);
54      }
55  
56      public void removeUnit(Unit unit)
57      {
58          this.units.remove(unit);
59      }
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see org.mule.jbi.registry.Assembly#getUnits()
65       */
66      public Unit[] getUnits()
67      {
68          Collection c = this.units;
69          return (Unit[])c.toArray(new Unit[c.size()]);
70      }
71  
72      /*
73       * (non-Javadoc)
74       * 
75       * @see org.mule.jbi.registry.mule.AbstractEntry#checkDescriptor()
76       */
77      protected void checkDescriptor() throws RegistryException
78      {
79          super.checkDescriptor();
80          // Check that it is a service assembly
81          if (!getDescriptor().isServiceAssembly())
82          {
83              throw new RegistryException("service-assembly should be set");
84          }
85      }
86  
87      /*
88       * (non-Javadoc)
89       * 
90       * @see org.mule.jbi.registry.Assembly#start()
91       */
92      public synchronized String start() throws RegistryException
93      {
94          if (getCurrentState().equals(UNKNOWN))
95          {
96              throw new RegistryException("Illegal status: " + getCurrentState());
97          }
98          if (!getCurrentState().equals(RUNNING))
99          {
100             Unit[] units = getUnits();
101             for (int i = 0; i < units.length; i++)
102             {
103                 units[i].start();
104             }
105             setCurrentState(RUNNING);
106         }
107         // TODO
108         return "";
109     }
110 
111     /*
112      * (non-Javadoc)
113      * 
114      * @see org.mule.jbi.registry.Assembly#stop()
115      */
116     public synchronized String stop() throws RegistryException
117     {
118         if (getCurrentState().equals(UNKNOWN) || getCurrentState().equals(SHUTDOWN))
119         {
120             throw new RegistryException("Illegal status: " + getCurrentState());
121         }
122         if (!getCurrentState().equals(STOPPED))
123         {
124             Unit[] units = getUnits();
125             for (int i = 0; i < units.length; i++)
126             {
127                 units[i].stop();
128             }
129             setCurrentState(STOPPED);
130         }
131         // TODO
132         return "";
133     }
134 
135     /*
136      * (non-Javadoc)
137      * 
138      * @see org.mule.jbi.registry.Assembly#shutDown()
139      */
140     public synchronized String shutDown() throws RegistryException
141     {
142         if (getCurrentState().equals(UNKNOWN))
143         {
144             throw new RegistryException("Illegal status: " + getCurrentState());
145         }
146         if (!getCurrentState().equals(SHUTDOWN))
147         {
148             stop();
149             Unit[] units = getUnits();
150             for (int i = 0; i < units.length; i++)
151             {
152                 units[i].shutDown();
153             }
154             setCurrentState(SHUTDOWN);
155         }
156         // TODO
157         return "";
158     }
159 
160     /*
161      * (non-Javadoc)
162      * 
163      * @see org.mule.jbi.registry.Assembly#undeploy()
164      */
165     public synchronized String undeploy() throws RegistryException
166     {
167         if (!getCurrentState().equals(SHUTDOWN) && !getCurrentState().equals(UNKNOWN))
168         {
169             throw new RegistryException("Illegal status: " + getCurrentState());
170         }
171         Unit[] units = getUnits();
172         for (int i = 0; i < units.length; i++)
173         {
174             units[i].undeploy();
175             // TODO: read output from undeploy() to analyse result
176         }
177         FileUtils.deleteTree(FileUtils.newFile(getInstallRoot()));
178         getRegistry().removeAssembly(this);
179         setCurrentState(UNKNOWN);
180         // TODO: return info
181         return null;
182     }
183 
184     /*
185      * (non-Javadoc)
186      * 
187      * @see org.mule.jbi.registry.Assembly#isTransient()
188      */
189     public boolean isTransient()
190     {
191         return isTransient;
192     }
193 
194     public void setTransient(boolean isTransient)
195     {
196         this.isTransient = isTransient;
197     }
198 
199     /*
200      * (non-Javadoc)
201      * 
202      * @see org.mule.jbi.registry.Assembly#restoreState()
203      */
204     public void restoreState() throws RegistryException
205     {
206         Unit[] units = getUnits();
207         for (int i = 0; i < units.length; i++)
208         {
209             units[i].init();
210             if (units[i].getStateAtShutdown().equals(Unit.RUNNING))
211             {
212                 units[i].start();
213             }
214             else if (units[i].getStateAtShutdown().equals(Unit.SHUTDOWN))
215             {
216                 units[i].shutDown();
217             }
218         }
219     }
220 
221     /*
222      * (non-Javadoc)
223      * 
224      * @see org.mule.jbi.registry.Assembly#saveAndShutdown()
225      */
226     public void saveAndShutdown() throws RegistryException
227     {
228         Unit[] units = getUnits();
229         for (int i = 0; i < units.length; i++)
230         {
231             units[i].setStateAtShutdown(units[i].getCurrentState());
232             units[i].shutDown();
233         }
234     }
235 
236     public void setDescriptor(RegistryDescriptor descriptor)
237     {
238         this.descriptor = descriptor;
239     }
240 
241 }