repositoryconfig.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 746 行 · 第 1/2 页
JAVA
746 行
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.core.config;import org.apache.jackrabbit.core.fs.FileSystem;import org.apache.jackrabbit.core.fs.FileSystemException;import org.apache.jackrabbit.core.fs.FileSystemPathUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.w3c.dom.Element;import org.xml.sax.InputSource;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.Reader;import java.io.Writer;import java.net.URI;import java.util.Collection;import java.util.HashMap;import java.util.Map;import java.util.Properties;/** * Repository configuration. This configuration class is used to * create configured repository objects. * <p> * The contained configuration information are: the home directory and name * of the repository, the access manager, file system and versioning * configuration, repository index configuration, the workspace directory, * the default workspace name, and the workspace configuration template. In * addition the workspace configuration object keeps track of all configured * workspaces. */public class RepositoryConfig { /** the default logger */ private static Logger log = LoggerFactory.getLogger(RepositoryConfig.class); /** Name of the workspace configuration file. */ private static final String WORKSPACE_XML = "workspace.xml"; /** * Convenience method that wraps the configuration file name into an * {@link InputSource} and invokes the * {@link #create(InputSource, String)} method. * * @param file repository configuration file name * @param home repository home directory * @return repository configuration * @throws ConfigurationException on configuration errors * @see #create(InputSource, String) */ public static RepositoryConfig create(String file, String home) throws ConfigurationException { URI uri = new File(file).toURI(); return create(new InputSource(uri.toString()), home); } /** * Convenience method that wraps the configuration URI into an * {@link InputSource} and invokes the * {@link #create(InputSource, String)} method. * * @param uri repository configuration URI * @param home repository home directory * @return repository configuration * @throws ConfigurationException on configuration errors * @see #create(InputSource, String) */ public static RepositoryConfig create(URI uri, String home) throws ConfigurationException { return create(new InputSource(uri.toString()), home); } /** * Convenience method that wraps the configuration input stream into an * {@link InputSource} and invokes the * {@link #create(InputSource, String)} method. * * @param input repository configuration input stream * @param home repository home directory * @return repository configuration * @throws ConfigurationException on configuration errors * @see #create(InputSource, String) */ public static RepositoryConfig create(InputStream input, String home) throws ConfigurationException { return create(new InputSource(input), home); } /** * Parses the given repository configuration document and returns the * parsed and initialized repository configuration. The given repository * home directory path will be used as the ${rep.home} parser variable. * <p> * Note that in addition to parsing the repository configuration, this * method also initializes the configuration (creates the configured * directories, etc.). The {@link ConfigurationParser} class should be * used directly to just parse the configuration. * * @param xml repository configuration document * @param home repository home directory * @return repository configuration * @throws ConfigurationException on configuration errors */ public static RepositoryConfig create(InputSource xml, String home) throws ConfigurationException { Properties variables = new Properties(); variables.setProperty( RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE, home); RepositoryConfigurationParser parser = new RepositoryConfigurationParser(variables); RepositoryConfig config = parser.parseRepositoryConfig(xml); config.init(); return config; } /** * map of workspace names and workspace configurations */ private Map workspaces; /** * Repository home directory. */ private final String home; /** * The security config. */ private final SecurityConfig sec; /** * Repository file system configuration. */ private final FileSystemConfig fsc; /** * Name of the default workspace. */ private final String defaultWorkspace; /** * the default parser */ private final RepositoryConfigurationParser parser; /** * Workspace physical root directory. This directory contains a subdirectory * for each workspace in this repository, i.e. the physical workspace home * directory. Each workspace is configured by a workspace configuration file * either contained in the workspace home directory or, optionally, located * in a subdirectory of {@link #workspaceConfigDirectory} within the * repository file system if such has been specified. */ private final String workspaceDirectory; /** * Path to workspace configuration root directory within the * repository file system or null if none was specified. */ private final String workspaceConfigDirectory; /** * Amount of time in seconds after which an idle workspace is automatically * shutdown. */ private final int workspaceMaxIdleTime; /** * The workspace configuration template. Used in creating new workspace * configuration files. */ private final Element template; /** * Repository versioning configuration. */ private final VersioningConfig vc; /** * Optional search configuration for system search manager. */ private final SearchConfig sc; /** * Optional cluster configuration. */ private final ClusterConfig cc; /** * Creates a repository configuration object. * * @param template workspace configuration template * @param home repository home directory * @param sec the security configuration * @param fsc file system configuration * @param workspaceDirectory workspace root directory * @param workspaceConfigDirectory optional workspace configuration directory * @param workspaceMaxIdleTime maximum workspace idle time in seconds * @param defaultWorkspace name of the default workspace * @param vc versioning configuration * @param sc search configuration for system search manager. * @param cc optional cluster configuration * @param parser configuration parser */ public RepositoryConfig(String home, SecurityConfig sec, FileSystemConfig fsc, String workspaceDirectory, String workspaceConfigDirectory, String defaultWorkspace, int workspaceMaxIdleTime, Element template, VersioningConfig vc, SearchConfig sc, ClusterConfig cc, RepositoryConfigurationParser parser) { workspaces = new HashMap(); this.home = home; this.sec = sec; this.fsc = fsc; this.workspaceDirectory = workspaceDirectory; this.workspaceConfigDirectory = workspaceConfigDirectory; this.workspaceMaxIdleTime = workspaceMaxIdleTime; this.defaultWorkspace = defaultWorkspace; this.template = template; this.vc = vc; this.sc = sc; this.cc = cc; this.parser = parser; } /** * Initializes the repository configuration. This method loads the * configurations for all available workspaces. * * @throws ConfigurationException on initialization errors * @throws IllegalStateException if the repository configuration has already * been initialized */ public void init() throws ConfigurationException, IllegalStateException { if (!workspaces.isEmpty()) { throw new IllegalStateException( "Repository configuration has already been initialized."); } // Get the physical workspace root directory (create it if not found) File directory = new File(workspaceDirectory); if (!directory.exists()) { directory.mkdirs(); } // Get all workspace subdirectories if (workspaceConfigDirectory != null) { // a configuration directoy had been specified; search for // workspace configurations in virtual repository file system // rather than in physical workspace root directory on disk FileSystem fs = fsc.createFileSystem(); try { if (!fs.exists(workspaceConfigDirectory)) { fs.createFolder(workspaceConfigDirectory); } else { String[] dirNames = fs.listFolders(workspaceConfigDirectory); for (int i = 0; i < dirNames.length; i++) { String configDir = workspaceConfigDirectory + FileSystem.SEPARATOR + dirNames[i]; WorkspaceConfig wc = loadWorkspaceConfig(fs, configDir); if (wc != null) { addWorkspaceConfig(wc); } } } } catch (FileSystemException e) { throw new ConfigurationException( "error while loading workspace configurations from path " + workspaceConfigDirectory, e); } finally { try { fs.close(); } catch (FileSystemException ignore) { } } } else { // search for workspace configurations in physical workspace root // directory on disk File[] files = directory.listFiles(); if (files == null) { throw new ConfigurationException( "Invalid workspace root directory: " + workspaceDirectory); } for (int i = 0; i < files.length; i++) { WorkspaceConfig wc = loadWorkspaceConfig(files[i]); if (wc != null) { addWorkspaceConfig(wc); } } } if (!workspaces.containsKey(defaultWorkspace)) { if (!workspaces.isEmpty()) { log.warn("Potential misconfiguration. No configuration found " + "for default workspace: " + defaultWorkspace); } // create initial default workspace createWorkspaceConfig(defaultWorkspace); } } /** * Attempts to load a workspace configuration from the given physical * workspace subdirectory. If the directory contains a valid workspace * configuration file, then the configuration is parsed and returned as a * workspace configuration object. The returned configuration object has not * been initialized. * <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 directory physical workspace configuration directory on disk * @return workspace configuration * @throws ConfigurationException if the workspace configuration is invalid */ private WorkspaceConfig loadWorkspaceConfig(File directory) throws ConfigurationException { try { File file = new File(directory, WORKSPACE_XML); InputSource xml = new InputSource(new FileReader(file)); xml.setSystemId(file.toURI().toString()); Properties variables = new Properties(); variables.setProperty( RepositoryConfigurationParser.WORKSPACE_HOME_VARIABLE, directory.getPath()); RepositoryConfigurationParser localParser = parser.createSubParser(variables); return localParser.parseWorkspaceConfig(xml); } catch (FileNotFoundException e) { return null; } } /** * Attempts to load a workspace configuration from the given workspace * subdirectory within the repository file system. If the directory contains * a valid workspace configuration file, then the configuration is parsed * and returned as a workspace configuration object. The returned * configuration object has not been initialized.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?