001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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 GmbH & Co. KG, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.db; 029 030import org.opencms.main.CmsLog; 031 032import java.io.File; 033import java.io.FileOutputStream; 034import java.io.OutputStream; 035import java.util.HashMap; 036import java.util.Iterator; 037import java.util.Map; 038import java.util.Set; 039 040import org.apache.commons.logging.Log; 041 042/** 043 * Provides methods to write export points to the "real" file system.<p> 044 * 045 * @since 6.0.0 046 */ 047public class CmsExportPointDriver implements I_CmsExportPointDriver { 048 049 /** The log object for this class. */ 050 private static final Log LOG = CmsLog.getLog(CmsExportPointDriver.class); 051 052 /** The export points resolved to a lookup map. */ 053 protected Map<String, String> m_exportpointLookupMap; 054 055 /** The configured export points. */ 056 protected Set<CmsExportPoint> m_exportpoints; 057 058 /** 059 * Constructor for a CmsExportPointDriver.<p> 060 * 061 * @param exportpoints the list of export points 062 */ 063 public CmsExportPointDriver(Set<CmsExportPoint> exportpoints) { 064 065 m_exportpoints = exportpoints; 066 m_exportpointLookupMap = new HashMap<String, String>(); 067 Iterator<CmsExportPoint> i = m_exportpoints.iterator(); 068 while (i.hasNext()) { 069 CmsExportPoint point = i.next(); 070 if (point.getDestinationPath() != null) { 071 // otherwise this point is not valid, but must be kept for serializing the configuration 072 m_exportpointLookupMap.put(point.getUri(), point.getDestinationPath()); 073 } 074 } 075 } 076 077 /** 078 * Empty default constructor, necessary for subclasses which may need to do initialization differently.<p> 079 */ 080 protected CmsExportPointDriver() { 081 082 // do nothing; this constructor is only needed for subclasses 083 } 084 085 /** 086 * If required, creates the folder with the given root path in the real file system.<p> 087 * 088 * @param resourceName the root path of the folder to create 089 * @param exportpoint the export point to create the folder in 090 */ 091 public void createFolder(String resourceName, String exportpoint) { 092 093 writeResource(resourceName, exportpoint, null); 094 } 095 096 /** 097 * Deletes a file or a folder in the real file sytem.<p> 098 * 099 * If the given resource name points to a folder, then this folder is only deleted if it is empty. 100 * This is required since the same export point RFS target folder may be used by multiple export points. 101 * For example, this is usually the case with the <code>/WEB-INF/classes/</code> and 102 * <code>/WEB-INF/lib/</code> folders which are export point for multiple modules. 103 * If all resources in the RFS target folder where deleted, uninstalling one module would delete the 104 * export <code>classes</code> and <code>lib</code> resources of all other modules.<p> 105 * 106 * @param resourceName the root path of the resource to be deleted 107 * @param exportpoint the name of the export point 108 */ 109 public void deleteResource(String resourceName, String exportpoint) { 110 111 File file = getExportPointFile(resourceName, exportpoint); 112 if (file.exists() && file.canWrite()) { 113 // delete the file (or folder) 114 file.delete(); 115 // also delete empty parent directories 116 File parent = file.getParentFile(); 117 if (parent.canWrite()) { 118 parent.delete(); 119 } 120 } 121 } 122 123 /** 124 * Returns the export point path for the given resource root path, 125 * or <code>null</code> if the resource is not contained in 126 * any export point.<p> 127 * 128 * @param rootPath the root path of a resource in the OpenCms VFS 129 * @return the export point path for the given resource, or <code>null</code> if the resource is not contained in 130 * any export point 131 */ 132 public String getExportPoint(String rootPath) { 133 134 Iterator<String> i = getExportPointPaths().iterator(); 135 while (i.hasNext()) { 136 String point = i.next(); 137 if (rootPath.startsWith(point)) { 138 return point; 139 } 140 } 141 return null; 142 } 143 144 /** 145 * Returns the set of all VFS paths that are exported as an export point.<p> 146 * 147 * @return the set of all VFS paths that are exported as an export point 148 */ 149 public Set<String> getExportPointPaths() { 150 151 return m_exportpointLookupMap.keySet(); 152 } 153 154 /** 155 * Writes the file with the given root path to the real file system.<p> 156 * 157 * If required, missing parent folders in the real file system are automatically created.<p> 158 * 159 * @param resourceName the root path of the file to write 160 * @param exportpoint the export point to write file to 161 * @param content the contents of the file to write 162 */ 163 public void writeFile(String resourceName, String exportpoint, byte[] content) { 164 165 writeResource(resourceName, exportpoint, content); 166 } 167 168 /** 169 * Returns the File for the given export point resource.<p> 170 * 171 * @param rootPath name of a file in the VFS 172 * @param exportpoint the name of the export point 173 * @return the File for the given export point resource 174 */ 175 protected File getExportPointFile(String rootPath, String exportpoint) { 176 177 StringBuffer exportpath = new StringBuffer(128); 178 exportpath.append(m_exportpointLookupMap.get(exportpoint)); 179 exportpath.append(rootPath.substring(exportpoint.length())); 180 return new File(exportpath.toString()); 181 } 182 183 /** 184 * Writes file data to the RFS.<p> 185 * 186 * @param file the RFS file location 187 * @param content the file content 188 */ 189 protected void writeResource(File file, byte[] content) { 190 191 try { 192 File folder; 193 if (content == null) { 194 // a folder is to be created 195 folder = file; 196 } else { 197 // a file is to be written 198 folder = file.getParentFile(); 199 } 200 // make sure the parent folder exists 201 if (!folder.exists()) { 202 boolean success = folder.mkdirs(); 203 if (!success) { 204 LOG.error( 205 Messages.get().getBundle().key(Messages.LOG_CREATE_FOLDER_FAILED_1, folder.getAbsolutePath())); 206 } 207 } 208 if (content != null) { 209 // now write the file to the real file system 210 OutputStream s = new FileOutputStream(file); 211 s.write(content); 212 s.close(); 213 } 214 } catch (Exception e) { 215 LOG.error( 216 Messages.get().getBundle().key(Messages.LOG_WRITE_EXPORT_POINT_FAILED_1, file.getAbsolutePath()), 217 e); 218 } 219 } 220 221 /** 222 * Writes (if required creates) a resource with the given name to the real file system.<p> 223 * 224 * @param resourceName the root path of the resource to write 225 * @param exportpoint the name of the export point 226 * @param content the contents of the file to write 227 */ 228 protected void writeResource(String resourceName, String exportpoint, byte[] content) { 229 230 File file = getExportPointFile(resourceName, exportpoint); 231 writeResource(file, content); 232 } 233}