in ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java [667:792]
private synchronized Set<ServiceComponentHostResponse> getHostComponents(
ServiceComponentHostRequest request) throws AmbariException {
if (request.getClusterName() == null
|| request.getClusterName().isEmpty()) {
throw new IllegalArgumentException("Invalid arguments, cluster name should not be null");
}
final Cluster cluster;
try {
cluster = clusters.getCluster(request.getClusterName());
} catch (ClusterNotFoundException e) {
throw new ParentObjectNotFoundException("Parent Cluster resource doesn't exist", e);
}
if (request.getHostname() != null) {
try {
if (! clusters.getClustersForHost(request.getHostname()).contains(cluster)) {
// case where host exists but not associated with given cluster
throw new ParentObjectNotFoundException("Parent Host resource doesn't exist",
new HostNotFoundException(request.getClusterName(), request.getHostname()));
}
} catch (HostNotFoundException e) {
// creating new HostNotFoundException to add cluster name
throw new ParentObjectNotFoundException("Parent Host resource doesn't exist",
new HostNotFoundException(request.getClusterName(), request.getHostname()));
}
}
if (request.getComponentName() != null) {
if (request.getServiceName() == null
|| request.getServiceName().isEmpty()) {
StackId stackId = cluster.getDesiredStackVersion();
String serviceName =
ambariMetaInfo.getComponentToService(stackId.getStackName(),
stackId.getStackVersion(), request.getComponentName());
if (LOG.isDebugEnabled()) {
LOG.debug("Looking up service name for component"
+ ", componentName=" + request.getComponentName()
+ ", serviceName=" + serviceName
+ ", stackInfo=" + stackId.getStackId());
}
if (serviceName == null
|| serviceName.isEmpty()) {
throw new ServiceComponentHostNotFoundException(
cluster.getClusterName(), null, request.getComponentName(),request.getHostname());
}
request.setServiceName(serviceName);
}
}
Set<Service> services = new HashSet<Service>();
if (request.getServiceName() != null && !request.getServiceName().isEmpty()) {
services.add(cluster.getService(request.getServiceName()));
} else {
services.addAll(cluster.getServices().values());
}
Set<ServiceComponentHostResponse> response =
new HashSet<ServiceComponentHostResponse>();
boolean checkDesiredState = false;
State desiredStateToCheck = null;
if (request.getDesiredState() != null
&& !request.getDesiredState().isEmpty()) {
desiredStateToCheck = State.valueOf(request.getDesiredState());
if (!desiredStateToCheck.isValidDesiredState()) {
throw new IllegalArgumentException("Invalid arguments, invalid desired"
+ " state, desiredState=" + desiredStateToCheck);
}
checkDesiredState = true;
}
for (Service s : services) {
// filter on component name if provided
Set<ServiceComponent> components = new HashSet<ServiceComponent>();
if (request.getComponentName() != null) {
components.add(s.getServiceComponent(request.getComponentName()));
} else {
components.addAll(s.getServiceComponents().values());
}
for(ServiceComponent sc : components) {
if (request.getComponentName() != null) {
if (!sc.getName().equals(request.getComponentName())) {
continue;
}
}
// filter on hostname if provided
// filter on desired state if provided
if (request.getHostname() != null) {
try {
ServiceComponentHost sch = sc.getServiceComponentHost(
request.getHostname());
if (checkDesiredState
&& (desiredStateToCheck != sch.getDesiredState())) {
continue;
}
ServiceComponentHostResponse r = sch.convertToResponse();
response.add(r);
} catch (ServiceComponentHostNotFoundException e) {
if (request.getServiceName() != null && request.getComponentName() != null) {
throw new ServiceComponentHostNotFoundException(cluster.getClusterName(),
request.getServiceName(), request.getComponentName(),request.getHostname());
} else {
// ignore this since host_component was not specified
// this is an artifact of how we get host_components and can happen
// in case where we get all host_components for a host
}
}
} else {
for (ServiceComponentHost sch :
sc.getServiceComponentHosts().values()) {
if (checkDesiredState
&& (desiredStateToCheck != sch.getDesiredState())) {
continue;
}
ServiceComponentHostResponse r = sch.convertToResponse();
response.add(r);
}
}
}
}
return response;
}