001/*
002 *  Copyright 2019 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.odf.schedulable;
017
018import java.io.File;
019import java.io.FileOutputStream;
020import java.io.OutputStream;
021
022import org.apache.avalon.framework.activity.Initializable;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.components.source.impl.SitemapSource;
026import org.apache.commons.io.FileUtils;
027import org.apache.excalibur.source.SourceResolver;
028import org.apache.excalibur.source.SourceUtil;
029import org.quartz.JobExecutionContext;
030
031import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
032import org.ametys.runtime.util.AmetysHomeHelper;
033
034/**
035 * Scheduler to check the global validation status on all programs.
036 */
037public class GlobalValidationSchedulable extends AbstractStaticSchedulable implements Initializable
038{
039    private SourceResolver _sourceResolver;
040    private File _outputFolder;
041
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _sourceResolver = (SourceResolver) smanager.lookup(SourceResolver.ROLE);
047    }
048    
049    public void initialize() throws Exception
050    {
051        _outputFolder = new File(AmetysHomeHelper.getAmetysHomeData(), "/odf/report");
052        FileUtils.forceMkdir(_outputFolder);
053    }
054
055    @Override
056    public void execute(JobExecutionContext context) throws Exception
057    {
058        SitemapSource source = null;
059        File reportTmpFile = null;
060        
061        try
062        {
063            // Resolve the report pipeline.
064            String url = "cocoon://_plugins/odf/global-validation/report.xml";
065            source = (SitemapSource) _sourceResolver.resolveURI(url);
066            
067            // Save the report into a temporary file.
068            reportTmpFile = new File(_outputFolder, "global-validation.tmp.xml");
069            OutputStream reportTmpOs = new FileOutputStream(reportTmpFile);
070            
071            SourceUtil.copy(source.getInputStream(), reportTmpOs);
072            
073            // If all went well until now, copy the temporary file to the real report file.
074            File reportFile = new File(_outputFolder, "global-validation.xml");
075            FileUtils.copyFile(reportTmpFile, reportFile);
076        }
077        finally
078        {
079            // Delete the temporary file.
080            if (reportTmpFile != null)
081            {
082                reportTmpFile.delete();
083            }
084
085            if (source != null)
086            {
087                _sourceResolver.release(source);
088            }
089        }
090    }
091}