-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
602c14c
commit b39771a
Showing
5 changed files
with
209 additions
and
82 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
44 changes: 44 additions & 0 deletions
44
interlok-core/src/main/java/com/adaptris/core/EventMatcher.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,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); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
interlok-core/src/main/java/com/adaptris/core/NullEventMatcher.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,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() { | ||
} | ||
|
||
@Override | ||
public boolean matches(Event event) { | ||
return false; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
interlok-core/src/main/java/com/adaptris/core/RegexEventMatcher.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,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<>()); | ||
} | ||
|
||
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; | ||
} | ||
|
||
public Set<String> getMatchTypes() { | ||
return matchTypes; | ||
} | ||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.