2014-09-30 24 views
3

我有一個熱門部署webapps的嵌入式jetty服務器。部署其他戰爭時,我將一個部署描述符文件添加到目錄以配置webappcontext。我希望將在另一個位置構建的jar的DIRECTORY添加到熱部署的戰爭(/ extJars)的類路徑中。我看到了如何在webapp descriptor.xml中做到這一點,我可以通過陳述個別的jar來實現這一點,但是我嘗試了多種配置,試圖簡單地讀取這個目錄中的所有jar文件,而沒有任何工作。這裏是我的另一個WebApp.xml,我們將它稱爲它,並且配置可用,並且註釋掉不起作用的配置。謝謝。通過WebAppContext管理多個jar extraClasspath

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> 
    <Configure class="org.eclipse.jetty.webapp.WebAppContext"> 
     <Set name="war"><SystemProperty name="JETTY_HOME" default="." />/extApps/otherWebApp.war</Set> 
     <Set name="contextPath">/otherWebApp</Set> 
     <Set name="tempDirectory" ><SystemProperty name="JETTY_HOME" />/jetty/webapps/otherWebApp</Set> 
<Set name="extraClasspath"><SystemProperty name="JETTY_HOME" />/extJars/cadi-core-1.0.12.jar,<SystemProperty name="JETTY_HOME" />/extJars/cadi-aaf-1.0.12.jar,<SystemProperty name="JETTY_HOME" />/extJars/GLCookieDecryption-1.0.jar,<SystemProperty name="JETTY_HOME" />/extJars/rosetta-1.1.1.jar,<SystemProperty name="AJSC_HOME" />/extJars/env-1.4.2.jar,<SystemProperty name="JETTY_HOME" />/extJars/dme2-2.5.22.jar</Set> 
<!-- <Set name="extraClasspath"><SystemProperty name="JETTY_HOME" />/extJars/</Set> 
    </Configure> doesn't work --> 
<!-- <Set name="extraClasspath"><SystemProperty name="JETTY_HOME" />/extJars/*</Set> 
    </Configure> doesn't work --> 
<!-- <Set name="extraClasspath"><SystemProperty name="JETTY_HOME" />/extJars/*.jar</Set> 
    </Configure> doesn't work --> 
<!-- <Set name="extraClasspath"><SystemProperty name="JETTY_HOME" />/extJars</Set> 
    </Configure> doesn't work --> 
+0

在將Java代碼中的'WebAppContext'作爲處理程序添加到服務器之前,您是否使用了'WebAppContext'?還是你依靠'DeploymentManager'來做到這一點? – 2014-09-30 19:36:40

+0

這些「extraClasspath」jar是否添加到嵌入式服務器中的所有WebAppContext?或只是特定的? – 2014-09-30 19:39:23

+0

我正在使用Deployment Manager創建Hot Deployable Folder位置。然後,我添加其他WebApp.war和其他WebApp。xml添加到該文件夾​​,Deployment Manager熱部署基於xml的戰爭。從技術上講,這些額外的jar被一個類加載器添加到主webapp中,但這些jar也需要被其他的WebApp使用。我猜想總體設計/工作流程是我有一個主要的jetty.xml,它設置了Deployment Manager,它部署了主要的webAppContext和熱部署文件夾。我只是希望所有extJars都使用xml在其他WebApp類路徑中。 – user2116247 2014-10-01 14:44:57

回答

2

您有3個選擇。

1:使用家長裝載機優先

在嵌入式,告訴WebAppContext使用父加載器的優先級。 (這將有利於在web應用類服務器類)

<Configure class="org.eclipse.jetty.webapp.WebAppContext"> 
    <Set name="contextPath">/example</Set> 
    <Set name="war">example.war</Set> 
    <Set name="parentLoaderPriority">true</Set> 
</Configure> 

這確實有副作用,如果服務器類做任何信息的高速緩存(在許多圖書館的常用技術),那麼緩存現在可用於所有的webapps 。

2:使用自定義的DeploymentManager綁定管理WebAppClassloader

每個WebAppContext使用可被配置​​爲一個WebAppClassloader:

  1. 揭露某些類別的服務器類加載器
  2. 樹立WebAppContext什麼如果Web應用程序和服務器之間存在衝突,則會執行此操作。

由於您使用的是DeploymentManager,因此可以通過綁定技術對其進行標準化。

綁定本身:ExposeServerCommonBinding.java

package jetty; 

import org.eclipse.jetty.deploy.App; 
import org.eclipse.jetty.deploy.AppLifeCycle; 
import org.eclipse.jetty.deploy.graph.Node; 
import org.eclipse.jetty.server.handler.ContextHandler; 
import org.eclipse.jetty.webapp.WebAppContext; 

public class ExposeServerCommonBinding implements AppLifeCycle.Binding 
{ 
    public String[] getBindingTargets() 
    { 
     return new String[] 
     { "deploying" }; 
    } 

    public void processBinding(Node node, App app) throws Exception 
    { 
     ContextHandler handler = app.getContextHandler(); 
     if (handler == null) 
     { 
      throw new NullPointerException("No Handler created for App: " + app); 
     } 

     if (handler instanceof WebAppContext) 
     { 
      WebAppContext webapp = (WebAppContext)handler; 

      // System classes (or namespaces) present in server classloader to expose to webapp 
      webapp.addSystemClass("org.apache.log4j."); 
      webapp.addSystemClass("org.slf4j."); 
      webapp.addSystemClass("org.apache.commons.logging."); 

      // Server classes that cannot be overridden by webapp 
      webapp.addServerClass("-org.apache.log4j."); 
      webapp.addServerClass("-org.slf4j."); 
      webapp.addServerClass("-org.apache.commons.logging."); 
     } 
    } 
} 

以及如何使用它

DeploymentManager mgr = new DeploymentManager(); 

WebAppProvider provider = new WebAppProvider(); 
provider.setMonitoredDirResource(Resource.newResource(new File("./webapps/"))); 
mgr.addAppProvider(provider); 

mgr.addLifeCycleBinding(new ExposeServerCommonBinding()); 

這項技術將適用於那些通過DeploymentManager下部署的所有WebAppContexts,讓您應用這些規則平等地所有的webapps。

3:使用自定義的DeploymentManager綁定管理extraClasspath

這裏是你可以用預建extraClasspath在服務器端,而另一個綁定替代時,Web應用程序部署,它會自動將這些extraClasspath到WebApp。

package jetty; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

import org.eclipse.jetty.deploy.App; 
import org.eclipse.jetty.deploy.AppLifeCycle; 
import org.eclipse.jetty.deploy.graph.Node; 
import org.eclipse.jetty.server.handler.ContextHandler; 
import org.eclipse.jetty.webapp.WebAppContext; 

public class CommonExtraClasspathBinding implements AppLifeCycle.Binding 
{ 
    private List<File> extraClasspath = new ArrayList<>(); 

    public String[] getBindingTargets() 
    { 
     return new String[] { "deploying" }; 
    } 

    public void addAllJars(File dir) 
    { 
     for (File file : dir.listFiles()) 
     { 
      if (!file.isFile()) 
      { 
       continue; 
      } 
      if (file.getName().toLowerCase(Locale.ENGLISH).equals(".jar")) 
      { 
       addJar(file); 
      } 
     } 
    } 

    public void addJar(File jar) 
    { 
     if (jar.exists() && jar.isFile()) 
     { 
      extraClasspath.add(jar); 
     } 
    } 

    public void processBinding(Node node, App app) throws Exception 
    { 
     ContextHandler handler = app.getContextHandler(); 
     if (handler == null) 
     { 
      throw new NullPointerException("No Handler created for App: " + app); 
     } 

     if (handler instanceof WebAppContext) 
     { 
      WebAppContext webapp = (WebAppContext)handler; 

      StringBuilder xtraCp = new StringBuilder(); 
      boolean delim = false; 
      for (File cp : extraClasspath) 
      { 
       if (delim) 
       { 
        xtraCp.append(File.pathSeparatorChar); 
       } 
       xtraCp.append(cp.getAbsolutePath()); 
       delim = true; 
      } 

      webapp.setExtraClasspath(xtraCp.toString()); 
     } 
    } 
}