KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > management > Registrar


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

16 package org.apache.axis.management;
17
18 import org.apache.axis.components.logger.LogFactory;
19 import org.apache.axis.i18n.Messages;
20 import org.apache.commons.logging.Log;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24
25 /**
26  * class to act as a dynamic loading registrar to commons-modeler, so
27  * as to autoregister stuff
28  *
29  * @link http://www.webweavertech.com/costin/archives/000168.html#000168
30  */

31 public class Registrar {
32     /**
33      * our log
34      */

35             protected static Log log = LogFactory.getLog(Registrar.class.getName());
36
37     /**
38      * register using reflection. The perf hit is moot as jmx is
39      * all reflection anyway
40      *
41      * @param objectToRegister
42      * @param name
43      * @param context
44      */

45     public static boolean register(Object JavaDoc objectToRegister,
46                                    String JavaDoc name, String JavaDoc context) {
47         if (isBound()) {
48             if (log.isDebugEnabled()) {
49                 log.debug("Registering " + objectToRegister + " as "
50                         + name);
51             }
52             return modelerBinding.register(objectToRegister, name, context);
53         } else {
54             return false;
55         }
56     }
57
58     /**
59      * Check for being bound to a modeler -this will force
60      * a binding if none existed.
61      *
62      * @return
63      */

64
65     public static boolean isBound() {
66         createModelerBinding();
67         return modelerBinding.canBind();
68     }
69
70     /**
71      * create the modeler binding if it is needed
72      * At the end of this call, modelerBinding != null
73      */

74     private static void createModelerBinding() {
75         if (modelerBinding == null) {
76             modelerBinding = new ModelerBinding();
77         }
78     }
79
80     /**
81      * the inner class that does the binding
82      */

83             private static ModelerBinding modelerBinding = null;
84
85     /**
86      * This class provides a dynamic binding to the
87      * commons-modeler registry at run time
88      */

89     static class ModelerBinding {
90         /**
91          * the constructor binds
92          */

93         public ModelerBinding() {
94             bindToModeler();
95         }
96
97         /**
98          * can the binding bind?
99          *
100          * @return true iff the classes are bound
101          */

102         public boolean canBind() {
103             return registry != null;
104         }
105
106         /**
107          * log
108          */

109                 protected static Log log = LogFactory.getLog(ModelerBinding.class.getName());
110         /**
111          * registry object
112          */

113                 Object JavaDoc registry;
114         /**
115          * method to call
116          */

117                 Method JavaDoc registerComponent;
118
119         /**
120          * register using reflection. The perf hit is moot as jmx is
121          * all reflection anyway
122          *
123          * @param objectToRegister
124          * @param name
125          * @param context
126          */

127         public boolean register(Object JavaDoc objectToRegister, String JavaDoc name, String JavaDoc context) {
128             if (registry != null) {
129                 Object JavaDoc args[] = new Object JavaDoc[]{objectToRegister, name, context};
130                 try {
131                     registerComponent.invoke(registry, args);
132                     if (log.isDebugEnabled()) {
133                         log.debug("Registered " + name + " in " + context);
134                     }
135                 } catch (IllegalAccessException JavaDoc e) {
136                     log.error(e);
137                     return false;
138                 } catch (IllegalArgumentException JavaDoc e) {
139                     log.error(e);
140                     return false;
141                 } catch (InvocationTargetException JavaDoc e) {
142                     log.error(e);
143                     return false;
144                 }
145                 return true;
146             } else {
147                 return false;
148             }
149         }
150
151         /**
152          * bind to the modeler; return success/failure flag
153          *
154          * @return true if the binding worked
155          */

156         private boolean bindToModeler() {
157             Exception JavaDoc ex = null;
158             Class JavaDoc clazz;
159             try {
160                 clazz = Class.forName("org.apache.commons.modeler.Registry");
161             } catch (ClassNotFoundException JavaDoc e) {
162                 // just ignore it silently if we don't have commons-modeler.jar around
163
registry = null;
164                 return false;
165             }
166             try {
167                 Class JavaDoc[] getRegistryArgs = new Class JavaDoc[]{Object JavaDoc.class, Object JavaDoc.class,};
168                 Method JavaDoc getRegistry = clazz.getMethod("getRegistry", getRegistryArgs);
169                 Object JavaDoc[] getRegistryOptions = new Object JavaDoc[]{null, null};
170                 registry = getRegistry.invoke(null, getRegistryOptions);
171                 Class JavaDoc[] registerArgs = new Class JavaDoc[]{Object JavaDoc.class,
172                     String JavaDoc.class,
173                     String JavaDoc.class};
174                 registerComponent = clazz.getMethod("registerComponent", registerArgs);
175             } catch (IllegalAccessException JavaDoc e) {
176                 ex = e;
177             } catch (IllegalArgumentException JavaDoc e) {
178                 ex = e;
179             } catch (InvocationTargetException JavaDoc e) {
180                 ex = e;
181             } catch (NoSuchMethodException JavaDoc e) {
182                 ex = e;
183             }
184             // handle any of these exceptions
185
if (ex != null) {
186                 //log the error
187
log.warn(Messages.getMessage("Registrar.cantregister"), ex);
188                 //mark the registration as a failure
189
registry = null;
190                 //and fail
191
return false;
192             } else {
193                 //success
194
return true;
195             }
196         }
197     }
198 }
199
Popular Tags