1 // Copyright 2004-2008 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 using IRuntimeServices
= NVelocity
.Runtime
.IRuntimeServices
;
16 using NVelocity
.Runtime
.Resource
;
17 using ResourceNotFoundException
= NVelocity
.Exception
.ResourceNotFoundException
;
19 namespace Castle
.MonoRail
.Framework
.Views
.NVelocity
24 /// Manages resource (views) loading and processing, calls and is also
25 /// called by NVelocity to allow recursive expansion of templates
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
);
54 if (resourceType
== ResourceType
.Content
)
56 resource
= new ContentResource();
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
;
78 if (!resource
.IsSourceModified())
80 cacheProvider
.Store(resourceName
, resource
);
85 if (resource
.IsSourceModified())
87 ProcessResourceWithSensibleExceptionWrapping(resourceName
, resource
);
94 public string GetLoaderNameForResource(string resourceName
)
99 private void ProcessResourceWithSensibleExceptionWrapping(string resourceName
,
100 global::NVelocity
.Runtime
.Resource
.Resource resource
)
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
;