1 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 ; 23 import java.lang.reflect.Method ; 24 25 31 public class Registrar { 32 35 protected static Log log = LogFactory.getLog(Registrar.class.getName()); 36 37 45 public static boolean register(Object objectToRegister, 46 String name, String 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 64 65 public static boolean isBound() { 66 createModelerBinding(); 67 return modelerBinding.canBind(); 68 } 69 70 74 private static void createModelerBinding() { 75 if (modelerBinding == null) { 76 modelerBinding = new ModelerBinding(); 77 } 78 } 79 80 83 private static ModelerBinding modelerBinding = null; 84 85 89 static class ModelerBinding { 90 93 public ModelerBinding() { 94 bindToModeler(); 95 } 96 97 102 public boolean canBind() { 103 return registry != null; 104 } 105 106 109 protected static Log log = LogFactory.getLog(ModelerBinding.class.getName()); 110 113 Object registry; 114 117 Method registerComponent; 118 119 127 public boolean register(Object objectToRegister, String name, String context) { 128 if (registry != null) { 129 Object args[] = new Object []{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 e) { 136 log.error(e); 137 return false; 138 } catch (IllegalArgumentException e) { 139 log.error(e); 140 return false; 141 } catch (InvocationTargetException e) { 142 log.error(e); 143 return false; 144 } 145 return true; 146 } else { 147 return false; 148 } 149 } 150 151 156 private boolean bindToModeler() { 157 Exception ex = null; 158 Class clazz; 159 try { 160 clazz = Class.forName("org.apache.commons.modeler.Registry"); 161 } catch (ClassNotFoundException e) { 162 registry = null; 164 return false; 165 } 166 try { 167 Class [] getRegistryArgs = new Class []{Object .class, Object .class,}; 168 Method getRegistry = clazz.getMethod("getRegistry", getRegistryArgs); 169 Object [] getRegistryOptions = new Object []{null, null}; 170 registry = getRegistry.invoke(null, getRegistryOptions); 171 Class [] registerArgs = new Class []{Object .class, 172 String .class, 173 String .class}; 174 registerComponent = clazz.getMethod("registerComponent", registerArgs); 175 } catch (IllegalAccessException e) { 176 ex = e; 177 } catch (IllegalArgumentException e) { 178 ex = e; 179 } catch (InvocationTargetException e) { 180 ex = e; 181 } catch (NoSuchMethodException e) { 182 ex = e; 183 } 184 if (ex != null) { 186 log.warn(Messages.getMessage("Registrar.cantregister"), ex); 188 registry = null; 190 return false; 192 } else { 193 return true; 195 } 196 } 197 } 198 } 199 | Popular Tags |