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