Skip to content

Commit

Permalink
[MSC-85] added MBean operations to display services in a particular s…
Browse files Browse the repository at this point in the history
…tate - applied Jaikiran's patch
  • Loading branch information
ropalka authored and dmlloyd committed Feb 6, 2013
1 parent 696b393 commit 087fc59
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/main/java/org/jboss/msc/service/ServiceContainerImpl.java
Expand Up @@ -240,6 +240,76 @@ public String dumpServiceDetails(final String serviceName) {
} }
return null; return null;
} }

@Override
public void dumpServicesByStatus(String status) {
System.out.printf("Services for %s with status:%s\n", getName(), status);
Collection<ServiceStatus> services = this.queryServicesByStatus(status);
if (services.isEmpty()) {
System.out.printf("There are no services with status: %s\n", status);
} else {
this.printServiceStatus(services, System.out);
}
}

@Override
public String dumpServicesToStringByStatus(String status) {
Collection<ServiceStatus> services = this.queryServicesByStatus(status);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = null;
try {
ps = new PrintStream(baos, false, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
ps.printf("Services for %s with status:%s\n", getName(), status);
if (services.isEmpty()) {
ps.printf("There are no services with status: %s\n", status);
} else {
this.printServiceStatus(services, ps);
}
ps.flush();
try {
return new String(baos.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}

/**
* Returns a collection of {@link ServiceStatus} of services, whose {@link org.jboss.msc.service.management.ServiceStatus#getStateName() status}
* matches the passed <code>status</code>. Returns an empty collection if there's no such services.
* @param status The status that we are interested in.
* @return
*/
private Collection<ServiceStatus> queryServicesByStatus(String status) {
final Collection<ServiceRegistrationImpl> registrations = registry.values();
final ArrayList<ServiceStatus> list = new ArrayList<ServiceStatus>(registrations.size());
for (ServiceRegistrationImpl registration : registrations) {
final ServiceControllerImpl<?> instance = registration.getInstance();
if (instance != null) {
ServiceStatus serviceStatus = instance.getStatus();
if (serviceStatus.getStateName().equals(status)) {
list.add(serviceStatus);
}
}
}
return list;
}

/**
* Print the passed {@link ServiceStatus}es to the {@link PrintStream}
* @param serviceStatuses
* @param out
*/
private void printServiceStatus(Collection<ServiceStatus> serviceStatuses, PrintStream out) {
if (serviceStatuses == null || serviceStatuses.isEmpty()) {
return;
}
for (ServiceStatus status : serviceStatuses) {
out.printf("%s\n", status);
}
}
}; };


ServiceContainerImpl(String name, int coreSize, long timeOut, TimeUnit timeOutUnit, final boolean autoShutdown) { ServiceContainerImpl(String name, int coreSize, long timeOut, TimeUnit timeOutUnit, final boolean autoShutdown) {
Expand Down
Expand Up @@ -81,4 +81,20 @@ public interface ServiceContainerMXBean {
* @return the details, as a string * @return the details, as a string
*/ */
String dumpServiceDetails(String serviceName); String dumpServiceDetails(String serviceName);

/**
* Dump the services, whose status matches the passed <code>status</code> to the console
*
* @param status The status of the services that we are interested in
*/
void dumpServicesByStatus(String status);

/**
* Dump the services, whose status matches the passed <code>status</code>, state to a big string.
* The string has no particular standard format and may change over time; this method is simply a convenience.
*
* @param status The status of the services that we are interested in
* @return Returns the string representation of the services whose status matches the passed <code>status</code>
*/
String dumpServicesToStringByStatus(String status);
} }

0 comments on commit 087fc59

Please sign in to comment.