View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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 if unable to pre configure
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 if unable to configure
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 if unable to post configure
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 if unable to deconfigure
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 if unable to destroy
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 if unable to clone
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=null;
132             if (server!=null)
133             {
134                 cl= server.getBean(ClassList.class);
135                 if (cl!=null)
136                     return new ClassList(cl);
137                 Object attr = server.getAttribute(ATTR);
138                 if (attr instanceof ClassList)
139                     return new ClassList((ClassList)attr);
140                 if (attr instanceof String[])
141                     return new ClassList((String[])attr);
142             }
143             return new ClassList();
144         }
145         
146         public ClassList()
147         {
148             this(WebAppContext.DEFAULT_CONFIGURATION_CLASSES);
149         }
150         
151         public ClassList(String[] classes)
152         {
153             addAll(Arrays.asList(classes));
154         }
155 
156         public ClassList(List<String> classes)
157         {
158             addAll(classes);
159         }
160         
161         public void addAfter(@Name("afterClass") String afterClass,@Name("configClass")String... configClass)
162         {
163             if (configClass!=null && afterClass!=null)
164             {
165                 ListIterator<String> iter = listIterator();
166                 while (iter.hasNext())
167                 {
168                     String cc=iter.next();
169                     if (afterClass.equals(cc))
170                     {
171                         for (int i=0;i<configClass.length;i++)
172                             iter.add(configClass[i]);
173                         return;
174                     }
175                 }
176             }
177             throw new IllegalArgumentException("afterClass '"+afterClass+"' not found in "+this);
178         }
179 
180         public void addBefore(@Name("beforeClass") String beforeClass,@Name("configClass")String... configClass)
181         {
182             if (configClass!=null && beforeClass!=null)
183             {
184                 ListIterator<String> iter = listIterator();
185                 while (iter.hasNext())
186                 {
187                     String cc=iter.next();
188                     if (beforeClass.equals(cc))
189                     {
190                         iter.previous();
191                         for (int i=0;i<configClass.length;i++)
192                             iter.add(configClass[i]);
193                         return;
194                     }
195                 }
196             }
197             throw new IllegalArgumentException("beforeClass '"+beforeClass+"' not found in "+this);
198         }
199         
200     }
201 }