KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > CmsExportPointDriver


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsExportPointDriver.java,v $
3  * Date : $Date: 2006/11/30 13:47:52 $
4  * Version: $Revision: 1.21 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db;
33
34 import org.opencms.main.CmsLog;
35
36 import java.io.File JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Set JavaDoc;
42
43 import org.apache.commons.logging.Log;
44
45 /**
46  * Provides methods to write export points to the "real" file system.<p>
47  *
48  * @author Alexander Kandzior
49  *
50  * @version $Revision: 1.21 $
51  *
52  * @since 6.0.0
53  */

54 public class CmsExportPointDriver {
55
56     /** The log object for this class. */
57     private static final Log LOG = CmsLog.getLog(CmsExportPointDriver.class);
58
59     /** The export points resolved to a lookup map. */
60     private HashMap JavaDoc m_exportpointLookupMap;
61
62     /** The configured export points. */
63     private Set JavaDoc m_exportpoints;
64
65     /**
66      * Constructor for a CmsExportPointDriver.<p>
67      *
68      * @param exportpoints the list of export points
69      */

70     public CmsExportPointDriver(Set JavaDoc exportpoints) {
71
72         m_exportpoints = exportpoints;
73         m_exportpointLookupMap = new HashMap JavaDoc();
74         Iterator JavaDoc i = m_exportpoints.iterator();
75         while (i.hasNext()) {
76             CmsExportPoint point = (CmsExportPoint)i.next();
77             if (point.getDestinationPath() != null) {
78                 // otherwise this point is not valid, but must be kept for serializing the configuration
79
m_exportpointLookupMap.put(point.getUri(), point.getDestinationPath());
80             }
81         }
82     }
83
84     /**
85      * If required, creates the folder with the given root path in the real file system.<p>
86      *
87      * @param resourceName the root path of the folder to create
88      * @param exportpoint the export point to create the folder in
89      */

90     public void createFolder(String JavaDoc resourceName, String JavaDoc exportpoint) {
91
92         writeResource(resourceName, exportpoint, null);
93     }
94
95     /**
96      * Deletes a file or a folder in the real file sytem.<p>
97      *
98      * If the given resource name points to a folder, then this folder is only deleted if it is empty.
99      * 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 JavaDoc resourceName, String JavaDoc exportpoint) {
109
110         File JavaDoc 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 JavaDoc 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 JavaDoc getExportPoint(String JavaDoc rootPath) {
132
133         Iterator JavaDoc i = getExportPointPaths().iterator();
134         while (i.hasNext()) {
135             String JavaDoc point = (String JavaDoc)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 JavaDoc 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 JavaDoc resourceName, String JavaDoc 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     private File JavaDoc getExportPointFile(String JavaDoc rootPath, String JavaDoc exportpoint) {
175
176         StringBuffer JavaDoc exportpath = new StringBuffer JavaDoc(128);
177         exportpath.append((String JavaDoc)m_exportpointLookupMap.get(exportpoint));
178         exportpath.append(rootPath.substring(exportpoint.length()));
179         return new File JavaDoc(exportpath.toString());
180     }
181
182     /**
183      * Writes (if required creates) a resource with the given name to the real file system.<p>
184      *
185      * @param resourceName the root path of the resource to write
186      * @param exportpoint the name of the export point
187      * @param content the contents of the file to write
188      */

189     private void writeResource(String JavaDoc resourceName, String JavaDoc exportpoint, byte[] content) {
190
191         File JavaDoc file = getExportPointFile(resourceName, exportpoint);
192         try {
193             File JavaDoc folder;
194             if (content == null) {
195                 // a folder is to be created
196
folder = file;
197             } else {
198                 // a file is to be written
199
folder = file.getParentFile();
200             }
201             // make sure the parent folder exists
202
if (!folder.exists()) {
203                 boolean success = folder.mkdirs();
204                 if (!success) {
205                     LOG.error(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 JavaDoc s = new FileOutputStream JavaDoc(file);
211                 s.write(content);
212                 s.close();
213             }
214         } catch (Exception JavaDoc e) {
215             LOG.error(
216                 Messages.get().getBundle().key(Messages.LOG_WRITE_EXPORT_POINT_FAILED_1, file.getAbsolutePath()),
217                 e);
218         }
219     }
220 }
Popular Tags