View Javadoc

1   package org.eclipse.jetty.deploy.providers;
2   
3   import java.io.File;
4   import java.io.FilenameFilter;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import org.eclipse.jetty.deploy.App;
9   import org.eclipse.jetty.deploy.ConfigurationManager;
10  import org.eclipse.jetty.deploy.ContextDeployer;
11  import org.eclipse.jetty.deploy.util.FileID;
12  import org.eclipse.jetty.server.handler.ContextHandler;
13  import org.eclipse.jetty.util.resource.Resource;
14  import org.eclipse.jetty.xml.XmlConfiguration;
15  
16  
17  /* ------------------------------------------------------------ */
18  /** Context directory App Provider.
19   * <p>This specialization of {@link MonitoredDirAppProvider} is the
20   * replacement for {@link ContextDeployer} and it will scan a directory
21   * only for context.xml files.
22   * @see ContextDeployer
23   */
24  public class ContextProvider extends ScanningAppProvider
25  {
26      private ConfigurationManager _configurationManager;
27  
28      public ContextProvider()
29      {
30          super(  new FilenameFilter()
31          {
32              public boolean accept(File dir, String name)
33              {
34                  if (!dir.exists())
35                      return false;
36                  String lowername = name.toLowerCase();
37                  return  (lowername.endsWith(".xml") && !new File(dir,name).isDirectory());
38              }
39          });
40      }
41  
42  
43      /* ------------------------------------------------------------ */
44      public ConfigurationManager getConfigurationManager()
45      {
46          return _configurationManager;
47      }
48      
49      /* ------------------------------------------------------------ */
50      /** Set the configurationManager.
51       * @param configurationManager the configurationManager to set
52       */
53      public void setConfigurationManager(ConfigurationManager configurationManager)
54      {
55          _configurationManager = configurationManager;
56      }
57  
58      /* ------------------------------------------------------------ */
59      public ContextHandler createContextHandler(App app) throws Exception
60      {
61          Resource resource = Resource.newResource(app.getOriginId());
62          File file = resource.getFile();
63          
64          if (resource.exists() && FileID.isXmlFile(file))
65          {
66              XmlConfiguration xmlc = new XmlConfiguration(resource.getURL());
67              Map<String,Object> props = new HashMap<String,Object>();
68              props.put("Server",getDeploymentManager().getServer());
69              if (getConfigurationManager() != null)
70                  props.putAll(getConfigurationManager().getProperties());
71              xmlc.setProperties(props);
72              return (ContextHandler)xmlc.configure();
73          }
74          
75          throw new IllegalStateException("App resouce does not exist "+resource);
76      }
77      
78  }