KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > manager > AbstractSystemManager


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.manager;
9
10 import java.util.HashMap JavaDoc;
11 import org.apache.avalon.excalibur.i18n.ResourceManager;
12 import org.apache.avalon.excalibur.i18n.Resources;
13 import org.apache.avalon.framework.activity.Disposable;
14 import org.apache.avalon.framework.activity.Initializable;
15 import org.apache.avalon.framework.activity.Startable;
16 import org.apache.avalon.framework.logger.AbstractLogEnabled;
17 import org.apache.avalon.phoenix.interfaces.ManagerException;
18 import org.apache.avalon.phoenix.interfaces.SystemManager;
19
20 /**
21  * This is abstract implementation of SystemManager.
22  *
23  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
24  */

25 public abstract class AbstractSystemManager
26     extends AbstractLogEnabled
27     implements SystemManager, Initializable, Startable, Disposable
28 {
29     private static final Resources REZ =
30         ResourceManager.getPackageResources( AbstractSystemManager.class );
31
32     private final HashMap JavaDoc m_entries = new HashMap JavaDoc();
33
34     private SubContext m_subContext;
35
36     public void initialize()
37         throws Exception JavaDoc
38     {
39         m_subContext = new SubContext( this, null, null );
40     }
41
42     /**
43      * @see SystemManager#register(String, Object, Class[])
44      */

45     public synchronized void register( final String JavaDoc name,
46                                        final Object JavaDoc object,
47                                        final Class JavaDoc[] interfaces )
48         throws ManagerException, IllegalArgumentException JavaDoc
49     {
50         if( null == interfaces )
51         {
52             final String JavaDoc message =
53                 REZ.getString( "manager.error.interfaces.null", name );
54             throw new IllegalArgumentException JavaDoc( message );
55         }
56         verifyInterfaces( object, interfaces );
57
58         doRegister( name, object, interfaces );
59     }
60
61     /**
62      * @see SystemManager#register(String, Object)
63      */

64     public synchronized void register( final String JavaDoc name,
65                                        final Object JavaDoc object )
66         throws ManagerException, IllegalArgumentException JavaDoc
67     {
68         doRegister( name, object, null );
69     }
70
71     /**
72      * @see SystemManager#unregister(String)
73      */

74     public synchronized void unregister( final String JavaDoc name )
75         throws ManagerException
76     {
77         final ManagedEntry entry = (ManagedEntry)m_entries.remove( name );
78         if( null == entry )
79         {
80             final String JavaDoc message =
81                 REZ.getString( "manager.error.unregister.noentry", name );
82             throw new ManagerException( message );
83         }
84
85         unexport( name, entry.getExportedObject() );
86     }
87
88     /**
89      * Returns the subcontext of the specified name. If it does not exist it
90      * is created.
91      *
92      * @throws ManagerException if context cannot be created or retrieved
93      * @return the subcontext with the specified name
94      */

95     public SystemManager getSubContext( String JavaDoc parent, String JavaDoc type )
96         throws ManagerException
97     {
98         return m_subContext.getSubContext( parent, type );
99     }
100
101     /**
102      * Export the object to the particular management medium using
103      * the supplied object and interfaces.
104      * This needs to be implemented by subclasses.
105      *
106      * @param name the name of object
107      * @param object the object
108      * @param interfaces the interfaces
109      * @return the exported object
110      * @throws ManagerException if an error occurs
111      */

112     protected abstract Object JavaDoc export( String JavaDoc name, Object JavaDoc object, Class JavaDoc[] interfaces )
113         throws ManagerException;
114
115     /**
116      * Stop the exported object from being managed.
117      *
118      * @param name the name of object
119      * @param exportedObject the object return by export
120      * @throws ManagerException if an error occurs
121      */

122     protected abstract void unexport( String JavaDoc name, Object JavaDoc exportedObject )
123         throws ManagerException;
124
125     /**
126      * Verfify that name is well formed.
127      *
128      * @param name the name
129      * @param object the object so named
130      */

131     protected void verifyName( final String JavaDoc name,
132                                final Object JavaDoc object )
133         throws ManagerException
134     {
135     }
136
137     /**
138      * Verify that an interface conforms to the requirements of management medium.
139      *
140      * @param clazz the interface class
141      * @throws ManagerException if verification fails
142      */

143     protected abstract void verifyInterface( Class JavaDoc clazz )
144         throws ManagerException;
145
146     /**
147      * Verify that object implements interfaces and interfaces are of "acceptable form".
148      * "Acceptable form" is determined by specific management policy.
149      *
150      * @param object the object
151      * @param interfaces the array of interfaces to check
152      * @throws ManagerException if an error occurs
153      */

154     private void verifyInterfaces( final Object JavaDoc object, final Class JavaDoc[] interfaces )
155         throws ManagerException
156     {
157         for( int i = 0; i < interfaces.length; i++ )
158         {
159             final Class JavaDoc clazz = interfaces[ i ];
160
161             if( !clazz.isInterface() )
162             {
163                 final String JavaDoc message =
164                     REZ.getString( "manager.error.verify.notinterface", clazz.getName() );
165                 throw new ManagerException( message );
166             }
167
168             if( !clazz.isInstance( object ) )
169             {
170                 final String JavaDoc message =
171                     REZ.getString( "manager.error.verify.notinstance", clazz.getName() );
172                 throw new ManagerException( message );
173             }
174
175             verifyInterface( clazz );
176         }
177     }
178
179     /**
180      * Helper method to help check before an objects registration.
181      * Verifies name and object are not null and verifies no entry exists using name.
182      *
183      * @param name the name of object
184      * @param object the object to be registered
185      * @throws ManagerException if name already exists
186      * @throws IllegalArgumentException if name or object is null
187      */

188     private void checkRegister( final String JavaDoc name, final Object JavaDoc object )
189         throws ManagerException, IllegalArgumentException JavaDoc
190     {
191         if( null == object )
192         {
193             throw new NullPointerException JavaDoc( "object" );
194         }
195
196         if( null == name )
197         {
198             throw new NullPointerException JavaDoc( "name" );
199         }
200
201         verifyName( name, object );
202
203         if( null != m_entries.get( name ) )
204         {
205             final String JavaDoc message = REZ.getString( "manager.error.register.exists", name );
206             throw new ManagerException( message );
207         }
208     }
209
210     /**
211      * Utility method that actually does the registration.
212      *
213      * @param name the name to register under
214      * @param object the object
215      * @param interfaces the interfaces (may be null)
216      * @throws ManagerException if error occurs
217      */

218     private void doRegister( final String JavaDoc name,
219                              final Object JavaDoc object,
220                              final Class JavaDoc[] interfaces )
221         throws ManagerException
222     {
223         checkRegister( name, object );
224
225         final Object JavaDoc exportedObject = export( name, object, interfaces );
226         final ManagedEntry entry =
227             new ManagedEntry( object, interfaces, exportedObject );
228         m_entries.put( name, entry );
229     }
230 }
231
Popular Tags