001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.cocoon; 017 018import java.io.IOException; 019import java.io.Serializable; 020import java.net.MalformedURLException; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.cocoon.ProcessingException; 028import org.apache.cocoon.ResourceNotFoundException; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Source; 031import org.apache.cocoon.environment.SourceResolver; 032import org.apache.cocoon.reading.ResourceReader; 033import org.xml.sax.SAXException; 034 035/** 036 * Resource reader but where the source resolver is the runtime one. 037 */ 038@SuppressWarnings("deprecation") 039public class RuntimeResourceReader extends ResourceReader implements Serviceable 040{ 041 /** last modified parameter name for resources parameters */ 042 public static final String LAST_MODIFIED = "lastModified"; 043 044 /** the avalon service manager */ 045 protected ServiceManager _manager; 046 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 _manager = manager; 050 } 051 052 @Override 053 public void setup(SourceResolver initialResolver, Map cocoonObjectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException 054 { 055 org.apache.excalibur.source.SourceResolver runtimeResolver; 056 try 057 { 058 runtimeResolver = (org.apache.excalibur.source.SourceResolver) _manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); 059 } 060 catch (ServiceException e) 061 { 062 String errorMessage = "The runtime resource reader cannot be setup : the runtime source resolver cannot be retrieved"; 063 getLogger().error(errorMessage); 064 throw new ProcessingException(errorMessage, e); 065 } 066 067 super.setup(new SourceResolverWrapper(runtimeResolver), cocoonObjectModel, src, par); 068 069 quickTest = true; 070 071 // Minimizer does not receive the real lastModified throught sitemap source 072 @SuppressWarnings("unchecked") 073 Map<String, Object> params = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 074 075 if (params != null) 076 { 077 params.put(LAST_MODIFIED, getLastModified()); 078 } 079 } 080 081 @Override 082 public Serializable getKey() 083 { 084 return inputSource.getURI(); 085 } 086 087 @Override 088 public void generate() throws IOException, ProcessingException 089 { 090 if (!inputSource.exists()) 091 { 092 throw new ResourceNotFoundException("Resource not found for URI : " + inputSource.getURI()); 093 } 094 095 super.generate(); 096 } 097 098 class SourceResolverWrapper implements org.apache.cocoon.environment.SourceResolver 099 { 100 org.apache.excalibur.source.SourceResolver _res; 101 102 /** 103 * Create a wrapper 104 * @param res The resolver to wrap 105 */ 106 public SourceResolverWrapper(org.apache.excalibur.source.SourceResolver res) 107 { 108 _res = res; 109 } 110 111 public Source resolve(String systemID) throws ProcessingException, SAXException, IOException 112 { 113 throw new ProcessingException("resolve not handled"); 114 } 115 116 public void release(org.apache.excalibur.source.Source eSource) 117 { 118 _res.release(eSource); 119 } 120 121 public org.apache.excalibur.source.Source resolveURI(String location) throws MalformedURLException, IOException 122 { 123 return _res.resolveURI(location); 124 } 125 126 public org.apache.excalibur.source.Source resolveURI(String location, String base, Map sParameters) throws MalformedURLException, IOException 127 { 128 return _res.resolveURI(location, base, sParameters); 129 } 130 } 131}