All Downloads are FREE. Search and download functionalities are using the official Maven repository.

stream.service.ServiceInfo Maven / Gradle / Ivy

The newest version!
package stream.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * This is a simple class that provides information about a service. The
 * information consists of the name of the service and the service interfaces,
 * provided.
 * 
 * @author Christian Bockermann
 * 
 */
public final class ServiceInfo implements Serializable {

	/** The unique class ID */
	private static final long serialVersionUID = -6189552556440798733L;

	final String name;
	final Class services[];

	private ServiceInfo(String name, Class services[]) {
		this.name = name;
		this.services = services;
	}

	public String getName() {
		return this.name;
	}

	public Class[] getServices() {
		return services;
	}

	public String toString() {
		StringBuffer s = new StringBuffer();
		s.append("ServiceInfo[" + name + "]{");
		for (int i = 0; i < services.length; i++) {
			s.append(services[i]);
			if (i + 1 < services.length)
				s.append(",");
		}
		s.append("}");
		return s.toString();
	}

	@SuppressWarnings("unchecked")
	public static Class[] getServiceInterfaces(
			Class serviceImpl) {
		Class cur = serviceImpl;
		List> intfs = new ArrayList>();

		while (cur != null) {
			for (Class clazz : cur.getInterfaces()) {
				if (clazz != Service.class && isServiceImplementation(clazz)) {
					intfs.add((Class) clazz);
				}
			}
			cur = cur.getSuperclass();
		}

		return (Class[]) intfs.toArray(new Class[intfs
				.size()]);
	}

	public static Class[] getServiceInterfaces(Service p) {
		return getServiceInterfaces(p.getClass());
	}

	public static ServiceInfo createServiceInfo(String name, Service service)
			throws Exception {
		return new ServiceInfo(name, getServiceInterfaces(service));
	}

	public static ServiceInfo createServiceInfo(String name,
			Class serviceClass) {
		return new ServiceInfo(name, getServiceInterfaces(serviceClass));
	}

	/**
	 * This method checks whether the given class implements the Service
	 * interface.
	 * 
	 * @param clazz
	 * @return
	 */
	public static boolean isServiceImplementation(Class clazz) {

		if (clazz == Service.class)
			return true;

		if (clazz.isArray()) {
			return false;
		}

		for (Class intf : clazz.getInterfaces()) {
			if (intf.equals(Service.class) || intf == Service.class) {
				return true;
			}
		}

		return false;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy