View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.deploy.providers;
20  
21  import java.io.File;
22  import java.io.FilenameFilter;
23  
24  import org.eclipse.jetty.deploy.App;
25  import org.eclipse.jetty.deploy.ConfigurationManager;
26  import org.eclipse.jetty.deploy.util.FileID;
27  import org.eclipse.jetty.server.handler.ContextHandler;
28  import org.eclipse.jetty.util.resource.Resource;
29  import org.eclipse.jetty.xml.XmlConfiguration;
30  
31  /** Context directory App Provider.
32   * <p>This specialization of {@link ScanningAppProvider} is the
33   * replacement for the old (and deprecated) <code>org.eclipse.jetty.deploy.ContextDeployer</code> and it will scan a directory
34   * only for context.xml files.
35   */
36  public class ContextProvider extends ScanningAppProvider
37  {
38      private ConfigurationManager _configurationManager;
39  
40      public ContextProvider()
41      {
42          super(  new FilenameFilter()
43          {
44              public boolean accept(File dir, String name)
45              {
46                  if (!dir.exists())
47                      return false;
48                  String lowername = name.toLowerCase();
49                  if (lowername.startsWith("."))
50                      return false;
51                  
52                  return  (lowername.endsWith(".xml") && !new File(dir,name).isDirectory());
53              }
54          });
55      }
56  
57  
58      /* ------------------------------------------------------------ */
59      public ConfigurationManager getConfigurationManager()
60      {
61          return _configurationManager;
62      }
63      
64      /* ------------------------------------------------------------ */
65      /** Set the configurationManager.
66       * @param configurationManager the configurationManager to set
67       */
68      public void setConfigurationManager(ConfigurationManager configurationManager)
69      {
70          _configurationManager = configurationManager;
71      }
72  
73      /* ------------------------------------------------------------ */
74      public ContextHandler createContextHandler(App app) throws Exception
75      {
76          Resource resource = Resource.newResource(app.getOriginId());
77          File file = resource.getFile();
78          
79          if (resource.exists() && FileID.isXmlFile(file))
80          {
81              XmlConfiguration xmlc = new XmlConfiguration(resource.getURL());
82              
83              xmlc.getIdMap().put("Server",getDeploymentManager().getServer());
84              if (getConfigurationManager() != null)
85                  xmlc.getProperties().putAll(getConfigurationManager().getProperties());
86              return (ContextHandler)xmlc.configure();
87          }
88          
89          throw new IllegalStateException("App resouce does not exist "+resource);
90      }
91      
92  }