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) {
}
}
}
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);
}
}
};
}
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());
}
};
}
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);
}
}
}
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);
}
}
Aggregations