Skip to content

Commit

Permalink
Updates to conform with adapter xml
Browse files Browse the repository at this point in the history
  • Loading branch information
telus-terchu committed Nov 27, 2024
1 parent 602c14c commit b39771a
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Adaptris Ltd.
* Copyright 2024 Adaptris Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,25 +19,17 @@
import com.adaptris.annotation.AdapterComponent;
import com.adaptris.annotation.AutoPopulated;
import com.adaptris.annotation.ComponentProfile;
import com.adaptris.annotation.InputFieldDefault;
import com.adaptris.annotation.InputFieldHint;
import com.adaptris.core.util.Args;
import com.adaptris.core.util.LifecycleHelper;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* <p>
Expand All @@ -52,14 +44,15 @@
*/
@XStreamAlias("configurable-event-handler")
@AdapterComponent
@ComponentProfile(summary = "Sends MessageLifecycleEvents to a destination based on the matching rule, or default if no match", tag = "base,events")
@ComponentProfile(summary = "Sends Events to a destination based on the matching rule, or default if no match", tag = "base,events")
public class ConfigurableEventHandler extends DefaultEventHandler {

@Valid
@NotNull
@AutoPopulated
@XStreamImplicit
@Getter
@Setter
private List<Rule> rules;

public ConfigurableEventHandler() {
Expand All @@ -82,8 +75,8 @@ public ConfigurableEventHandler(AdaptrisConnection connection, AdaptrisMessagePr
*/
protected AdaptrisMessageSender resolveEventSender(Event event) {
for (Rule rule : rules) {
if (rule.matcher.matches(event)) {
return rule.standaloneProducer;
if (rule.getMatcher() != null && rule.getMatcher().matches(event)) {
return rule.getStandaloneProducer();
}
}
return getProducer();
Expand All @@ -100,7 +93,7 @@ public void send(Event evt, Map<String, String> properties) throws CoreException
protected void eventHandlerInit() throws CoreException {
super.eventHandlerInit();
for(Rule rule : rules) {
rule.standaloneProducer.init();
if (rule.getStandaloneProducer() != null) rule.getStandaloneProducer().init();
}

Check warning on line 97 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L97

Added line #L97 was not covered by tests
}

Expand All @@ -109,7 +102,7 @@ protected void eventHandlerInit() throws CoreException {
protected void eventHandlerStart() throws CoreException {
super.eventHandlerStart();
for(Rule rule : rules) {
rule.standaloneProducer.start();
if (rule.getStandaloneProducer() != null) rule.getStandaloneProducer().start();
}

Check warning on line 106 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L106

Added line #L106 was not covered by tests
}

Expand All @@ -118,7 +111,7 @@ protected void eventHandlerStart() throws CoreException {
protected void eventHandlerStop() {
super.eventHandlerStop();
for(Rule rule : rules) {
rule.standaloneProducer.stop();
if (rule.getStandaloneProducer() != null) rule.getStandaloneProducer().stop();
}

Check warning on line 115 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L115

Added line #L115 was not covered by tests
}

Expand All @@ -127,76 +120,48 @@ protected void eventHandlerStop() {
protected void eventHandlerClose() {
super.eventHandlerClose();
for(Rule rule : rules) {
rule.standaloneProducer.close();
if (rule.getStandaloneProducer() != null) rule.getStandaloneProducer().close();
}

Check warning on line 124 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L124

Added line #L124 was not covered by tests
}

@Override
public void prepare() throws CoreException {
super.prepare();
for(Rule rule : rules) {
rule.standaloneProducer.prepare();
if (rule.getStandaloneProducer() != null) rule.getStandaloneProducer().prepare();
}

Check warning on line 132 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L132

Added line #L132 was not covered by tests
}


/**
* Matches on an event
*/
interface EventMatcher {
enum MatchType {
SOURCE_ID(event -> event.getSourceId()),
DESTINATION_ID(event -> event.getDestinationId()),
NAMESPACE(event -> event.getNameSpace()),
TYPE(event -> event.getClass().getCanonicalName());

Function<Event, Object> getter;
MatchType(Function<Event, Object> getter) {
this.getter = getter;
}
@AllArgsConstructor
@AdapterComponent
@XStreamAlias("rule")
@ComponentProfile(summary = "Combination of a matcher to match events and a standalone producer for the destination", tag = "events")
public static class Rule {

public Object getProperty(Event event) {
return getter.apply(event);
private EventMatcher matcher;

private StandaloneProducer standaloneProducer;

public Rule() {
matcher = new NullEventMatcher();
standaloneProducer = new StandaloneProducer();
}

Check warning on line 149 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L146-L149

Added lines #L146 - L149 were not covered by tests
}

boolean matches(Event event);
}
public EventMatcher getMatcher() {
return matcher;
}

/**
* Matches based on supplied regex
*/
@XStreamAlias("regex-event-matcher")
static class RegexEventMatcher implements EventMatcher {
@NotNull
@NotBlank
private String regex;
@NotNull
private Set<MatchType> matchTypes;

private transient Pattern compiledRegex;

public RegexEventMatcher(String regex, Set<MatchType> matchTypes) {
this.regex = regex;
this.matchTypes = matchTypes;
this.compiledRegex = Pattern.compile(regex);
}
public void setMatcher(EventMatcher matcher) {
this.matcher = matcher;
}

Check warning on line 157 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L156-L157

Added lines #L156 - L157 were not covered by tests

@Override
public boolean matches(Event event) {
for (MatchType matchType : matchTypes) {
if (compiledRegex.matcher((String)matchType.getProperty(event)).matches()) return true;
public StandaloneProducer getStandaloneProducer() {
return standaloneProducer;
}
return false;
}
}

@XStreamAlias("rule")
@AllArgsConstructor
static class Rule {
@NotNull
private EventMatcher matcher;
@NotNull
private StandaloneProducer standaloneProducer;
public void setStandaloneProducer(StandaloneProducer standaloneProducer) {
this.standaloneProducer = standaloneProducer;
}

Check warning on line 165 in interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/ConfigurableEventHandler.java#L164-L165

Added lines #L164 - L165 were not covered by tests
}
}
44 changes: 44 additions & 0 deletions interlok-core/src/main/java/com/adaptris/core/EventMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024 Adaptris Ltd.
*
* 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.adaptris.core;

import java.util.function.Function;

/**
* Matches on an event
*/
public interface EventMatcher {

boolean matches(Event event);

enum EventMatchType {
SOURCE_ID(event -> event.getSourceId()),
DESTINATION_ID(event -> event.getDestinationId()),
NAMESPACE(event -> event.getNameSpace()),
TYPE(event -> event.getClass().getCanonicalName());

Function<Event, Object> getter;

EventMatchType(Function<Event, Object> getter) {
this.getter = getter;
}

public Object getProperty(Event event) {
return getter.apply(event);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Adaptris Ltd.
*
* 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.adaptris.core;

import com.adaptris.annotation.AdapterComponent;
import com.thoughtworks.xstream.annotations.XStreamAlias;

/**
* Matches based on supplied regex
*/
@XStreamAlias("null-event-matcher")
@AdapterComponent
public class NullEventMatcher implements EventMatcher {

public NullEventMatcher() {
}

Check warning on line 30 in interlok-core/src/main/java/com/adaptris/core/NullEventMatcher.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/NullEventMatcher.java#L29-L30

Added lines #L29 - L30 were not covered by tests

@Override
public boolean matches(Event event) {
return false;

Check warning on line 34 in interlok-core/src/main/java/com/adaptris/core/NullEventMatcher.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/NullEventMatcher.java#L34

Added line #L34 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2024 Adaptris Ltd.
*
* 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.adaptris.core;

import com.adaptris.annotation.AdapterComponent;
import com.adaptris.annotation.ComponentProfile;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Matches based on supplied regex
*/
@XStreamAlias("regex-event-matcher")
@AdapterComponent
@ComponentProfile(summary = "A matcher that uses regex to match on an event's properties", tag = "events")
public class RegexEventMatcher implements EventMatcher {


private String regex;

@XStreamImplicit(itemFieldName = "match-type")
private Set<String> matchTypes;

private transient Set<EventMatchType> matchTypeEnumSet;

private transient Pattern compiledRegex;

public RegexEventMatcher() {
setRegex("");
setMatchTypes(new LinkedHashSet<>());
}

Check warning on line 50 in interlok-core/src/main/java/com/adaptris/core/RegexEventMatcher.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/RegexEventMatcher.java#L47-L50

Added lines #L47 - L50 were not covered by tests

public RegexEventMatcher(String regex, Set<String> matchTypes) {
setMatchTypes(matchTypes);
setRegex(regex);
}

public void setRegex(String regex) {
this.regex = regex;
this.compiledRegex = Pattern.compile(regex);
}

public String getRegex() {
return regex;

Check warning on line 63 in interlok-core/src/main/java/com/adaptris/core/RegexEventMatcher.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/RegexEventMatcher.java#L63

Added line #L63 was not covered by tests
}

public Set<String> getMatchTypes() {
return matchTypes;

Check warning on line 67 in interlok-core/src/main/java/com/adaptris/core/RegexEventMatcher.java

View check run for this annotation

Codecov / codecov/patch

interlok-core/src/main/java/com/adaptris/core/RegexEventMatcher.java#L67

Added line #L67 was not covered by tests
}

public void setMatchTypes(Set<String> matchTypes) {
this.matchTypeEnumSet = matchTypes.stream().map(EventMatchType::valueOf).collect(Collectors.toSet());
this.matchTypes = matchTypes;
}

@Override
public boolean matches(Event event) {
for (EventMatchType matchType : matchTypeEnumSet) {
if (compiledRegex.matcher((String) matchType.getProperty(event)).matches()) return true;
}
return false;
}
}
Loading

0 comments on commit b39771a

Please sign in to comment.