1 //
2 // ========================================================================
3 // Copyright (c) 1995-2015 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.webapp;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.ListIterator;
25
26 import org.eclipse.jetty.server.Server;
27 import org.eclipse.jetty.util.annotation.Name;
28
29
30 /* ------------------------------------------------------------------------------- */
31 /** Base Class for WebApplicationContext Configuration.
32 * This class can be extended to customize or extend the configuration
33 * of the WebApplicationContext.
34 */
35 public interface Configuration
36 {
37 public final static String ATTR="org.eclipse.jetty.webapp.configuration";
38
39 /* ------------------------------------------------------------------------------- */
40 /** Set up for configuration.
41 * <p>
42 * Typically this step discovers configuration resources
43 * @param context The context to configure
44 * @throws Exception
45 */
46 public void preConfigure (WebAppContext context) throws Exception;
47
48
49 /* ------------------------------------------------------------------------------- */
50 /** Configure WebApp.
51 * <p>
52 * Typically this step applies the discovered configuration resources to
53 * either the {@link WebAppContext} or the associated {@link MetaData}.
54 * @param context The context to configure
55 * @throws Exception
56 */
57 public void configure (WebAppContext context) throws Exception;
58
59
60 /* ------------------------------------------------------------------------------- */
61 /** Clear down after configuration.
62 * @param context The context to configure
63 * @throws Exception
64 */
65 public void postConfigure (WebAppContext context) throws Exception;
66
67 /* ------------------------------------------------------------------------------- */
68 /** DeConfigure WebApp.
69 * This method is called to undo all configuration done. This is
70 * called to allow the context to work correctly over a stop/start cycle
71 * @param context The context to configure
72 * @throws Exception
73 */
74 public void deconfigure (WebAppContext context) throws Exception;
75
76 /* ------------------------------------------------------------------------------- */
77 /** Destroy WebApp.
78 * This method is called to destroy a webappcontext. It is typically called when a context
79 * is removed from a server handler hierarchy by the deployer.
80 * @param context The context to configure
81 * @throws Exception
82 */
83 public void destroy (WebAppContext context) throws Exception;
84
85
86 /* ------------------------------------------------------------------------------- */
87 /** Clone configuration instance.
88 * <p>
89 * Configure an instance of a WebAppContext, based on a template WebAppContext that
90 * has previously been configured by this Configuration.
91 * @param template The template context
92 * @param context The context to configure
93 * @throws Exception
94 */
95 public void cloneConfigure (WebAppContext template, WebAppContext context) throws Exception;
96
97
98 public class ClassList extends ArrayList<String>
99 {
100 /* ------------------------------------------------------------ */
101 /** Get/Set/Create the server default Configuration ClassList.
102 * <p>Get the class list from: a Server bean; or the attribute (which can
103 * either be a ClassList instance or an String[] of class names); or a new instance
104 * with default configuration classes.</p>
105 * <p>This method also adds the obtained ClassList instance as a dependent bean
106 * on the server and clears the attribute</p>
107 * @param server The server the default is for
108 * @return the server default ClassList instance of the configuration classes for this server. Changes to this list will change the server default instance.
109 */
110 public static ClassList setServerDefault(Server server)
111 {
112 ClassList cl=server.getBean(ClassList.class);
113 if (cl!=null)
114 return cl;
115 cl=serverDefault(server);
116 server.addBean(cl);
117 server.setAttribute(ATTR,null);
118 return cl;
119 }
120
121 /* ------------------------------------------------------------ */
122 /** Get/Create the server default Configuration ClassList.
123 * <p>Get the class list from: a Server bean; or the attribute (which can
124 * either be a ClassList instance or an String[] of class names); or a new instance
125 * with default configuration classes.
126 * @param server The server the default is for
127 * @return A copy of the server default ClassList instance of the configuration classes for this server. Changes to the returned list will not change the server default.
128 */
129 public static ClassList serverDefault(Server server)
130 {
131 ClassList cl=server.getBean(ClassList.class);
132 if (cl!=null)
133 return new ClassList(cl);
134 Object attr = server.getAttribute(ATTR);
135 if (attr instanceof ClassList)
136 return new ClassList((ClassList)attr);
137 if (attr instanceof String[])
138 return new ClassList((String[])attr);
139 return new ClassList();
140 }
141
142 public ClassList()
143 {
144 this(WebAppContext.DEFAULT_CONFIGURATION_CLASSES);
145 }
146
147 public ClassList(String[] classes)
148 {
149 addAll(Arrays.asList(classes));
150 }
151
152 public ClassList(List<String> classes)
153 {
154 addAll(classes);
155 }
156
157 public void addAfter(@Name("afterClass") String afterClass,@Name("configClass")String... configClass)
158 {
159 if (configClass!=null && afterClass!=null)
160 {
161 ListIterator<String> iter = listIterator();
162 while (iter.hasNext())
163 {
164 String cc=iter.next();
165 if (afterClass.equals(cc))
166 {
167 for (int i=0;i<configClass.length;i++)
168 iter.add(configClass[i]);
169 return;
170 }
171 }
172 }
173 throw new IllegalArgumentException("afterClass '"+afterClass+"' not found in "+this);
174 }
175
176 public void addBefore(@Name("beforeClass") String beforeClass,@Name("configClass")String... configClass)
177 {
178 if (configClass!=null && beforeClass!=null)
179 {
180 ListIterator<String> iter = listIterator();
181 while (iter.hasNext())
182 {
183 String cc=iter.next();
184 if (beforeClass.equals(cc))
185 {
186 iter.previous();
187 for (int i=0;i<configClass.length;i++)
188 iter.add(configClass[i]);
189 return;
190 }
191 }
192 }
193 throw new IllegalArgumentException("beforeClass '"+beforeClass+"' not found in "+this);
194 }
195
196 }
197 }