Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Pinger fixes:
Browse files Browse the repository at this point in the history
* use JNDI instead of CDI for inventory injection to prevent initialization
errors (kudos to jpkroehling for suggesting that)
* fix resource update when publishing traits
  • Loading branch information
metlos committed May 23, 2015
1 parent a2aba2b commit 34c6611
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
7 changes: 1 addition & 6 deletions modules/pinger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>hawkular-inventory-cdi</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hawkular.metrics</groupId>
<artifactId>clients-common</artifactId>
Expand Down Expand Up @@ -96,6 +90,7 @@
<version>1.1-rev-1</version> <!-- TODO move to parent -->
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.hawkular.inventory.api.Inventory;
import org.hawkular.inventory.api.filters.With;
import org.hawkular.inventory.api.model.Resource;
import org.hawkular.inventory.cdi.Observable;
import org.hawkular.inventory.cdi.ObservableAutoTenant;
import rx.functions.Action1;

import javax.annotation.PostConstruct;
Expand All @@ -32,7 +30,6 @@
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -41,7 +38,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

Expand Down Expand Up @@ -119,26 +115,11 @@ public List<PingDestination> getNewUrls() {
@EJB
TraitsPublisher traitsPublisher;

@Inject
@Observable
@javax.annotation.Resource(lookup = "java:global/Hawkular/ObservableInventory")
private Inventory.Mixin.Observable inventory;

@Inject
@ObservableAutoTenant
private Inventory.Mixin.AutoTenantAndObservable autotenantInventory;

final NewUrlsCollector newUrlsCollector = new NewUrlsCollector();

private void addUrl(String tenantId, String envId, String url) {
Log.LOG.infof("Putting url to inventory: %s", url);

//we want the magic to happen, so using the autotenant here
autotenantInventory.tenants().get(tenantId).environments().get(envId).feedlessResources().create(
Resource.Blueprint.builder().withId(UUID.randomUUID().toString()).withResourceType("URL")
.withProperty("url", url).build());

}

@PostConstruct
public void startUp() {

Expand All @@ -148,22 +129,16 @@ public void startUp() {
*/
inventory.observable(Interest.in(Resource.class).being(Action.created())).subscribe(newUrlsCollector);

//we must not use the autotenantInventory here, because it would disallow us from "seeing" all the tenants.
//We need that though and at the same time we don't need any of the features offered by autotenant, so it's ok.
//we use just an observable inventory here, because it allows us to see all the tenants. This essentially
//circumvents any authz present on the inventory.
//We need that though because pinger doesn't have storage of its own and is considered "trusted", so it's ok.
Set<Resource> urls = inventory.tenants().getAll().resourceTypes().getAll(With.id(PingDestination.URL_TYPE))
.resources().getAll().entities();
Log.LOG.infof("About to initialize Hawkular Pinger with %d URLs", urls.size());

for (Resource r : urls) {
destinations.add(PingDestination.from(r));
}

//eff testing purposes, this causes order-of-initialization problems...
// if (destinations.isEmpty()) {
// /* for test purposes */
// addUrl("jdoe", "test", "http://hawkular.github.io");
// }
//
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
*/
package org.hawkular.component.pinger;

import java.util.Map.Entry;

import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Inject;

import org.hawkular.component.pinger.Traits.TraitHeader;
import org.hawkular.inventory.api.Inventory;
import org.hawkular.inventory.api.Resources;
import org.hawkular.inventory.api.model.Resource;
import org.hawkular.inventory.api.model.Resource.Update.Builder;
import org.hawkular.inventory.cdi.Observable;

import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import java.util.Map.Entry;

/**
* Stores ping results to Hawkular Inventory.
Expand All @@ -35,8 +33,8 @@
*/
@Stateless
public class TraitsPublisher {
@Inject
@Observable

@javax.annotation.Resource(lookup = "java:global/Hawkular/ObservableInventory")
private Inventory.Mixin.Observable inventory;

/**
Expand All @@ -48,15 +46,27 @@ public class TraitsPublisher {
@Asynchronous
public void publish(PingStatus status) {
final Traits traits = status.getTraits();
Builder resourceBuilder = Resource.Update.builder();
resourceBuilder.withProperty("traits-collected-on", traits.getTimestamp());

PingDestination dest = status.getDestination();

Resources.ReadWrite resourceAccess = inventory.tenants().get(dest.getTenantId()).environments()
.get(dest.getEnvironmentId()).feedlessResources();

Resource resource = resourceAccess.get(dest.getResourceId()).entity();

Builder updateBuilder = Resource.Update.builder();

//keep the properties already present on the resource
updateBuilder.withProperties(resource.getProperties());

//add/modify our own
updateBuilder.withProperty("traits-collected-on", traits.getTimestamp());
for (Entry<TraitHeader, String> entry : traits.getItems().entrySet()) {
resourceBuilder.withProperty("trait-" + entry.getKey().toString(), entry.getValue());
updateBuilder.withProperty("trait-" + entry.getKey().toString(), entry.getValue());
}

PingDestination dest = status.getDestination();
inventory.tenants().get(dest.getTenantId()).environments().get(dest.getEnvironmentId()).feedlessResources()
.update(dest.getResourceId(), resourceBuilder.build());
.update(dest.getResourceId(), updateBuilder.build());
}

}

0 comments on commit 34c6611

Please sign in to comment.