1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
.MicroKernel
.SubSystems
.Resource
18 using System
.Collections
;
20 using Castle
.Core
.Resource
;
25 public class DefaultResourceSubSystem
: AbstractSubSystem
, IResourceSubSystem
27 private readonly IList resourceFactories
= new ArrayList();
29 public DefaultResourceSubSystem()
31 InitDefaultResourceFactories();
34 protected virtual void InitDefaultResourceFactories()
36 RegisterResourceFactory( new AssemblyResourceFactory() );
37 RegisterResourceFactory( new FileResourceFactory() );
38 RegisterResourceFactory( new UncResourceFactory() );
39 RegisterResourceFactory( new ConfigResourceFactory() );
42 public void RegisterResourceFactory(IResourceFactory resourceFactory
)
44 if (resourceFactory
== null) throw new ArgumentNullException("resourceFactory");
46 resourceFactories
.Add( resourceFactory
);
49 public IResource
CreateResource(String resource
)
51 if (resource
== null) throw new ArgumentNullException("resource");
53 return CreateResource(new CustomUri(resource
));
56 public IResource
CreateResource(String resource
, String basePath
)
58 if (resource
== null) throw new ArgumentNullException("resource");
60 return CreateResource(new CustomUri(resource
), basePath
);
63 public IResource
CreateResource(CustomUri uri
)
65 if (uri
== null) throw new ArgumentNullException("uri");
67 foreach(IResourceFactory resFactory
in resourceFactories
)
69 if (resFactory
.Accept(uri
))
71 return resFactory
.Create(uri
);
75 throw new KernelException("No Resource factory was able to " +
76 "deal with Uri " + uri
.ToString());
79 public IResource
CreateResource(CustomUri uri
, String basePath
)
81 if (uri
== null) throw new ArgumentNullException("uri");
82 if (basePath
== null) throw new ArgumentNullException("basePath");
84 foreach(IResourceFactory resFactory
in resourceFactories
)
86 if (resFactory
.Accept(uri
))
88 return resFactory
.Create(uri
, basePath
);
92 throw new KernelException("No Resource factory was able to " +
93 "deal with Uri " + uri
.ToString());