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

com.waspring.framework.antenna.service.AbstractServiceContainer Maven / Gradle / Ivy

The newest version!
package com.waspring.framework.antenna.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.waspring.framework.antenna.core.service.IService;
import com.waspring.framework.antenna.core.service.IServiceContainer;
import com.waspring.framework.antenna.core.visitor.IRequest;
import com.waspring.framework.antenna.core.visitor.IResponse;

/**
 * 
 * @author felly
 *
 */
public abstract class AbstractServiceContainer extends AbstractService implements IServiceContainer {

	@Override
	public IService[] getChildServices() {
		List list = new ArrayList();
		expandChildren(list, null);
		return list.toArray(new IService[list.size()]);
	}

	@Override
	public IService[] getChildServicesByClass(Class byclass) {
		List list = new ArrayList();
		expandChildren(list, byclass);
		return list.toArray(new IService[list.size()]);
	}

	@Override
	public  T getChildServiceByClass(Class byclass) {
		List list = new ArrayList();
		expandChildren(list, byclass);
		if (list.isEmpty())
			return null;
		return (T) list.get(0);
	}

	@Override
	public String getServiceName() {
		return getClass().getName();
	}

	@Override
	public abstract void service(IRequest request, IResponse reponse) throws  Exception;

	protected void expandHandler(IService handler, List list, Class byClass) {
		if (handler == null)
			return;

		if (byClass == null || byClass.isAssignableFrom(handler.getClass()))
			list.add(handler);

		if (handler instanceof AbstractServiceContainer)
			((AbstractServiceContainer) handler).expandChildren(list, byClass);
		else if (handler instanceof IServiceContainer) {
			IServiceContainer container = (IServiceContainer) handler;
			IService[] handlers = byClass == null ? container.getChildServices()
					: container.getChildServicesByClass(byClass);
			list.addAll(Arrays.asList(handlers));
		}
	}

	/* ------------------------------------------------------------ */
	protected void expandChildren(List list, Class byClass) {
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy