repositoryconfig.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 746 行 · 第 1/2 页

JAVA
746
字号
     * <p>     * This method returns <code>null</code>, if the given directory does     * not exist or does not contain a workspace configuration file. If an     * invalid configuration file is found, then a     * {@link ConfigurationException ConfigurationException} is thrown.     *     * @param fs virtual file system where to look for the configuration file     * @param configDir workspace configuration directory in virtual file system     * @return workspace configuration     * @throws ConfigurationException if the workspace configuration is invalid     */    private WorkspaceConfig loadWorkspaceConfig(FileSystem fs, String configDir)            throws ConfigurationException {        Reader configReader = null;        try {            String configPath = configDir + FileSystem.SEPARATOR + WORKSPACE_XML;            if (!fs.exists(configPath)) {                // no configuration file in this directory                return null;            }            configReader = new InputStreamReader(fs.getInputStream(configPath));            InputSource xml = new InputSource(configReader);            xml.setSystemId(configPath);            // the physical workspace home directory (TODO encode name?)            File homeDir = new File(                    workspaceDirectory, FileSystemPathUtil.getName(configDir));            if (!homeDir.exists()) {                homeDir.mkdir();            }            Properties variables = new Properties();            variables.setProperty(                    RepositoryConfigurationParser.WORKSPACE_HOME_VARIABLE,                    homeDir.getPath());            RepositoryConfigurationParser localParser =                parser.createSubParser(variables);            return localParser.parseWorkspaceConfig(xml);        } catch (FileSystemException e) {            throw new ConfigurationException("Failed to load workspace configuration", e);        } finally {            if (configReader != null) {                try {                    configReader.close();                } catch (IOException ignore) {                }            }        }    }    /**     * Adds the given workspace configuration to the repository.     *     * @param wc workspace configuration     * @throws ConfigurationException if a workspace with the same name     *                                already exists     */    private void addWorkspaceConfig(WorkspaceConfig wc)            throws ConfigurationException {        String name = wc.getName();        if (!workspaces.containsKey(name)) {            workspaces.put(name, wc);        } else {            throw new ConfigurationException(                    "Duplicate workspace configuration: " + name);        }    }    /**     * 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     * @return created workspace configuration     * @throws ConfigurationException if creating the workspace configuration     *                                failed     */    private synchronized WorkspaceConfig internalCreateWorkspaceConfig(String name,                                                                       Element template)            throws ConfigurationException {        // The physical workspace home directory on disk (TODO encode name?)        File directory = new File(workspaceDirectory, name);        // Create the physical workspace directory, fail if it exists        // 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) {            // a configuration directoy had been specified;            // workspace configurations are maintained in            // virtual repository file system            virtualFS = fsc.createFileSystem();        } 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");                transformer.transform(                        new DOMSource(template), new StreamResult(configWriter));            } 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 {                try {                    configWriter.close();                } catch (IOException ignore) {                }            }            // 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) {            }        }    }    /**     * Creates a new workspace configuration with the specified name.     * 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     * @return created workspace configuration     * @throws ConfigurationException if creating the workspace configuration     *                                failed     */    public WorkspaceConfig createWorkspaceConfig(String name)            throws ConfigurationException {        // use workspace template from repository.xml        return internalCreateWorkspaceConfig(name, template);    }    /**     * Creates a new workspace configuration with the specified name. This     * method uses the provided workspace <code>template</code> to create the     * repository config instead of the template that is present in the     * repository configuration.     * <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     * @return created workspace configuration     * @throws ConfigurationException if creating the workspace configuration     *                                failed     */    public WorkspaceConfig createWorkspaceConfig(String name,                                                 InputSource template)            throws ConfigurationException {        ConfigurationParser parser = new ConfigurationParser(new Properties());        Element workspaceTemplate = parser.parseXML(template);        return internalCreateWorkspaceConfig(name, workspaceTemplate);    }    /**     * Returns the repository home directory.     *     * @return repository home directory     */    public String getHomeDir() {        return home;    }    /**     * Returns the repository file system configuration.     *     * @return file system configuration     */    public FileSystemConfig getFileSystemConfig() {        return fsc;    }    /**     * Returns the repository name. The repository name can be used for     * JAAS app-entry configuration.     *     * @return repository name     */    public String getAppName() {        return sec.getAppName();    }    /**     * Returns the repository access manager configuration.     *     * @return access manager configuration     */    public AccessManagerConfig getAccessManagerConfig() {        return sec.getAccessManagerConfig();    }    /**     * Returns the repository login module configuration.     *     * @return login module configuration, or <code>null</code> if standard     *         JAAS mechanism should be used.     */    public LoginModuleConfig getLoginModuleConfig() {        return sec.getLoginModuleConfig();    }    /**     * Returns the repository security configuration.     *     * @return security configutation     */    public SecurityConfig getSecurityConfig() {        return sec;    }    /**     * Returns the workspace root directory.     *     * @return workspace root directory     */    public String getWorkspacesConfigRootDir() {        return workspaceDirectory;    }    /**     * Returns the name of the default workspace.     *     * @return name of the default workspace     */    public String getDefaultWorkspaceName() {        return defaultWorkspace;    }    /**     * Returns the amount of time in seconds after which an idle workspace is     * automatically shutdown. If zero then idle workspaces will never be     * automatically shutdown.     *     * @return maximum workspace idle time in seconds     */    public int getWorkspaceMaxIdleTime() {        return workspaceMaxIdleTime;    }    /**     * Returns all workspace configurations.     *     * @return workspace configurations     */    public Collection getWorkspaceConfigs() {        return workspaces.values();    }    /**     * Returns the configuration of the specified workspace.     *     * @param name workspace name     * @return workspace configuration, or <code>null</code> if the named     *         workspace does not exist     */    public WorkspaceConfig getWorkspaceConfig(String name) {        return (WorkspaceConfig) workspaces.get(name);    }    /**     * Returns the repository versioning configuration.     *     * @return versioning configuration     */    public VersioningConfig getVersioningConfig() {        return vc;    }    /**     * Returns the system search index configuration. Returns     * <code>null</code> if no search index has been configured.     *     * @return search index configuration, or <code>null</code>     */    public SearchConfig getSearchConfig() {        return sc;    }    /**     * Returns the cluster configuration. Returns <code>null</code> if clustering     * has not been configured.     */    public ClusterConfig getClusterConfig() {        return cc;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?