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 GmbH & Co. KG, 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.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        // do nothing; this constructor is only needed for subclasses
082    }
083
084    /**
085     * If required, creates the folder with the given root path in the real file system.<p>
086     *
087     * @param resourceName the root path of the folder to create
088     * @param exportpoint the export point to create the folder in
089     */
090    public void createFolder(String resourceName, String exportpoint) {
091
092        writeResource(resourceName, exportpoint, null);
093    }
094
095    /**
096     * Deletes a file or a folder in the real file sytem.<p>
097     *
098     * If the given resource name points to a folder, then this folder is only deleted if it is empty.
099     * This is required since the same export point RFS target folder may be used by multiple export points.
100     * For example, this is usually the case with the <code>/WEB-INF/classes/</code> and
101     * <code>/WEB-INF/lib/</code> folders which are export point for multiple modules.
102     * If all resources in the RFS target folder where deleted, uninstalling one module would delete the
103     * export <code>classes</code> and <code>lib</code> resources of all other modules.<p>
104     *
105     * @param resourceName the root path of the resource to be deleted
106     * @param exportpoint the name of the export point
107     */
108    public void deleteResource(String resourceName, String exportpoint) {
109
110        File file = getExportPointFile(resourceName, exportpoint);
111        if (file.exists() && file.canWrite()) {
112            // delete the file (or folder)
113            file.delete();
114            // also delete empty parent directories
115            File parent = file.getParentFile();
116            if (parent.canWrite()) {
117                parent.delete();
118            }
119        }
120    }
121
122    /**
123     * Returns the export point path for the given resource root path,
124     * or <code>null</code> if the resource is not contained in
125     * any export point.<p>
126     *
127     * @param rootPath the root path of a resource in the OpenCms VFS
128     * @return the export point path for the given resource, or <code>null</code> if the resource is not contained in
129     *      any export point
130     */
131    public String getExportPoint(String rootPath) {
132
133        Iterator<String> i = getExportPointPaths().iterator();
134        while (i.hasNext()) {
135            String point = i.next();
136            if (rootPath.startsWith(point)) {
137                return point;
138            }
139        }
140        return null;
141    }
142
143    /**
144     * Returns the set of all VFS paths that are exported as an export point.<p>
145     *
146     * @return the set of all VFS paths that are exported as an export point
147     */
148    public Set<String> getExportPointPaths() {
149
150        return m_exportpointLookupMap.keySet();
151    }
152
153    /**
154     * Writes the file with the given root path to the real file system.<p>
155     *
156     * If required, missing parent folders in the real file system are automatically created.<p>
157     *
158     * @param resourceName the root path of the file to write
159     * @param exportpoint the export point to write file to
160     * @param content the contents of the file to write
161     */
162    public void writeFile(String resourceName, String exportpoint, byte[] content) {
163
164        writeResource(resourceName, exportpoint, content);
165    }
166
167    /**
168     * Returns the File for the given export point resource.<p>
169     *
170     * @param rootPath name of a file in the VFS
171     * @param exportpoint the name of the export point
172     * @return the File for the given export point resource
173     */
174    protected File getExportPointFile(String rootPath, String exportpoint) {
175
176        StringBuffer exportpath = new StringBuffer(128);
177        exportpath.append(m_exportpointLookupMap.get(exportpoint));
178        exportpath.append(rootPath.substring(exportpoint.length()));
179        return new File(exportpath.toString());
180    }
181
182    /**
183     * Writes file data to the RFS.<p>
184     *
185     * @param file the RFS file location
186     * @param content the file content
187     */
188    protected void writeResource(File file, byte[] content) {
189
190        try {
191            File folder;
192            if (content == null) {
193                // a folder is to be created
194                folder = file;
195            } else {
196                // a file is to be written
197                folder = file.getParentFile();
198            }
199            // make sure the parent folder exists
200            if (!folder.exists()) {
201                boolean success = folder.mkdirs();
202                if (!success) {
203                    LOG.error(
204                        Messages.get().getBundle().key(Messages.LOG_CREATE_FOLDER_FAILED_1, folder.getAbsolutePath()));
205                }
206            }
207            if (content != null) {
208                // now write the file to the real file system
209                OutputStream s = new FileOutputStream(file);
210                s.write(content);
211                s.close();
212            }
213        } catch (Exception e) {
214            LOG.error(
215                Messages.get().getBundle().key(Messages.LOG_WRITE_EXPORT_POINT_FAILED_1, file.getAbsolutePath()),
216                e);
217        }
218    }
219
220    /**
221     * Writes (if required creates) a resource with the given name to the real file system.<p>
222     *
223     * @param resourceName the root path of the resource to write
224     * @param exportpoint the name of the export point
225     * @param content the contents of the file to write
226     */
227    protected void writeResource(String resourceName, String exportpoint, byte[] content) {
228
229        File file = getExportPointFile(resourceName, exportpoint);
230        writeResource(file, content);
231    }
232}