/*
* 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.Framework;
using Apache.Avalon.Meta;
/// The model repository interface declares operations through which
/// clients may resolve registered model instances relative to
/// a stage or service dependencies.
///
///
/// Stephen McConnell
///
/// $Revision: 1.2 $ $Date: 2004/03/07 22:06:40 $
///
public class DefaultModelRepository : IModelRepository
{
private void InitBlock()
{
m_models = new System.Collections.Hashtable();
}
//------------------------------------------------------------------
// immutable state
//------------------------------------------------------------------
/// The parent appliance repository.
private IModelRepository m_parent;
private ILogger m_logger;
/// Table of registered appliance instances keyed by name.
private System.Collections.Hashtable m_models;
//------------------------------------------------------------------
// constructor
//------------------------------------------------------------------
public DefaultModelRepository(IModelRepository parent, ILogger logger)
{
InitBlock();
m_parent = parent;
m_logger = logger;
}
//------------------------------------------------------------------
// IModelRepository
//------------------------------------------------------------------
/// Return a sequence of all of the local models.
///
///
/// the model sequence
///
public virtual IDeploymentModel[] Models
{
get
{
return (IDeploymentModel[])
new System.Collections.ArrayList( m_models.Values ).ToArray( typeof(IDeploymentModel) );
}
}
/// Locate an model meeting the supplied criteria.
///
///
/// a component service dependency
///
/// the model or null if no matching model is resolved
///
public virtual IDeploymentModel GetModel(DependencyDescriptor dependency)
{
//
// attempt to locate a solution locally
//
System.Collections.IEnumerator iterator = m_models.Values.GetEnumerator();
while (iterator.MoveNext())
{
IDeploymentModel model = (IDeploymentModel) iterator.Current;
if (model.IsaCandidate(dependency))
{
return model;
}
}
//
// attempt to locate a solution from the parent
//
if (m_parent != null)
{
return m_parent.GetModel(dependency);
}
return null;
}
/// Locate all models meeting the supplied criteria.
///
///
/// a component stage dependency
///
/// the candidate models
///
public virtual IDeploymentModel[] GetCandidateProviders(StageDescriptor stage)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
System.Collections.IEnumerator iterator = m_models.Values.GetEnumerator();
while (iterator.MoveNext())
{
IDeploymentModel model = (IDeploymentModel) iterator.Current;
if (model.IsaCandidate(stage))
{
list.Add(model);
}
}
if (m_parent != null)
{
IDeploymentModel[] models = m_parent.GetCandidateProviders(stage);
for (int i = 0; i < models.Length; i++)
{
list.Add(models[i]);
}
}
return (IDeploymentModel[]) list.ToArray( typeof(IDeploymentModel) );
}
/// Locate all models meeting the supplied dependency criteria.
///
/// a component service dependency
///
/// the candidate models
///
public virtual IDeploymentModel[] GetCandidateProviders(DependencyDescriptor dependency)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
System.Collections.IEnumerator iterator = m_models.Values.GetEnumerator();
while (iterator.MoveNext())
{
IDeploymentModel model = (IDeploymentModel) iterator.Current;
if (model.IsaCandidate(dependency))
{
list.Add(model);
}
}
if (m_parent != null)
{
IDeploymentModel[] models = m_parent.GetCandidateProviders(dependency);
for (int i = 0; i < models.Length; i++)
{
list.Add(models[i]);
}
}
return (IDeploymentModel[]) list.ToArray( typeof(IDeploymentModel) );
}
///
/// Locate a model meeting the supplied criteria.
///
/// a component stage dependency
///
/// the model
///
public virtual IDeploymentModel GetModel(StageDescriptor stage)
{
System.Collections.IEnumerator iterator = m_models.Values.GetEnumerator();
while (iterator.MoveNext())
{
IDeploymentModel model = (IDeploymentModel) iterator.Current;
if (model.IsaCandidate(stage))
{
return model;
}
}
if (m_parent != null)
{
return m_parent.GetModel(stage);
}
return null;
}
//------------------------------------------------------------------
// implementation
//------------------------------------------------------------------
/// Add an model to the repository.
///
/// the model to add
///
public virtual void AddModel(IDeploymentModel model)
{
object tempObject;
tempObject = model;
m_models[model.Name] = tempObject;
System.Object generatedAux = tempObject;
}
/// Add an model to the repository.
///
///
/// the name to register the model under
///
/// the model to add
///
public virtual void AddModel(String name, IDeploymentModel model)
{
object tempObject;
tempObject = model;
m_models[name] = tempObject;
System.Object generatedAux = tempObject;
}
/// Remove an model from the repository.
///
///
/// the model to remove
///
public virtual void RemoveModel(IDeploymentModel model)
{
m_models.Remove(model.Name);
}
/// Locate a local model matching the supplied name.
///
///
/// the model name
///
/// the model or null if the model name is unknown
///
public virtual IDeploymentModel GetModel(String name)
{
IDeploymentModel model = (IDeploymentModel) m_models[name];
if (model == null && m_logger != null)
{
m_logger.Debug("Can't find '" + name + "' in model repository: " + m_models);
}
return model;
}
}
}