001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ugc; 029 030import org.opencms.file.CmsFile; 031import org.opencms.file.CmsObject; 032import org.opencms.main.CmsException; 033import org.opencms.main.CmsLog; 034import org.opencms.ugc.shared.CmsUgcConstants; 035import org.opencms.ugc.shared.CmsUgcException; 036import org.opencms.util.CmsUUID; 037 038import java.util.concurrent.ConcurrentHashMap; 039 040import javax.servlet.http.HttpServletRequest; 041import javax.servlet.http.HttpSession; 042 043import org.apache.commons.logging.Log; 044 045/** 046 * Factory to create the form editing sessions.<p> 047 */ 048public class CmsUgcSessionFactory { 049 050 /** The log instance for this class. */ 051 private static final Log LOG = CmsLog.getLog(CmsUgcSessionFactory.class); 052 053 /** The factory instance. */ 054 private static CmsUgcSessionFactory INSTANCE; 055 056 /** Admin CmsObject instance. */ 057 private static CmsObject m_adminCms; 058 059 /** The session queues. */ 060 private ConcurrentHashMap<CmsUUID, CmsUgcSessionQueue> m_queues = new ConcurrentHashMap<CmsUUID, CmsUgcSessionQueue>(); 061 062 /** 063 * Constructor.<p> 064 */ 065 private CmsUgcSessionFactory() { 066 067 } 068 069 /** 070 * Returns the factory instance.<p> 071 * 072 * @return the factory instance 073 */ 074 public static synchronized CmsUgcSessionFactory getInstance() { 075 076 if (INSTANCE == null) { 077 INSTANCE = new CmsUgcSessionFactory(); 078 } 079 return INSTANCE; 080 } 081 082 /** 083 * Sets the admin CmsObject instance. 084 * 085 * @param adminCms the admin CmsObject 086 */ 087 public static void setAdminCms(CmsObject adminCms) { 088 089 m_adminCms = adminCms; 090 } 091 092 /** 093 * Creates a new editing session.<p> 094 * 095 * @param cms the cms context 096 * @param request the request 097 * @param config the configuration 098 * 099 * @return the form session 100 * 101 * @throws CmsUgcException if creating the session fails 102 */ 103 public CmsUgcSession createSession(CmsObject cms, HttpServletRequest request, CmsUgcConfiguration config) 104 throws CmsUgcException { 105 106 CmsUgcSession session = createSession(cms, config); 107 HttpSession httpSession = request.getSession(true); 108 httpSession.setAttribute("" + session.getId(), session); 109 return session; 110 } 111 112 /** 113 * Creates a new editing session.<p> 114 * 115 * @param cms the cms context 116 * @param request the request 117 * @param sitePath the configuration site path 118 * 119 * @return the form session 120 * 121 * @throws CmsUgcException if creating the session fails 122 */ 123 public CmsUgcSession createSession(CmsObject cms, HttpServletRequest request, String sitePath) 124 throws CmsUgcException { 125 126 CmsUgcConfigurationReader reader = new CmsUgcConfigurationReader(cms); 127 CmsUgcConfiguration config = null; 128 try { 129 CmsFile configFile = cms.readFile(sitePath); 130 config = reader.readConfiguration(configFile); 131 } catch (Exception e) { 132 LOG.error(e.getLocalizedMessage(), e); 133 throw new CmsUgcException(e, CmsUgcConstants.ErrorCode.errConfiguration, e.getLocalizedMessage()); 134 } 135 return createSession(cms, request, config); 136 } 137 138 /** 139 * Creates a new session for a given file.<p> 140 * 141 * @param cms the CMS context to use 142 * @param request the current request 143 * @param configPath the path of the form configuration 144 * @param fileName the file name (*not* path) of the XML content for which the session should be initialized 145 * 146 * @return the newly created session 147 * @throws CmsUgcException if something goes wrong 148 */ 149 public CmsUgcSession createSessionForFile( 150 CmsObject cms, 151 HttpServletRequest request, 152 String configPath, 153 String fileName) 154 throws CmsUgcException { 155 156 CmsUgcSession session = createSession(cms, request, configPath); 157 session.loadXmlContent(fileName); 158 159 // when we open a session for existing files, we do not want them to get automatically deleted 160 session.disableCleanup(); 161 return session; 162 } 163 164 /** 165 * Returns the session, if already initialized.<p> 166 * 167 * @param request the request 168 * @param sessionId the form session id 169 * 170 * @return the session 171 */ 172 public CmsUgcSession getSession(HttpServletRequest request, CmsUUID sessionId) { 173 174 return (CmsUgcSession)request.getSession(true).getAttribute("" + sessionId); 175 } 176 177 /** 178 * Creates a new editing session.<p> 179 * 180 * @param cms the cms context 181 * @param config the configuration 182 * 183 * @return the form session 184 * 185 * @throws CmsUgcException if the session creation fails 186 */ 187 private CmsUgcSession createSession(CmsObject cms, CmsUgcConfiguration config) throws CmsUgcException { 188 189 if (getQueue(config).waitForSlot()) { 190 try { 191 return new CmsUgcSession(m_adminCms, cms, config); 192 } catch (CmsException e) { 193 LOG.error(e.getLocalizedMessage(), e); 194 throw new CmsUgcException(e); 195 } 196 } else { 197 String message = Messages.get().container(Messages.ERR_WAIT_QUEUE_EXCEEDED_0).key( 198 cms.getRequestContext().getLocale()); 199 throw new CmsUgcException(CmsUgcConstants.ErrorCode.errMaxQueueLengthExceeded, message); 200 } 201 } 202 203 /** 204 * Returns the session queue.<p> 205 * 206 * @param config the form configuration 207 * 208 * @return the queue 209 */ 210 private CmsUgcSessionQueue getQueue(CmsUgcConfiguration config) { 211 212 CmsUgcSessionQueue queue = m_queues.get(config.getId()); 213 if (queue == null) { 214 queue = CmsUgcSessionQueue.createQueue(config); 215 m_queues.put(config.getId(), queue); 216 } else { 217 queue.updateFromConfiguration(config); 218 } 219 return queue; 220 } 221}