KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > manager > SubContext


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.manager;
88
89 import java.util.HashMap JavaDoc;
90
91 import org.codehaus.loom.interfaces.LoomException;
92 import org.codehaus.loom.interfaces.SystemManager;
93 import org.codehaus.spice.salt.i18n.ResourceManager;
94 import org.codehaus.spice.salt.i18n.Resources;
95
96 /**
97  * Implements a management context local to a particular process with which it
98  * can register its managed object. The naming scheme that results is meant to
99  * be compatible with jmx.
100  *
101  * @author <a HREF="mailto:huw@apache.org">Huw Roberts</a>
102  */

103 class SubContext
104     implements SystemManager
105 {
106     private static final Resources REZ =
107         ResourceManager.getPackageResources( SubContext.class );
108
109     private static final String JavaDoc EMPTY_STRING = "";
110
111     private final HashMap JavaDoc m_subcontexts = new HashMap JavaDoc();
112     private final SystemManager m_parent;
113     private final String JavaDoc m_name;
114     private final String JavaDoc m_type;
115
116     /**
117      * Creates new SubContext to the specified context. Objects registered
118      * under the subcontext will be typed with the name of the context so the
119      * jmx name becomes 'contextName' + 'objectNameSoFar'
120      *
121      * @param parent the parent context
122      * @param name the subcontext name
123      */

124     public SubContext( final SystemManager parent,
125                        final String JavaDoc name,
126                        final String JavaDoc type )
127     {
128         if( null == parent )
129         {
130             throw new NullPointerException JavaDoc( "parent" );
131         }
132         m_parent = parent;
133         m_name = name;
134         m_type = type;
135     }
136
137     /**
138      * Register an object for management. The object is exported through some
139      * management scheme (typically JMX). Note that the particular management
140      * scheme will most likely use reflection to extract manageable
141      * information.
142      *
143      * @param name the name to register object under
144      * @param object the object
145      * @throws LoomException if an error occurs such as name already
146      * registered.
147      * @throws IllegalArgumentException if object is null
148      */

149     public void register( final String JavaDoc name, final Object JavaDoc object )
150         throws LoomException, IllegalArgumentException JavaDoc
151     {
152         m_parent.register( jmxName( name ), object );
153     }
154
155     /**
156      * Unregister named object.
157      *
158      * @param name the name of object to unregister
159      * @throws LoomException if an error occurs such as when no such object
160      * registered.
161      */

162     public void unregister( final String JavaDoc name )
163         throws LoomException
164     {
165         m_parent.unregister( jmxName( name ) );
166     }
167
168     /**
169      * Returns the subcontext of the specified name. If it does not exist it is
170      * created.
171      *
172      * @return the subcontext with the specified name
173      * @throws LoomException if context cannot be created or retrieved
174      */

175     public SystemManager getSubContext( final String JavaDoc name,
176                                         final String JavaDoc type )
177         throws LoomException
178     {
179         if( null == type || EMPTY_STRING.equals( type ) )
180         {
181             final String JavaDoc message =
182                 REZ.getString( "subcontext.error.no.subcontext" );
183             throw new LoomException( message );
184         }
185         else if( null != name && this.m_type == null )
186         {
187             final String JavaDoc message =
188                 REZ.getString( "subcontext.error.no.subcontext" );
189             throw new LoomException( message );
190         }
191
192         // get from list if possible
193
final String JavaDoc key = contextKey( name, type );
194         SystemManager subcontext =
195             (SystemManager)m_subcontexts.get( key );
196
197         // otherwise create and add to list
198
if( subcontext == null )
199         {
200             subcontext = new SubContext( this, name, type );
201             m_subcontexts.put( key, subcontext );
202         }
203
204         return subcontext;
205     }
206
207     /**
208      * Helper method used to generate the jmx name by appending the current name
209      * and passing up the chain to the root context
210      */

211     private String JavaDoc jmxName( final String JavaDoc name )
212     {
213         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
214         if( null != m_name )
215         {
216             sb.append( m_name );
217             sb.append( ',' );
218         }
219         if( null != m_type )
220         {
221             sb.append( m_type );
222             sb.append( '=' );
223         }
224         sb.append( name );
225
226         return sb.toString();
227     }
228
229     /**
230      * Helper method to get key used to store subcontexts in m_subcontexts
231      */

232     private String JavaDoc contextKey( final String JavaDoc parent,
233                                final String JavaDoc type )
234     {
235         return parent + "|" + type;
236     }
237 }
238
Popular Tags