1 23 24 31 32 package com.sun.enterprise.admin.server.core.mbean.config.naming; 33 34 import com.sun.enterprise.admin.common.Name; 36 37 import com.sun.enterprise.admin.common.exception.MBeanConfigException; 38 import com.sun.enterprise.admin.common.constant.AdminConstants; 39 40 41 import com.sun.enterprise.instance.InstanceEnvironment; 42 43 import com.sun.enterprise.config.ConfigException; 45 import com.sun.enterprise.config.ConfigContext; 46 import com.sun.enterprise.config.ConfigFactory; 47 import com.sun.enterprise.config.ConfigBean; 48 import com.sun.enterprise.config.ConfigBeansFactory; 49 50 import javax.management.ObjectName ; 52 53 import java.util.Hashtable ; 54 import java.util.HashSet ; 55 import java.util.logging.Level ; 56 import java.util.logging.Logger ; 57 58 61 public class ConfigMBeansNaming extends MBeansDescriptions 62 { 63 static MBeanNamingDescriptor[] m_mbeanDescr = initConfigMBeanNaming(); 64 public static final Logger sLogger = Logger.getLogger(AdminConstants.kLoggerName); 65 private final static String MSG_FINDNAMINDESCRIPTOR_FAILED = "mbean.config.findnamingdescriptor_failed"; 66 private final static String MSG_MALFORMED_DOTTED_NAME = "mbean.config.malformed_dotted_name"; 67 private final static String MSG_EXCEPTION_DURING_LIST_NAMES = "mbean.config.list_names_failed"; 68 static MBeanNamingDescriptor findNamingDescriptorByType(String type) 70 { 71 try 72 { 73 for(int i=0; i<m_mbeanDescr.length; i++) 74 { 75 if(type.equals(m_mbeanDescr[i].getType())) 76 return m_mbeanDescr[i]; 77 } 78 } 79 catch (Exception e) 80 { 81 sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e); 82 } 83 return null; 84 } 85 static MBeanNamingDescriptor findNamingDescriptor(String dottedName) 87 { 88 try 89 { 90 Name name = new Name(dottedName); 91 for(int i=0; i<m_mbeanDescr.length; i++) 92 { 93 if(m_mbeanDescr[i].isDottedPatternMatch(name)) 94 return m_mbeanDescr[i]; 95 } 96 } 97 catch (Exception e) 98 { 99 sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e); 100 } 101 return null; 102 } 103 static MBeanNamingDescriptor findNamingDescriptor(ObjectName objectName) 105 { 106 try 107 { 108 Hashtable ht = objectName.getKeyPropertyList(); 109 for(int i=0; i<m_mbeanDescr.length; i++) 110 { 111 if(m_mbeanDescr[i].isObjectNamePatternMatch(ht)) 112 return m_mbeanDescr[i]; 113 } 114 } 115 catch (Exception e) 116 { 117 sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e); 118 } 119 return null; 120 } 121 122 public static String [] findNameContinuation(String instanceName, String dottedName) 124 { 125 HashSet hs = new HashSet (); 126 int wildDescrIndex = -1; 127 Name name = null; 128 int nNameTokens = 0; 129 130 InstanceEnvironment instanceEnvironment= new InstanceEnvironment(instanceName); 132 133 try 135 { 136 name = new Name(dottedName); 137 nNameTokens = name.getNumParts(); 138 } 139 catch (Exception e) 140 { 141 sLogger.log(Level.FINE, MSG_MALFORMED_DOTTED_NAME, e); 142 return new String [0]; 143 } 144 145 for(int i=0; i<m_mbeanDescr.length; i++) { 147 Object [][] tokens = m_mbeanDescr[i].getDottedTokens(); 148 if(tokens!=null) 149 { 150 for(int j=0; j<tokens.length; j++) { 152 if(MBeanNamingDescriptor.isDottedPatternMatch(name, tokens[j], false) && tokens[j].length>nNameTokens) 153 { 154 if(!(tokens[j][nNameTokens] instanceof String )) { 157 if(tokens[j].length==nNameTokens+1) wildDescrIndex = i; 159 } 160 else 161 { 162 hs.add(dottedName+"."+tokens[j][nNameTokens]); 163 } 164 } 165 } 166 } 167 } 168 String xpath = null; 170 if(wildDescrIndex>=0) 171 { 172 try 173 { 174 ConfigMBeanNamingInfo info = new ConfigMBeanNamingInfo(dottedName + ".fake"); 175 xpath = info.getXPath(); 176 } 177 catch (Exception e) 178 { 179 sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, e); 180 } 181 } 182 if(xpath!=null) 183 { 184 String attributeName = null; 185 String elementName= null; 186 xpath = xpath.trim(); 188 if(xpath.length()>0 && xpath.endsWith("]")) 189 { 190 int i = xpath.lastIndexOf('@') + 1; 191 int j = xpath.indexOf('=',i) ; 192 if(i>0 && j>i) 193 { 194 attributeName = xpath.substring(i,j).trim(); 195 j = xpath.lastIndexOf('['); 196 if(j>0 && j<i) 197 { 198 xpath = xpath.substring(0,j); 199 j = xpath.lastIndexOf('/'); 200 if(j>0 && j<xpath.length()-2) 201 { 202 elementName = xpath.substring(j+1).trim(); 203 xpath = xpath.substring(0,j); 204 } 205 } 206 } 207 208 } 209 210 if(attributeName!=null && elementName!=null) { 212 ConfigContext configContext; 214 try 215 { 216 String fileUrl = instanceEnvironment.getConfigFilePath(); 217 configContext = ConfigFactory.createConfigContext(fileUrl); 218 ConfigBean bean = ConfigBeansFactory.getConfigBeanByXPath(configContext, xpath); 219 ConfigBean[] childs = bean.getChildBeansByName(elementName); 220 for(int i=0; i<childs.length; i++) 221 { 222 String next = childs[i].getAttributeValue(attributeName); 223 if(next!=null) 224 hs.add(dottedName+"."+next); 225 } 226 227 } 228 catch (ConfigException ce) 229 { 230 sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, ce); 231 } 232 catch (NullPointerException npe) { 234 sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, npe); 235 } 236 } 237 } 238 return (String [])hs.toArray(new String [hs.size()]); 239 } 240 241 synchronized static private MBeanNamingDescriptor[] initConfigMBeanNaming() { 243 MBeanNamingDescriptor[] descrs = null; 244 try 245 { 246 descrs = new MBeanNamingDescriptor[mbean_descriptions.length]; 247 for(int i=0; i<mbean_descriptions.length; i++) 248 { 249 descrs[i] = new MBeanNamingDescriptor(mbean_descriptions[i]); 250 } 251 } 252 catch (MBeanConfigException e) 253 { 254 } 255 return descrs; 256 } 257 258 275 276 public static void main(String args[]) 277 { 278 testDottedName("myInstance.http-listener.myListener"); 279 testDottedName("myInstance.http-listener"); 280 testDottedName("myInstance.http-server"); 281 testDottedName("myInstance.http-server.mymy"); 282 testDottedName("myInstance.http-listeners.myListener"); 283 testDottedName("myInstance.http-serve"); 284 testDottedName("myInstance.http-server.http-listener.myListener"); 285 286 try 287 { 288 testObjectName(new ObjectName ("ias:type=http-listener,instance-name=myServer,name=myListener")); 289 testObjectName(new ObjectName ("ias:type=http-listener,instance-name=myServer,name=myListener,chtoto=to")); 290 testObjectName(new ObjectName ("ias:type=http-service,instance-name=myServer,name=jhgv")); 291 testObjectName(new ObjectName ("ias:type=http-service,server-instances=myServer")); 292 testObjectName(new ObjectName ("ias:type=http-service,instance-name=myServer")); 293 testObjectName(new ObjectName ("ias:name=myListener,instance-name=myServer,type=http-listener")); 294 } 295 catch (Throwable e) 296 { 297 print(">>>>>>EXCEPTION: " + e); 298 e.printStackTrace(); 299 } 300 } 301 302 private static void print(String str) 303 { 304 System.out.println(str); 305 } 306 307 private static void testDottedName(String dottedName) 308 { 309 try 310 { 311 print("\n\n\n>>>>>>test for dotted name: " + dottedName); 312 ConfigMBeanNamingInfo mbi = new ConfigMBeanNamingInfo(dottedName); 313 print(" ConfigMBeanNamingInfo =" + mbi); 314 print(" ObjectName =" + mbi.getObjectName()); 315 print(" XPath =" + mbi.getXPath()); 316 } 317 catch (Throwable e) 318 { 319 print(">>>>>>EXCEPTION: " + e); 320 } 322 323 } 324 private static void testObjectName(ObjectName name) 325 { 326 try 327 { 328 print("\n\n\n>>>>>>test for object name: " + name); 329 ConfigMBeanNamingInfo mbi = new ConfigMBeanNamingInfo(name); 330 print(" ConfigMBeanNamingInfo =" + mbi); 331 print(" ObjectName =" + mbi.getObjectName()); 332 print(" XPath =" + mbi.getXPath()); 333 } 334 catch (Throwable e) 335 { 336 print(">>>>>>EXCEPTION: " + e); 337 e.printStackTrace(); 338 } 339 340 } 341 342 } 343 | Popular Tags |