1 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 ; 40 import java.io.Serializable ; 41 import java.util.ArrayList ; 42 import java.util.Collections ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 46 55 public class CmsSynchronizeSettings implements Serializable { 56 57 58 private static final long serialVersionUID = 3713893787290111758L; 59 60 61 private String m_destinationPathInRfs; 62 63 64 private boolean m_enabled; 65 66 67 private List m_sourceListInVfs; 68 69 72 public CmsSynchronizeSettings() { 73 74 m_sourceListInVfs = new ArrayList (); 75 } 76 77 84 public void checkValues(CmsObject cms) throws CmsException { 85 86 if (isEnabled() && (m_destinationPathInRfs == null)) { 87 throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_NO_RFS_DESTINATION_0)); 89 } 90 if (isEnabled() && ((m_sourceListInVfs == null) || (m_sourceListInVfs.size() == 0))) { 91 throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_NO_VFS_SOURCE_0)); 93 } 94 Iterator i = m_sourceListInVfs.iterator(); 95 String currentSite = cms.getRequestContext().getSiteRoot(); 97 cms.getRequestContext().setSiteRoot(""); 99 try { 100 while (i.hasNext()) { 101 cms.readResource((String )i.next()); 103 } 104 } finally { 105 cms.getRequestContext().setSiteRoot(currentSite); 107 } 108 } 109 110 115 public String getDestinationPathInRfs() { 116 117 return m_destinationPathInRfs; 118 } 119 120 127 public List getSourceListInVfs() { 128 129 return m_sourceListInVfs; 130 } 131 132 137 public boolean isEnabled() { 138 139 return m_enabled; 140 } 141 142 150 public boolean isSyncEnabled() { 151 152 return isEnabled() && (m_sourceListInVfs != null) && (m_destinationPathInRfs != null); 153 } 154 155 160 public void setDestinationPathInRfs(String destinationPathInRfs) { 161 162 String destination; 163 if (CmsStringUtil.isEmptyOrWhitespaceOnly(destinationPathInRfs)) { 164 destination = null; 165 } else { 166 destination = destinationPathInRfs.trim(); 167 } 168 if (destination != null) { 169 File destinationFolder = new File (destination); 170 if (!destinationFolder.exists() || !destinationFolder.isDirectory()) { 171 throw new CmsIllegalArgumentException(Messages.get().container( 173 Messages.ERR_RFS_DESTINATION_NOT_THERE_1, 174 destination)); 175 } 176 if (!destinationFolder.canWrite()) { 177 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 destination = destination.substring(0, destination.length() - 1); 186 } 187 } 188 m_destinationPathInRfs = destination; 189 } 190 191 196 public void setEnabled(boolean enabled) { 197 198 m_enabled = enabled; 199 } 200 201 208 public void setSourceListInVfs(List sourceListInVfs) { 209 210 if (sourceListInVfs == null) { 211 m_sourceListInVfs = new ArrayList (); 212 } else { 213 m_sourceListInVfs = optimizeSourceList(sourceListInVfs); 214 } 215 } 216 217 220 public String toString() { 221 222 StringBuffer result = new StringBuffer (); 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 i = m_sourceListInVfs.iterator(); 234 while (i.hasNext()) { 235 String path = (String )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 253 protected List optimizeSourceList(List sourceListInVfs) { 254 255 List input = new ArrayList (sourceListInVfs); 257 Collections.sort(input); 258 259 List result = new ArrayList (); 260 Iterator i = input.iterator(); 261 while (i.hasNext()) { 262 String sourceInVfs = (String )i.next(); 264 if (CmsStringUtil.isEmpty(sourceInVfs)) { 265 continue; 267 } 268 boolean found = false; 269 for (int j = (result.size() - 1); j >= 0; j--) { 270 String check = (String )result.get(j); 272 if (sourceInVfs.startsWith(check)) { 273 found = true; 274 break; 275 } 276 } 277 if (!found) { 278 result.add(sourceInVfs); 280 } 281 } 282 283 return result; 284 } 285 } | Popular Tags |