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 using IRuntimeServices
= NVelocity
.Runtime
.IRuntimeServices
;
16 using NVelocity
.Runtime
.Resource
;
17 using ResourceNotFoundException
= NVelocity
.Exception
.ResourceNotFoundException
;
19 namespace Castle
.MonoRail
.Framework
.Views
.NVelocity
23 using Castle
.MonoRail
.Framework
.Internal
;
26 /// Manages resource (views) loading and processing, calls and is also
27 /// called by NVelocity to allow recursive expansion of templates
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
);
56 if (resourceType
== ResourceType
.Content
)
58 resource
= new ContentResource();
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
;
80 if (!resource
.IsSourceModified())
82 cacheProvider
.Store(resourceName
, resource
);
87 if (resource
.IsSourceModified())
89 ProcessResourceWithSensibleExceptionWrapping(resourceName
, resource
);
96 public string GetLoaderNameForResource(string resourceName
)
101 private void ProcessResourceWithSensibleExceptionWrapping(string resourceName
,
102 global::NVelocity
.Runtime
.Resource
.Resource resource
)
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
;