Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * OfficeFloor - http://www.officefloor.net
   * Copyright (C) 2005-2011 Daniel Sagenschneider
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.officefloor.plugin.work.clazz;
 
 import java.util.List;
 import java.util.Map;
 
 
 public class ClassWorkSource extends AbstractWorkSource<ClassWorkimplements
Property name providing the java.lang.Class name.
 
 	public static final String CLASS_NAME_PROPERTY_NAME = "class.name";

 
Initiate.
 
 	public ClassWorkSource() {
 		// Add the default manufacturers
 						FlowInterface.class));
 
 		// Load any additional manufacturers
 	}

Override to add additional ClassWorkSource.ParameterManufacturer instances.

Parameters:
manufacturers List of ClassWorkSource.ParameterManufacturer instances to use.
 
 	protected void loadParameterManufacturers(
 			List<ParameterManufacturermanufacturers) {
 		// By default adds no further manufacturers
 	}

 
 	protected WorkFactory<ClassWorkcreateWorkFactory(Class<?> clazz) {
 		return new ClassWorkFactory(clazz);
	}

Allows overriding the creation of the net.officefloor.frame.api.build.TaskFactory.

Parameters:
clazz net.officefloor.frame.api.execute.Work class.
method java.lang.reflect.Method on the class.
isStatic Indicates if the java.lang.reflect.Method is static.
parameters ParameterFactory instances.
Returns:
net.officefloor.frame.api.build.TaskFactory.
			Class<?> clazzMethod methodboolean isStatic,
			ParameterFactory[] parameters) {
		return new ClassTaskFactory(methodisStaticparameters);
	}

			WorkTypeBuilder<ClassWorkworkTypeBuilderString taskName,
			Sequence objectSequenceSequence flowSequence) {
		// Include method as task in type definition
		TaskTypeBuilder<IndexedIndexedtaskTypeBuilder = workTypeBuilder
				.addTaskType(taskNametaskFactorynullnull);
		// Return the task type builder
		return taskTypeBuilder;
	}
	/*
	 * =================== WorkSourceService ================================
	 */
		return "CLASS";
	}
		return ClassWorkSource.class;
	}
	/*
	 * =================== AbstractWorkLoader ==============================
	 */
	protected void loadSpecification(SpecificationContext context) {
	}
	@SuppressWarnings("unchecked")
	public void sourceWork(WorkTypeBuilder<ClassWorkworkTypeBuilder,
			WorkSourceContext contextthrows Exception {
		// Obtain the class loader
		ClassLoader classLoader = context.getClassLoader();
		// Obtain the class
		Class<?> clazz = classLoader.loadClass(className);
		// Define the work factory
		WorkFactory<ClassWorkworkFactory = this.createWorkFactory(clazz);
		workTypeBuilder.setWorkFactory(workFactory);
		// Obtain the listing of tasks from the methods of the class
		for (Method method : clazz.getMethods()) {
			// Ignore non-public methods
			if (!Modifier.isPublic(method.getModifiers())) {
				continue;
			}
			// Ignore Object methods
			if (Object.class.equals(method.getDeclaringClass())) {
				continue;
			}
			// Obtain details of the method
			String methodName = method.getName();
			Class<?>[] paramTypes = method.getParameterTypes();
			// Create to parameters to method to be populated
			ParameterFactory[] parameters = new ParameterFactory[paramTypes.length];
			// Determine if the method is static
			boolean isStatic = Modifier.isStatic(method.getModifiers());
			// Create the sequences for indexes to the objects and flows
			Sequence objectSequence = new Sequence();
			Sequence flowSequence = new Sequence();
			// Create the task factory
			TaskFactory<ClassWorkIndexedIndexedtaskFactory = this
					.createTaskFactory(clazzmethodisStaticparameters);
			// Include method as task in type definition
			TaskTypeBuilder<IndexedIndexedtaskTypeBuilder = this
					.addTaskType(clazzworkTypeBuildermethodName,
							taskFactoryobjectSequenceflowSequence);
			// Define the return type (it not void)
			Class<?> returnType = method.getReturnType();
			if ((returnType != null) && (!..equals(returnType))) {
				taskTypeBuilder.setReturnType(returnType);
			}
			// Define the listing of task objects and flows
			for (int i = 0; i < paramTypes.lengthi++) {
				// Obtain the parameter type
				Class<?> paramType = paramTypes[i];
				// Obtain the parameter factory
				ParameterFactory parameterFactory = null;
				CREATED: for (ParameterManufacturer manufacturer : this.) {
					parameterFactory = manufacturer.createParameterFactory(
							methodNameparamTypetaskTypeBuilder,
							objectSequenceflowSequenceclassLoader);
					if (parameterFactory != null) {
						// Created parameter factory, so use
						break CREATED;
					}
				}
				// Default to object if no parameter factory
				if (parameterFactory == null) {
					// Otherwise must be an object
					parameterFactory = new ObjectParameterFactory(
							objectSequence.nextIndex());
					TaskObjectTypeBuilder<IndexedobjectTypeBuilder = taskTypeBuilder
							.addObject(paramType);
					objectTypeBuilder.setLabel(paramType.getSimpleName());
				}
				// Load the parameter factory
				parameters[i] = parameterFactory;
			}
			// Define the escalation listing
			for (Class<?> escalationType : method.getExceptionTypes()) {
				taskTypeBuilder
						.addEscalation((Class<Throwable>) escalationType);
			}
		}
	}

Manufactures the ParameterFactory.
	protected static interface ParameterManufacturer {

Creates the ParameterFactory.

Parameters:
taskName Name of the net.officefloor.frame.api.execute.Task.
parameterType Parameter type.
taskTypeBuilder net.officefloor.compile.spi.work.source.TaskTypeBuilder.
objectSequence Object Sequence.
flowSequence Flow Sequence.
classLoader java.lang.ClassLoader.
Returns:
ParameterFactory or null if not appropriate for this to manufacture a ParameterFactory.
Throws:
java.lang.Exception If fails to create the ParameterFactory.
				Class<?> parameterType,
				TaskTypeBuilder<IndexedIndexedtaskTypeBuilder,
				Sequence objectSequenceSequence flowSequence,
				ClassLoader classLoaderthrows Exception;
	}

	protected static class TaskContextParameterManufacturer implements
				Class<?> parameterType,
				TaskTypeBuilder<IndexedIndexedtaskTypeBuilder,
				Sequence objectSequenceSequence flowSequence,
				ClassLoader classLoader) {
			// Determine if task context
			if (TaskContext.class.equals(parameterType)) {
				// Parameter is a task context
			}
			// Not task context
			return null;
		}
	}

	protected static class FlowInterfaceParameterManufacturer<A extends Annotation>
			implements ParameterManufacturer {

Annotation class expected on the parameter type.
		private final Class<A> annotationClass;

Initiate.

Parameters:
annotationClass Annotation class expected on the parameter type.
		public FlowInterfaceParameterManufacturer(Class<A> annotationClass) {
			this. = annotationClass;
		}
		/*
		 * ================== ParameterManufacturer ====================
		 */
				Class<?> parameterType,
				TaskTypeBuilder<IndexedIndexedtaskTypeBuilder,
				Sequence objectSequenceSequence flowSequence,
				ClassLoader classLoaderthrows Exception {
			// Determine if flow interface
			if (parameterType.getAnnotation(this.) == null) {
				return null// not a flow interface
			}
			// Ensure is an interface
			if (!parameterType.isInterface()) {
				throw new Exception(
						"Parameter "
parameterType.getSimpleName()
" on method "
taskName
" must be an interface as parameter type is annotated with "
			}
			// Obtain the methods sorted (deterministic order)
			Method[] flowMethods = parameterType.getMethods();
			Arrays.sort(flowMethodsnew Comparator<Method>() {
				public int compare(Method aMethod b) {
					return a.getName().compareTo(b.getName());
				}
			});
			// Create a flow for each method of the interface
			Map<StringFlowMethodMetaDataflowMethodMetaDatas = new HashMap<StringFlowMethodMetaData>(
					flowMethods.length);
			for (int m = 0; m < flowMethods.lengthm++) {
				Method flowMethod = flowMethods[m];
				String flowMethodName = flowMethod.getName();
				// Not include object methods
				if (Object.class.equals(flowMethod.getDeclaringClass())) {
					continue;
				}
				// Ensure not duplicate flow names
				if (flowMethodMetaDatas.containsKey(flowMethodName)) {
					throw new Exception(
							"May not have duplicate flow method names (task="
taskName + ", flow="
parameterType.getSimpleName() + "."
flowMethodName + ")");
				}
				// Ensure at most one parameter
				Class<?> flowParameterType;
				Class<?>[] flowMethodParams = flowMethod.getParameterTypes();
				if (flowMethodParams.length == 0) {
					flowParameterType = null;
else if (flowMethodParams.length == 1) {
					flowParameterType = flowMethodParams[0];
else {
					// Invalid to have more than one parameter
					throw new Exception(
							"Flow methods may only have at most one parameter (task "
taskName + ", flow "
parameterType.getSimpleName() + "."
flowMethodName + ")");
				}
				// Ensure correct return type
				boolean isReturnFlowFuture;
				Class<?> flowReturnType = flowMethod.getReturnType();
				if (FlowFuture.class.equals(flowReturnType)) {
					// Returns a flow future
					isReturnFlowFuture = true;
else if (..equals(flowReturnType)
						|| (flowReturnType == null)) {
					// Does not return value
					isReturnFlowFuture = false;
else {
					// Invalid return type
					throw new Exception("Flow method "
parameterType.getSimpleName() + "." + taskName
" return type is invalid (return type="
flowReturnType.getName() + ", task=" + taskName
")");
				}
				// Create and register the flow method meta-data
				FlowMethodMetaData flowMethodMetaData = new FlowMethodMetaData(
						parameterTypeflowMethodflowSequence.nextIndex(),
						(flowParameterType != null), isReturnFlowFuture);
				flowMethodMetaDatas.put(flowMethodNameflowMethodMetaData);
				// Register the flow
				TaskFlowTypeBuilder<IndexedflowTypeBuilder = taskTypeBuilder
				flowTypeBuilder.setLabel(flowMethodName);
				if (flowParameterType != null) {
					flowTypeBuilder.setArgumentType(flowParameterType);
				}
			}
			// Create and return the flow interface parameter factory
			return new FlowParameterFactory(classLoaderparameterType,
					flowMethodMetaDatas);
		}
	}
New to GrepCode? Check out our FAQ X