KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > application > ExportHelper


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.application;
9
10 import org.apache.avalon.excalibur.i18n.ResourceManager;
11 import org.apache.avalon.excalibur.i18n.Resources;
12 import org.apache.avalon.framework.CascadingException;
13 import org.apache.avalon.framework.logger.AbstractLogEnabled;
14 import org.apache.avalon.phoenix.interfaces.ApplicationContext;
15 import org.apache.avalon.phoenix.metadata.BlockMetaData;
16 import org.apache.avalon.phoenix.metainfo.ServiceDescriptor;
17
18 /**
19  * Utility class to help with exporting Blocks to management subsystem.
20  *
21  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
22  * @version $Revision: 1.3 $ $Date: 2002/08/06 11:57:39 $
23  */

24 class ExportHelper
25     extends AbstractLogEnabled
26 {
27     private static final Resources REZ =
28         ResourceManager.getPackageResources( ExportHelper.class );
29
30     /**
31      * Export the services of block, declared to be management
32      * services, into management system.
33      */

34     void exportBlock( final ApplicationContext context,
35                       final BlockMetaData metaData,
36                       final Object JavaDoc block )
37         throws CascadingException
38     {
39         final ServiceDescriptor[] services = metaData.getBlockInfo().getManagementAccessPoints();
40         final String JavaDoc name = metaData.getName();
41         final ClassLoader JavaDoc classLoader = block.getClass().getClassLoader();
42
43         final Class JavaDoc[] serviceClasses = new Class JavaDoc[ services.length ];
44
45         for( int i = 0; i < services.length; i++ )
46         {
47             final ServiceDescriptor service = services[ i ];
48             try
49             {
50                 serviceClasses[ i ] = classLoader.loadClass( service.getName() );
51             }
52             catch( final Exception JavaDoc e )
53             {
54                 final String JavaDoc reason = e.toString();
55                 final String JavaDoc message =
56                     REZ.getString( "bad-mx-service.error", name, service.getName(), reason );
57                 getLogger().error( message );
58                 throw new CascadingException( message, e );
59             }
60         }
61
62         try
63         {
64             context.exportObject( name, serviceClasses, block );
65         }
66         catch( final Exception JavaDoc e )
67         {
68             final String JavaDoc message =
69                 REZ.getString( "export.error", name, e );
70             getLogger().error( message );
71             throw new CascadingException( message, e );
72         }
73
74     }
75
76     /**
77      * Unxport the services of block, declared to be management
78      * services, into management system.
79      */

80     void unexportBlock( final ApplicationContext context,
81                         final BlockMetaData metaData,
82                         final Object JavaDoc block )
83     {
84         final String JavaDoc name = metaData.getName();
85         try
86         {
87             context.unexportObject( name );
88         }
89         catch( final Exception JavaDoc e )
90         {
91             final String JavaDoc message =
92                 REZ.getString( "unexport.error", name, e );
93             getLogger().error( message );
94         }
95     }
96 }
97
Popular Tags