1 22 package org.jboss.ejb; 23 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.Iterator ; 27 import java.util.StringTokenizer ; 28 29 import javax.management.MBeanServer ; 30 31 import org.jboss.deployers.spi.deployer.DeploymentUnit; 32 import org.jboss.deployers.spi.structure.DeploymentContext; 33 import org.jboss.deployment.MainDeployerMBean; 34 import org.jboss.logging.Logger; 35 import org.jboss.metadata.ApplicationMetaData; 36 import org.jboss.metadata.BeanMetaData; 37 import org.jboss.metadata.MessageDestinationMetaData; 38 import org.jboss.metadata.WebMetaData; 39 import org.jboss.util.Strings; 40 41 50 public final class EjbUtil50 51 { 52 private static final Logger log = Logger.getLogger(EjbUtil50.class); 53 54 64 public static String findEjbLink(MBeanServer server, DeploymentUnit unit, String link) 65 { 66 return resolveLink(server, unit, link, false); 67 } 68 69 79 public static String findLocalEjbLink(MBeanServer server, DeploymentUnit unit, String link) 80 { 81 return resolveLink(server, unit, link, true); 82 } 83 84 94 public static MessageDestinationMetaData findMessageDestination(MBeanServer server, DeploymentUnit di, String link) 95 { 96 return resolveMessageDestination(server, di, link); 97 } 98 99 private static String resolveLink(MBeanServer server, DeploymentUnit di, String link, boolean isLocal) 100 { 101 if (link == null) 102 { 103 return null; 104 } 105 106 if (log.isTraceEnabled()) 107 { 108 log.trace("resolveLink( {" + di + "}, {" + link + "}, {" + isLocal + "}"); 109 } 110 111 if (di == null) 112 { 113 return null; 115 } 116 117 if (link.indexOf('#') != -1) 118 { 119 return resolveRelativeLink(server, di, link, isLocal); 121 } 122 else 123 { 124 DeploymentUnit top = di; 126 while (top.getDeploymentContext().getParent() != null) 127 { 128 top = top.getDeploymentContext().getParent().getDeploymentUnit(); 129 } 130 131 return resolveAbsoluteLink(top, link, isLocal); 132 } 133 } 134 135 private static String resolveRelativeLink(MBeanServer server, DeploymentUnit unit, String link, boolean isLocal) 136 { 137 138 String path = link.substring(0, link.indexOf('#')); 139 String ejbName = link.substring(link.indexOf('#') + 1); 140 String us = unit.getName(); 141 142 if (us.charAt(us.length() - 1) == '/') 144 us = us.substring(0, us.length() - 1); 145 146 String ourPath = us.substring(0, us.lastIndexOf('/')); 147 148 if (log.isTraceEnabled()) 149 { 150 log.trace("Resolving relative link: " + link); 151 log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'"); 152 } 153 154 for (StringTokenizer st = new StringTokenizer (path, "/"); st.hasMoreTokens();) 155 { 156 String s = st.nextToken(); 157 if (s.equals("..")) 158 { 159 ourPath = ourPath.substring(0, ourPath.lastIndexOf('/')); 160 } 161 else 162 { 163 ourPath += "/" + s; 164 } 165 } 166 167 URL target = null; 168 169 try 170 { 171 target = Strings.toURL(ourPath); 172 } 173 catch (MalformedURLException mue) 174 { 175 log.warn("Can't construct URL for: " + ourPath); 176 return null; 177 } 178 179 DeploymentUnit targetUnit = null; 180 try 181 { 182 targetUnit = (DeploymentUnit)server.invoke(MainDeployerMBean.OBJECT_NAME, "getDeployment", new Object [] { target }, new String [] { URL .class.getName() }); 183 } 184 catch (Exception e) 185 { 186 log.warn("Got Exception when looking for DeploymentUnit: " + e); 187 return null; 188 } 189 190 if (targetUnit == null) 191 { 192 log.warn("Can't locate DeploymentUnit for target: " + target); 193 return null; 194 } 195 196 if (log.isTraceEnabled()) 197 { 198 log.trace("Found appropriate DeploymentUnit: " + targetUnit); 199 } 200 201 String linkTarget = null; 202 if (targetUnit.getAttachment(ApplicationMetaData.class) != null) 203 { 204 ApplicationMetaData appMD = targetUnit.getAttachment(ApplicationMetaData.class); 205 BeanMetaData beanMD = appMD.getBeanByEjbName(ejbName); 206 207 if (beanMD != null) 208 { 209 linkTarget = getJndiName(beanMD, isLocal); 210 } 211 else 212 { 213 log.warn("No Bean named '" + ejbName + "' found in '" + path + "'!"); 214 } 215 } 216 else 217 { 218 log.warn("DeploymentUnit " + targetUnit + " is not an EJB .jar " + "file!"); 219 } 220 221 return linkTarget; 222 } 223 224 private static String resolveAbsoluteLink(DeploymentUnit unit, String link, boolean isLocal) 225 { 226 if (log.isTraceEnabled()) 227 { 228 log.trace("Resolving absolute link, di: " + unit); 229 } 230 231 String ejbName = null; 232 233 if (unit.getAttachment(ApplicationMetaData.class) != null) 235 { 236 ApplicationMetaData appMD = unit.getAttachment(ApplicationMetaData.class); 237 BeanMetaData beanMD = appMD.getBeanByEjbName(link); 238 if (beanMD != null) 239 { 240 ejbName = getJndiName(beanMD, isLocal); 241 if (log.isTraceEnabled()) 242 { 243 log.trace("Found Bean: " + beanMD + ", resolves to: " + ejbName); 244 } 245 246 return ejbName; 247 } 248 else if (log.isTraceEnabled()) 249 { 250 log.trace("No match for ejb-link: " + link + ", module names:"); 252 Iterator iter = appMD.getEnterpriseBeans(); 253 while (iter.hasNext()) 254 { 255 beanMD = (BeanMetaData)iter.next(); 256 String beanEjbName = getJndiName(beanMD, isLocal); 257 log.trace("... ejbName: " + beanEjbName); 258 } 259 } 260 } 261 262 Iterator it = unit.getDeploymentContext().getChildren().iterator(); 264 while (it.hasNext() && ejbName == null) 265 { 266 DeploymentUnit child = ((DeploymentContext)it.next()).getDeploymentUnit(); 267 ejbName = resolveAbsoluteLink(child, link, isLocal); 268 } 269 270 return ejbName; 271 } 272 273 private static String getJndiName(BeanMetaData beanMD, boolean isLocal) 274 { 275 String jndiName = null; 276 if (isLocal) 277 { 278 String localHome = beanMD.getLocalHome(); 280 if (localHome != null) 281 jndiName = beanMD.getLocalJndiName(); 282 else 283 { 284 log.warn("LocalHome jndi name requested for: '" + beanMD.getEjbName() + "' but there is no LocalHome class"); 285 } 286 } 287 else 288 { 289 jndiName = beanMD.getJndiName(); 290 } 291 return jndiName; 292 } 293 294 private static MessageDestinationMetaData resolveMessageDestination(MBeanServer server, DeploymentUnit di, String link) 295 { 296 if (link == null) 297 return null; 298 299 if (log.isTraceEnabled()) 300 log.trace("resolveLink( {" + di + "}, {" + link + "})"); 301 302 if (di == null) 303 return null; 305 306 if (link.indexOf('#') != -1) 307 return resolveRelativeMessageDestination(server, di, link); 309 else 310 { 311 DeploymentUnit top = di; 313 while (top.getDeploymentContext().getParent() != null) 314 top = top.getDeploymentContext().getParent().getDeploymentUnit(); 315 316 return resolveAbsoluteMessageDestination(top, link); 317 } 318 } 319 320 private static MessageDestinationMetaData resolveRelativeMessageDestination(MBeanServer server, DeploymentUnit unit, String link) 321 { 322 String path = link.substring(0, link.indexOf('#')); 323 String destinationName = link.substring(link.indexOf('#') + 1); 324 String us = unit.getName(); 325 326 if (us.charAt(us.length() - 1) == '/') 328 us = us.substring(0, us.length() - 1); 329 330 String ourPath = us.substring(0, us.lastIndexOf('/')); 331 332 if (log.isTraceEnabled()) 333 { 334 log.trace("Resolving relative message-destination-link: " + link); 335 log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'"); 336 } 337 338 for (StringTokenizer st = new StringTokenizer (path, "/"); st.hasMoreTokens();) 339 { 340 String s = st.nextToken(); 341 if (s.equals("..")) 342 ourPath = ourPath.substring(0, ourPath.lastIndexOf('/')); 343 else ourPath += "/" + s; 344 } 345 346 URL target = null; 347 try 348 { 349 target = Strings.toURL(ourPath); 350 } 351 catch (MalformedURLException mue) 352 { 353 log.warn("Can't construct URL for: " + ourPath); 354 return null; 355 } 356 357 DeploymentUnit targetUnit = null; 358 try 359 { 360 targetUnit = (DeploymentUnit)server.invoke(MainDeployerMBean.OBJECT_NAME, "getDeployment", new Object [] { target }, new String [] { URL .class.getName() }); 361 } 362 catch (Exception e) 363 { 364 log.warn("Got Exception when looking for DeploymentUnit: " + e); 365 return null; 366 } 367 368 if (targetUnit == null) 369 { 370 log.warn("Can't locate DeploymentUnit for target: " + target); 371 return null; 372 } 373 374 if (log.isTraceEnabled()) 375 log.trace("Found appropriate DeploymentUnit: " + targetUnit); 376 377 if (targetUnit.getAttachment(ApplicationMetaData.class) != null) 378 { 379 ApplicationMetaData appMD = targetUnit.getAttachment(ApplicationMetaData.class); 380 return appMD.getMessageDestination(destinationName); 381 } 382 if (targetUnit.getAttachment(WebMetaData.class) != null) 383 { 384 WebMetaData webMD = targetUnit.getAttachment(WebMetaData.class); 385 return webMD.getMessageDestination(destinationName); 386 } 387 else 388 { 389 log.warn("DeploymentUnit " + targetUnit + " is not an EJB .jar " + "file!"); 390 return null; 391 } 392 } 393 394 private static MessageDestinationMetaData resolveAbsoluteMessageDestination(DeploymentUnit unit, String link) 395 { 396 if (log.isTraceEnabled()) 397 log.trace("Resolving absolute link, di: " + unit); 398 399 if (unit.getAttachment(ApplicationMetaData.class) != null) 401 { 402 ApplicationMetaData appMD = unit.getAttachment(ApplicationMetaData.class); 403 MessageDestinationMetaData mdMD = appMD.getMessageDestination(link); 404 if (mdMD != null) 405 return mdMD; 406 } 407 if (unit.getAttachment(WebMetaData.class) != null) 408 { 409 WebMetaData webMD = unit.getAttachment(WebMetaData.class); 410 return webMD.getMessageDestination(link); 411 } 412 413 Iterator it = unit.getDeploymentContext().getChildren().iterator(); 415 while (it.hasNext()) 416 { 417 DeploymentUnit child = ((DeploymentContext)it.next()).getDeploymentUnit(); 418 MessageDestinationMetaData mdMD = resolveAbsoluteMessageDestination(child, link); 419 if (mdMD != null) 420 return mdMD; 421 } 422 423 return null; 425 } 426 } | Popular Tags |