KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > synchronize > CmsSynchronizeSettings


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/synchronize/CmsSynchronizeSettings.java,v $
3  * Date : $Date: 2005/06/23 11:11:23 $
4  * Version: $Revision: 1.13 $
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.synchronize;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.CmsException;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.util.CmsStringUtil;
38
39 import java.io.File JavaDoc;
40 import java.io.Serializable JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45
46 /**
47  * Contains the settings for the synchronization.<p>
48  *
49  * @author Alexander Kandzior
50  *
51  * @version $Revision: 1.13 $
52  *
53  * @since 6.0.0
54  */

55 public class CmsSynchronizeSettings implements Serializable JavaDoc {
56
57     /** use well defined serialVersionUID to avoid issues with serialization. */
58     private static final long serialVersionUID = 3713893787290111758L;
59
60     /** The destination path of the synchronization in the "real" file system. */
61     private String JavaDoc m_destinationPathInRfs;
62
63     /** Indicates if the synchronization is enabled or not. */
64     private boolean m_enabled;
65
66     /** The source path list of the synchronization in the OpenCms VFS. */
67     private List JavaDoc m_sourceListInVfs;
68
69     /**
70      * Empty constructor, called from the configuration.<p>
71      */

72     public CmsSynchronizeSettings() {
73
74         m_sourceListInVfs = new ArrayList JavaDoc();
75     }
76
77     /**
78      * Performs a check if the values that have been set are valid.<p>
79      *
80      * @param cms the current users OpenCms context
81      *
82      * @throws CmsException in case the values are not valid
83      */

84     public void checkValues(CmsObject cms) throws CmsException {
85
86         if (isEnabled() && (m_destinationPathInRfs == null)) {
87             // if enabled, it's required to have RFS destination folder available
88
throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_NO_RFS_DESTINATION_0));
89         }
90         if (isEnabled() && ((m_sourceListInVfs == null) || (m_sourceListInVfs.size() == 0))) {
91             // if enabled, it's required to have at last one source folder
92
throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_NO_VFS_SOURCE_0));
93         }
94         Iterator JavaDoc i = m_sourceListInVfs.iterator();
95         // store the current site root
96
String JavaDoc currentSite = cms.getRequestContext().getSiteRoot();
97         // switch to root site
98
cms.getRequestContext().setSiteRoot("");
99         try {
100             while (i.hasNext()) {
101                 // try to read all given resources, this will cause an error if the resource does not exist
102
cms.readResource((String JavaDoc)i.next());
103             }
104         } finally {
105             // reset to current site root
106
cms.getRequestContext().setSiteRoot(currentSite);
107         }
108     }
109
110     /**
111      * Returns the destination path of the synchronization in the "real" file system.<p>
112      *
113      * @return the destination path of the synchronization in the "real" file system
114      */

115     public String JavaDoc getDestinationPathInRfs() {
116
117         return m_destinationPathInRfs;
118     }
119
120     /**
121      * Returns the source path list of the synchronization in the OpenCms VFS.<p>
122      *
123      * The objects in the list are of type <code>{@link String}</code>.
124      *
125      * @return the source path list of the synchronization in the OpenCms VFS
126      */

127     public List JavaDoc getSourceListInVfs() {
128
129         return m_sourceListInVfs;
130     }
131
132     /**
133      * Returns the enabled flag which indicates if this synchronize settings are enabled or not.<p>
134      *
135      * @return the enabled flag
136      */

137     public boolean isEnabled() {
138
139         return m_enabled;
140     }
141
142     /**
143      * Returns <code>true</code> if the synchonization is enabled.<p>
144      *
145      * The synchonization is enabled if both source and destination
146      * path is set, and also the enabled flag is true.<p>
147      *
148      * @return <code>true</code> if the synchonization is enabled
149      */

150     public boolean isSyncEnabled() {
151
152         return isEnabled() && (m_sourceListInVfs != null) && (m_destinationPathInRfs != null);
153     }
154
155     /**
156      * Sets the destination path of the synchronization in the "real" file system.<p>
157      *
158      * @param destinationPathInRfs the destination path of the synchronization in the "real" file system to set
159      */

160     public void setDestinationPathInRfs(String JavaDoc destinationPathInRfs) {
161
162         String JavaDoc destination;
163         if (CmsStringUtil.isEmptyOrWhitespaceOnly(destinationPathInRfs)) {
164             destination = null;
165         } else {
166             destination = destinationPathInRfs.trim();
167         }
168         if (destination != null) {
169             File JavaDoc destinationFolder = new File JavaDoc(destination);
170             if (!destinationFolder.exists() || !destinationFolder.isDirectory()) {
171                 // destination folder does not exist
172
throw new CmsIllegalArgumentException(Messages.get().container(
173                     Messages.ERR_RFS_DESTINATION_NOT_THERE_1,
174                     destination));
175             }
176             if (!destinationFolder.canWrite()) {
177                 // destination folder can't be written to
178
throw new CmsIllegalArgumentException(Messages.get().container(
179                     Messages.ERR_RFS_DESTINATION_NO_WRITE_1,
180                     destination));
181             }
182             destination = destinationFolder.getAbsolutePath();
183             if (destination.endsWith(File.separator)) {
184                 // ensure that the destination folder DOES NOT end with a file separator
185
destination = destination.substring(0, destination.length() - 1);
186             }
187         }
188         m_destinationPathInRfs = destination;
189     }
190
191     /**
192      * Sets the enabled flag which indicates if this synchronize settings are enabled or not.<p>
193      *
194      * @param enabled the enabled flag to set
195      */

196     public void setEnabled(boolean enabled) {
197
198         m_enabled = enabled;
199     }
200
201     /**
202      * Sets the source path list of the synchronization in the OpenCms VFS.<p>
203      *
204      * The objects in the list must be of type <code>{@link String}</code>.
205      *
206      * @param sourceListInVfs the source path list of the synchronization in the OpenCms VFS to set
207      */

208     public void setSourceListInVfs(List JavaDoc sourceListInVfs) {
209
210         if (sourceListInVfs == null) {
211             m_sourceListInVfs = new ArrayList JavaDoc();
212         } else {
213             m_sourceListInVfs = optimizeSourceList(sourceListInVfs);
214         }
215     }
216
217     /**
218      * @see java.lang.Object#toString()
219      */

220     public String JavaDoc toString() {
221
222         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
223         result.append("[");
224         result.append(this.getClass().getName());
225         result.append(", enabled: ");
226         result.append(m_enabled);
227         result.append(", RFS destination path: ");
228         result.append(m_destinationPathInRfs);
229         result.append(", VFS source path list: ");
230         if (m_sourceListInVfs == null) {
231             result.append(m_sourceListInVfs);
232         } else {
233             Iterator JavaDoc i = m_sourceListInVfs.iterator();
234             while (i.hasNext()) {
235                 String JavaDoc path = (String JavaDoc)i.next();
236                 result.append(path);
237                 if (i.hasNext()) {
238                     result.append(", ");
239                 }
240             }
241         }
242         result.append("]");
243         return result.toString();
244     }
245
246     /**
247      * Optimizes the list of VFS source files by removing all resources that
248      * have a parent resource already included in the list.<p>
249      *
250      * @param sourceListInVfs the list of VFS resources to optimize
251      * @return the optimized result list
252      */

253     protected List JavaDoc optimizeSourceList(List JavaDoc sourceListInVfs) {
254
255         // input should be sorted but may be immutable
256
List JavaDoc input = new ArrayList JavaDoc(sourceListInVfs);
257         Collections.sort(input);
258
259         List JavaDoc result = new ArrayList JavaDoc();
260         Iterator JavaDoc i = input.iterator();
261         while (i.hasNext()) {
262             // check all sources in the list
263
String JavaDoc sourceInVfs = (String JavaDoc)i.next();
264             if (CmsStringUtil.isEmpty(sourceInVfs)) {
265                 // skip empty strings
266
continue;
267             }
268             boolean found = false;
269             for (int j = (result.size() - 1); j >= 0; j--) {
270                 // check if this source is indirectly contained because a parent folder is contained
271
String JavaDoc check = (String JavaDoc)result.get(j);
272                 if (sourceInVfs.startsWith(check)) {
273                     found = true;
274                     break;
275                 }
276             }
277             if (!found) {
278                 // the source is not already contained in the result
279
result.add(sourceInVfs);
280             }
281         }
282
283         return result;
284     }
285 }
Popular Tags