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 javax.mail.MessagingException;
024
025import org.apache.avalon.framework.context.Context;
026import org.apache.avalon.framework.context.ContextException;
027import org.apache.avalon.framework.context.Contextualizable;
028import org.apache.avalon.framework.logger.AbstractLogEnabled;
029import org.apache.cocoon.Constants;
030import org.apache.commons.io.FileUtils;
031import org.apache.commons.lang.StringUtils;
032
033import org.ametys.core.util.mail.SendMailHelper;
034import org.ametys.runtime.config.Config;
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 from = Config.getInstance().getValue("smtp.mail.from");
080        String sysadminMail = Config.getInstance().getValue("smtp.mail.sysadminto");
081        
082        try
083        {
084            if (StringUtils.isNotBlank(sysadminMail))
085            {
086                String hostName = InetAddress.getLocalHost().getHostName();
087                String basePath = _environmentContext.getRealPath("/");
088                
089                String subject = "An error occurred on a website application installed on server '" + hostName + "'";
090                String body = "You're receiving this notification because the website application on the server '" + hostName + "' failed to start successfully.\r\n"
091                            + "This is probably a rights issue on the static cache directory which may prevent the pages from displaying correctly.\r\n\r\n"
092                            + "The application is located at path '" + basePath + "'. Please check the application logs for further information.";
093                
094                SendMailHelper.sendMail(subject, null, body, sysadminMail, from);
095            }
096        }
097        catch (MessagingException e)
098        {
099            getLogger().warn("Error sending an error mail to the administrator.", e);
100        }
101        catch (IOException e)
102        {
103            getLogger().warn("Error sending an error mail to the administrator.", e);
104        }
105    }
106}