KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > impl > ObjectNameBuilderImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management.impl;
16
17 import java.beans.PropertyEditorManager JavaDoc;
18
19 import javax.management.MalformedObjectNameException JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.internal.ServicePoint;
24 import org.apache.hivemind.management.ObjectNameBuilder;
25 import org.apache.hivemind.util.IdUtils;
26
27 /**
28  * Implementation of {@link org.apache.hivemind.management.ObjectNameBuilder}. A configurable domain
29  * is prepended to the ObjectNames. The ObjectNames include the module, extensionId and a type as
30  * key properties. Example for a service:
31  * HiveMind:module=hivemind,type=servicePoint,id=hivemind.Startup When using this naming Jconsole
32  * interprets the module key as package name and id as a class name.
33  *
34  * @author Achim Huegen
35  * @since 1.1
36  */

37 public class ObjectNameBuilderImpl implements ObjectNameBuilder
38 {
39     private String JavaDoc _domain = "hivemind";
40
41     static
42     {
43         // Register PropertyEditor for ObjectNames. This is needed
44
// in MBeans contributions. Since ObjectNameBuilder is injected in
45
// MBeanRegistry, this is done just in time here
46
// Registration should be done in a more general way,
47
// but the concept discussed here:
48
// http://wiki.apache.org/jakarta-hivemind/ExtendingSmartTranslator
49
// doesn't work because MBeanRegistry is eagerly loaded.
50
PropertyEditorManager.registerEditor(ObjectName JavaDoc.class, ObjectNameEditor.class);
51     }
52
53     /**
54      * Creates an ObjectName from a String
55      */

56     protected ObjectName JavaDoc createObjectNameInstance(String JavaDoc name)
57     {
58         ObjectName JavaDoc objectName;
59         try
60         {
61             objectName = new ObjectName JavaDoc(name);
62         }
63         catch (MalformedObjectNameException JavaDoc e)
64         {
65             // Should never occur
66
throw new ApplicationRuntimeException(e);
67         }
68         return objectName;
69
70     }
71
72     /**
73      * Creates an ObjectName from list of keys and values and prepends the domain. Maintains the
74      * order of the keys and this distinguishes the method from the ObjectName constructor that
75      * accepts an hashtable of keys and values. The order influences the visualization in JConsole.
76      * Example: Hivemind:key1=value1,key2=value2
77      */

78     public ObjectName JavaDoc createObjectName(String JavaDoc[] keys, String JavaDoc[] values)
79     {
80         if (keys.length != values.length)
81             throw new IllegalArgumentException JavaDoc("Arrays keys and values must have same length");
82         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
83         sb.append(_domain + ':');
84         for (int i = 0; i < values.length; i++)
85         {
86             if (i > 0)
87                 sb.append(",");
88             sb.append(keys[i]);
89             sb.append("=");
90             sb.append(values[i]);
91         }
92         return createObjectNameInstance(sb.toString());
93     }
94
95     /**
96      * @see org.apache.hivemind.management.ObjectNameBuilder#createObjectName(java.lang.String,
97      * java.lang.String)
98      */

99     public ObjectName JavaDoc createObjectName(String JavaDoc qualifiedId, String JavaDoc type)
100     {
101         String JavaDoc moduleId = IdUtils.extractModule(qualifiedId);
102         if (moduleId == null)
103             moduleId = "(default package)";
104         String JavaDoc id = IdUtils.stripModule(qualifiedId);
105         return createObjectName(moduleId, id, type);
106     }
107
108     /**
109      * @see org.apache.hivemind.management.ObjectNameBuilder#createObjectName(java.lang.String,
110      * java.lang.String, java.lang.String)
111      */

112     public ObjectName JavaDoc createObjectName(String JavaDoc moduleId, String JavaDoc id, String JavaDoc type)
113     {
114         return createObjectName(new String JavaDoc[]
115         { "module", "type", "id" }, new String JavaDoc[]
116         { moduleId, type, id });
117     }
118
119     /**
120      * @see org.apache.hivemind.management.ObjectNameBuilder#createServiceObjectName(org.apache.hivemind.internal.ServicePoint)
121      */

122     public ObjectName JavaDoc createServiceObjectName(ServicePoint servicePoint)
123     {
124         return createObjectName(servicePoint.getExtensionPointId(), "service");
125     }
126
127     /**
128      * @see org.apache.hivemind.management.ObjectNameBuilder#createServiceDecoratorName(org.apache.hivemind.internal.ServicePoint,
129      * java.lang.String)
130      */

131     public ObjectName JavaDoc createServiceDecoratorName(ServicePoint servicePoint, String JavaDoc decoratorType)
132     {
133         return createObjectName(new String JavaDoc[]
134         { "module", "type", "id", "decorator" }, new String JavaDoc[]
135         { servicePoint.getModule().getModuleId(), "service",
136                 IdUtils.stripModule(servicePoint.getExtensionPointId()), decoratorType });
137     }
138
139     public String JavaDoc getDomain()
140     {
141         return _domain;
142     }
143
144     public void setDomain(String JavaDoc domain)
145     {
146         _domain = domain;
147     }
148
149 }
Popular Tags