// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Linq;
using System.Threading.Tasks;
using CoCoL;
namespace Duplicati.Library.UsageReporter
{
public abstract class ShutdownHelper : IDisposable
{
///
/// Uses reflection to find all properties and fields that are of type ContinuationChannel
/// and calls the Retire method on them
///
/// The instance to examine.
public static void RetireAllChannels(object item)
{
if (item == null)
return;
foreach(var f in item.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance))
if (typeof(IRetireAbleChannel).IsAssignableFrom(f.FieldType))
{
try
{
((IRetireAbleChannel)f.GetValue(item)).Retire();
}
catch
{
}
}
foreach(var f in item.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance))
if (typeof(IRetireAbleChannel).IsAssignableFrom(f.PropertyType))
{
try
{
((IRetireAbleChannel)f.GetValue(item)).Retire();
}
catch
{
}
}
}
///
/// Runs a method and disposes this instance afterwards
///
/// The task for completion.
/// The callback method that does the actual work.
/// If set to true any RetiredExceptions are caught and ignored.
protected async Task RunProtected(Func method, bool catchRetiredExceptions = true)
{
try
{
using(this)
await method();
}
catch(AggregateException ex)
{
if (catchRetiredExceptions)
{
var lst = from n in ex.Flatten().InnerExceptions
where !(n is RetiredException)
select n;
if (lst.Count() == 0)
return;
else if (lst.Count() == 1)
throw lst.First();
else
throw new AggregateException(lst);
}
if (ex.Flatten().InnerExceptions.Count == 1)
throw ex.Flatten().InnerExceptions.First();
throw;
}
catch(RetiredException)
{
if (!catchRetiredExceptions)
throw;
}
}
///
/// Releases all resource used by the object.
///
/// Call when you are finished using the
/// . The method leaves the
/// in an unusable state. After calling
/// , you must release all references to the
/// so the garbage collector can reclaim the memory
/// that the was occupying.
public void Dispose()
{
RetireAllChannels(this);
}
}
}