Search in sources :

Example 6 with FileSystemException

use of org.apache.jackrabbit.core.fs.FileSystemException in project jackrabbit by apache.

the class RepositoryConfig method internalCreateWorkspaceConfig.

/**
     * Creates a new workspace configuration with the specified name and the
     * specified workspace <code>template</.
     * <p>
     * This method creates a workspace configuration subdirectory,
     * copies the workspace configuration template into it, and finally
     * adds the created workspace configuration to the repository.
     * The initialized workspace configuration object is returned to
     * the caller.
     *
     * @param name workspace name
     * @param template the workspace template
     * @param configContent optional stringbuffer that will have the content
     *        of workspace configuration file written in
     * @return created workspace configuration
     * @throws ConfigurationException if creating the workspace configuration
     *                                failed
     */
private synchronized WorkspaceConfig internalCreateWorkspaceConfig(String name, Element template, StringBuffer configContent) throws ConfigurationException {
    // The physical workspace home directory on disk (TODO encode name?)
    File directory = new File(workspaceDirectory, name);
    // or cannot be created
    if (!directory.mkdir()) {
        if (directory.exists()) {
            throw new ConfigurationException("Workspace directory already exists: " + name);
        } else {
            throw new ConfigurationException("Failed to create workspace directory: " + name);
        }
    }
    FileSystem virtualFS;
    if (workspaceConfigDirectory != null) {
        // virtual repository file system
        try {
            virtualFS = fsf.getFileSystem();
        } catch (RepositoryException e) {
            throw new ConfigurationException("File system configuration error", e);
        }
    } else {
        // workspace configurations are maintained on disk
        virtualFS = null;
    }
    try {
        Writer configWriter;
        // get a writer for the workspace configuration file
        if (virtualFS != null) {
            // a configuration directoy had been specified; create workspace
            // configuration in virtual repository file system rather than
            // on disk
            String configDir = workspaceConfigDirectory + FileSystem.SEPARATOR + name;
            String configFile = configDir + FileSystem.SEPARATOR + WORKSPACE_XML;
            try {
                // Create the directory
                virtualFS.createFolder(configDir);
                configWriter = new OutputStreamWriter(virtualFS.getOutputStream(configFile));
            } catch (FileSystemException e) {
                throw new ConfigurationException("failed to create workspace configuration at path " + configFile, e);
            }
        } else {
            File file = new File(directory, WORKSPACE_XML);
            try {
                configWriter = new FileWriter(file);
            } catch (IOException e) {
                throw new ConfigurationException("failed to create workspace configuration at path " + file.getPath(), e);
            }
        }
        // the configuration writer.
        try {
            template.setAttribute("name", name);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            if (configContent == null) {
                transformer.transform(new DOMSource(template), new StreamResult(configWriter));
            } else {
                StringWriter writer = new StringWriter();
                transformer.transform(new DOMSource(template), new StreamResult(writer));
                String s = writer.getBuffer().toString();
                configWriter.write(s);
                configContent.append(s);
            }
        } catch (IOException e) {
            throw new ConfigurationException("Cannot create a workspace configuration file", e);
        } catch (TransformerConfigurationException e) {
            throw new ConfigurationException("Cannot create a workspace configuration writer", e);
        } catch (TransformerException e) {
            throw new ConfigurationException("Cannot create a workspace configuration file", e);
        } finally {
            IOUtils.closeQuietly(configWriter);
        }
        // Load the created workspace configuration.
        WorkspaceConfig wc;
        if (virtualFS != null) {
            String configDir = workspaceConfigDirectory + FileSystem.SEPARATOR + name;
            wc = loadWorkspaceConfig(virtualFS, configDir);
        } else {
            wc = loadWorkspaceConfig(directory);
        }
        if (wc != null) {
            addWorkspaceConfig(wc);
            return wc;
        } else {
            throw new ConfigurationException("Failed to load the created configuration for workspace " + name + ".");
        }
    } finally {
        try {
            if (virtualFS != null) {
                virtualFS.close();
            }
        } catch (FileSystemException ignore) {
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileWriter(java.io.FileWriter) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) StringWriter(java.io.StringWriter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileSystem(org.apache.jackrabbit.core.fs.FileSystem) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 7 with FileSystemException

use of org.apache.jackrabbit.core.fs.FileSystemException in project jackrabbit by apache.

the class RepositoryConfigurationParser method getFileSystemFactory.

/**
     * Creates and returns a factory object that creates {@link FileSystem}
     * instances based on the bean configuration at the named element.
     *
     * @param parent parent element
     * @param name name of the bean configuration element
     * @return file system factory
     * @throws ConfigurationException if the bean configuration is invalid
     */
protected FileSystemFactory getFileSystemFactory(Element parent, String name) throws ConfigurationException {
    final BeanConfig config = parseBeanConfig(parent, name);
    return new FileSystemFactory() {

        public FileSystem getFileSystem() throws RepositoryException {
            try {
                FileSystem fs = config.newInstance(FileSystem.class);
                fs.init();
                return fs;
            } catch (FileSystemException e) {
                throw new RepositoryException("File system initialization failure.", e);
            }
        }
    };
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) FileSystem(org.apache.jackrabbit.core.fs.FileSystem) RepositoryException(javax.jcr.RepositoryException) FileSystemFactory(org.apache.jackrabbit.core.fs.FileSystemFactory)

Example 8 with FileSystemException

use of org.apache.jackrabbit.core.fs.FileSystemException in project jackrabbit by apache.

the class MemoryFileSystem method getOutputStream.

public OutputStream getOutputStream(String filePath) throws FileSystemException {
    if (isFolder(filePath)) {
        throw new FileSystemException("path denotes folder: " + filePath);
    }
    String folderPath = filePath;
    if (filePath.lastIndexOf(FileSystem.SEPARATOR) > 0) {
        folderPath = filePath.substring(0, filePath.lastIndexOf("/"));
    } else {
        folderPath = "/";
    }
    assertIsFolder(folderPath);
    final MemoryFile file = new MemoryFile();
    entries.put(filePath, file);
    return new FilterOutputStream(new ByteArrayOutputStream()) {

        public void write(byte[] bytes, int off, int len) throws IOException {
            out.write(bytes, off, len);
        }

        public void close() throws IOException {
            out.close();
            file.setData(((ByteArrayOutputStream) out).toByteArray());
        }
    };
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilterOutputStream(java.io.FilterOutputStream)

Example 9 with FileSystemException

use of org.apache.jackrabbit.core.fs.FileSystemException in project jackrabbit by apache.

the class DatabaseFileSystem method createDeepFolder.

/**
     * Creates the specified files system folder entry, recursively creating
     * any non-existing intermediate folder entries.
     *
     * @param folderPath folder entry to create
     * @throws FileSystemException if an error occurs
     */
protected void createDeepFolder(String folderPath) throws FileSystemException {
    String parentDir = FileSystemPathUtil.getParentDir(folderPath);
    String name = FileSystemPathUtil.getName(folderPath);
    if (!FileSystemPathUtil.denotesRoot(folderPath)) {
        if (!exists(parentDir)) {
            createDeepFolder(parentDir);
        }
    }
    synchronized (insertFolderSQL) {
        try {
            conHelper.exec(insertFolderSQL, new Object[] { parentDir, name, new Long(System.currentTimeMillis()) });
        } catch (SQLException e) {
            String msg = "failed to create folder entry: " + folderPath;
            log.error(msg, e);
            throw new FileSystemException(msg, e);
        }
    }
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) SQLException(java.sql.SQLException)

Example 10 with FileSystemException

use of org.apache.jackrabbit.core.fs.FileSystemException in project jackrabbit by apache.

the class DatabaseFileSystem method deleteFile.

/**
     * {@inheritDoc}
     */
public void deleteFile(String filePath) throws FileSystemException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    FileSystemPathUtil.checkFormat(filePath);
    String parentDir = FileSystemPathUtil.getParentDir(filePath);
    String name = FileSystemPathUtil.getName(filePath);
    int count = 0;
    synchronized (deleteFileSQL) {
        try {
            count = conHelper.update(deleteFileSQL, new Object[] { parentDir, name });
        } catch (SQLException e) {
            String msg = "failed to delete file: " + filePath;
            log.error(msg, e);
            throw new FileSystemException(msg, e);
        }
    }
    if (count == 0) {
        throw new FileSystemException("no such file: " + filePath);
    }
}
Also used : FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) SQLException(java.sql.SQLException)

Aggregations

FileSystemException (org.apache.jackrabbit.core.fs.FileSystemException)54 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)19 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)18 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)16 IOException (java.io.IOException)15 SQLException (java.sql.SQLException)15 File (java.io.File)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)7 RepositoryException (javax.jcr.RepositoryException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 OutputStreamWriter (java.io.OutputStreamWriter)4 ArrayList (java.util.ArrayList)4 FileSystem (org.apache.jackrabbit.core.fs.FileSystem)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedWriter (java.io.BufferedWriter)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Properties (java.util.Properties)3 FileFilter (java.io.FileFilter)2 FileInputStream (java.io.FileInputStream)2