Update Castle.MicroKernel-vs2005.csproj with missing files.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Views.NVelocity / CustomResourceManager.cs
blob1c0d66a4cd18a9249811da3dc8140589e7dd85f7
1 // Copyright 2004-2008 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 using IRuntimeServices = NVelocity.Runtime.IRuntimeServices;
16 using NVelocity.Runtime.Resource;
17 using ResourceNotFoundException = NVelocity.Exception.ResourceNotFoundException;
19 namespace Castle.MonoRail.Framework.Views.NVelocity
21 using System;
23 ///<summary>
24 /// Manages resource (views) loading and processing, calls and is also
25 /// called by NVelocity to allow recursive expansion of templates
26 ///</summary>
27 public class CustomResourceManager : IResourceManager
29 private IRuntimeServices runtimeServices;
30 private ResourceLoaderAdapter resourceLoaderAdapter;
31 private ICacheProvider cacheProvider;
33 public CustomResourceManager(IViewSourceLoader sourceLoader)
35 resourceLoaderAdapter = new ResourceLoaderAdapter(sourceLoader);
38 public void Initialize(IRuntimeServices runtimeServices)
40 this.runtimeServices = runtimeServices;
42 IServiceProvider serviceProvider =
43 (IServiceProvider) runtimeServices.GetApplicationAttribute(NVelocityViewEngine.ServiceProvider);
45 cacheProvider = (ICacheProvider) serviceProvider.GetService(typeof(ICacheProvider));
48 public Resource GetResource(string resourceName, ResourceType resourceType, string encoding)
50 Resource resource = (Resource) cacheProvider.Get(resourceName);
52 if (resource == null)
54 if (resourceType == ResourceType.Content)
56 resource = new ContentResource();
58 else
60 resource = new CustomTemplate();
63 InitializeResource(resource, resourceName, encoding);
65 resource.ResourceLoader = resourceLoaderAdapter;
67 ProcessResourceWithSensibleExceptionWrapping(resourceName, resource);
69 if (resource.Data == null)
71 throw new ResourceNotFoundException("Unable to find resource '" + resourceName + "'");
74 resource.LastModified = DateTime.Now.Ticks;
75 resource.ModificationCheckInterval = resourceLoaderAdapter.ModificationCheckInterval;
76 resource.Touch();
78 if (!resource.IsSourceModified())
80 cacheProvider.Store(resourceName, resource);
83 else
85 if (resource.IsSourceModified())
87 ProcessResourceWithSensibleExceptionWrapping(resourceName, resource);
91 return resource;
94 public string GetLoaderNameForResource(string resourceName)
96 return "default";
99 private void ProcessResourceWithSensibleExceptionWrapping(string resourceName,
100 global::NVelocity.Runtime.Resource.Resource resource)
104 resource.Process();
106 catch(Exception e)
108 throw new ResourceProcessingException(resourceName, e);
112 private void InitializeResource(Resource resource, string resourceName, string encoding)
114 resource.RuntimeServices = runtimeServices;
115 resource.Name = resourceName;
116 resource.Encoding = encoding;