View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  import java.util.Locale;
24  
25  import org.eclipse.jetty.deploy.App;
26  import org.eclipse.jetty.deploy.ConfigurationManager;
27  import org.eclipse.jetty.deploy.util.FileID;
28  import org.eclipse.jetty.server.handler.ContextHandler;
29  import org.eclipse.jetty.util.resource.Resource;
30  import org.eclipse.jetty.xml.XmlConfiguration;
31  
32  /** Context directory App Provider.
33   * <p>This specialization of {@link ScanningAppProvider} is the
34   * replacement for the old (and deprecated) <code>org.eclipse.jetty.deploy.ContextDeployer</code> and it will scan a directory
35   * only for context.xml files.
36   */
37  public class ContextProvider extends ScanningAppProvider
38  {
39      private ConfigurationManager _configurationManager;
40  
41      public ContextProvider()
42      {
43          super(  new FilenameFilter()
44          {
45              public boolean accept(File dir, String name)
46              {
47                  if (!dir.exists())
48                      return false;
49                  String lowername = name.toLowerCase(Locale.ENGLISH);
50                  if (lowername.startsWith("."))
51                      return false;
52                  
53                  return  (lowername.endsWith(".xml") && !new File(dir,name).isDirectory());
54              }
55          });
56      }
57  
58  
59      /* ------------------------------------------------------------ */
60      public ConfigurationManager getConfigurationManager()
61      {
62          return _configurationManager;
63      }
64      
65      /* ------------------------------------------------------------ */
66      /** Set the configurationManager.
67       * @param configurationManager the configurationManager to set
68       */
69      public void setConfigurationManager(ConfigurationManager configurationManager)
70      {
71          _configurationManager = configurationManager;
72      }
73  
74      /* ------------------------------------------------------------ */
75      public ContextHandler createContextHandler(App app) throws Exception
76      {
77          Resource resource = Resource.newResource(app.getOriginId());
78          File file = resource.getFile();
79          
80          if (resource.exists() && FileID.isXmlFile(file))
81          {
82              XmlConfiguration xmlc = new XmlConfiguration(resource.getURL());
83              
84              xmlc.getIdMap().put("Server",getDeploymentManager().getServer());
85              if (getConfigurationManager() != null)
86                  xmlc.getProperties().putAll(getConfigurationManager().getProperties());
87              return (ContextHandler)xmlc.configure();
88          }
89          
90          throw new IllegalStateException("App resouce does not exist "+resource);
91      }
92      
93  }