/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.cocoon.pipeline.builder; import java.io.OutputStream; import java.util.Collections; import java.util.Map; import org.apache.cocoon.pipeline.AsyncCachePipeline; import org.apache.cocoon.pipeline.CachingPipeline; import org.apache.cocoon.pipeline.NonCachingPipeline; import org.apache.cocoon.pipeline.Pipeline; import org.apache.cocoon.pipeline.component.Finisher; import org.apache.cocoon.pipeline.component.PipelineComponent; import org.apache.cocoon.pipeline.component.Starter; public final class PipelineBuilder { /** * Hidden constructor, this class can't be instantiated. */ private PipelineBuilder() { // do nothing } /** * {@inheritDoc} */ public static LinkedPipelineStarterBuilder newAsyncCachePipeline() { return newPipeline(new AsyncCachePipeline()); } /** * {@inheritDoc} */ public static LinkedPipelineStarterBuilder newCachingPipeline() { return newPipeline(new CachingPipeline()); } /** * {@inheritDoc} */ public static LinkedPipelineStarterBuilder newNonCachingPipeline() { return newPipeline(new NonCachingPipeline()); } private static LinkedPipelineStarterBuilder newPipeline( final Pipeline pipeline) { return new LinkedPipelineStarterBuilder() { @Override public LinkedPipelineComponentBuilder setStarter(final PC starter) { if (starter == null) { throw new IllegalArgumentException("Parameter 'starter' must be not null"); } if (!(starter instanceof Starter)) { throw new IllegalArgumentException( "Parameter 'starter' must be " + Starter.class.getName() + " instance"); } pipeline.addComponent(starter); return new LinkedPipelineComponentBuilder() { @Override public LinkedPipelineComponentBuilder addComponent(final PC pipelineComponent) { if (pipelineComponent == null) { throw new IllegalArgumentException("Parameter 'pipelineComponent' must be not null"); } pipeline.addComponent(pipelineComponent); return this; } @Override public LinkedPipelineConfigurationBuilder setFinisher(final PC finisher) { if (finisher == null) { throw new IllegalArgumentException("Parameter 'finisher' must be not null"); } if (!(finisher instanceof Finisher)) { throw new IllegalArgumentException( "Parameter 'finisher' must be " + Finisher.class.getName() + " instance"); } pipeline.addComponent(finisher); return new LinkedPipelineConfigurationBuilder() { @SuppressWarnings("unchecked") @Override public LinkedPipelineSetupBuilder withEmptyConfiguration() { return this.setConfiguration(Collections.EMPTY_MAP); } @Override public LinkedPipelineSetupBuilder setConfiguration( final Map parameters) { if (parameters == null) { throw new IllegalArgumentException("Parameter 'parameters' must be not null"); } pipeline.setConfiguration(parameters); return new LinkedPipelineSetupBuilder() { @Override public Pipeline setup(final OutputStream outputStream) { if (outputStream == null) { throw new IllegalArgumentException( "Parameter 'outputStream' must be not null"); } pipeline.setup(outputStream); return pipeline; } @Override public Pipeline setup(final OutputStream outputStream, final Map parameters) { if (outputStream == null) { throw new IllegalArgumentException( "Parameter 'outputStream' must be not null"); } pipeline.setup(outputStream, parameters); return pipeline; } }; } }; } }; } }; } }