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.webapp.WebAppContext;
31 import org.eclipse.jetty.xml.XmlConfiguration;
32
33 /** Context directory App Provider.
34 * <p>This specialization of {@link ScanningAppProvider} is the
35 * replacement for the old (and deprecated) <code>org.eclipse.jetty.deploy.ContextDeployer</code> and it will scan a directory
36 * only for context.xml files.
37 */
38 public class ContextProvider extends ScanningAppProvider
39 {
40 private ConfigurationManager _configurationManager;
41 private boolean _parentLoaderPriority = false;
42 private String _defaultsDescriptor;
43
44 public ContextProvider()
45 {
46 super( new FilenameFilter()
47 {
48 public boolean accept(File dir, String name)
49 {
50 if (!dir.exists())
51 return false;
52 String lowername = name.toLowerCase(Locale.ENGLISH);
53 if (lowername.startsWith("."))
54 return false;
55
56 return (lowername.endsWith(".xml") && !new File(dir,name).isDirectory());
57 }
58 });
59 }
60
61
62 /* ------------------------------------------------------------ */
63 public ConfigurationManager getConfigurationManager()
64 {
65 return _configurationManager;
66 }
67
68 /* ------------------------------------------------------------ */
69 /** Set the configurationManager.
70 * @param configurationManager the configurationManager to set
71 */
72 public void setConfigurationManager(ConfigurationManager configurationManager)
73 {
74 _configurationManager = configurationManager;
75 }
76
77 /* ------------------------------------------------------------ */
78 public ContextHandler createContextHandler(App app) throws Exception
79 {
80 Resource resource = Resource.newResource(app.getOriginId());
81 File file = resource.getFile();
82
83 if (resource.exists() && FileID.isXmlFile(file))
84 {
85 XmlConfiguration xmlc = new XmlConfiguration(resource.getURL())
86 {
87 @Override
88 public void initializeDefaults(Object context)
89 {
90 super.initializeDefaults(context);
91
92 if (context instanceof WebAppContext)
93 {
94 WebAppContext webapp = (WebAppContext)context;
95 webapp.setParentLoaderPriority(_parentLoaderPriority);
96 if (_defaultsDescriptor!=null)
97 webapp.setDefaultsDescriptor(_defaultsDescriptor);
98 }
99 }
100 };
101
102 xmlc.getIdMap().put("Server",getDeploymentManager().getServer());
103 if (getConfigurationManager() != null)
104 xmlc.getProperties().putAll(getConfigurationManager().getProperties());
105 return (ContextHandler)xmlc.configure();
106 }
107
108 throw new IllegalStateException("App resouce does not exist "+resource);
109 }
110
111 /* ------------------------------------------------------------ */
112 /** Get the parentLoaderPriority.
113 * @return the parentLoaderPriority
114 */
115 public boolean isParentLoaderPriority()
116 {
117 return _parentLoaderPriority;
118 }
119
120 /* ------------------------------------------------------------ */
121 /** Set the parentLoaderPriority.
122 * <p>If the context created is a WebAppContext, then set the
123 * default value for {@link WebAppContext#setParentLoaderPriority(boolean)}.
124 * @param parentLoaderPriority the parentLoaderPriority to set
125 */
126 public void setParentLoaderPriority(boolean parentLoaderPriority)
127 {
128 _parentLoaderPriority = parentLoaderPriority;
129 }
130
131 /* ------------------------------------------------------------ */
132 /** Get the defaultsDescriptor.
133 * @return the defaultsDescriptor
134 */
135 public String getDefaultsDescriptor()
136 {
137 return _defaultsDescriptor;
138 }
139
140 /* ------------------------------------------------------------ */
141 /** Set the defaultsDescriptor.
142 * <p>If the context created is a WebAppContext, then set the
143 * default value for {@link WebAppContext#setDefaultsDescriptor(String)}
144 * @param defaultsDescriptor the defaultsDescriptor to set
145 */
146 public void setDefaultsDescriptor(String defaultsDescriptor)
147 {
148 _defaultsDescriptor = defaultsDescriptor;
149 }
150 }