Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2005-2017 ManyDesigns srl. All rights reserved.
* http://www.manydesigns.com/
*
* This 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 3 of
* the License, or (at your option) any later version.
*
* This software 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.manydesigns.portofino.dispatcher;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.manydesigns.elements.ElementsThreadLocals;
import com.manydesigns.elements.options.DefaultSelectionProvider;
import com.manydesigns.elements.options.SelectionProvider;
import com.manydesigns.elements.util.ElementsFileUtils;
import com.manydesigns.portofino.actions.safemode.SafeModeAction;
import com.manydesigns.portofino.di.Injections;
import com.manydesigns.portofino.pageactions.PageActionLogic;
import com.manydesigns.portofino.pages.ChildPage;
import com.manydesigns.portofino.pages.Page;
import com.manydesigns.portofino.scripting.ScriptingUtil;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* A collection of methods that operate on {@link Dispatch} instances and related objects.
*
* @author Paolo Predonzani - [email protected]
* @author Angelo Lupo - [email protected]
* @author Giampiero Granatella - [email protected]
* @author Alessio Stalla - [email protected]
*/
public class DispatcherLogic {
public static final String copyright =
"Copyright (C) 2005-2017 ManyDesigns srl";
public static final Logger logger = LoggerFactory.getLogger(DispatcherLogic.class);
public static final String INVALID_PAGE_INSTANCE = "validDispatchPathLength";
public static SelectionProvider createPagesSelectionProvider
(File baseDir, File... excludes) {
return createPagesSelectionProvider(baseDir, false, false, excludes);
}
public static SelectionProvider createPagesSelectionProvider
(File baseDir, boolean includeRoot, boolean includeDetailChildren,
File... excludes) {
DefaultSelectionProvider selectionProvider = new DefaultSelectionProvider("pages");
if (includeRoot) {
Page rootPage;
try {
rootPage = getPage(baseDir);
} catch (Exception e) {
throw new RuntimeException("Couldn't load root page", e);
}
selectionProvider.appendRow("/", rootPage.getTitle() + " (top level)", true);
}
appendChildrenToPagesSelectionProvider
(baseDir, baseDir, null, selectionProvider, includeDetailChildren, excludes);
return selectionProvider;
}
protected static void appendChildrenToPagesSelectionProvider
(File baseDir, File parentDir, String breadcrumb,
DefaultSelectionProvider selectionProvider, boolean includeDetailChildren, File... excludes) {
FileFilter filter = new FileFilter() {
public boolean accept(File pathname) {
return pathname.isDirectory();
}
};
for (File dir : parentDir.listFiles(filter)) {
try {
appendToPagesSelectionProvider
(baseDir, dir, breadcrumb, selectionProvider, includeDetailChildren, excludes);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
private static void appendToPagesSelectionProvider
(File baseDir, File file, String breadcrumb,
DefaultSelectionProvider selectionProvider, boolean includeDetailChildren, File... excludes) {
if (ArrayUtils.contains(excludes, file)) {
return;
}
if (PageInstance.DETAIL.equals(file.getName())) {
if (includeDetailChildren) {
breadcrumb += " (detail)"; //TODO I18n
selectionProvider.appendRow
("/" + ElementsFileUtils.getRelativePath(baseDir, file), breadcrumb, true);
appendChildrenToPagesSelectionProvider
(baseDir, file, breadcrumb, selectionProvider, includeDetailChildren, excludes);
}
} else {
Page page;
try {
page = getPage(file);
} catch (Exception e) {
throw new RuntimeException("Couldn't load page", e);
}
if (breadcrumb == null) {
breadcrumb = page.getTitle();
} else {
breadcrumb = String.format("%s > %s", breadcrumb, page.getTitle());
}
selectionProvider.appendRow
("/" + ElementsFileUtils.getRelativePath(baseDir, file), breadcrumb, true);
appendChildrenToPagesSelectionProvider
(baseDir, file, breadcrumb, selectionProvider, includeDetailChildren, excludes);
}
}
protected static final JAXBContext pagesJaxbContext;
static {
try {
pagesJaxbContext = JAXBContext.newInstance(Page.class.getPackage().getName());
} catch (JAXBException e) {
throw new Error("Can't instantiate pages jaxb context", e);
}
}
/**
* Persists a page to the file system.
* @param pageInstance the live PageInstance containing the Page to save.
* @return the file where the page was saved.
* @throws Exception in case the save fails.
*/
public static File savePage(PageInstance pageInstance) throws Exception {
return savePage(pageInstance.getDirectory(), pageInstance.getPage());
}
/**
* Persists a page to the file system.
* @param directory the directory where to save the page.xml file.
* @param page the page to save.
* @return the file where the page was saved.
* @throws Exception in case the save fails.
*/
public static File savePage(File directory, Page page) throws Exception {
File pageFile = getPageFile(directory);
Marshaller marshaller = pagesJaxbContext.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(page, pageFile);
pageCache.invalidate(pageFile);
return pageFile;
}
//Cache configuration properties
public static final String PAGE_CACHE_SIZE = "page.cache.size";
public static final String PAGE_CACHE_CHECK_FREQUENCY = "page.cache.check.frequency";
public static final String CONFIGURATION_CACHE_SIZE = "configuration.cache.size";
public static final String CONFIGURATION_CACHE_CHECK_FREQUENCY = "configuration.cache.check.frequency";
public static void init(Configuration portofinoConfiguration) {
int maxSize, refreshCheckFrequency;
maxSize = portofinoConfiguration.getInt(PAGE_CACHE_SIZE, 1000);
refreshCheckFrequency =
portofinoConfiguration.getInt(PAGE_CACHE_CHECK_FREQUENCY, 5);
initPageCache(maxSize, refreshCheckFrequency);
maxSize = portofinoConfiguration.getInt(CONFIGURATION_CACHE_SIZE, 1000);
refreshCheckFrequency =
portofinoConfiguration.getInt(CONFIGURATION_CACHE_CHECK_FREQUENCY, 5);
initConfigurationCache(maxSize, refreshCheckFrequency);
}
protected static class FileCacheEntry {
public final T object;
public final long lastModified;
public final boolean error;
public FileCacheEntry(T object, long lastModified, boolean error) {
this.object = object;
this.lastModified = lastModified;
this.error = error;
}
}
protected static class ConfigurationCacheEntry extends FileCacheEntry