throw new ConfigurationException(
"Failed to create workspace directory: " + name);
}
}
FileSystem virtualFS;
if (workspaceConfigDirectory != null) {
// a configuration directoy had been specified;
// workspace configurations are maintained in
// 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);
}
}
// Create the workspace.xml file using the configuration template and
// 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) {
}
}
}