forked from eclipse-hawkbit/hawkbit
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encode class type information in message payload instead of header (e…
…clipse-hawkbit#383) * encode class type information in message payload instead of header Signed-off-by: Michael Hirsch <[email protected]>
- Loading branch information
1 parent
7a7e52d
commit 63adbd0
Showing
5 changed files
with
348 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/event/EventType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.hawkbit.event; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.hawkbit.repository.event.remote.DistributionSetDeletedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.DistributionSetTagDeletedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.DownloadProgressEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.TargetAssignDistributionSetEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.TargetDeletedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.TargetTagDeletedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.ActionCreatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.ActionUpdatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.CancelTargetAssignmentEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagCreatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagUpdateEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetUpdateEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.RolloutGroupCreatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.RolloutGroupUpdatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.RolloutUpdatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagCreatedEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagUpdateEvent; | ||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent; | ||
|
||
/** | ||
* The {@link EventType} class declares the event-type and it's corresponding | ||
* encoding value in the payload of an remote header. The event-type is encoded | ||
* into the payload of the message which is distributed. | ||
* | ||
* To encode and decode the event class type we need some conversation mapping | ||
* between the actual class and the corresponding integer value which is the | ||
* encoded value in the byte-payload. | ||
*/ | ||
public class EventType { | ||
|
||
private static final Map<Integer, Class<?>> TYPES = new HashMap<>(); | ||
|
||
/** | ||
* The associated event-type-value must remain the same as initially | ||
* declared. Otherwise messages cannot correctly de-serialized. | ||
*/ | ||
static { | ||
|
||
// target | ||
TYPES.put(1, TargetCreatedEvent.class); | ||
TYPES.put(2, TargetUpdatedEvent.class); | ||
TYPES.put(3, TargetDeletedEvent.class); | ||
TYPES.put(4, CancelTargetAssignmentEvent.class); | ||
TYPES.put(5, TargetAssignDistributionSetEvent.class); | ||
|
||
// target tag | ||
TYPES.put(6, TargetTagCreatedEvent.class); | ||
TYPES.put(7, TargetTagUpdateEvent.class); | ||
TYPES.put(8, TargetTagDeletedEvent.class); | ||
|
||
// action | ||
TYPES.put(9, ActionCreatedEvent.class); | ||
TYPES.put(10, ActionUpdatedEvent.class); | ||
|
||
// distribution set | ||
TYPES.put(11, DistributionSetCreatedEvent.class); | ||
TYPES.put(12, DistributionSetUpdateEvent.class); | ||
TYPES.put(13, DistributionSetDeletedEvent.class); | ||
|
||
// distribution set tag | ||
TYPES.put(14, DistributionSetTagCreatedEvent.class); | ||
TYPES.put(15, DistributionSetTagUpdateEvent.class); | ||
TYPES.put(16, DistributionSetTagDeletedEvent.class); | ||
|
||
// rollout | ||
TYPES.put(17, RolloutUpdatedEvent.class); | ||
|
||
// rollout group | ||
TYPES.put(18, RolloutGroupCreatedEvent.class); | ||
TYPES.put(19, RolloutGroupUpdatedEvent.class); | ||
|
||
// download | ||
TYPES.put(20, DownloadProgressEvent.class); | ||
} | ||
|
||
private int value; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public EventType() { | ||
// for marshalling and unmarshalling. | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param value | ||
* the value to initialize | ||
*/ | ||
public EventType(final int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
public Class<?> getTargetClass() { | ||
return TYPES.get(value); | ||
} | ||
|
||
/** | ||
* Returns a {@link EventType} based on the given class type. | ||
* | ||
* @param clazz | ||
* the clazz type to retrieve the corresponding {@link EventType} | ||
* . | ||
* @return the corresponding {@link EventType} or {@code null} if the clazz | ||
* does not have a {@link EventType}. | ||
*/ | ||
public static EventType from(final Class<?> clazz) { | ||
final Optional<Integer> foundEventType = TYPES.entrySet().stream() | ||
.filter(entry -> entry.getValue().equals(clazz)).map(entry -> entry.getKey()).findFirst(); | ||
if (!foundEventType.isPresent()) { | ||
return null; | ||
} | ||
return new EventType(foundEventType.get()); | ||
} | ||
} |
Oops, something went wrong.