Introducing IController - just a bare bone extract interface at this point
[castle.git] / Core / Castle.Core / Resource / UncResource.cs
blobd0bfc5e71bb1d8137d5c6034c68328d111e3516e
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Core.Resource
17 using System;
18 using System.IO;
20 /// <summary>
21 /// Enable access to files on network shares
22 /// </summary>
23 public class UncResource : AbstractStreamResource
25 private readonly Stream stream;
26 private String basePath;
27 private string filePath;
29 public UncResource(CustomUri resource)
31 stream = CreateStreamFromUri(resource, DefaultBasePath);
34 public UncResource(CustomUri resource, String basePath)
36 stream = CreateStreamFromUri(resource, basePath);
39 public UncResource(String resourceName) : this(new CustomUri(resourceName))
43 public UncResource(String resourceName, String basePath) : this(new CustomUri(resourceName), basePath)
47 public override String FileBasePath
49 get { return basePath; }
52 public override IResource CreateRelative(String resourceName)
54 return new UncResource(Path.Combine(basePath, resourceName));
57 public override string ToString()
59 return String.Format("UncResource: [{0}] [{1}]", filePath, basePath);
62 protected override Stream Stream
64 get { return stream; }
67 private Stream CreateStreamFromUri(CustomUri resource, String basePath)
69 if (resource == null)
70 throw new ArgumentNullException("resource");
71 if (!resource.IsUnc)
72 throw new ArgumentException("Resource must be an Unc", "resource");
73 if (!resource.IsFile)
74 throw new ArgumentException("The specified resource is not a file", "resource");
76 String filePath = resource.Path;
78 if (!File.Exists(filePath) && basePath != null)
80 filePath = Path.Combine(basePath, filePath);
83 this.filePath = Path.GetFileName(filePath);
84 this.basePath = Path.GetDirectoryName(filePath);
86 CheckFileExists(filePath);
88 return File.OpenRead(filePath);
91 private void CheckFileExists(String path)
93 if (!File.Exists(path))
95 String message = String.Format("File {0} could not be found", path);
96 throw new ResourceException(message);