/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EventObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.management.event.ExchangeSendingEvent;
import org.apache.camel.management.event.RouteAddedEvent;
import org.apache.camel.management.event.RouteRemovedEvent;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.spi.RuntimeEndpointRegistry;
import org.apache.camel.spi.UnitOfWork;
import org.apache.camel.support.EventNotifierSupport;
import org.apache.camel.util.LRUCache;
public class DefaultRuntimeEndpointRegistry extends EventNotifierSupport implements RuntimeEndpointRegistry {
// route id -> endpoint urls
private Map<String, Set<String>> inputs;
private Map<String, Map<String, String>> outputs;
private int limit = 1000;
private boolean enabled = true;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public List<String> getAllEndpoints(boolean includeInputs) {
List<String> answer = new ArrayList<String>();
if (includeInputs) {
for (Map.Entry<String, Set<String>> entry : inputs.entrySet()) {
answer.addAll(entry.getValue());
}
}
for (Map.Entry<String, Map<String, String>> entry : outputs.entrySet()) {
answer.addAll(entry.getValue().keySet());
}
return Collections.unmodifiableList(answer);
}
@Override
public List<String> getEndpointsPerRoute(String routeId, boolean includeInputs) {
List<String> answer = new ArrayList<String>();
if (includeInputs) {
Set<String> uris = inputs.get(routeId);
if (uris != null) {
answer.addAll(uris);
}
}
Map<String, String> uris = outputs.get(routeId);
if (uris != null) {
answer.addAll(uris.keySet());
}
return Collections.unmodifiableList(answer);
}
@Override
public int getLimit() {
return limit;
}
@Override
public void setLimit(int limit) {
this.limit = limit;
}
@Override
public void reset() {
inputs.clear();
outputs.clear();
}
@Override
public int size() {
int total = inputs.values().size();
total += outputs.values().size();
return total;
}
@Override
protected void doStart() throws Exception {
if (inputs == null) {
inputs = new HashMap<String, Set<String>>();
}
if (outputs == null) {
outputs = new HashMap<String, Map<String, String>>();
}
}
@Override
protected void doStop() throws Exception {
reset();
}
@Override
public void notify(EventObject event) throws Exception {
if (event instanceof RouteAddedEvent) {
RouteAddedEvent rse = (RouteAddedEvent) event;
Endpoint endpoint = rse.getRoute().getEndpoint();
String routeId = rse.getRoute().getId();
// a HashSet is fine for inputs as we only have a limited number of those
Set<String> uris = new HashSet<String>();
uris.add(endpoint.getEndpointUri());
inputs.put(routeId, uris);
// use a LRUCache for outputs as we could potential have unlimited uris if dynamic routing is in use
// and therefore need to have the limit in use
outputs.put(routeId, new LRUCache<String, String>(limit));
} else if (event instanceof RouteRemovedEvent) {
RouteRemovedEvent rse = (RouteRemovedEvent) event;
String routeId = rse.getRoute().getId();
inputs.remove(routeId);
outputs.remove(routeId);
} else {
ExchangeSendingEvent ese = (ExchangeSendingEvent) event;
Endpoint endpoint = ese.getEndpoint();
String routeId = getRouteId(ese.getExchange());
String uri = endpoint.getEndpointUri();
Map<String, String> uris = outputs.get(routeId);
if (uris != null && !uris.containsKey(uri)) {
uris.put(uri, uri);
}
}
}
private String getRouteId(Exchange exchange) {
String answer = null;
UnitOfWork uow = exchange.getUnitOfWork();
RouteContext rc = uow != null ? uow.getRouteContext() : null;
if (rc != null) {
answer = rc.getRoute().getId();
}
if (answer == null) {
// fallback and get from route id on the exchange
answer = exchange.getFromRouteId();
}
return answer;
}
@Override
public boolean isEnabled(EventObject event) {
return enabled && event instanceof ExchangeSendingEvent
|| event instanceof RouteAddedEvent
|| event instanceof RouteRemovedEvent;
}
}