1 31 32 package org.opencms.importexport; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsResource; 36 import org.opencms.file.CmsResourceFilter; 37 import org.opencms.i18n.CmsMessageContainer; 38 import org.opencms.main.CmsEvent; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.main.I_CmsEventListener; 42 import org.opencms.main.OpenCms; 43 import org.opencms.report.I_CmsReport; 44 import org.opencms.security.CmsRole; 45 import org.opencms.security.CmsRoleViolationException; 46 import org.opencms.util.CmsFileUtil; 47 import org.opencms.xml.CmsXmlException; 48 import org.opencms.xml.CmsXmlUtils; 49 50 import java.io.File ; 51 import java.io.FileNotFoundException ; 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.util.ArrayList ; 55 import java.util.Collections ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.zip.ZipEntry ; 59 import java.util.zip.ZipFile ; 60 61 import org.apache.commons.logging.Log; 62 63 import org.dom4j.Document; 64 import org.dom4j.Element; 65 66 79 public class CmsImport { 80 81 82 private static final Log LOG = CmsLog.getLog(CmsImport.class); 83 84 85 protected CmsObject m_cms; 86 87 88 protected Document m_docXml; 89 90 91 protected String m_importFile; 92 93 94 protected List m_importImplementations; 95 96 97 protected boolean m_importingChannelData; 98 99 100 protected String m_importPath; 101 102 103 protected File m_importResource; 104 105 114 protected int m_importVersion; 115 116 117 protected ZipFile m_importZip; 118 119 120 protected I_CmsReport m_report; 121 122 125 public CmsImport() { 126 127 super(); 129 } 130 131 142 public CmsImport(CmsObject cms, String importFile, String importPath, I_CmsReport report) 143 throws CmsRoleViolationException { 144 145 cms.checkRole(CmsRole.IMPORT_DATABASE); 147 148 m_cms = cms; 150 m_importFile = importFile; 151 m_importPath = importPath; 152 m_report = report; 153 m_importingChannelData = false; 154 m_importImplementations = OpenCms.getImportExportManager().getImportVersionClasses(); 155 } 156 157 164 public static String getChildElementTextValue(Element parentElement, String elementName) { 165 166 try { 167 Element childElement = (Element)parentElement.selectNodes("./" + elementName).get(0); 169 return childElement.getTextTrim(); 171 } catch (Exception e) { 172 return null; 173 } 174 } 175 176 183 public List getConflictingFilenames() throws CmsXmlException, CmsImportExportException { 184 185 List fileNodes; 186 Element currentElement; 187 String source, destination; 188 List conflictNames = new ArrayList (); 189 191 if (m_docXml == null) { 192 openImportFile(); 193 } 194 195 fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE); 197 198 for (int i = 0; i < fileNodes.size(); i++) { 200 currentElement = (Element)fileNodes.get(i); 201 source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE); 202 destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION); 203 if (source != null) { 204 boolean exists = true; 206 try { 207 CmsResource res = m_cms.readResource(m_importPath + destination); 208 if (res.getState() == CmsResource.STATE_DELETED) { 209 exists = false; 210 } 211 } catch (CmsException e) { 212 exists = false; 213 } 214 if (exists) { 215 conflictNames.add(m_importPath + destination); 216 } 217 } 218 } 219 220 if (m_importZip != null) { 221 try { 222 m_importZip.close(); 223 } catch (IOException e) { 224 225 CmsMessageContainer message = Messages.get().container( 226 Messages.ERR_IMPORTEXPORT_ERROR_CLOSING_ZIP_ARCHIVE_1, 227 m_importZip.getName()); 228 if (LOG.isDebugEnabled()) { 229 LOG.debug(message.key(), e); 230 } 231 232 throw new CmsImportExportException(message, e); 233 } 234 } 235 return conflictNames; 236 } 237 238 246 public List getResourcesForProject() throws CmsImportExportException { 247 248 List fileNodes; 249 Element currentElement; 250 String destination; 251 List resources = new ArrayList (); 252 253 try { 254 if (m_importingChannelData) { 255 m_cms.getRequestContext().saveSiteRoot(); 256 m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 257 } 258 259 fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE); 261 262 for (int i = 0; i < fileNodes.size(); i++) { 264 currentElement = (Element)fileNodes.get(i); 265 destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION); 266 267 try { 269 String resource = destination.substring(0, destination.indexOf("/", 1) + 1); 270 resource = m_importPath + resource; 271 if ((!resources.contains(resource)) && (!resource.equals(m_importPath))) { 273 try { 274 m_cms.readFolder(resource, CmsResourceFilter.IGNORE_EXPIRATION); 275 resources.add(resource); 277 } catch (CmsException exc) { 278 resources.add("/"); 280 } 281 } 282 } catch (StringIndexOutOfBoundsException exc) { 283 } 285 } 286 } finally { 287 if (m_importingChannelData) { 288 m_cms.getRequestContext().restoreSiteRoot(); 289 } 290 } 291 292 closeImportFile(); 293 294 if (resources.contains("/")) { 295 resources.clear(); 297 resources.add("/"); 298 } 299 300 return resources; 301 } 302 303 310 public void importResources() throws CmsImportExportException, CmsXmlException { 311 312 boolean run = false; 314 openImportFile(); 315 m_report.println( 316 Messages.get().container(Messages.RPT_IMPORT_VERSION_1, String.valueOf(m_importVersion)), 317 I_CmsReport.FORMAT_NOTE); 318 try { 319 Iterator i = m_importImplementations.iterator(); 321 while (i.hasNext()) { 322 I_CmsImport imp = (I_CmsImport)i.next(); 323 if (imp.getVersion() == m_importVersion) { 324 imp.importResources( 326 m_cms, 327 m_importPath, 328 m_report, 329 m_importResource, 330 m_importZip, 331 m_docXml); 332 run = true; 333 break; 334 } 335 } 336 if (!run) { 337 m_report.println( 338 Messages.get().container(Messages.RPT_IMPORT_DB_NO_CLASS_0), 339 I_CmsReport.FORMAT_WARNING); 340 } 341 } finally { 342 closeImportFile(); 344 OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES, Collections.EMPTY_MAP)); 345 } 346 } 347 348 353 protected void closeImportFile() throws CmsImportExportException { 354 355 if (m_importZip != null) { 356 try { 357 m_importZip.close(); 358 } catch (IOException e) { 359 m_report.println(e); 360 361 CmsMessageContainer message = Messages.get().container( 362 Messages.ERR_IMPORTEXPORT_ERROR_CLOSING_ZIP_ARCHIVE_1, 363 m_importZip.getName()); 364 if (LOG.isDebugEnabled()) { 365 LOG.debug(message.key(), e); 366 } 367 368 throw new CmsImportExportException(message, e); 369 } 370 } 371 } 372 373 379 protected byte[] getFileBytes(String filename) { 380 381 try { 382 if (m_importZip != null) { 384 ZipEntry entry = m_importZip.getEntry(filename); 386 InputStream stream = m_importZip.getInputStream(entry); 387 int size = new Long (entry.getSize()).intValue(); 388 return CmsFileUtil.readFully(stream, size); 389 } else { 390 File file = new File (m_importResource, filename); 392 return CmsFileUtil.readFile(file); 393 } 394 } catch (FileNotFoundException fnfe) { 395 m_report.println(fnfe); 396 } catch (IOException ioe) { 397 m_report.println(ioe); 398 } 399 return "".getBytes(); 401 } 402 403 408 protected void getImportResource() throws CmsImportExportException { 409 410 try { 411 m_importResource = new File (OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(m_importFile)); 413 414 if (m_importResource.isFile()) { 416 m_importZip = new ZipFile (m_importResource); 417 } 418 } catch (IOException e) { 419 m_report.println(e); 420 421 CmsMessageContainer message = Messages.get().container( 422 Messages.ERR_IMPORTEXPORT_ERROR_OPENING_ZIP_ARCHIVE_1, 423 m_importFile); 424 if (LOG.isDebugEnabled()) { 425 LOG.debug(message.key(), e); 426 } 427 428 throw new CmsImportExportException(message, e); 429 } 430 } 431 432 438 protected void openImportFile() throws CmsXmlException, CmsImportExportException { 439 440 getImportResource(); 442 443 m_docXml = CmsXmlUtils.unmarshalHelper(getFileBytes(CmsImportExportManager.EXPORT_MANIFEST), null); 445 446 try { 448 m_importVersion = Integer.parseInt(((Element)m_docXml.selectNodes("//" + CmsImportExportManager.N_VERSION).get( 449 0)).getTextTrim()); 450 } catch (Exception e) { 451 } 453 } 454 } | Popular Tags |