Jump to: navigation, search

Difference between revisions of "Jetty/Feature/ContextProvider"

< Jetty‎ | Feature
m
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Jetty Feature
 
{{Jetty Feature
 
| introduction =
 
| introduction =
{{warning|Deprecated:|ContextDeployer has been superseded by the new Deployment Manager module.}}
+
Jetty provides capability to deploy an arbitrary context or web application with Jetty-specific configuration. The ContextProvider mechanism, which is now an extension of the core deployment infrastructure, implements this capability.
Jetty provides capability to deploy an arbitrary context or web application with Jetty-specific configuration. This capability is implemented via ContextDeployer mechanism.
+
 
 +
You can use the [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java?root=RT_JETTY&view=markup ContextProvider] to (hot) deploy an arbitrary context or web application with Jetty-specific configuration. To statically deploy only standard web applications at startup, use the [[Jetty/Feature/WebAppDeployer|WebApp Provider]].
 +
 
 
| body =
 
| body =
The [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/ContextDeployer.java?root=RT_JETTY&view=markup ContextDeployer] may be used to (hot)deploy an arbitrary Context or Web Application with Jetty specific configuration. To statically deploy only standard web applications at startup, use the [[Jetty/Feature/WebAppDeployer|WebApp Deployer]].
 
  
==Overview==
+
A <tt>jetty.xml</tt> typically defines a ContextProvider; within the stock jetty distribution the name of this file is<tt>jetty-contexts.xml</tt>:
  
Typically a ContextDeployer is defined in a jetty.xml file:
 
 
<source lang="xml">  
 
<source lang="xml">  
  <Call name="addLifeCycle">
+
  <Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Arg>
+
        <Ref id="DeploymentManager">
    <New class="org.eclipse.jetty.deployer.ContextDeployer">
+
          <Call name="addAppProvider">
      <Set name="contexts"><Ref id="Contexts"</Set>
+
            <Arg>
      <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>
+
              <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
      <Set name="scanInterval">1</Set>
+
                <Set name="monitoredDir"><Property name="jetty.home" default="." />/contexts</Set>
    </New>
+
                <Set name="scanInterval">1</Set>
  </Arg>
+
              </New>
</Call>
+
            </Arg>
 +
          </Call>
 +
        </Ref>
 +
</Configure>
 
</source>
 
</source>
  
The ContextDeployer will scan the '''configurationDir''' directory at intervals of '''scanInterval''' seconds for xml descriptors that define contexts. Any contexts found are deployed to the passed '''contexts''' reference to a HandlerContainer (this is normally an instance of ContextHandlerCollection).
+
The ContextProvider scans the ''monitoredDir'' directory at intervals of ''scanInterval'' seconds for XML descriptors that define contexts. It then deploys any contexts to the passed ''contexts'' reference to a HandlerContainer (normally an instance of ContextHandlerCollection). The deployment descriptors are in [[Jetty/Reference/jetty.xml syntax|jetty xml]] format, and define and configure individual contexts. A minimal example is:
The deployment descriptors are in [[Jetty/Reference/jetty.xml syntax|jetty xml]] format and define and configure individual contexts. A minimal example is:
+
 
<source lang="xml">
 
<source lang="xml">
 
  <?xml version="1.0"  encoding="ISO-8859-1"?>
 
  <?xml version="1.0"  encoding="ISO-8859-1"?>
Line 32: Line 34:
 
</source>
 
</source>
  
This example creates an instance of [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java?root=RT_JETTY&view=markup WebAppContext] and sets the '''contextPath''' to be "/test" and the '''resourceBase''' to be "$jetty.home/webapps/test". Because the context used is a standard web application context, when started it will inspect the resourceBase for a WEB-INF/web.xml for further configuration.
+
This example creates an instance of [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java?root=RT_JETTY&view=markup WebAppContext] and sets the ''contextPath'' to <tt>/test</tt> and the resourceBase to <tt>$jetty.home/webapps/test</tt>. Because the context used is a standard web application context, when started it inspects the resourceBase for a <tt>WEB-INF/web.xml</tt> for further configuration.
  
The ContextDeployer is added to the server as a LifeCycle. This simply means that the deployer will be started and stopped with the server. Ie when server.start() is called, then start will also be called on the deployer.
+
The ContextProvider is added to the server as a LifeCycle. This simply means that the provider starts and stops with the server. That is, when <tt>server.start()</tt> is called, then <tt>start</tt> is also called on the provider.
  
===Property value substitution===
+
===Property Value Substitution===
  
The ContextDeployer can automatically do property substitution on the context files that it deploys. You define a ConfigurationManager that manages the properties and pass this into the ContextDeployer. There is currently one implementation of the ConfigurationManager, and that is the <tt>org.mortbay.jetty.deployer.FileConfigurationManager</tt> that reads a properties file and makes available the property values to the ContextDeployer. Here's how you would configure the ContextDeployer:
+
The ContextProvider can automatically do property substitution on the context files that it deploys. You define a ConfigurationManager that manages the properties and pass this into the ContextProvider. There is currently one implementation of the ConfigurationManager, and that is the <tt>org.mortbay.jetty.deployer.FileConfigurationManager</tt>, which reads a properties file and makes available the property values to the ContextProvider. Here's how you would configure the ContextProvider:
  
 
<source lang="xml">
 
<source lang="xml">
  <Call name="addLifeCycle">
+
  <Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Arg>
+
        <Ref id="DeploymentManager">
    <New class="org.mortbay.jetty.deployer.ContextDeployer">
+
          <Call name="addAppProvider">
      <Set name="contexts"><Ref id="Contexts"/></Set>
+
            <Arg>
      <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>
+
              <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
      <Set name="scanInterval">1</Set>
+
                <Set name="monitoredDir"><Property name="jetty.home" default="." />/contexts</Set>
      <Set name="configurationManager">
+
                <Set name="scanInterval">1</Set>
        <New class="org.mortbay.jetty.deployer.FileConfigurationManager">
+
                <Set name="configurationManager">
          <Set name="file"><SystemProperty name="jetty.home" default="."/>/foo.properties</Set>
+
                  <New class="org.mortbay.jetty.deployer.FileConfigurationManager">
        </New>
+
                    <Set name="file"><SystemProperty name="jetty.home" default="."/>/foo.properties</Set>
      </Set>
+
                  </New>
    </New>
+
                </Set>
  </Arg>
+
              </New>
</Call>
+
            </Arg>
 +
          </Call>
 +
        </Ref>
 +
</Configure>
 
</source>
 
</source>
Here's an example of the contents of the foo.properties file:
+
Here's an example of the contents of the <tt>foo.properties</tt> file:
 
<source lang="text">  
 
<source lang="text">  
 
  foo = /funkyapp
 
  foo = /funkyapp
Line 73: Line 78:
 
==Contexts==
 
==Contexts==
  
Because the class of the context is defined in the Configure clause, any type of ContextHandler may be deployed with this mechanism, included base [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java?root=RT_JETTY&view=markup ContextHandler], [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java?root=RT_JETTY&view=markup ServletContextHandler], [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java?root=RT_JETTY&view=markup WebAppContext] or any class derived from them.
+
Because the class of the context is defined in the Configure clause, you can deploy any type of ContextHandler with this mechanism, including base [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java?root=RT_JETTY&view=markup ContextHandler], [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java?root=RT_JETTY&view=markup ServletContextHandler], [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java?root=RT_JETTY&view=markup WebAppContext], or any class derived from them.
  
==Hot deploy==
+
==Hot Deploy==
  
If the scan interval is non-zero, the configuration directory is scanned at that interval (in seconds) for changes to the deployment descriptors. If a descriptor is added to the directory, the new context will be deployed. If a descriptor is touched/updated then it's context will be stopped, reconfigured and redeployed. If a descriptor is removed, then it's context will be stopped and removed from the server.
+
If the scan interval is non-zero, the ContextProvider scans the configuration directory at that interval (in seconds) for changes to the deployment descriptors. If you add a descriptor to the directory, it deploys the new context. If you touch or update a descriptor, the provider stops, reconfigures, and redeploys its context. If you remove a descriptor, the provider stops and removes its context from the server.
  
If the scan interval is zero, then the directory is only scanned at startup.
+
If the scan interval is zero, the ContextProvider scans the directory only at startup.
  
==Other things to configure==
+
==Configuring Other Things ==
  
This mechanism allows the most of the API available on [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java?root=RT_JETTY&view=markup ContextHandler] or its derived classes to be called to configure the web application. Useful things to configure include:
+
This mechanism allows you to call most of the API available on [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java?root=RT_JETTY&view=markup ContextHandler] or its derived classes to configure the web application. Useful things to configure include:
  
* setAttribute
+
* <tt>setAttribute</tt>
* setClassLoader
+
* <tt>setClassLoader</tt>
* setContextPath
+
* <tt>setContextPath</tt>
* setVirtualHosts
+
* <tt>setVirtualHosts</tt>
* addServlet
+
* <tt>addServlet</tt>
* setConfigurations
+
* <tt>setConfigurations</tt>
* setExtraClassPath
+
* <tt>setExtraClassPath</tt>
* setDefaultsDescriptor
+
* <tt>setDefaultsDescriptor</tt>
* setDescriptor
+
* <tt>setDescriptor</tt>
* setOverrideDescriptor
+
* <tt>setOverrideDescriptor</tt>
* setSystemClasses
+
* <tt>setSystemClasses</tt>
* setServerClasses
+
* <tt>setServerClasses</tt>
 
| category = [[Category:Jetty Feature]]
 
| category = [[Category:Jetty Feature]]
 
}}
 
}}

Revision as of 14:18, 6 June 2011



Introduction

Jetty provides capability to deploy an arbitrary context or web application with Jetty-specific configuration. The ContextProvider mechanism, which is now an extension of the core deployment infrastructure, implements this capability.

You can use the ContextProvider to (hot) deploy an arbitrary context or web application with Jetty-specific configuration. To statically deploy only standard web applications at startup, use the WebApp Provider.

Feature

A jetty.xml typically defines a ContextProvider; within the stock jetty distribution the name of this file isjetty-contexts.xml:

 
 <Configure id="Server" class="org.eclipse.jetty.server.Server">
        <Ref id="DeploymentManager">
          <Call name="addAppProvider">
            <Arg>
              <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
                <Set name="monitoredDir"><Property name="jetty.home" default="." />/contexts</Set>
                <Set name="scanInterval">1</Set>
              </New>
            </Arg>
          </Call>
        </Ref>
</Configure>

The ContextProvider scans the monitoredDir directory at intervals of scanInterval seconds for XML descriptors that define contexts. It then deploys any contexts to the passed contexts reference to a HandlerContainer (normally an instance of ContextHandlerCollection). The deployment descriptors are in jetty xml format, and define and configure individual contexts. A minimal example is:

 <?xml version="1.0"  encoding="ISO-8859-1"?>
 <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
 <Configure class="org.eclipse.jetty.webapp.WebAppContext">
   <Set name="contextPath">/test</Set>
   <Set name="war"><SystemProperty name="jetty.home" default<nowiki>=</nowiki>"."/>/webapps/test</Set>
 </Configure>

This example creates an instance of WebAppContext and sets the contextPath to /test and the resourceBase to $jetty.home/webapps/test. Because the context used is a standard web application context, when started it inspects the resourceBase for a WEB-INF/web.xml for further configuration.

The ContextProvider is added to the server as a LifeCycle. This simply means that the provider starts and stops with the server. That is, when server.start() is called, then start is also called on the provider.

Property Value Substitution

The ContextProvider can automatically do property substitution on the context files that it deploys. You define a ConfigurationManager that manages the properties and pass this into the ContextProvider. There is currently one implementation of the ConfigurationManager, and that is the org.mortbay.jetty.deployer.FileConfigurationManager, which reads a properties file and makes available the property values to the ContextProvider. Here's how you would configure the ContextProvider:

 <Configure id="Server" class="org.eclipse.jetty.server.Server">
        <Ref id="DeploymentManager">
          <Call name="addAppProvider">
            <Arg>
              <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
                <Set name="monitoredDir"><Property name="jetty.home" default="." />/contexts</Set>
                <Set name="scanInterval">1</Set>
                <Set name="configurationManager">
                  <New class="org.mortbay.jetty.deployer.FileConfigurationManager">
                     <Set name="file"><SystemProperty name="jetty.home" default="."/>/foo.properties</Set>
                  </New>
                </Set>
              </New>
            </Arg>
          </Call>
        </Ref>
</Configure>

Here's an example of the contents of the foo.properties file:

 
 foo = /funkyapp

Here's how you would use this in a context xml file:

 <?xml version="1.0"  encoding="ISO-8859-1"?>
 <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
 <Configure class="org.mortbay.jetty.webapp.WebAppContext">
   <Set name="contextPath"><Property name="foo"/></Set>
   <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test</Set>
 </Configure>

Contexts

Because the class of the context is defined in the Configure clause, you can deploy any type of ContextHandler with this mechanism, including base ContextHandler, ServletContextHandler, WebAppContext, or any class derived from them.

Hot Deploy

If the scan interval is non-zero, the ContextProvider scans the configuration directory at that interval (in seconds) for changes to the deployment descriptors. If you add a descriptor to the directory, it deploys the new context. If you touch or update a descriptor, the provider stops, reconfigures, and redeploys its context. If you remove a descriptor, the provider stops and removes its context from the server.

If the scan interval is zero, the ContextProvider scans the directory only at startup.

Configuring Other Things

This mechanism allows you to call most of the API available on ContextHandler or its derived classes to configure the web application. Useful things to configure include:

  • setAttribute
  • setClassLoader
  • setContextPath
  • setVirtualHosts
  • addServlet
  • setConfigurations
  • setExtraClassPath
  • setDefaultsDescriptor
  • setDescriptor
  • setOverrideDescriptor
  • setSystemClasses
  • setServerClasses