/*
* Copyright 2003-2004 The Apache 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.
*/
namespace Apache.Avalon.Composition.Model.Default
{
using System;
using Apache.Avalon.Composition.Data;
using Apache.Avalon.Composition.Model;
using Apache.Avalon.Meta;
/// Default selector class. The default selector selcts profiles based
/// of ranking of profile relative to EXPLICIT, PACKAGED and IMPLICIT
/// status. For each category, if a supplied profile matches the category
/// the first profile matching the category is returned.
///
///
/// Stephen McConnell
///
/// $Revision: 1.2 $ $Date: 2004/03/07 22:06:40 $
///
class DefaultModelSelector : IModelSelector
{
//==============================================================
// ModelSelector
//==============================================================
/// Returns the preferred model from an available selection of
/// candidates capable of fulfilling a supplied service dependency.
///
///
/// the set of candidate models
///
/// a service dependency
///
/// the preferred model or null if no satisfactory
/// provider can be established
///
public virtual IDeploymentModel Select(IDeploymentModel[] models, DependencyDescriptor dependency)
{
IDeploymentModel[] candidates = FilterCandidateProviders(models, dependency);
return Select(candidates);
}
/// Returns the preferred model from an available selection of candidates
/// the set of candidate models
///
/// the stage dependency
///
/// the preferred provider or null if no satisfactory
/// provider can be established
///
public virtual IDeploymentModel Select(IDeploymentModel[] models, StageDescriptor stage)
{
IDeploymentModel[] candidates = FilterCandidateProviders(models, stage);
return Select(candidates);
}
//==============================================================
// implementation
//==============================================================
private IDeploymentModel[] FilterCandidateProviders(IDeploymentModel[] models, DependencyDescriptor dependency)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
for (int i = 0; i < models.Length; i++)
{
IDeploymentModel model = models[i];
if (model.IsaCandidate(dependency))
{
list.Add(model);
}
}
return (IDeploymentModel[]) list.ToArray( typeof(IDeploymentModel) );
}
private IDeploymentModel[] FilterCandidateProviders(IDeploymentModel[] models, StageDescriptor stage)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
for (int i = 0; i < models.Length; i++)
{
IDeploymentModel model = models[i];
if (model.IsaCandidate(stage))
{
list.Add(model);
}
}
return (IDeploymentModel[]) list.ToArray( typeof(IDeploymentModel) );
}
/// Select a model from a set of models based on a priority ordering
/// of EXPLICIT, PACKAGE and lastly IMPLICIT. If multiple candidates
/// exist for a particulr mode, return the first candidate.
///
/// the set of candidate profiles
///
/// the service dependency
///
/// the preferred profile or null if no satisfactory
/// provider can be established
///
private IDeploymentModel Select(IDeploymentModel[] models)
{
if (models.Length == 0)
{
return null;
}
for (int i = 0; i < models.Length; i++)
{
if (models[i].Mode.Equals(Mode.Explicit))
{
return models[i];
}
}
for (int i = 0; i < models.Length; i++)
{
if (models[i].Mode.Equals(Mode.Packaged))
{
return models[i];
}
}
for (int i = 0; i < models.Length; i++)
{
if (models[i].Mode.Equals(Mode.Implicit))
{
return models[i];
}
}
return null;
}
}
}