This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from ppalaga/HAWKULAR-235
HAWKULAR-235 Make Pinger listen to URLs removed
- Loading branch information
Showing
6 changed files
with
317 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
Change this file to force Travis to perform a rebuild and redeploy to repo | ||
- |
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
157 changes: 157 additions & 0 deletions
157
modules/pinger/src/main/java/org/hawkular/component/pinger/UrlChangesCollector.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,157 @@ | ||
/* | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* 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 org.hawkular.component.pinger; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.hawkular.inventory.api.Action; | ||
import org.hawkular.inventory.api.Inventory; | ||
import org.hawkular.inventory.api.model.Resource; | ||
|
||
import rx.functions.Action1; | ||
|
||
/** | ||
* Collects URL additions and removals reported by {@link PingManager#inventory} and synchronizes the various threads | ||
* reporting the new URLs and those ones consuming them. | ||
* | ||
* @author <a href="https://github.com/ppalaga">Peter Palaga</a> | ||
*/ | ||
public class UrlChangesCollector { | ||
/** | ||
* An add or remove operation that can be performed on a {@link Set}. | ||
* | ||
* @author <a href="https://github.com/ppalaga">Peter Palaga</a> | ||
*/ | ||
private static class UrlChange { | ||
|
||
/** The nature of the change - {@link Action#created()} or {@link Action#deleted()} */ | ||
private final Action<?, ?> action; | ||
|
||
/** The {@link PingDestination} that the change is related to */ | ||
private final PingDestination destination; | ||
|
||
/** | ||
* Creates a new {@link UrlChange}. | ||
* | ||
* @param action the nature of the change - {@link Action#created()} or {@link Action#deleted()} | ||
* @param destination the {@link PingDestination} that the change is related to | ||
*/ | ||
public UrlChange(Action<?, ?> action, PingDestination destination) { | ||
super(); | ||
this.action = action; | ||
this.destination = destination; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A callback for the {@link Inventory} that collects newly added URLs. | ||
*/ | ||
private final Action1<Resource> urlCreatedAction = new Action1<Resource>() { | ||
/** | ||
* It is safe to call this method concurrently from any random thread. | ||
* | ||
* @see rx.functions.Action1#call(java.lang.Object) | ||
*/ | ||
@Override | ||
public void call(Resource r) { | ||
if (PingDestination.isUrl(r)) { | ||
synchronized (lock) { | ||
changes.add(new UrlChange(Action.created(), PingDestination.from(r))); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* A callback for the {@link Inventory} that collects URL removals. | ||
*/ | ||
private final Action1<Resource> urlDeletedAction = new Action1<Resource>() { | ||
/** | ||
* It is safe to call this method concurrently from any random thread. | ||
* | ||
* @see rx.functions.Action1#call(java.lang.Object) | ||
*/ | ||
@Override | ||
public void call(Resource r) { | ||
if (PingDestination.isUrl(r)) { | ||
synchronized (lock) { | ||
changes.add(new UrlChange(Action.deleted(), PingDestination.from(r))); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
private final Object lock = new Object(); | ||
private List<UrlChangesCollector.UrlChange> changes = new ArrayList<>(); | ||
|
||
/** | ||
* Applies the {@link UrlChange}s collected by this {@link UrlChangesCollector} to the given {@link Set} of | ||
* {@link PingDestination}s - i.e. {@link PingDestination}s are either added or removed to/from the Set. | ||
* | ||
* @param destinations the {@link Set} of {@link PingDestination}s that the changes should be applied to. | ||
*/ | ||
public void apply(Set<PingDestination> destinations) { | ||
List<UrlChange> changesCopy = getChanges(); | ||
|
||
for (UrlChange change : changesCopy) { | ||
if (Action.created().equals(change.action)) { | ||
destinations.add(change.destination); | ||
} else if (Action.deleted().equals(change.action)) { | ||
destinations.remove(change.destination); | ||
} else { | ||
throw new IllegalStateException("Unexpected action '" + change.action | ||
+ "'; expected Action.created() or Action.deleted()"); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the list of {@link UrlChange}s collected by this {@link UrlChangesCollector}. It is safe to call this | ||
* method concurrently from any random thread. | ||
* | ||
* @return the list of {@link PingDestination}s | ||
*/ | ||
private List<UrlChangesCollector.UrlChange> getChanges() { | ||
synchronized (lock) { | ||
if (this.changes.isEmpty()) { | ||
return Collections.emptyList(); | ||
} else { | ||
List<UrlChangesCollector.UrlChange> result = this.changes; | ||
this.changes = new ArrayList<>(); | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns a callback for the {@link Inventory} that collects newly added URLs. | ||
*/ | ||
public Action1<Resource> getUrlCreatedAction() { | ||
return urlCreatedAction; | ||
} | ||
|
||
/** | ||
* Returns a callback for the {@link Inventory} that collects URL removals. | ||
*/ | ||
public Action1<Resource> getUrlDeletedAction() { | ||
return urlDeletedAction; | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
modules/pinger/src/test/java/org/hawkular/component/pinger/PingerTestUtils.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,54 @@ | ||
/* | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* 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 org.hawkular.component.pinger; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.hawkular.component.pinger.PingDestination.ResourceField; | ||
import org.hawkular.inventory.api.model.Resource; | ||
import org.hawkular.inventory.api.model.ResourceType; | ||
|
||
/** | ||
* @author <a href="https://github.com/ppalaga">Peter Palaga</a> | ||
*/ | ||
public class PingerTestUtils { | ||
|
||
public static final String TEST_RESOURCE_ID = "test-rsrc"; | ||
public static final String TEST_TENANT_ID = "test-tenat"; | ||
public static final String TEST_ENVIRONMENT_ID = "test-env"; | ||
public static final String TEST_URL = "http://hawkular.github.io"; | ||
public static final String GET_METHOD = "GET"; | ||
|
||
public static Resource createTestResource() { | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put(ResourceField.url.name(), PingerTestUtils.TEST_URL); | ||
props.put(ResourceField.method.name(), PingerTestUtils.GET_METHOD); | ||
ResourceType urlType = new ResourceType(PingerTestUtils.TEST_TENANT_ID, PingDestination.URL_TYPE, "0"); | ||
Resource urlResource = new Resource(PingerTestUtils.TEST_TENANT_ID, PingerTestUtils.TEST_ENVIRONMENT_ID, null, | ||
PingerTestUtils.TEST_RESOURCE_ID, urlType, props); | ||
return urlResource; | ||
} | ||
|
||
/** | ||
* @return | ||
*/ | ||
public static PingDestination createTestPingDestination() { | ||
return new PingDestination(PingerTestUtils.TEST_TENANT_ID, PingerTestUtils.TEST_ENVIRONMENT_ID, | ||
PingerTestUtils.TEST_RESOURCE_ID, PingerTestUtils.TEST_URL, PingerTestUtils.GET_METHOD); | ||
} | ||
} |
Oops, something went wrong.