summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/plugins/PluginContentScanner.java
blob: b19d6de7cc3e05342282da3693d6ba7a3aa3d3ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.plugins;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.nio.file.NoSuchFileException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.Optional;
import java.util.jar.Manifest;

/**
 * Scans the plugin returning classes and resources.
 *
 * <p>Gerrit uses the scanner to automatically discover the classes and resources exported by the
 * plugin for auto discovery of exported SSH commands, Servlets and listeners.
 */
public interface PluginContentScanner {

  /** Scanner without resources. */
  PluginContentScanner EMPTY =
      new PluginContentScanner() {
        @Override
        public Manifest getManifest() throws IOException {
          return new Manifest();
        }

        @Override
        public Map<Class<? extends Annotation>, Iterable<ExtensionMetaData>> scan(
            String pluginName, Iterable<Class<? extends Annotation>> annotations)
            throws InvalidPluginException {
          return Collections.emptyMap();
        }

        @Override
        public Optional<PluginEntry> getEntry(String resourcePath) {
          return Optional.empty();
        }

        @Override
        public InputStream getInputStream(PluginEntry entry) throws IOException {
          throw new NoSuchFileException("Empty plugin");
        }

        @Override
        public Enumeration<PluginEntry> entries() {
          return Collections.emptyEnumeration();
        }
      };

  /**
   * Plugin class extension meta-data
   *
   * <p>Class name and annotation value of the class provided by a plugin to extend an existing
   * extension point in Gerrit.
   */
  class ExtensionMetaData {
    public final String className;
    public final String annotationValue;

    public ExtensionMetaData(String className, String annotationValue) {
      this.className = className;
      this.annotationValue = annotationValue;
    }
  }

  /**
   * Return the plugin meta-data manifest
   *
   * @return Manifest of the plugin or null if plugin has no meta-data
   * @throws IOException if an I/O problem occurred whilst accessing the Manifest
   */
  Manifest getManifest() throws IOException;

  /**
   * Scans the plugin for declared public annotated classes
   *
   * @param pluginName the plugin name
   * @param annotations annotations declared by the plugin classes
   * @return map of annotations and associated plugin classes found
   * @throws InvalidPluginException if the plugin is not valid or corrupted
   */
  Map<Class<? extends Annotation>, Iterable<ExtensionMetaData>> scan(
      String pluginName, Iterable<Class<? extends Annotation>> annotations)
      throws InvalidPluginException;

  /**
   * Return the plugin resource associated to a path
   *
   * @param resourcePath full path of the resource inside the plugin package
   * @return the resource object or Optional.absent() if the resource was not found
   * @throws IOException if there was a problem retrieving the resource
   */
  Optional<PluginEntry> getEntry(String resourcePath) throws IOException;

  /**
   * Return the InputStream of the resource entry
   *
   * @param entry resource entry inside the plugin package
   * @return the resource input stream
   * @throws IOException if there was an I/O problem accessing the resource
   */
  InputStream getInputStream(PluginEntry entry) throws IOException;

  /**
   * Return all the resources inside a plugin
   *
   * @return the enumeration of all resources found
   */
  Enumeration<PluginEntry> entries();
}