Example #1
0
        static void Main(string[] args)
        {
            // force reference for reflection.
            var folderBlueprint = new FolderBlueprint();

            var spec = new Specification("Folder", new object());

            var allocator = new DefaultAllocator();
            if (!allocator.CanLease(spec))
            {
                Console.WriteLine("Can't provide leased folder.");
            }
            var folderLease = allocator.Lease(spec).Result as FolderLease;
            Debug.Assert(folderLease != null, "folderLease != null");
            Console.WriteLine("Got folder lease at " + folderLease.Directory.FullName);
            folderLease.Release();
        }
Example #2
0
        public async Task<ILease> Lease(Specification spec)
        {
            // Check to see if any resources can give us a lease.
            foreach (var resource in _activeResources.Where(resource => resource.CanLease(this, spec)))
                return await resource.Lease(this, spec);

            // Nope, see if any blueprints can allocate a resource that
            // will give us a lease.
            foreach (var blueprint in _blueprints.Where(blueprint => blueprint.CanAllocate(this, spec)))
            {
                var resource = await blueprint.Allocate(this, spec);
                if (resource.CanLease(this, spec))
                    return await resource.Lease(this, spec);
                resource.Deallocate();
                throw new Exception("Blueprint stated that it could allocate a resource matching the spec, but the new resource could not lease.");
            }

            // Otherwise throw an exception.
            throw new Exception("Lease can't be provided.");
        }
Example #3
0
 public ILease WaitForLease(Specification spec)
 {
     return Lease(spec).Result;
 }
Example #4
0
 public bool CanLease(Specification spec)
 {
     if (_activeResources.Any(resource => resource.CanLease(this, spec)))
         return true;
     return _blueprints.Any(blueprint => blueprint.CanAllocate(this, spec));
 }
Example #5
0
 public async Task<ILease> Lease(IAllocator allocator, Specification spec)
 {
     LeasedOut = true;
     _folderLease = new FolderLease(this, Directory);
     return _folderLease;
 }
Example #6
0
 public bool CanLease(IAllocator allocator, Specification spec)
 {
     return spec.ResourceType == "Folder" && !LeasedOut;
 }
Example #7
0
 public async Task<IResource> Allocate(IAllocator allocator, Specification spec)
 {
     var directory = new DirectoryInfo("C:\\Workspace");
     return new FolderResource(directory.CreateSubdirectory("FolderAllocation_" + RandomString()));
 }
Example #8
0
 public bool CanAllocate(IAllocator allocator, Specification spec)
 {
     return spec.ResourceType == "Folder" &&
         Directory.Exists("C:\\Workspace");
 }