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.autowire;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
net.officefloor.compile.spi.office.source.OfficeSource implementation that auto-wires the configuration based on type.

Author(s):
Daniel Sagenschneider
 
 public class AutoWireOfficeSource extends AbstractOfficeSource {

 
 	private final OfficeFloorCompiler compiler;

AutoWireSection instances.
 
 	private final List<AutoWireSectionsections = new LinkedList<AutoWireSection>();

 
 	private final List<Linklinks = new LinkedList<Link>();

 
Initiate.

 
 		this. = compiler;
 	}

Default constructor.
 
 	public AutoWireOfficeSource() {
 		this(OfficeFloorCompiler.newOfficeFloorCompiler());
 	}

Adds an net.officefloor.compile.spi.office.OfficeSection.

Parameters:
sectionName Name of the net.officefloor.compile.spi.office.OfficeSection.
sectionSourceClass net.officefloor.compile.spi.section.source.SectionSource class.
sectionLocation net.officefloor.compile.spi.office.OfficeSection location.
Returns:
AutoWireSection to configure properties and link flows.
	public <S extends SectionSourceAutoWireSection addSection(
			String sectionNameClass<S> sectionSourceClass,
			String sectionLocation) {
		// Create the properties
		// Create and add the section
				sectionNamesectionSourceClasssectionLocationproperties);
		this..add(section);
		// Return the section
		return section;
	}

Links the source net.officefloor.compile.spi.section.SectionOutput to a target net.officefloor.compile.spi.section.SectionInput.

Parameters:
sourceSection Source section.
sourceOutputName Name of the source net.officefloor.compile.spi.section.SectionOutput.
targetSection Target section.
targetInputName Name of the target net.officefloor.compile.spi.section.SectionInput.
	public void link(AutoWireSection sourceSectionString sourceOutputName,
			AutoWireSection targetSectionString targetInputName) {
		this..add(new Link(sourceSectionsourceOutputNametargetSection,
				targetInputName));
	}

Determines if the AutoWireSection output is configured for linking.

This aids configuration by allowing to know if net.officefloor.compile.spi.section.SectionOutput flows have been configured (linked).

Parameters:
section AutoWireSection.
sectionOutputName net.officefloor.compile.spi.section.SectionOutput name.
Returns:
true if configured for linking, otherwise false.
	public boolean isLinked(AutoWireSection sectionString sectionOutputName) {
		// Determine if linked
		for (Link link : this.) {
			if ((link.sourceSection.getSectionName().equals(section
					&& (link.sourceOutputName.equals(sectionOutputName))) {
				// Matching section output so configured for linking
				return true;
			}
		}
		// As here, not linked
		return false;
	}

Adds an net.officefloor.compile.spi.office.OfficeTeam responsible for executing net.officefloor.frame.api.execute.Task instances that has an object dependency of the input type.

Parameters:
dependencyType Object dependency type for the net.officefloor.frame.api.execute.Task.
Returns:
AutoWireResponsibility for the net.officefloor.compile.spi.office.OfficeTeam.
	public AutoWireResponsibility addResponsibility(Class<?> dependencyType) {
				dependencyType"team-" + dependencyType.getName());
		this..add(responsibility);
		return responsibility;
	}
	/*
	 * ===================== OfficeSource =========================
	 */
	protected void loadSpecification(SpecificationContext context) {
		// No specification
	}
	public void sourceOffice(OfficeArchitect architect,
			OfficeSourceContext contextthrows Exception {
		// Add the default team
		OfficeTeam defaultTeam = architect.addOfficeTeam("team");
		// Add the responsibility teams
		List<ResponsibleTeamresponsibleTeams = new ArrayList<ResponsibleTeam>(
		for (AutoWireResponsibility responsibility : this.) {
			// Add the Office Team
			OfficeTeam officeTeam = architect.addOfficeTeam(responsibility
			// Register the responsible team
			Class<?> dependencyType = responsibility.getDependencyType();
			ResponsibleTeam responsibleTeam = new ResponsibleTeam(
					dependencyTypeofficeTeam);
			// Add the responsible team
			responsibleTeams.add(responsibleTeam);
		}
		// Load the sections
		Map<StringOfficeObjectobjects = new HashMap<StringOfficeObject>();
		for (AutoWireSection section : this.) {
			// Obtain the section name
			String sectionName = section.getSectionName();
			// Add the section
			OfficeSection officeSection = architect.addOfficeSection(
					sectionNamesection.getSectionSourceClass().getName(),
					section.getSectionLocation(), section.getProperties());
			// Link the objects
			for (OfficeSectionObject object : officeSection
				String objectType = object.getObjectType();
				OfficeObject officeObject = objects.get(objectType);
				if (officeObject == null) {
					officeObject = architect.addOfficeObject(objectType,
							objectType);
					objects.put(objectTypeofficeObject);
				}
				architect.link(objectofficeObject);
			}
			// Register the inputs
			inputs.put(sectionNamesectionInputs);
			for (OfficeSectionInput input : officeSection
				String inputName = input.getOfficeSectionInputName();
				sectionInputs.put(inputNameinput);
			}
			// Register the outputs
			outputs.put(sectionNamesectionOutputs);
			for (OfficeSectionOutput output : officeSection
				String outputName = output.getOfficeSectionOutputName();
				sectionOutputs.put(outputNameoutput);
			}
			// Link section tasks to team.
			// Must be after objects to ensure linked.
			for (OfficeTask task : officeSection.getOfficeTasks()) {
				this.assignTeam(taskresponsibleTeamsdefaultTeamarchitect);
			}
			// Link sub section tasks to team
			for (OfficeSubSection subSection : officeSection
				this.linkTasksToTeams(subSectionresponsibleTeams,
						defaultTeamarchitect);
			}
		}
		// Link outputs to inputs
		for (Link link : this.) {
			// Obtain the link details
			String sourceSectionName = link.sourceSection.getSectionName();
			String sourceOutputName = link.sourceOutputName;
			String outputName = sourceSectionName + ":" + sourceOutputName;
			String targetSectionName = link.targetSection.getSectionName();
			String targetInputName = link.targetInputName;
			String inputName = targetSectionName + ":" + targetInputName;
			// Obtain the output
			OfficeSectionOutput sectionOutput = null;
			Map<StringOfficeSectionOutputsectionOutputs = outputs
					.get(sourceSectionName);
			if (sectionOutputs != null) {
				sectionOutput = sectionOutputs.get(sourceOutputName);
			}
			if (sectionOutput == null) {
				architect.addIssue("Unknown section output '" + outputName
"' to link to section input '" + inputName + "'",
						.outputName);
				continue// no output so can not link
			}
			// Obtain the input
			OfficeSectionInput sectionInput = null;
			Map<StringOfficeSectionInputsectionInputs = inputs
					.get(targetSectionName);
			if (sectionInputs != null) {
				sectionInput = sectionInputs.get(targetInputName);
			}
			if (sectionInput == null) {
				architect.addIssue("Unknown section input '" + inputName
"' for linking section output '" + outputName + "'",
						.inputName);
				continue// no input so can not link
			}
			// Link the output to the input
			architect.link(sectionOutputsectionInput);
		}
	}

	private void linkTasksToTeams(OfficeSubSection subSection,
			List<ResponsibleTeamresponsibleTeamsOfficeTeam defaultTeam,
			OfficeArchitect architect) {
		// Link section tasks to team
		for (OfficeTask task : subSection.getOfficeTasks()) {
			this.assignTeam(taskresponsibleTeamsdefaultTeamarchitect);
		}
		// Recursively link the sub sections
		for (OfficeSubSection subSubSection : subSection.getOfficeSubSections()) {
			this.linkTasksToTeams(subSubSectionresponsibleTeamsdefaultTeam,
					architect);
		}
	}

	private void assignTeam(OfficeTask task,
			List<ResponsibleTeamresponsibleTeamsOfficeTeam defaultTeam,
			OfficeArchitect architect) {
		// Determine if team to be responsible
		for (ResponsibleTeam responsibleTeam : responsibleTeams) {
			if (responsibleTeam.isResponsible(task)) {
				// Team responsible for the task, so link
				architect.link(task.getTeamResponsible(),
						responsibleTeam.officeTeam);
				// Team assigned
				return;
			}
		}
		// As here, default team is responsible
		architect.link(task.getTeamResponsible(), defaultTeam);
	}

	private static class Link {

Source AutoWireSection.
		public final String sourceOutputName;

Target AutoWireSection.
		public final String targetInputName;

Initiate.

Parameters:
sourceSection Source AutoWireSection.
sourceOutputName Source net.officefloor.compile.spi.section.SectionOutput name.
targetSection Target AutoWireSection.
targetInputName Target net.officefloor.compile.spi.section.SectionInput name.
		public Link(AutoWireSection sourceSectionString sourceOutputName,
				AutoWireSection targetSectionString targetInputName) {
			this. = sourceSection;
			this. = sourceOutputName;
			this. = targetSection;
			this. = targetInputName;
		}
	}

	private static class ResponsibleTeam {

Dependency type.
		private final Class<?> dependencyType;

		public final OfficeTeam officeTeam;

Initiate.

Parameters:
dependencyType Dependency type.
officeTeam net.officefloor.compile.spi.office.OfficeTeam.
		public ResponsibleTeam(Class<?> dependencyTypeOfficeTeam officeTeam) {
			this. = dependencyType;
			this. = officeTeam;
		}

		public boolean isResponsible(OfficeTask task) {
		}

		private boolean isResponsible(ObjectDependency[] dependencies,
			// Determine if any objects are of dependency type
			for (ObjectDependency dependency : dependencies) {
				if (this..equals(dependency
					// Matching dependency type, so is responsible
					return true;
				}
			}
			// Determine if responsible for dependent objects
			for (ObjectDependency dependency : dependencies) {
				// Obtain the dependent object
				DependentManagedObject object = dependency
				if (object == null) {
					continue// ignore if dependency not linked
				}
				// Determine if already processed dependent object
				if (objects.contains(object)) {
					continue// ignore as already processed object
				}
				objects.add(object);
				// Recursively determine if responsible
				if (this.isResponsible(object.getObjectDependencies(), objects)) {
					return true// responsible
				}
			}
			// As here, not responsible
			return false;
		}
	}
New to GrepCode? Check out our FAQ X