/*
* 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;
using Apache.Avalon.Repository;
using Apache.Avalon.Composition.Data;
using Apache.Avalon.Composition.Logging;
using Apache.Avalon.Composition.Model;
/// Implementation of a system context that exposes a system wide set of parameters.
///
///
/// Avalon Development Team
///
/// $Revision: 1.2 $ $Date: 2004/02/29 18:07:17 $
///
public class DefaultSystemContext : Apache.Avalon.Framework.DefaultContext, ISystemContext
{
//--------------------------------------------------------------
// static
//--------------------------------------------------------------
//--------------------------------------------------------------
// immutable state
//--------------------------------------------------------------
private System.IO.FileInfo m_base;
private System.IO.FileInfo m_home;
private System.IO.FileInfo m_temp;
private IRepository m_repository;
private ILoggingManager m_logging;
private ILogger m_logger;
private IModelFactory m_factory;
private IRuntime m_runtime;
private long m_timeout;
private bool m_secure;
//--------------------------------------------------------------
// mutable state
//--------------------------------------------------------------
private bool m_trace;
//--------------------------------------------------------------
// constructor
//--------------------------------------------------------------
/// Creation of a new system context.
///
///
/// a repository initial context
///
/// an artifact identifying the default runtime
///
/// the logging manager
///
/// the base directory from which relative references
/// within a classpath or library directive shall be resolved
///
/// the home directory
///
/// the temp directory
///
/// the application repository to be used when resolving
/// resource directives
///
/// the kernel logging category name
///
/// flag indicating if internal logging is enabled
///
/// a system wide default deployment timeout
///
/*public DefaultSystemContext(Artifact artifact, ILoggingManager logging,
System.IO.FileInfo base_Renamed, System.IO.FileInfo home,
System.IO.FileInfo temp, IRepository repository,
String category, bool trace, long timeout,
bool secure) :
this(artifact, null, logging, base_Renamed, home, temp,
repository, category, trace, timeout, secure)
{
}*/
/// Creation of a new system context.
///
///
/// the runtime instance
///
/// the logging manager
///
/// the base directory from which relative references
/// within a classpath or library directive shall be resolved
///
/// the home directory
///
/// the temp directory
///
/// the application repository to be used when resolving
/// resource directives
///
/// the kernel logging category name
///
/// flag indicating if internal logging is enabled
///
/// a system wide default deployment timeout
///
/*public DefaultSystemContext(IRuntime runtime, ILoggingManager logging,
System.IO.FileInfo base_Renamed, System.IO.FileInfo home, System.IO.FileInfo temp,
IRepository repository, String category, bool trace, long timeout, bool secure) :
this(runtime, logging, base_Renamed, home, temp, repository, category, trace, timeout, secure)
{
}*/
/// Creation of a new system context.
///
///
/// the repository intial context
///
/// the runtime instance
///
/// the runtime class
///
/// the logging manager
///
/// the base directory from which relative references
/// within a classpath or library directive shall be resolved
///
/// the home directory
///
/// the temp directory
///
/// the application repository to be used when resolving
/// resource directives
///
/// the kernel logging category name
///
/// flag indicating if internal logging is enabled
///
/// a system wide default deployment timeout
///
public DefaultSystemContext(IRuntime runtime,
ILoggingManager logging, System.IO.FileInfo base_Renamed, System.IO.FileInfo home,
System.IO.FileInfo temp, IRepository repository, String category, bool trace,
long timeout, bool secure)
{
if (base_Renamed == null)
{
throw new System.ArgumentNullException("base");
}
/*
if (repository == null)
{
throw new System.ArgumentNullException("repository");
}*/
if (logging == null)
{
throw new System.ArgumentNullException("logger");
}
if (!System.IO.Directory.Exists(base_Renamed.FullName))
{
String error = "system.error.base-not-a-directory" + " " + base_Renamed.FullName;
throw new System.ArgumentException(error);
}
m_base = base_Renamed;
m_home = home;
m_temp = temp;
m_trace = trace;
m_repository = repository;
m_logging = logging;
m_timeout = timeout;
m_secure = secure;
m_runtime = runtime;
m_logger = m_logging.GetLoggerForCategory(category);
m_factory = new StandardModelFactory(this);
}
//--------------------------------------------------------------
// ISystemContext
//--------------------------------------------------------------
/// Return the model factory.
///
///
/// the factory
///
public virtual IModelFactory ModelFactory
{
get
{
return m_factory;
}
}
/// Return the base directory from which relative classloader
/// references may be resolved.
///
///
/// the base directory
///
public virtual System.IO.FileInfo BaseDirectory
{
get
{
return m_base;
}
}
/// Return the working directory from which containers may
/// establish persistent content.
///
///
/// the working directory
///
public virtual System.IO.FileInfo HomeDirectory
{
get
{
return m_home;
}
}
/// Return the temporary directory from which a container
/// may use to establish a transient content directory.
///
///
/// the temporary directory
///
public virtual System.IO.FileInfo TempDirectory
{
get
{
return m_temp;
}
}
/// Return the system wide repository from which resource
/// directives can be resolved.
///
///
/// the repository
///
public virtual IRepository Repository
{
get
{
return m_repository;
}
}
/// Return the system trace flag.
///
///
/// the trace flag
///
public virtual bool IsTraceEnabled
{
get
{
return m_trace;
}
}
/// Set the system trace flag.
///
///
/// the trace flag
///
public virtual void SetTraceEnabled(bool trace)
{
m_trace = trace;
}
/// Return the logging manager.
///
///
/// the logging manager.
///
public virtual ILoggingManager LoggingManager
{
get
{
return m_logging;
}
}
/// Return the system logging channel.
///
///
/// the system logging channel
///
public virtual ILogger Logger
{
get
{
return m_logger;
}
}
/// Return the default deployment phase timeout value.
/// the timeout value
///
public virtual long DefaultDeploymentTimeout
{
get
{
return m_timeout;
}
}
/// Return the enabled status of the code security policy.
/// the code security enabled status
///
/*public virtual bool isCodeSecurityEnabled()
{
return m_secure;
}*/
//------------------------------------------------------------------
// runtime operations
//------------------------------------------------------------------
/// Request the commissioning of a runtime for a supplied deployment
/// model.
///
/// the deployment model
///
/// Exception of a commissioning error occurs
///
public virtual void Commission(IDeploymentModel model)
{
Runtime.Commission(model);
}
/// Request the decommissioning of a runtime for a supplied deployment
/// model.
///
/// the deployment model
///
/// Exception of a commissioning error occurs
///
public virtual void Decommission(IDeploymentModel model)
{
Runtime.Decommission(model);
}
/// Request resolution of an object from the runtime.
/// the deployment model
///
/// Exception if a deployment error occurs
///
public virtual System.Object Resolve(IDeploymentModel model)
{
return Runtime.Resolve(model);
}
/// Request the release of an object from the runtime.
/// the deployment model
///
/// the object to release
///
/// Exception if a deployment error occurs
///
public virtual void Release(IDeploymentModel model, System.Object instance)
{
Runtime.Release(model, instance);
}
/// Return the runtime factory.
///
///
/// the factory
///
private IRuntime Runtime
{
get
{
return m_runtime;
}
}
//------------------------------------------------------------------
// runtime operations
//------------------------------------------------------------------
/// Get the runtime class referenced by the artifact.
/// the repository initial context
///
/// the factory artifact
///
/// the Runtime class
///
/*private System.Type GetRuntimeClass(InitialContext context, Artifact artifact)
{
if (null == artifact)
return null;
try
{
ClassLoader classloader = typeof(DefaultSystemContext).getClassLoader();
Builder builder = context.newBuilder(classloader, artifact);
return builder.getFactoryClass();
}
catch (System.Exception e)
{
String error = "todo: error message"; //"system.error.load", artifact.toString());
throw new SystemException(error, e);
}
}*/
/// Build a runtime using a supplied class.
///
///
/// the log target factory class
///
/// a instance of the class
///
/// SystemException if the class does not expose a public
/// constructor, or the constructor requires arguments that the
/// builder cannot resolve, or if a unexpected instantiation error
/// ooccurs
///
/*
public virtual Runtime buildRuntimeInstance(InitialContext context, System.Type type)
{
if (null == type)
return null;
System.Reflection.ConstructorInfo[] constructors = type.GetConstructors();
if (constructors.Length < 1)
{
String error = "todo: error message"; //"system.error.runtime.no-constructor", type.FullName);
throw new SystemException(error);
}
//
// log target factories only have one constructor
//
System.Reflection.ConstructorInfo constructor = constructors[0];
System.Type[] classes = constructor.GetParameters();
System.Object[] args = new System.Object[classes.Length];
for (int i = 0; i < classes.Length; i++)
{
System.Type c = classes[i];
if (typeof(ISystemContext).IsAssignableFrom(c))
{
args[i] = this;
}
else if (typeof(InitialContext).IsAssignableFrom(c))
{
args[i] = context;
}
else
{
String error = "todo: error message"; //"system.error.unrecognized-runtime-parameter", c.FullName, type.FullName);
throw new SystemException(error);
}
}
//
// instantiate the factory
//
return instantiateRuntime(constructor, args);
}*/
/// Instantiation of a runtime using a supplied constructor
/// and arguments.
///
///
/// the runtime constructor
///
/// the constructor arguments
///
/// the runtime instance
///
/// SystemException if an instantiation error occurs
///
/*
private Runtime instantiateRuntime(System.Reflection.ConstructorInfo constructor, System.Object[] args)
{
System.Type type = constructor.DeclaringType;
try
{
return (Runtime) constructor.newInstance(args);
}
catch (System.Exception e)
{
String error = "todo: error message"; //"system.error.runtime-instantiation", type.FullName);
throw new SystemException(error, e);
}
}*/
}
}