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.quickstart;
20  
21  import org.eclipse.jetty.util.log.Log;
22  import org.eclipse.jetty.util.log.Logger;
23  import org.eclipse.jetty.webapp.Descriptor;
24  import org.eclipse.jetty.webapp.IterativeDescriptorProcessor;
25  import org.eclipse.jetty.webapp.WebAppContext;
26  import org.eclipse.jetty.xml.XmlParser;
27  
28  /**
29   * Preconfigure DescriptorProcessor
30   *
31   * Saves literal XML snippets
32   *
33   */
34  
35  public class PreconfigureDescriptorProcessor extends IterativeDescriptorProcessor
36  {
37      private static final Logger LOG = Log.getLogger(PreconfigureDescriptorProcessor.class);
38      
39      private final StringBuilder _buffer = new StringBuilder();
40      private final boolean _showOrigin;
41      private String _origin;
42  
43      public PreconfigureDescriptorProcessor ()
44      {
45          _showOrigin=LOG.isDebugEnabled();
46          try
47          {
48              registerVisitor("env-entry", getClass().getMethod("saveSnippet", __signature));
49              registerVisitor("resource-ref", getClass().getMethod("saveSnippet", __signature));
50              registerVisitor("resource-env-ref", getClass().getMethod("saveSnippet", __signature));
51              registerVisitor("message-destination-ref", getClass().getMethod("saveSnippet", __signature));
52              registerVisitor("data-source", getClass().getMethod("saveSnippet", __signature));
53          }
54          catch (Exception e)
55          {
56              throw new IllegalStateException(e);
57          }
58      }
59  
60      @Override
61      public void start(WebAppContext context, Descriptor descriptor)
62      {
63          LOG.debug("process {}",descriptor);
64          _origin=("  <!-- "+descriptor+" -->\n");
65      }
66  
67  
68      @Override
69      public void end(WebAppContext context,Descriptor descriptor)
70      {
71      }
72  
73  
74      public void saveSnippet (WebAppContext context, Descriptor descriptor, XmlParser.Node node)
75      throws Exception
76      {
77          LOG.debug("save {}",node.getTag());
78          if (_showOrigin)
79              _buffer.append(_origin);
80          _buffer.append("  ").append(node.toString()).append("\n");
81      }
82      
83      public String getXML()
84      {
85          return _buffer.toString();
86      }
87  
88  }