#region Disclaimer / License
// Copyright (C) 2010, Jackie Ng
// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using OSGeo.MapGuide.MaestroAPI;
using ICSharpCode.Core;
using System.ComponentModel;
using Maestro.Shared.UI;
namespace Maestro.Base.Services
{
///
/// Defines a method for connection-related events
///
///
///
public delegate void ServerConnectionEventHandler(object sender, string name);
///
/// Defines a method that handles connection pre-removal
///
///
///
public delegate void ServerConnectionRemovingEventHandler(object sender, ServerConnectionRemovingEventArgs e);
///
/// Defines a cancelable event for a connection that is about to be closed
///
public class ServerConnectionRemovingEventArgs : CancelEventArgs
{
public ServerConnectionRemovingEventArgs(string name)
{
this.ConnectionName = name;
base.Cancel = false;
}
public string ConnectionName { get; set; }
}
///
/// Manages instances
///
public class ServerConnectionManager : ServiceBase
{
///
/// Raised when a connection has been added
///
public event ServerConnectionEventHandler ConnectionAdded;
///
/// Raised when a connection is about to be removed. Subscribers can cancel this event if required
///
public event ServerConnectionRemovingEventHandler ConnectionRemoving;
///
/// Raised when a connection has been removed
///
public event ServerConnectionEventHandler ConnectionRemoved;
private Dictionary _connections = new Dictionary();
///
/// Gets the names of all currently open connections
///
///
public ICollection GetConnectionNames()
{
return _connections.Keys;
}
///
/// Initializes this instance
///
public override void Initialize()
{
base.Initialize();
LoggingService.Info(Strings.Service_Init_Server_Connection_Manager);
}
///
/// Gets the connection by its registered name
///
///
///
public IServerConnection GetConnection(string name)
{
if (_connections.ContainsKey(name))
return _connections[name];
return null;
}
///
/// Registers a connection by a given name
///
///
///
public void AddConnection(string name, IServerConnection conn)
{
_connections.Add(name, conn);
var handler = this.ConnectionAdded;
if (handler != null)
handler(this, name);
}
///
/// Removes a connection by its given name
///
///
///
public IServerConnection RemoveConnection(string name)
{
if (_connections.ContainsKey(name))
{
var removing = this.ConnectionRemoving;
var ce = new ServerConnectionRemovingEventArgs(name);
if (removing != null)
removing(this, ce);
if (ce.Cancel)
return null;
IServerConnection conn = _connections[name];
_connections.Remove(name);
var removed = this.ConnectionRemoved;
if (removed != null)
removed(this, name);
return conn;
}
return null;
}
}
}