001/*
002 *  Copyright 2010 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 */
016
017package org.ametys.site;
018
019import java.io.File;
020import java.io.IOException;
021import java.net.InetAddress;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.logger.AbstractLogEnabled;
027import org.apache.cocoon.Constants;
028import org.apache.commons.io.FileUtils;
029import org.apache.commons.lang.StringUtils;
030
031import org.ametys.core.util.mail.SendMailHelper;
032import org.ametys.runtime.config.Config;
033
034import jakarta.mail.MessagingException;
035
036/**
037 * Init class for FO application.
038 */
039public class Init extends AbstractLogEnabled implements org.ametys.runtime.plugin.Init, Contextualizable
040{
041    
042    /** The avalon context. */
043    protected Context _context;
044    
045    /** The cocoon environment context. */
046    protected org.apache.cocoon.environment.Context _environmentContext;
047    
048    @Override
049    public void contextualize(Context context) throws ContextException
050    {
051        _context = context;
052        _environmentContext = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
053    }
054    
055    @Override
056    public void init() throws Exception
057    {
058        File root = SiteCacheHelper.getRootCache();
059        
060        try
061        {
062            FileUtils.forceMkdir(root);
063            FileUtils.cleanDirectory(root);
064        }
065        catch (IOException e)
066        {
067            getLogger().error("Error creating or cleaning the static cache root directory.", e);
068            
069            // Notify the administrator that the server cache isn't up-to-date.
070            sendErrorMail();
071        }
072    }
073    
074    /**
075     * Send an error e-mail to the administrator to notify that the server cache can't be written.
076     */
077    protected void sendErrorMail()
078    {
079        String sysadminMail = Config.getInstance().getValue("smtp.mail.sysadminto");
080        
081        try
082        {
083            if (StringUtils.isNotBlank(sysadminMail))
084            {
085                String hostName = InetAddress.getLocalHost().getHostName();
086                String basePath = _environmentContext.getRealPath("/");
087                
088                String subject = "An error occurred on a website application installed on server '" + hostName + "'";
089                String body = "You're receiving this notification because the website application on the server '" + hostName + "' failed to start successfully.\r\n"
090                            + "This is probably a rights issue on the static cache directory which may prevent the pages from displaying correctly.\r\n\r\n"
091                            + "The application is located at path '" + basePath + "'. Please check the application logs for further information.";
092                
093                SendMailHelper.newMail()
094                              .withSubject(subject)
095                              .withTextBody(body)
096                              .withRecipient(sysadminMail)
097                              .sendMail();
098            }
099        }
100        catch (MessagingException | IOException e)
101        {
102            getLogger().warn("Error sending an error mail to the administrator.", e);
103        }
104    }
105}