KickJava   Java API By Example, From Geeks To Geeks.

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


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.phoenix.interfaces.ManagerException;
14 import org.apache.avalon.phoenix.interfaces.SystemManager;
15
16 /**
17  * Implements a management context local to a particular process with which
18  * it can register its managed object. The naming scheme that results is
19  * meant to be compatible with jmx.
20  *
21  * @author <a HREF="mailto:huw@apache.org">Huw Roberts</a>
22  */

23 class SubContext
24     implements SystemManager
25 {
26     private static final Resources REZ =
27         ResourceManager.getPackageResources( SubContext.class );
28
29     private static final String JavaDoc EMPTY_STRING = "";
30
31     private final HashMap JavaDoc m_subcontexts = new HashMap JavaDoc();
32     private final SystemManager m_parent;
33     private final String JavaDoc m_name;
34     private final String JavaDoc m_type;
35
36     /**
37      * Creates new SubContext to the specified context. Objects registered
38      * under the subcontext will be typed with the name of the context so the
39      * jmx name becomes 'contextName' + 'objectNameSoFar'
40      *
41      * @param parent the parent context
42      * @param name the subcontext name
43      */

44     public SubContext( final SystemManager parent,
45                        final String JavaDoc name,
46                        final String JavaDoc type )
47     {
48         if( null == parent )
49         {
50             throw new NullPointerException JavaDoc( "parent" );
51         }
52         m_parent = parent;
53         m_name = name;
54         m_type = type;
55     }
56
57     /**
58      * Register an object for management.
59      * The object is exported through some management scheme
60      * (typically JMX) and the management is restricted
61      * to the interfaces passed in as a parameter to method.
62      *
63      * @param name the name to register object under
64      * @param object the object
65      * @param interfaces the interfaces to register the component under
66      * @throws ManagerException if an error occurs. An error could occur if the object doesn't
67      * implement the interfaces, the interfaces parameter contain non-instance
68      * classes, the name is already registered etc.
69      * @throws IllegalArgumentException if object or interfaces is null
70      */

71     public void register( String JavaDoc name, Object JavaDoc object, Class JavaDoc[] interfaces )
72         throws ManagerException, IllegalArgumentException JavaDoc
73     {
74         m_parent.register( jmxName( name ), object, interfaces );
75     }
76
77     /**
78      * Register an object for management.
79      * The object is exported through some management scheme
80      * (typically JMX). Note that the particular management scheme
81      * will most likely use reflection to extract manageable information.
82      *
83      * @param name the name to register object under
84      * @param object the object
85      * @throws ManagerException if an error occurs such as name already registered.
86      * @throws IllegalArgumentException if object is null
87      */

88     public void register( String JavaDoc name, Object JavaDoc object )
89         throws ManagerException, IllegalArgumentException JavaDoc
90     {
91         m_parent.register( jmxName( name ), object );
92     }
93
94     /**
95      * Unregister named object.
96      *
97      * @param name the name of object to unregister
98      * @throws ManagerException if an error occurs such as when no such object registered.
99      */

100     public void unregister( String JavaDoc name )
101         throws ManagerException
102     {
103         m_parent.unregister( jmxName( name ) );
104     }
105
106     /**
107      * Returns the subcontext of the specified name. If it does not exist it
108      * is created.
109      *
110      * @return the subcontext with the specified name
111      * @throws ManagerException if context cannot be created or retrieved
112      */

113     public SystemManager getSubContext( final String JavaDoc name,
114                                         final String JavaDoc type )
115         throws ManagerException
116     {
117         if( null == type || EMPTY_STRING.equals( type ) )
118         {
119             final String JavaDoc message =
120                 REZ.getString( "subcontext.error.no.subcontext" );
121             throw new ManagerException( message );
122         }
123         else if( null != name && this.m_type == null )
124         {
125             final String JavaDoc message =
126                 REZ.getString( "subcontext.error.no.subcontext" );
127             throw new ManagerException( message );
128         }
129
130         // get from list if possible
131
final String JavaDoc key = contextKey( name, type );
132         SystemManager subcontext =
133             (SystemManager)m_subcontexts.get( key );
134
135         // otherwise create and add to list
136
if( subcontext == null )
137         {
138             subcontext = new SubContext( this, name, type );
139             m_subcontexts.put( key, subcontext );
140         }
141
142         return subcontext;
143     }
144
145     /**
146      * Helper method used to generate the jmx name by appending the current name
147      * and passing up the chain to the root context
148      */

149     private String JavaDoc jmxName( final String JavaDoc name )
150     {
151         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
152         if( null != m_name )
153         {
154             sb.append( m_name );
155             sb.append( ',' );
156         }
157         if( null != m_type )
158         {
159             sb.append( m_type );
160             sb.append( '=' );
161         }
162         sb.append( name );
163
164         return sb.toString();
165     }
166
167     /**
168      * Helper method to get key used to store subcontexts
169      * in m_subcontexts
170      */

171     private String JavaDoc contextKey( final String JavaDoc parent,
172                                final String JavaDoc type )
173     {
174         return parent + "|" + type;
175     }
176 }
177
Popular Tags