Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.extensions.events; 16 : 17 : import com.google.gerrit.common.UsedAt; 18 : import com.google.gerrit.extensions.events.PluginEventListener; 19 : import com.google.gerrit.server.plugincontext.PluginSetContext; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Singleton; 22 : 23 : /** Helper class to let plugins fire a plugin-specific event. */ 24 : @Singleton 25 : @UsedAt(UsedAt.Project.PLUGINS_ALL) 26 : public class PluginEvent { 27 : private final PluginSetContext<PluginEventListener> listeners; 28 : 29 : @Inject 30 0 : PluginEvent(PluginSetContext<PluginEventListener> listeners) { 31 0 : this.listeners = listeners; 32 0 : } 33 : 34 : public void fire(String pluginName, String type, String data) { 35 0 : if (!listeners.iterator().hasNext()) { 36 0 : return; 37 : } 38 0 : Event e = new Event(pluginName, type, data); 39 0 : listeners.runEach(l -> l.onPluginEvent(e)); 40 0 : } 41 : 42 : /** Event to be fired by plugins. */ 43 : private static class Event extends AbstractNoNotifyEvent implements PluginEventListener.Event { 44 : private final String pluginName; 45 : private final String type; 46 : private final String data; 47 : 48 0 : Event(String pluginName, String type, String data) { 49 0 : this.pluginName = pluginName; 50 0 : this.type = type; 51 0 : this.data = data; 52 0 : } 53 : 54 : @Override 55 : public String pluginName() { 56 0 : return pluginName; 57 : } 58 : 59 : @Override 60 : public String getType() { 61 0 : return type; 62 : } 63 : 64 : @Override 65 : public String getData() { 66 0 : return data; 67 : } 68 : } 69 : }