KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > registry > impl > AbstractRegistryComponent


1 /*
2  * $Id: AbstractRegistryComponent.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 javax.management.ObjectName JavaDoc;
23
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * todo document
32  *
33  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
34  * @version $Revision: 3798 $
35  */

36 public abstract class AbstractRegistryComponent extends AbstractEntry implements RegistryComponent
37 {
38
39     protected ComponentType type;
40     protected String JavaDoc name;
41     protected transient ObjectName JavaDoc objectName;
42     protected List JavaDoc units;
43     protected List JavaDoc libraries;
44     protected String JavaDoc workspaceRoot;
45     protected List JavaDoc classPathElements;
46     protected String JavaDoc componentClassName;
47     protected boolean isClassLoaderParentFirst;
48     protected boolean isTransient;
49     protected Object JavaDoc component;
50     protected RegistryDescriptor descriptor;
51
52     protected AbstractRegistryComponent(String JavaDoc name, ComponentType type, Registry registry)
53     {
54         super(registry);
55         this.type = type;
56         this.name = name;
57         this.units = new ArrayList JavaDoc();
58         this.libraries = new ArrayList JavaDoc();
59     }
60
61     public ComponentType getType()
62     {
63         return type;
64     }
65
66     protected void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
67     {
68         super.readObject(in);
69         in.defaultReadObject();
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.mule.jbi.registry.Component#getLibraries()
76      */

77     public Library[] getLibraries()
78     {
79         Collection JavaDoc c = new ArrayList JavaDoc();
80         for (Iterator JavaDoc it = this.libraries.iterator(); it.hasNext();)
81         {
82             String JavaDoc ref = (String JavaDoc)it.next();
83             Library library = getRegistry().getLibrary(ref);
84             c.add(library);
85         }
86         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         Collection JavaDoc c = new ArrayList JavaDoc();
97         for (Iterator JavaDoc it = this.units.iterator(); it.hasNext();)
98         {
99             String JavaDoc ref = (String JavaDoc)it.next();
100             String JavaDoc[] refs = ref.split("/");
101             if (refs.length != 2)
102             {
103                 throw new IllegalStateException JavaDoc("Malformed unit ref");
104             }
105             Unit unit = getRegistry().getAssembly(refs[0]).getUnit(refs[1]);
106             c.add(unit);
107         }
108         return (Unit[])c.toArray(new Unit[c.size()]);
109     }
110
111     public void addUnit(Unit unit)
112     {
113         this.units.add(unit.getAssembly().getName() + "/" + unit.getName());
114     }
115
116     public void removeUnit(Unit unit)
117     {
118         this.units.remove(unit.getAssembly().getName() + "/" + unit.getName());
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.mule.jbi.registry.Component#getObjectName()
125      */

126     public ObjectName JavaDoc getObjectName()
127     {
128         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         super.checkDescriptor();
139         // Check that it is a service assembly
140
if (!getDescriptor().isComponent())
141         {
142             throw new RegistryException("component should be set");
143         }
144     }
145
146     protected void createComponent() throws RegistryException
147     {
148         try
149         {
150             ClassLoader JavaDoc loader = ClassLoaderFactory.getInstance().createComponentClassLoader(this);
151             Class JavaDoc cl = Class.forName(this.componentClassName, true, loader);
152             this.component = cl.newInstance();
153         }
154         catch (Exception JavaDoc e)
155         {
156             throw new RegistryException(e);
157         }
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see org.mule.jbi.registry.Component#install()
164      */

165     public synchronized void install() throws RegistryException
166     {
167         if (!getCurrentState().equals(UNKNOWN))
168         {
169             throw new RegistryException("Illegal status: " + getCurrentState());
170         }
171         if (isTransient)
172         {
173             return;
174         }
175
176         try
177         {
178             doInstall();
179         }
180         catch (Exception JavaDoc e)
181         {
182             throw new RegistryException(e);
183         }
184
185         // Create component
186
createComponent();
187         try
188         {
189             objectName = initComponent();
190         }
191         catch (Exception JavaDoc e)
192         {
193             throw new RegistryException(e);
194         }
195     }
196
197     protected abstract void doInstall() throws Exception JavaDoc;
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         if (!getCurrentState().equals(UNKNOWN) && !getCurrentState().equals(INITIALIZED))
207         {
208             throw new RegistryException("Illegal status: " + getCurrentState());
209         }
210         try
211         {
212             if (!isTransient)
213             {
214                 createComponent();
215                 initComponent();
216             }
217             if (getStateAtShutdown().equals(RUNNING))
218             {
219                 start();
220             }
221             doRestoreState();
222         }
223         catch (Exception JavaDoc e)
224         {
225             throw new RegistryException(e);
226         }
227     }
228
229     protected abstract void doRestoreState() throws Exception JavaDoc;
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see org.mule.jbi.registry.Component#saveAndShutdown()
235      */

236     public synchronized void saveAndShutdown() throws RegistryException
237     {
238         setStateAtShutdown(getCurrentState());
239         Unit[] units = getUnits();
240         for (int i = 0; i < units.length; i++)
241         {
242             units[i].setStateAtShutdown(units[i].getCurrentState());
243         }
244         shutDown();
245     }
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         if (getCurrentState().equals(UNKNOWN))
255         {
256             throw new RegistryException("Illegal status: " + getCurrentState());
257         }
258         if (getCurrentState().equals(RUNNING))
259         {
260             return;
261         }
262         try
263         {
264             doStart();
265         }
266         catch (Exception JavaDoc e)
267         {
268             throw new RegistryException(e);
269         }
270         setCurrentState(RUNNING);
271     }
272
273     protected abstract void doStart() throws Exception JavaDoc;
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         if (getCurrentState().equals(UNKNOWN) || getCurrentState().equals(SHUTDOWN))
283         {
284             throw new RegistryException("Illegal status: " + getCurrentState());
285         }
286         if (getCurrentState().equals(STOPPED))
287         {
288             return;
289         }
290         try
291         {
292             doStop();
293         }
294         catch (Exception JavaDoc e)
295         {
296             throw new RegistryException(e);
297         }
298         setCurrentState(STOPPED);
299     }
300
301     protected abstract void doStop() throws Exception JavaDoc;
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         if (getCurrentState().equals(UNKNOWN))
311         {
312             throw new RegistryException("Illegal status: " + getCurrentState());
313         }
314         if (getCurrentState().equals(SHUTDOWN))
315         {
316             return;
317         }
318         stop();
319         // TODO: unregister mbean
320
try
321         {
322             doShutDown();
323         }
324         catch (Exception JavaDoc e)
325         {
326             throw new RegistryException(e);
327         }
328         setCurrentState(SHUTDOWN);
329     }
330
331     protected abstract void doShutDown() throws Exception JavaDoc;
332
333     /*
334      * (non-Javadoc)
335      *
336      * @see org.mule.jbi.registry.Component#uninstall()
337      */

338     public synchronized void uninstall() throws RegistryException
339     {
340         if (!getCurrentState().equals(SHUTDOWN) && !getCurrentState().equals(UNKNOWN))
341         {
342             throw new RegistryException("Illegal status: " + getCurrentState());
343         }
344         if (this.units.size() > 0)
345         {
346             throw new RegistryException("Component has service units deployed");
347         }
348         Library[] libraries = getLibraries();
349         for (int i = 0; i < libraries.length; i++)
350         {
351             libraries[i].removeComponent(this);
352         }
353         // Remove directories
354
registry.getManagementContext().deleteDir(getInstallRoot());
355         registry.getManagementContext().deleteDir(getWorkspaceRoot());
356         // Remove component from registry
357
getRegistry().removeComponent(this);
358         setCurrentState(UNKNOWN);
359     }
360
361     /*
362      * (non-Javadoc)
363      *
364      * @see org.mule.jbi.registry.Component#getWorkspaceRoot()
365      */

366     public String JavaDoc getWorkspaceRoot()
367     {
368         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 JavaDoc workspaceRoot)
377     {
378         this.workspaceRoot = workspaceRoot;
379     }
380
381     /*
382      * (non-Javadoc)
383      *
384      * @see org.mule.jbi.registry.Component#getClassPathElements()
385      */

386     public List JavaDoc getClassPathElements()
387     {
388         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 JavaDoc classPathElements)
397     {
398         this.classPathElements = classPathElements;
399     }
400
401     /*
402      * (non-Javadoc)
403      *
404      * @see org.mule.jbi.registry.Component#isClassLoaderParentFirst()
405      */

406     public boolean isClassLoaderParentFirst()
407     {
408         return this.isClassLoaderParentFirst;
409     }
410
411     public void setComponent(Object JavaDoc component)
412     {
413         this.component = component;
414     }
415
416     public boolean isTransient()
417     {
418         return isTransient;
419     }
420
421     public void setTransient(boolean isTransient)
422     {
423         this.isTransient = isTransient;
424     }
425
426     /**
427      * Return the component implementation.
428      *
429      * @return
430      */

431     public Object JavaDoc getComponent()
432     {
433         return component;
434     }
435
436     /**
437      * Return the descriptor for this component.
438      *
439      * @return
440      */

441     public RegistryDescriptor getDescriptor() throws RegistryException
442     {
443         return descriptor;
444     }
445
446     public void setDescriptor(RegistryDescriptor descriptor) throws RegistryException
447     {
448         this.descriptor = descriptor;
449     }
450 }
451
Popular Tags