Created
May 30, 2013 16:29
-
-
Save bhameyie/5679240 to your computer and use it in GitHub Desktop.
My Task Catalog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.Composition; | |
using System.ComponentModel.Composition.Hosting; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using OC.SchedulerInterface; | |
using System.ComponentModel.Composition.Primitives; | |
namespace OC.Scheduler.Core | |
{ | |
public class TaskCatalog | |
{ | |
[ImportMany] | |
List<ISchedulerTask> m_tasks; | |
readonly CompositionContainer m_container; | |
readonly FileSystemWatcher m_fileSystemWatcher; | |
public List<ISchedulerTask> AvailableTasks | |
{ | |
get { return m_tasks; } | |
} | |
public TaskCatalog() | |
{ | |
var catalog = new AggregateCatalog(); | |
var folders = Directory.GetDirectories(ConfigSettings.Instance.TaskBinLocation, "*", SearchOption.AllDirectories); | |
foreach (string folder in folders) | |
{ | |
var directoryCatalog = new DirectoryCatalog(folder); | |
catalog.Catalogs.Add(directoryCatalog); | |
} | |
catalog.Catalogs.Add(new DirectoryCatalog(ConfigSettings.Instance.TaskBinLocation)); | |
m_fileSystemWatcher = new FileSystemWatcher(ConfigSettings.Instance.TaskBinLocation); | |
m_fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size; | |
m_fileSystemWatcher.Changed += OnDllAdded; | |
m_fileSystemWatcher.Created += OnDllAdded; | |
m_fileSystemWatcher.EnableRaisingEvents = true; | |
m_container = new CompositionContainer(catalog); | |
m_container.ComposeParts(this); | |
} | |
private void OnDllAdded(object sender, FileSystemEventArgs e) | |
{ | |
var catalog = (AggregateCatalog)m_container.Catalog; | |
var folders = Directory.GetDirectories(ConfigSettings.Instance.TaskBinLocation, "*", SearchOption.AllDirectories); | |
foreach (string folder in folders) | |
{ | |
if (catalog.Catalogs.Count(fol => ((DirectoryCatalog)fol).Path == folder) > 0) | |
continue; | |
var directoryCatalog = new DirectoryCatalog(folder); | |
catalog.Catalogs.Add(directoryCatalog); | |
} | |
Refresh(); | |
} | |
public void Refresh() | |
{ | |
m_container.ComposeParts(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment