如何获取glassfish/javaee应用版本?

How to get glassfish/javaee application version?

美好的一天。 我部署了一些带有版本的测试应用程序,如 https://community.oracle.com/blogs/serli/2010/08/30/how-use-glassfish-application-versioning

$ asadmin deploy --name=test:BETA-1.1 test.war

但是当我尝试获取应用程序名称时 http://javahowto.blogspot.ru/2009/12/how-to-get-module-name-and-app-name.html

initialContext.lookup("java:app/AppName") - 它没有版本返回。 是否可以通过编程方式获取应用程序的版本?

$ asadmin 列表应用程序 打印带有版本的应用程序名称

我在源代码中找到了一个获取 AppName 的地方:

glassfish3\glassfish\modules\container-common.jar!com/sun/enterprise/container/common/impl/JavaModuleNamingProxy.class

  private String getAppName() throws NamingException {
    ComponentEnvManager namingMgr = (ComponentEnvManager)this.habitat.getComponent(ComponentEnvManager.class);

    String appName = null;
    if (namingMgr != null) {
      JndiNameEnvironment env = namingMgr.getCurrentJndiNameEnvironment();

      BundleDescriptor bd = null;
      if ((env instanceof EjbDescriptor)) {
        bd = ((EjbDescriptor)env).getEjbBundleDescriptor();
      } else if ((env instanceof BundleDescriptor)) {
        bd = (BundleDescriptor)env;
      }
      if (bd != null) {
        Application app = bd.getApplication();
        appName = app.getAppName();  // <-- HERE!
      }
    }
    if (appName == null)
      throw new NamingException("Could not resolve java:app/AppName");

    return appName;
  }

和 Application.getAppName() 在 createApplication

中设置

glassfish3\glassfish\modules\dol.jar!com/sun/enterprise/deployment/Application.class

  public static Application createApplication(Habitat habitat, String name, ModuleDescriptor < BundleDescriptor > newModule) {
    Application application = new Application(habitat);
    application.setVirtual(true);
    if ((name == null) && (newModule.getDescriptor() != null)) {
      name = ((BundleDescriptor)newModule.getDescriptor()).getDisplayName();
    }
    String untaggedName = VersioningUtils.getUntaggedName(name);
    if (name != null) {
      application.setDisplayName(untaggedName);
      application.setName(untaggedName);
      application.setAppName(untaggedName);
    }
    newModule.setStandalone(true);
    newModule.setArchiveUri(untaggedName);
    if (newModule.getDescriptor() != null) {
      ((BundleDescriptor)newModule.getDescriptor()).setApplication(application);
    }
    application.addModule(newModule);
    return application;
  }

如您所见,它不会在应用程序中保存标记名称。为什么不使用显示名称? 不过,我会尝试找到另一种获取版本信息的方法...可能是 set/read glassfish-application.xml/version-identifier

更新: glassfish 在(域|节点)/applications/appName~版本中创建版本文件夹。所以我们可以分析一些资源路径:)

static final Pattern APP_VER = Pattern.compile("/applications/([^~/]+)(?:~([^/]*))?");

public String getAppVersion() {
  URL res = getClass().getResource("/META-INF/somefile.xml");
  if (res != null) {
    Matcher ver = APP_VER.matcher(res.getPath());
    if (ver.find())
      return ver.group(2);
  }

  return null;
}

玩得开心!也许有人有更漂亮的解决方案?