use of com.openshift.client.IApplication in project camel by apache.
the class OpenShiftConsumer method doPollOnChange.
protected int doPollOnChange(IDomain domain) {
// an app can either be
// - added
// - removed
// - state changed
Map<ApplicationState, ApplicationState> newState = new HashMap<ApplicationState, ApplicationState>();
List<IApplication> apps = domain.getApplications();
for (IApplication app : apps) {
ApplicationState state = new ApplicationState(app.getUUID(), app, OpenShiftHelper.getStateForApplication(app));
newState.put(state, state);
}
// compute what is the delta from last time
// so we split up into 3 groups, of added/removed/changed
Map<ApplicationState, ApplicationState> added = new HashMap<ApplicationState, ApplicationState>();
Map<ApplicationState, ApplicationState> removed = new HashMap<ApplicationState, ApplicationState>();
Map<ApplicationState, ApplicationState> changed = new HashMap<ApplicationState, ApplicationState>();
for (ApplicationState state : newState.keySet()) {
if (!oldState.containsKey(state)) {
// its a new app added
added.put(state, state);
} else {
ApplicationState old = oldState.get(state);
if (old != null && !old.getState().equals(state.getState())) {
// the state changed
state.setOldState(old.getState());
changed.put(state, state);
}
}
}
for (ApplicationState state : oldState.keySet()) {
if (!newState.containsKey(state)) {
// its a app removed
removed.put(state, state);
}
}
// only start emitting events after first init poll
int processed = 0;
if (!initialPoll) {
for (ApplicationState add : added.keySet()) {
Exchange exchange = getEndpoint().createExchange(add.getApplication());
exchange.getIn().setHeader(OpenShiftConstants.EVENT_TYPE, "added");
try {
processed++;
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
}
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error during processing exchange.", exchange, exchange.getException());
}
}
for (ApplicationState remove : removed.keySet()) {
Exchange exchange = getEndpoint().createExchange(remove.getApplication());
exchange.getIn().setHeader(OpenShiftConstants.EVENT_TYPE, "removed");
try {
processed++;
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
}
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error during processing exchange.", exchange, exchange.getException());
}
}
for (ApplicationState change : changed.keySet()) {
Exchange exchange = getEndpoint().createExchange(change.getApplication());
exchange.getIn().setHeader(OpenShiftConstants.EVENT_TYPE, "changed");
exchange.getIn().setHeader(OpenShiftConstants.EVENT_OLD_STATE, change.getOldState());
exchange.getIn().setHeader(OpenShiftConstants.EVENT_NEW_STATE, change.getState());
try {
processed++;
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
}
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error during processing exchange.", exchange, exchange.getException());
}
}
}
// update old state with latest state for next poll
oldState.clear();
oldState.putAll(newState);
initialPoll = false;
return processed;
}
use of com.openshift.client.IApplication in project camel by apache.
the class OpenShiftProducer method doStart.
protected void doStart(Exchange exchange, IDomain domain) throws CamelExchangeException {
String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
if (name == null) {
throw new CamelExchangeException("Application not specified", exchange);
}
IApplication app = domain.getApplicationByName(name);
if (app == null) {
throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
} else {
app.start();
}
}
use of com.openshift.client.IApplication in project camel by apache.
the class OpenShiftProducer method doRemoveEnvironmentVariable.
protected void doRemoveEnvironmentVariable(Exchange exchange, IDomain domain) throws CamelExchangeException {
String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
if (name == null) {
throw new CamelExchangeException("Application not specified", exchange);
}
IApplication app = domain.getApplicationByName(name);
if (app == null) {
throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
} else {
String variableName = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_NAME, getEndpoint().getApplication(), String.class);
if (!app.canGetEnvironmentVariables()) {
throw new CamelExchangeException("The application with id " + name + " can't get Environment Variables", exchange);
}
if (ObjectHelper.isNotEmpty(variableName)) {
app.removeEnvironmentVariable(variableName);
exchange.getIn().setBody(variableName);
} else {
throw new CamelExchangeException("Environment variable name not specified", exchange);
}
}
}
use of com.openshift.client.IApplication in project camel by apache.
the class OpenShiftProducer method doGetDeploymentType.
protected void doGetDeploymentType(Exchange exchange, IDomain domain) throws CamelExchangeException {
String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
if (name == null) {
throw new CamelExchangeException("Application not specified", exchange);
}
IApplication app = domain.getApplicationByName(name);
if (app == null) {
throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
} else {
String deploymentType = app.getDeploymentType();
exchange.getIn().setBody(deploymentType);
}
}
use of com.openshift.client.IApplication in project camel by apache.
the class OpenShiftProducer method doGetEnvironmentVariableValue.
protected void doGetEnvironmentVariableValue(Exchange exchange, IDomain domain) throws CamelExchangeException {
String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
if (name == null) {
throw new CamelExchangeException("Application not specified", exchange);
}
IApplication app = domain.getApplicationByName(name);
if (app == null) {
throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
} else {
String variableName = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_NAME, getEndpoint().getApplication(), String.class);
if (!app.canGetEnvironmentVariables()) {
throw new CamelExchangeException("The application with id " + name + " can't get Environment Variables", exchange);
}
if (ObjectHelper.isNotEmpty(variableName)) {
IEnvironmentVariable result = app.getEnvironmentVariable(variableName);
exchange.getIn().setBody(result.getValue());
} else {
throw new CamelExchangeException("Environment variable name not specified", exchange);
}
}
}
Aggregations