001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.util; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsVfsResourceAlreadyExistsException; 033import org.opencms.file.types.CmsResourceTypeFolder; 034import org.opencms.main.CmsException; 035import org.opencms.main.CmsLog; 036import org.opencms.main.OpenCms; 037 038import java.util.ArrayList; 039import java.util.List; 040 041import org.apache.commons.logging.Log; 042 043import com.google.common.collect.Lists; 044 045/** 046 * Provides Vfs utility functions.<p> 047 * 048 * @since 11.0.0 049 */ 050public final class CmsVfsUtil { 051 052 /** The log instance for this class. */ 053 private static final Log LOG = CmsLog.getLog(CmsVfsUtil.class); 054 055 /** 056 * Hides the public constructor.<p> 057 */ 058 private CmsVfsUtil() { 059 060 // empty 061 } 062 063 /** 064 * Creates a folder and its parent folders if they don't exist.<p> 065 * 066 * @param cms the CMS context to use 067 * @param rootPath the folder root path 068 * 069 * @throws CmsException if something goes wrong 070 */ 071 public static void createFolder(CmsObject cms, String rootPath) throws CmsException { 072 073 CmsObject rootCms = OpenCms.initCmsObject(cms); 074 rootCms.getRequestContext().setSiteRoot(""); 075 List<String> parents = new ArrayList<String>(); 076 String currentPath = rootPath; 077 while (currentPath != null) { 078 if (rootCms.existsResource(currentPath)) { 079 break; 080 } 081 parents.add(currentPath); 082 currentPath = CmsResource.getParentFolder(currentPath); 083 } 084 parents = Lists.reverse(parents); 085 for (String parent : parents) { 086 try { 087 rootCms.createResource( 088 parent, 089 OpenCms.getResourceManager().getResourceType(CmsResourceTypeFolder.getStaticTypeName())); 090 try { 091 rootCms.unlockResource(parent); 092 } catch (CmsException e) { 093 // may happen if parent folder is locked also 094 if (LOG.isInfoEnabled()) { 095 LOG.info(e); 096 } 097 } 098 } catch (CmsVfsResourceAlreadyExistsException e) { 099 // nop 100 } 101 } 102 } 103 104}