Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / MonoRail / Castle.MonoRail.Framework.Views.NVelocity / CustomResourceManager.cs
blobcee64307a88c245a72502a0c13a9deb8767c44f5
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 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 using Castle.MonoRail.Framework.Internal;
25 ///<summary>
26 /// Manages resource (views) loading and processing, calls and is also
27 /// called by NVelocity to allow recursive expansion of templates
28 ///</summary>
29 public class CustomResourceManager : IResourceManager
31 private IRuntimeServices runtimeServices;
32 private ResourceLoaderAdapter resourceLoaderAdapter;
33 private ICacheProvider cacheProvider;
35 public CustomResourceManager(IViewSourceLoader sourceLoader)
37 resourceLoaderAdapter = new ResourceLoaderAdapter(sourceLoader);
40 public void Initialize(IRuntimeServices runtimeServices)
42 this.runtimeServices = runtimeServices;
44 IServiceProvider serviceProvider =
45 (IServiceProvider) runtimeServices.GetApplicationAttribute(NVelocityViewEngine.ServiceProvider);
47 cacheProvider = (ICacheProvider) serviceProvider.GetService(typeof(ICacheProvider));
50 public Resource GetResource(string resourceName, ResourceType resourceType, string encoding)
52 Resource resource = (Resource) cacheProvider.Get(resourceName);
54 if (resource == null)
56 if (resourceType == ResourceType.Content)
58 resource = new ContentResource();
60 else
62 resource = new CustomTemplate();
65 InitializeResource(resource, resourceName, encoding);
67 resource.ResourceLoader = resourceLoaderAdapter;
69 ProcessResourceWithSensibleExceptionWrapping(resourceName, resource);
71 if (resource.Data == null)
73 throw new ResourceNotFoundException("Unable to find resource '" + resourceName + "'");
76 resource.LastModified = DateTime.Now.Ticks;
77 resource.ModificationCheckInterval = resourceLoaderAdapter.ModificationCheckInterval;
78 resource.Touch();
80 if (!resource.IsSourceModified())
82 cacheProvider.Store(resourceName, resource);
85 else
87 if (resource.IsSourceModified())
89 ProcessResourceWithSensibleExceptionWrapping(resourceName, resource);
93 return resource;
96 public string GetLoaderNameForResource(string resourceName)
98 return "default";
101 private void ProcessResourceWithSensibleExceptionWrapping(string resourceName,
102 global::NVelocity.Runtime.Resource.Resource resource)
106 resource.Process();
108 catch(Exception e)
110 throw new ResourceProcessingException(resourceName, e);
114 private void InitializeResource(Resource resource, string resourceName, string encoding)
116 resource.RuntimeServices = runtimeServices;
117 resource.Name = resourceName;
118 resource.Encoding = encoding;