📄 portletpagefactory.java
字号:
package org.gridsphere.layout;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.gridsphere.portlet.impl.SportletProperties;import org.gridsphere.portlet.service.spi.PortletServiceFactory;import org.gridsphere.portletcontainer.GridSphereEvent;import org.gridsphere.portletcontainer.PortletSessionListener;import org.gridsphere.portletcontainer.impl.PortletSessionManager;import org.gridsphere.services.core.portal.PortalConfigService;import org.gridsphere.services.core.user.User;import javax.portlet.PortletRequest;import javax.portlet.PortletSession;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import java.io.*;import java.net.URL;import java.util.*;/** * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a> * @version $Id: PortletPageFactory.java 5032 2006-08-17 18:15:06Z novotny $ */public class PortletPageFactory implements PortletSessionListener { private Log log = LogFactory.getLog(PortletPageFactory.class); public static final String TCK_PAGE = "TCK"; //public static final String SETUP_PAGE = "SetupLayout"; public static final String ERROR_PAGE = "error"; public static final String GUEST_PAGE = "guest"; // TODO these need refactoring later on public static final String TEMPLATE_PAGE = "TemplateLayout"; public static final String USER_PAGE = "loggedin"; private static String USER_LAYOUT_DIR = null; public static final String DEFAULT_THEME = "default"; private static PortletPageFactory instance = null; private PortletSessionManager sessionManager = PortletSessionManager.getInstance(); private PortalConfigService portalConfigService = null; protected URL LAYOUT_MAPPING_PATH = getClass().getResource("/org/gridsphere/layout/layout-mapping.xml"); // Store user layouts in a hash private static Map<String, PortletPage> userLayouts = new HashMap<String, PortletPage>(); // a hash of hashes to contain all the users layouts private static Map<String, Map<String, PortletPage>> layouts = new HashMap<String, Map<String, PortletPage>>(); // a hash of loaded master layouts used to make copies private static Map<String, PortletPage> masterLayouts = new HashMap<String, PortletPage>(); private static Set<String> editableLayoutIds = new HashSet<String>(); private ServletContext context; private PortletPageFactory() { } public void init(ServletContext ctx) { this.context = ctx; USER_LAYOUT_DIR = ctx.getRealPath("/WEB-INF/CustomPortal/layouts/users"); String layoutsDirPath = ctx.getRealPath("/WEB-INF/CustomPortal/layouts"); File layoutsDir = new File(layoutsDirPath); File[] layoutFiles = layoutsDir.listFiles(); PortletPage page = null; for (int i = 0; i < layoutFiles.length; i++) { File layoutFile = layoutFiles[i]; String layoutFileName = layoutFile.getName(); if (layoutFileName.endsWith(".xml")) { String layoutId = layoutFileName.substring(0, layoutFileName.indexOf(".xml")); try { page = PortletLayoutDescriptor.loadPortletPage(layoutFile.getAbsolutePath(), LAYOUT_MAPPING_PATH); page.setLayoutDescriptor(layoutFile.getAbsolutePath()); if (page.getEditable()) editableLayoutIds.add(layoutId); masterLayouts.put(layoutId, page); } catch (Exception e) { log.error("Unable to load portlet page: " + layoutFileName, e); } } } String newuserLayoutPath = ctx.getRealPath("/WEB-INF/CustomPortal/layouts/users/"); File userdir = new File(newuserLayoutPath); if (!userdir.exists()) { userdir.mkdir(); } // test page creation times /* PortletPage copy = null; PortletPage guest = masterLayouts.get(GUEST_PAGE); long startTime = System.currentTimeMillis(); try { for (int i = 0; i < 1000; i++) { copy = (PortletPage) deepCopy2(guest); } long endTime = System.currentTimeMillis(); System.err.println("Serialize copy 1000 pages in = " + (endTime - startTime) + " (ms) "); for (int i = 0; i < 1000; i++) { copy = (PortletPage) deepCopy(guest); } startTime = System.currentTimeMillis(); System.err.println("Clone copy 1000 pages in = " + (startTime - endTime) + " (ms) "); } catch (Exception e) { e.printStackTrace(); } */ portalConfigService = (PortalConfigService) PortletServiceFactory.createPortletService(PortalConfigService.class, true); } public static synchronized PortletPageFactory getInstance() { if (instance == null) { instance = new PortletPageFactory(); } return instance; } public void login(HttpServletRequest request) { } public void logout(HttpSession session) { log.debug("in logout PortletPageFactory"); String sessionId = session.getId(); Map usersLayouts = (Map) layouts.get(sessionId); if (usersLayouts != null) { Iterator it = usersLayouts.keySet().iterator(); while (it.hasNext()) { String layoutId = (String) it.next(); log.debug("Removing " + layoutId + " container for:" + sessionId); it.remove(); } layouts.remove(sessionId); } if (userLayouts.containsKey(sessionId)) { log.debug("Removing user container for:" + sessionId); userLayouts.remove(sessionId); } } public Set<String> getEditableLayoutIds() { return editableLayoutIds; } public Set<String> getLayoutIds() { return masterLayouts.keySet(); } public PortletTabbedPane getUserTabbedPane(PortletRequest req) { String sessionId = req.getPortletSession(true).getId(); String userLayout = USER_LAYOUT_DIR + File.separator + req.getUserPrincipal().getName(); if (userLayouts.containsKey(sessionId)) { PortletPage page = (PortletPage) userLayouts.get(USER_PAGE); PortletTabbedPane pane = new PortletTabbedPane(); pane.setLayoutDescriptor(userLayout); PortletComponent comp = (PortletComponent) page.getPortletComponent(); PortletTabbedPane existPane = (PortletTabbedPane) comp; List<PortletTab> tabs = existPane.getPortletTabs(); for (PortletTab tab : tabs) { if (tab.getCanModify()) { pane.addTab(tab); } } return (!pane.getPortletTabs().isEmpty() ? pane : null); } File f = new File(userLayout); PortletTabbedPane pane = null; if (f.exists()) { try { pane = PortletLayoutDescriptor.loadPortletTabs(userLayout, LAYOUT_MAPPING_PATH); pane.setLayoutDescriptor(userLayout); log.debug("Adding user tab to layout"); } catch (Exception e) { log.error("Unable to make a clone of the templatePage", e); return null; } } else { return null; } // create tmp page PortletPage tmpPage = new PortletPage(); try { //tmpPage.setLayoutDescriptor(userLayout + ".tmp"); PortletTabbedPane tmpPane = (PortletTabbedPane) deepCopy(pane); tmpPage.setPortletComponent(tmpPane); this.setPageTheme(tmpPage, req); tmpPage.init(req, new ArrayList<ComponentIdentifier>()); tmpPane.save(); return tmpPane; } catch (Exception e) { log.error("Unable to save user pane!", e); } return null; } public void setPageTheme(PortletPage page, PortletRequest req) { String theme = null; User user = (User) req.getAttribute(SportletProperties.PORTLET_USER); if (user != null) theme = (String) user.getAttribute(User.THEME); if (theme == null) { theme = portalConfigService.getProperty(PortalConfigService.DEFAULT_THEME); } req.getPortletSession().setAttribute(SportletProperties.LAYOUT_THEME, theme, PortletSession.APPLICATION_SCOPE); } public PortletPage createPortletPageCopy(String layoutId) { // get the master copy of the page PortletPage masterPage = (PortletPage) masterLayouts.get(layoutId); PortletPage copy = null; // there are two cases where a master may not be there, TCK case and logged in user try { copy = (PortletPage) deepCopy(masterPage); } catch (Exception e) { log.error("Failed to make a copy of the master page: " + layoutId); return createErrorPage(); } return copy; } public void savePortletPageMaster(PortletPage page) { String layoutDesc = page.getLayoutDescriptor(); String layoutId = layoutDesc.substring(layoutDesc.lastIndexOf(File.separator) + 1, layoutDesc.lastIndexOf(".xml")); log.debug("saving layout: " + layoutId); try { PortletLayoutDescriptor.saveLayoutComponent(page, layoutDesc, LAYOUT_MAPPING_PATH); masterLayouts.put(layoutId, page); } catch (Exception e) { log.error("Unable to save layout descriptor: " + layoutDesc, e); } // remove any active layouts with this layoutId for (Map<String, PortletPage> map : layouts.values()) { Map userLayouts = (Map) map; userLayouts.remove(layoutId); log.debug("removing a layout: " + layoutId);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -