📄 cmsafterpublishstaticexporthandler.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/staticexport/CmsAfterPublishStaticExportHandler.java,v $
* Date : $Date: 2006/03/27 14:52:43 $
* Version: $Revision: 1.19 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.staticexport;
import org.opencms.db.CmsPublishedResource;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.loader.I_CmsResourceLoader;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.workplace.CmsWorkplace;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
/**
* Implementation for the <code>{@link I_CmsStaticExportHandler}</code> interface.<p>
*
* This handler exports all changes immediately after something is published.<p>
*
* @author Michael Moossen
*
* @version $Revision: 1.19 $
*
* @since 6.0.0
*
* @see I_CmsStaticExportHandler
*/
public class CmsAfterPublishStaticExportHandler extends A_CmsStaticExportHandler implements I_CmsStaticExportHandler {
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsAfterPublishStaticExportHandler.class);
/**
* Does the actual static export.<p>
*
* @param resources a list of CmsPublishedREsources to start the static export with
* @param report an <code>{@link I_CmsReport}</code> instance to print output message, or <code>null</code> to write messages to the log file
*
* @throws CmsException in case of errors accessing the VFS
* @throws IOException in case of erros writing to the export output stream
* @throws ServletException in case of errors accessing the servlet
*/
public void doExportAfterPublish(List resources, I_CmsReport report)
throws CmsException, IOException, ServletException {
boolean templatesFound;
// export must be done in the context of the export user
CmsObject cmsExportObject = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
List resourcesToExport = getRelatedResources(cmsExportObject, resources);
// first export all non-template resources
templatesFound = exportNonTemplateResources(cmsExportObject, resourcesToExport, report);
// export template resourses (check "plainoptimization" setting)
if ((templatesFound) || (!OpenCms.getStaticExportManager().getQuickPlainExport())) {
CmsStaticExportManager manager = OpenCms.getStaticExportManager();
// build resource filter set
Set resourceFilter = new HashSet();
Iterator itExpRes = resourcesToExport.iterator();
while (itExpRes.hasNext()) {
CmsPublishedResource pubResource = (CmsPublishedResource)itExpRes.next();
String rfsName = manager.getRfsName(cmsExportObject, pubResource.getRootPath());
resourceFilter.add(rfsName.substring(manager.getRfsPrefixForRfsName(rfsName).length()));
}
long timestamp = 0;
List publishedTemplateResources;
boolean newTemplateLinksFound;
int linkMode = CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER;
do {
// get all template resources which are potential candidates for a static export
publishedTemplateResources = cmsExportObject.readStaticExportResources(linkMode, timestamp);
newTemplateLinksFound = publishedTemplateResources.size() > 0;
if (newTemplateLinksFound) {
if (linkMode == CmsStaticExportManager.EXPORT_LINK_WITHOUT_PARAMETER) {
// first loop, switch mode to parameter links, leave the timestamp unchanged
linkMode = CmsStaticExportManager.EXPORT_LINK_WITH_PARAMETER;
// filter without parameter
publishedTemplateResources.retainAll(resourceFilter);
} else {
// second and subsequent loops, only look for links not already exported
// this can only be the case for a link with parameters
// that was present on a page also generated with parameters
timestamp = System.currentTimeMillis();
// filter with parameter
Iterator itPubTemplates = publishedTemplateResources.iterator();
while (itPubTemplates.hasNext()) {
String rfsName = (String)itPubTemplates.next();
if (!resourceFilter.contains(rfsName.substring(0, rfsName.lastIndexOf('_')))) {
itPubTemplates.remove();
}
}
}
// leave if no template left
if (publishedTemplateResources == null || publishedTemplateResources.isEmpty()) {
break;
}
// export
exportTemplateResources(cmsExportObject, publishedTemplateResources, report);
}
// if no new template links where found we are finished
} while (newTemplateLinksFound);
}
}
/**
* Gets all resources within the folder tree.<p>
* Since the long min and max value do not work with the sql timestamp function in the driver, we must calculate
* some different, but usable start and endtime values first.<p>
*
* @param cms the cms context
*
* @return all resources within the folder tree
*
* @throws CmsException if something goes wrong
*/
public List getAllResources(CmsObject cms) throws CmsException {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_GET_ALL_RESOURCES_0));
}
List resources = new ArrayList();
//starttime to 01.01.1970
long starttime = 0;
// endtime to now plus one week
long endtime = System.currentTimeMillis() + 604800000;
List vfsResources = cms.getResourcesInTimeRange("/", starttime, endtime);
// loop through the list and create the list of CmsPublishedResources
Iterator i = vfsResources.iterator();
while (i.hasNext()) {
CmsResource vfsResource = (CmsResource)i.next();
if ((vfsResource.getFlags() & CmsResource.FLAG_INTERNAL) == CmsResource.FLAG_INTERNAL) {
// skip internal files
continue;
}
CmsPublishedResource resource = new CmsPublishedResource(vfsResource);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_PROCESSING_1, resource.getRootPath()));
}
resources.add(resource);
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_NUM_RESOURCES_1, new Integer(resources.size())));
}
return resources;
}
/**
* @see org.opencms.staticexport.I_CmsStaticExportHandler#performEventPublishProject(org.opencms.util.CmsUUID, org.opencms.report.I_CmsReport)
*/
public void performEventPublishProject(CmsUUID publishHistoryId, I_CmsReport report) {
try {
m_busy = true;
exportAfterPublish(publishHistoryId, report);
} catch (Throwable t) {
LOG.error(Messages.get().getBundle().key(Messages.LOG_STATIC_EXPORT_ERROR_0), t);
} finally {
m_busy = false;
}
}
/**
* @see org.opencms.staticexport.A_CmsOnDemandStaticExportHandler#getRelatedFilesToPurge(java.lang.String, java.lang.String)
*/
protected List getRelatedFilesToPurge(String exportFileName, String vfsName) {
return Collections.EMPTY_LIST;
}
/**
* Starts the static export on publish.<p>
*
* Exports all modified resources after a publish process into the real FS.<p>
*
* @param publishHistoryId the publichHistoryId of the published project
* @param report an <code>{@link I_CmsReport}</code> instance to print output message, or <code>null</code> to write messages to the log file
*
* @throws CmsException in case of errors accessing the VFS
* @throws IOException in case of erros writing to the export output stream
* @throws ServletException in case of errors accessing the servlet
*/
private void exportAfterPublish(CmsUUID publishHistoryId, I_CmsReport report)
throws CmsException, IOException, ServletException {
// first check if the test resource was published already
// if not, we must do a complete export of all static resources
String rfsName = CmsFileUtil.normalizePath(OpenCms.getStaticExportManager().getExportPath(
OpenCms.getStaticExportManager().getTestResource())
+ OpenCms.getStaticExportManager().getTestResource());
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_CHECKING_TEST_RESOURCE_1, rfsName));
}
File file = new File(rfsName);
if (!file.exists()) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_TEST_RESOURCE_NOT_EXISTANT_0));
}
// the file is not there, so export everything
OpenCms.getStaticExportManager().exportFullStaticRender(true, report);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_TEST_RESOURCE_EXISTS_0));
}
// delete all resources deleted during the publish process
scrubExportFolders(publishHistoryId);
// get the list of published resources from the publish history table
CmsObject cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
List publishedResources = cms.readPublishedResources(publishHistoryId);
// do the export
doExportAfterPublish(publishedResources, report);
}
}
/**
* Exports all non template resources found in a list of published resources.<p>
*
* @param cms the current cms object
* @param publishedResources the list of published resources
* @param report an I_CmsReport instance to print output message, or null to write messages to the log file
*
* @return true if some template resources were found whil looping the list of published resources
*
* @throws CmsException in case of errors accessing the VFS
* @throws IOException in case of erros writing to the export output stream
* @throws ServletException in case of errors accessing the servlet
*/
private boolean exportNonTemplateResources(CmsObject cms, List publishedResources, I_CmsReport report)
throws CmsException, IOException, ServletException {
CmsStaticExportManager manager = OpenCms.getStaticExportManager();
String vfsName = null;
List resourcesToExport = new ArrayList();
boolean templatesFound = false;
int count = 1;
report.println(
Messages.get().container(Messages.RPT_STATICEXPORT_NONTEMPLATE_RESOURCES_BEGIN_0),
I_CmsReport.FORMAT_HEADLINE);
// loop through all resources
Iterator i = publishedResources.iterator();
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_EXPORTING_NON_TEMPLATE_1,
new Integer(publishedResources.size())));
}
while (i.hasNext()) {
CmsPublishedResource pupRes = (CmsPublishedResource)i.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -