View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.manifests;
6   
7   import com.jcabi.log.Logger;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.net.URL;
11  import java.util.ArrayList;
12  import java.util.Collection;
13  import javax.servlet.ServletContext;
14  
15  /**
16   * Manifests in servlet context.
17   *
18   * Append attributes from the web application {@code MANIFEST.MF}.
19   *
20   * <p>You can use this class in your own
21   * {@link javax.servlet.Filter} or
22   * {@link javax.servlet.ServletContextListener},
23   * in order to inject {@code MANIFEST.MF} attributes to the class:
24   *
25   * <pre> Manifests.append(new ServletMfs(context));</pre>
26   *
27   * <p>The class is thread-safe.
28   *
29   * @since 1.0
30   */
31  public final class ServletMfs implements Mfs {
32  
33      /**
34       * Servlet context.
35       */
36      private final transient ServletContext ctx;
37  
38      /**
39       * Ctor.
40       * @param context Context
41       */
42      public ServletMfs(final ServletContext context) {
43          this.ctx = context;
44      }
45  
46      @Override
47      public Collection<InputStream> fetch() throws IOException {
48          final URL main = this.ctx.getResource("/META-INF/MANIFEST.MF");
49          final Collection<InputStream> streams = new ArrayList<>(1);
50          if (main == null) {
51              Logger.warn(this, "MANIFEST.MF not found in WAR package");
52          } else {
53              streams.add(main.openStream());
54          }
55          return streams;
56      }
57  
58  }