001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/synchronize/CmsSynchronizeSettings.java,v $
003: * Date : $Date: 2008-02-27 12:05:52 $
004: * Version: $Revision: 1.15 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.synchronize;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.main.CmsException;
036: import org.opencms.main.CmsIllegalArgumentException;
037: import org.opencms.util.CmsStringUtil;
038:
039: import java.io.File;
040: import java.io.Serializable;
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: /**
047: * Contains the settings for the synchronization.<p>
048: *
049: * @author Alexander Kandzior
050: *
051: * @version $Revision: 1.15 $
052: *
053: * @since 6.0.0
054: */
055: public class CmsSynchronizeSettings implements Serializable {
056:
057: /** use well defined serialVersionUID to avoid issues with serialization. */
058: private static final long serialVersionUID = 3713893787290111758L;
059:
060: /** The destination path of the synchronization in the "real" file system. */
061: private String m_destinationPathInRfs;
062:
063: /** Indicates if the synchronization is enabled or not. */
064: private boolean m_enabled;
065:
066: /** The source path list of the synchronization in the OpenCms VFS. */
067: private List m_sourceListInVfs;
068:
069: /**
070: * Empty constructor, called from the configuration.<p>
071: */
072: public CmsSynchronizeSettings() {
073:
074: m_sourceListInVfs = new ArrayList();
075: }
076:
077: /**
078: * Performs a check if the values that have been set are valid.<p>
079: *
080: * @param cms the current users OpenCms context
081: *
082: * @throws CmsException in case the values are not valid
083: */
084: public void checkValues(CmsObject cms) throws CmsException {
085:
086: if (isEnabled() && (m_destinationPathInRfs == null)) {
087: // if enabled, it's required to have RFS destination folder available
088: throw new CmsSynchronizeException(Messages.get().container(
089: Messages.ERR_NO_RFS_DESTINATION_0));
090: }
091: if (isEnabled()
092: && ((m_sourceListInVfs == null) || (m_sourceListInVfs
093: .size() == 0))) {
094: // if enabled, it's required to have at last one source folder
095: throw new CmsSynchronizeException(Messages.get().container(
096: Messages.ERR_NO_VFS_SOURCE_0));
097: }
098: Iterator i = m_sourceListInVfs.iterator();
099: // store the current site root
100: String currentSite = cms.getRequestContext().getSiteRoot();
101: // switch to root site
102: cms.getRequestContext().setSiteRoot("");
103: try {
104: while (i.hasNext()) {
105: // try to read all given resources, this will cause an error if the resource does not exist
106: cms.readResource((String) i.next());
107: }
108: } finally {
109: // reset to current site root
110: cms.getRequestContext().setSiteRoot(currentSite);
111: }
112: }
113:
114: /**
115: * Returns the destination path of the synchronization in the "real" file system.<p>
116: *
117: * @return the destination path of the synchronization in the "real" file system
118: */
119: public String getDestinationPathInRfs() {
120:
121: return m_destinationPathInRfs;
122: }
123:
124: /**
125: * Returns the source path list of the synchronization in the OpenCms VFS.<p>
126: *
127: * The objects in the list are of type <code>{@link String}</code>.
128: *
129: * @return the source path list of the synchronization in the OpenCms VFS
130: */
131: public List getSourceListInVfs() {
132:
133: return m_sourceListInVfs;
134: }
135:
136: /**
137: * Returns the enabled flag which indicates if this synchronize settings are enabled or not.<p>
138: *
139: * @return the enabled flag
140: */
141: public boolean isEnabled() {
142:
143: return m_enabled;
144: }
145:
146: /**
147: * Returns <code>true</code> if the synchonization is enabled.<p>
148: *
149: * The synchonization is enabled if both source and destination
150: * path is set, and also the enabled flag is true.<p>
151: *
152: * @return <code>true</code> if the synchonization is enabled
153: */
154: public boolean isSyncEnabled() {
155:
156: return isEnabled() && (m_sourceListInVfs != null)
157: && (m_destinationPathInRfs != null);
158: }
159:
160: /**
161: * Sets the destination path of the synchronization in the "real" file system.<p>
162: *
163: * @param destinationPathInRfs the destination path of the synchronization in the "real" file system to set
164: */
165: public void setDestinationPathInRfs(String destinationPathInRfs) {
166:
167: String destination;
168: if (CmsStringUtil.isEmptyOrWhitespaceOnly(destinationPathInRfs)) {
169: destination = null;
170: } else {
171: destination = destinationPathInRfs.trim();
172: }
173: if (destination != null) {
174: File destinationFolder = new File(destination);
175: if (!destinationFolder.exists()
176: || !destinationFolder.isDirectory()) {
177: // destination folder does not exist
178: throw new CmsIllegalArgumentException(
179: Messages
180: .get()
181: .container(
182: Messages.ERR_RFS_DESTINATION_NOT_THERE_1,
183: destination));
184: }
185: if (!destinationFolder.canWrite()) {
186: // destination folder can't be written to
187: throw new CmsIllegalArgumentException(
188: Messages
189: .get()
190: .container(
191: Messages.ERR_RFS_DESTINATION_NO_WRITE_1,
192: destination));
193: }
194: destination = destinationFolder.getAbsolutePath();
195: if (destination.endsWith(File.separator)) {
196: // ensure that the destination folder DOES NOT end with a file separator
197: destination = destination.substring(0, destination
198: .length() - 1);
199: }
200: }
201: m_destinationPathInRfs = destination;
202: }
203:
204: /**
205: * Sets the enabled flag which indicates if this synchronize settings are enabled or not.<p>
206: *
207: * @param enabled the enabled flag to set
208: */
209: public void setEnabled(boolean enabled) {
210:
211: m_enabled = enabled;
212: }
213:
214: /**
215: * Sets the source path list of the synchronization in the OpenCms VFS.<p>
216: *
217: * The objects in the list must be of type <code>{@link String}</code>.
218: *
219: * @param sourceListInVfs the source path list of the synchronization in the OpenCms VFS to set
220: */
221: public void setSourceListInVfs(List sourceListInVfs) {
222:
223: if (sourceListInVfs == null) {
224: m_sourceListInVfs = new ArrayList();
225: } else {
226: m_sourceListInVfs = optimizeSourceList(sourceListInVfs);
227: }
228: }
229:
230: /**
231: * @see java.lang.Object#toString()
232: */
233: public String toString() {
234:
235: StringBuffer result = new StringBuffer();
236: result.append("[");
237: result.append(this .getClass().getName());
238: result.append(", enabled: ");
239: result.append(m_enabled);
240: result.append(", RFS destination path: ");
241: result.append(m_destinationPathInRfs);
242: result.append(", VFS source path list: ");
243: if (m_sourceListInVfs == null) {
244: result.append(m_sourceListInVfs);
245: } else {
246: Iterator i = m_sourceListInVfs.iterator();
247: while (i.hasNext()) {
248: String path = (String) i.next();
249: result.append(path);
250: if (i.hasNext()) {
251: result.append(", ");
252: }
253: }
254: }
255: result.append("]");
256: return result.toString();
257: }
258:
259: /**
260: * Optimizes the list of VFS source files by removing all resources that
261: * have a parent resource already included in the list.<p>
262: *
263: * @param sourceListInVfs the list of VFS resources to optimize
264: * @return the optimized result list
265: */
266: protected List optimizeSourceList(List sourceListInVfs) {
267:
268: // input should be sorted but may be immutable
269: List input = new ArrayList(sourceListInVfs);
270: Collections.sort(input);
271:
272: List result = new ArrayList();
273: Iterator i = input.iterator();
274: while (i.hasNext()) {
275: // check all sources in the list
276: String sourceInVfs = (String) i.next();
277: if (CmsStringUtil.isEmpty(sourceInVfs)) {
278: // skip empty strings
279: continue;
280: }
281: boolean found = false;
282: for (int j = (result.size() - 1); j >= 0; j--) {
283: // check if this source is indirectly contained because a parent folder is contained
284: String check = (String) result.get(j);
285: if (sourceInVfs.startsWith(check)) {
286: found = true;
287: break;
288: }
289: }
290: if (!found) {
291: // the source is not already contained in the result
292: result.add(sourceInVfs);
293: }
294: }
295:
296: return result;
297: }
298: }
|