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  
20  package org.eclipse.jetty.maven.plugin;
21  
22  import java.io.File;
23  import java.util.Iterator;
24  
25  import org.eclipse.jetty.quickstart.QuickStartConfiguration;
26  import org.eclipse.jetty.util.IO;
27  import org.eclipse.jetty.util.log.Log;
28  import org.eclipse.jetty.util.log.Logger;
29  import org.eclipse.jetty.util.resource.Resource;
30  import org.eclipse.jetty.util.resource.ResourceCollection;
31  import org.eclipse.jetty.webapp.WebAppClassLoader;
32  import org.eclipse.jetty.webapp.WebAppContext;
33  
34  /**
35   * MavenQuickStartConfiguration
36   *
37   *
38   */
39  public class MavenQuickStartConfiguration extends QuickStartConfiguration
40  {
41      private static final Logger LOG = Log.getLogger(QuickStartConfiguration.class);
42      
43      private Resource _quickStartWebXml;
44  
45  
46      public void setQuickStartWebXml (Resource r)
47      {
48          _quickStartWebXml = r;
49      }
50      
51     
52      
53      @Override
54      public Resource getQuickStartWebXml(WebAppContext context) throws Exception
55      {
56          return _quickStartWebXml;
57      }
58  
59      @Override
60      public void preConfigure(WebAppContext context) throws Exception
61      {
62          //check that webapp is suitable for quick start 
63          if (context.getBaseResource() == null)
64              throw new IllegalStateException ("No location for webapp");  
65  
66          
67          //look for quickstart-web.xml in WEB-INF of webapp
68          Resource quickStartWebXml = getQuickStartWebXml(context);
69          LOG.debug("quickStartWebXml={}",quickStartWebXml);
70          
71          context.getMetaData().setWebXml(quickStartWebXml);
72      }
73  
74  
75      @Override
76      public void configure(WebAppContext context) throws Exception
77      {
78          
79         JettyWebAppContext jwac = (JettyWebAppContext)context;
80          
81          //put the classes dir and all dependencies into the classpath
82          if (jwac.getClassPathFiles() != null)
83          {
84              if (LOG.isDebugEnabled()) LOG.debug("Setting up classpath ...");
85              Iterator itor = jwac.getClassPathFiles().iterator();
86              while (itor.hasNext())
87                  ((WebAppClassLoader)context.getClassLoader()).addClassPath(((File)itor.next()).getCanonicalPath());
88          }
89          
90          //Set up the quickstart environment for the context
91          super.configure(context);
92          
93          // knock out environmental maven and plexus classes from webAppContext
94          String[] existingServerClasses = context.getServerClasses();
95          String[] newServerClasses = new String[2+(existingServerClasses==null?0:existingServerClasses.length)];
96          newServerClasses[0] = "org.apache.maven.";
97          newServerClasses[1] = "org.codehaus.plexus.";
98          System.arraycopy( existingServerClasses, 0, newServerClasses, 2, existingServerClasses.length );
99          if (LOG.isDebugEnabled())
100         {
101             LOG.debug("Server classes:");
102             for (int i=0;i<newServerClasses.length;i++)
103                 LOG.debug(newServerClasses[i]);
104         }
105         context.setServerClasses( newServerClasses ); 
106     }
107     
108     @Override
109     public void deconfigure(WebAppContext context) throws Exception
110     {
111         //if we're not persisting the temp dir, get rid of any overlays
112         if (!context.isPersistTempDirectory())
113         {
114             Resource originalBases = (Resource)context.getAttribute("org.eclipse.jetty.resources.originalBases");
115             String originalBaseStr = originalBases.toString();
116 
117             //Iterate over all of the resource bases and ignore any that were original bases, just
118             //deleting the overlays
119             Resource res = context.getBaseResource();
120             if (res instanceof ResourceCollection)
121             {
122                 for (Resource r:((ResourceCollection)res).getResources())
123                 {
124                     if (originalBaseStr.contains(r.toString()))
125                         continue;
126                     IO.delete(r.getFile());
127                 }
128             }
129         }
130     }
131     
132 }