Merge changes from topic 'events'
* changes: Add a custom JSON deserializer for ChangeEvents Add EventTypes class registering all stream event types
This commit is contained in:
@@ -415,6 +415,13 @@ methods in the `ChangeHookRunner` class, passing an instance of
|
|||||||
its own custom event class derived from
|
its own custom event class derived from
|
||||||
`com.google.gerrit.server.events.Event`.
|
`com.google.gerrit.server.events.Event`.
|
||||||
|
|
||||||
|
Plugins which define new Events should register them via the
|
||||||
|
`com.google.gerrit.server.events.EventTypes.registerClass()`
|
||||||
|
method. This will make the EventType known to the system.
|
||||||
|
Deserialzing events with the
|
||||||
|
`com.google.gerrit.server.events.EventDeserializer` class requires
|
||||||
|
that the event be registered in EventTypes.
|
||||||
|
|
||||||
[[validation]]
|
[[validation]]
|
||||||
== Validation Listeners
|
== Validation Listeners
|
||||||
|
|
||||||
|
|||||||
@@ -21,15 +21,19 @@ import org.eclipse.jgit.revwalk.RevCommit;
|
|||||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||||
|
|
||||||
public class CommitReceivedEvent extends RefEvent {
|
public class CommitReceivedEvent extends RefEvent {
|
||||||
public final ReceiveCommand command;
|
public ReceiveCommand command;
|
||||||
public final Project project;
|
public Project project;
|
||||||
public final String refName;
|
public String refName;
|
||||||
public final RevCommit commit;
|
public RevCommit commit;
|
||||||
public final IdentifiedUser user;
|
public IdentifiedUser user;
|
||||||
|
|
||||||
|
public CommitReceivedEvent() {
|
||||||
|
super("commit-received");
|
||||||
|
}
|
||||||
|
|
||||||
public CommitReceivedEvent(ReceiveCommand command, Project project,
|
public CommitReceivedEvent(ReceiveCommand command, Project project,
|
||||||
String refName, RevCommit commit, IdentifiedUser user) {
|
String refName, RevCommit commit, IdentifiedUser user) {
|
||||||
super("commit-received");
|
this();
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.project = project;
|
this.project = project;
|
||||||
this.refName = refName;
|
this.refName = refName;
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
// 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.events;
|
||||||
|
|
||||||
|
import com.google.gson.JsonDeserializationContext;
|
||||||
|
import com.google.gson.JsonDeserializer;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON deserializer for {@link Event}s.
|
||||||
|
* <p>
|
||||||
|
* Deserialized objects are of an appropriate subclass based on the value of the
|
||||||
|
* top-level "type" element.
|
||||||
|
*/
|
||||||
|
public class EventDeserializer implements JsonDeserializer<Event> {
|
||||||
|
@Override
|
||||||
|
public Event deserialize(JsonElement json, Type typeOfT,
|
||||||
|
JsonDeserializationContext context) throws JsonParseException {
|
||||||
|
if (!json.isJsonObject()) {
|
||||||
|
throw new JsonParseException("Not an object");
|
||||||
|
}
|
||||||
|
JsonElement typeJson = json.getAsJsonObject().get("type");
|
||||||
|
if (typeJson == null || !typeJson.isJsonPrimitive()
|
||||||
|
|| !typeJson.getAsJsonPrimitive().isString()) {
|
||||||
|
throw new JsonParseException("Type is not a string: " + typeJson);
|
||||||
|
}
|
||||||
|
String type = typeJson.getAsJsonPrimitive().getAsString();
|
||||||
|
Class<?> cls = EventTypes.getClass(type);
|
||||||
|
if (cls == null) {
|
||||||
|
throw new JsonParseException("Unknown event type: " + type);
|
||||||
|
}
|
||||||
|
return context.deserialize(json, cls);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
// 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.events;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/** Class for registering event types */
|
||||||
|
public class EventTypes {
|
||||||
|
private static final Map<String, Class<?>> typesByString = new HashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
registerClass(new ChangeAbandonedEvent());
|
||||||
|
registerClass(new ChangeMergedEvent());
|
||||||
|
registerClass(new ChangeRestoredEvent());
|
||||||
|
registerClass(new CommentAddedEvent());
|
||||||
|
registerClass(new CommitReceivedEvent());
|
||||||
|
registerClass(new DraftPublishedEvent());
|
||||||
|
registerClass(new HashtagsChangedEvent());
|
||||||
|
registerClass(new MergeFailedEvent());
|
||||||
|
registerClass(new RefUpdatedEvent());
|
||||||
|
registerClass(new RefReceivedEvent());
|
||||||
|
registerClass(new ReviewerAddedEvent());
|
||||||
|
registerClass(new PatchSetCreatedEvent());
|
||||||
|
registerClass(new TopicChangedEvent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Register an event.
|
||||||
|
*
|
||||||
|
* @param event The event to register.
|
||||||
|
* @throws IllegalArgumentException if the event's type is already
|
||||||
|
* registered.
|
||||||
|
**/
|
||||||
|
public static void registerClass(Event event) {
|
||||||
|
String type = event.getType();
|
||||||
|
if (typesByString.containsKey(type)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Event type already registered: " + type);
|
||||||
|
}
|
||||||
|
typesByString.put(type, event.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the class for an event type.
|
||||||
|
*
|
||||||
|
* @param type The type.
|
||||||
|
* @return The event class, or null if no class is registered with the
|
||||||
|
* given type
|
||||||
|
**/
|
||||||
|
public static Class<?> getClass(String type) {
|
||||||
|
return typesByString.get(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user