package org.nnsoft.guice.autobind.install;
/*
* Copyright 2012 The 99 Software Foundation
*
* Licensed 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.
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* This class is internally used, to bind Installation Requests and process them
* sequentially in a predefined order.
*/
public class InstallationContext {
private final Map<BindingStage, List<Callable<?>>> context = new HashMap<BindingStage, List<Callable<?>>>();
public void process()
throws Exception
{
for ( BindingStage stage : BindingStage.ORDERED )
{
List<Callable<?>> requests = context.get( stage );
if ( requests != null )
{
for ( Callable<?> request : requests )
{
request.call();
}
}
}
}
public void add( BindingStage stage, Callable<?> request )
{
synchronized ( context )
{
List<Callable<?>> requests = context.get( stage );
if ( requests == null )
{
requests = new ArrayList<Callable<?>>();
context.put( stage, requests );
}
requests.add( request );
}
}
public void add( StageableRequest request )
{
synchronized ( context )
{
List<Callable<?>> requests = context.get( request.getExecutionStage() );
if ( requests == null )
{
requests = new ArrayList<Callable<?>>();
context.put( request.getExecutionStage(), requests );
}
requests.add( request );
}
}
public static interface StageableRequest
extends Callable<Void>
{
BindingStage getExecutionStage();
}
}