repositoryimpl.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,761 行 · 第 1/5 页
JAVA
1,761 行
throws RepositoryException { FileSystem fs = vConfig.getFileSystemConfig().createFileSystem(); PersistenceManager pm = createPersistenceManager(vConfig.getHomeDir(), fs, vConfig.getPersistenceManagerConfig(), rootNodeId, nsReg, ntReg); return new VersionManagerImpl(pm, fs, ntReg, delegatingDispatcher, VERSION_STORAGE_NODE_ID, SYSTEM_ROOT_NODE_ID, cacheFactory); } /** * Initialize startup workspaces. Base implementation will initialize the * default workspace. Derived classes may initialize their own startup * workspaces <b>after</b> having called the base implementation. * * @throws RepositoryException if an error occurs */ protected void initStartupWorkspaces() throws RepositoryException { String wspName = repConfig.getDefaultWorkspaceName(); try { initWorkspace((WorkspaceInfo) wspInfos.get(wspName)); } catch (RepositoryException e) { // if default workspace failed to initialize, shutdown again log.error("Failed to initialize workspace '" + wspName + "'", e); log.error("Unable to start repository, forcing shutdown..."); shutdown(); throw e; } } /** * Returns the root node uuid. * @param fs * @return * @throws RepositoryException */ protected NodeId loadRootNodeId(FileSystem fs) throws RepositoryException { FileSystemResource uuidFile = new FileSystemResource(fs, "rootUUID"); try { if (uuidFile.exists()) { try { // load uuid of the repository's root node InputStream in = uuidFile.getInputStream();/* // uuid is stored in binary format (16 bytes) byte[] bytes = new byte[16]; try { in.read(bytes); } finally { try { in.close(); } catch (IOException ioe) { // ignore } } rootNodeUUID = new UUID(bytes).toString(); // uuid is stored in binary format (16 bytes)*/ // uuid is stored in text format (36 characters) for better readability char[] chars = new char[36]; InputStreamReader reader = new InputStreamReader(in); try { reader.read(chars); } finally { try { reader.close(); } catch (IOException ioe) { // ignore } } return NodeId.valueOf(new String(chars)); } catch (Exception e) { String msg = "failed to load persisted repository state"; log.debug(msg); throw new RepositoryException(msg, e); } } else { // create new uuid/* UUID rootUUID = UUID.randomUUID(); // version 4 uuid rootNodeUUID = rootUUID.toString();*/ /** * use hard-coded uuid for root node rather than generating * a different uuid per repository instance; using a * hard-coded uuid makes it easier to copy/move entire * workspaces from one repository instance to another. */ try { // persist uuid of the repository's root node OutputStream out = uuidFile.getOutputStream();/* // store uuid in binary format try { out.write(rootUUID.getBytes()); } finally { try { out.close(); } catch (IOException ioe) { // ignore } }*/ // store uuid in text format for better readability OutputStreamWriter writer = new OutputStreamWriter(out); try { writer.write(ROOT_NODE_ID.toString()); } finally { try { writer.close(); } catch (IOException ioe) { // ignore } } return ROOT_NODE_ID; } catch (Exception e) { String msg = "failed to persist repository state"; log.debug(msg); throw new RepositoryException(msg, e); } } } catch (FileSystemException fse) { String msg = "failed to access repository state"; log.debug(msg); throw new RepositoryException(msg, fse); } } /** * Creates the <code>NamespaceRegistry</code> instance. * * @param fs * @return * @throws RepositoryException */ protected NamespaceRegistryImpl createNamespaceRegistry(FileSystem fs) throws RepositoryException { return new NamespaceRegistryImpl(fs); } /** * Creates the <code>NodeTypeRegistry</code> instance. * * @param fs * @return * @throws RepositoryException */ protected NodeTypeRegistry createNodeTypeRegistry(NamespaceRegistry nsReg, FileSystem fs) throws RepositoryException { return NodeTypeRegistry.create(nsReg, fs); } /** * Creates a new <code>RepositoryImpl</code> instance. * <p/> * * @param config the configuration of the repository * @return a new <code>RepositoryImpl</code> instance * @throws RepositoryException If an error occurs */ public static RepositoryImpl create(RepositoryConfig config) throws RepositoryException { return new RepositoryImpl(config); } /** * Performs a sanity check on this repository instance. * * @throws IllegalStateException if this repository has been rendered * invalid for some reason (e.g. if it has * been shut down) */ protected void sanityCheck() throws IllegalStateException { // check repository status if (disposed) { throw new IllegalStateException("repository instance has been shut down"); } } private void initWorkspace(WorkspaceInfo wspInfo) throws RepositoryException { // first initialize workspace info if (!wspInfo.initialize()) { // workspace has already been initialized, we're done return; } // get system session and Workspace instance SessionImpl sysSession = wspInfo.getSystemSession(); WorkspaceImpl wsp = (WorkspaceImpl) sysSession.getWorkspace(); /** * todo implement 'System' workspace * FIXME * - the should be one 'System' workspace per repository * - the 'System' workspace should have the /jcr:system node * - versions, version history and node types should be reflected in * this system workspace as content under /jcr:system * - all other workspaces should be dynamic workspaces based on * this 'read-only' system workspace * * for now, the jcr:system node is created in * {@link org.apache.jackrabbit.core.state.SharedItemStateManager#createRootNodeState} */ // register the repository as event listener for keeping repository statistics wsp.getObservationManager().addEventListener(this, Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_REMOVED, "/", true, null, null, false); // register SearchManager as event listener SearchManager searchMgr = wspInfo.getSearchManager(); if (searchMgr != null) { wsp.getObservationManager().addEventListener(searchMgr, Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_REMOVED | Event.PROPERTY_CHANGED, "/", true, null, null, false); } } /** * Returns the system search manager or <code>null</code> if none is * configured. */ private SearchManager getSystemSearchManager(String wspName) throws RepositoryException { if (systemSearchMgr == null) { if (repConfig.getSearchConfig() != null) { SystemSession defSysSession = getSystemSession(wspName); systemSearchMgr = new SearchManager(repConfig.getSearchConfig(), nsReg, ntReg, defSysSession.getItemStateManager(), SYSTEM_ROOT_NODE_ID, null, null); ObservationManager obsMgr = defSysSession.getWorkspace().getObservationManager(); obsMgr.addEventListener(systemSearchMgr, Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, "/" + defSysSession.getJCRName(QName.JCR_SYSTEM), true, null, null, false); } } return systemSearchMgr; } /** * Creates the cluster node. * * @return clustered node */ protected ClusterNode createClusterNode() throws RepositoryException { try { ClusterNode clusterNode = new ClusterNode(); clusterNode.init(new ExternalEventListener()); return clusterNode; } catch (Exception e) { throw new RepositoryException(e); } } protected NamespaceRegistryImpl getNamespaceRegistry() { // check sanity of this instance sanityCheck(); return nsReg; } protected NodeTypeRegistry getNodeTypeRegistry() { // check sanity of this instance sanityCheck(); return ntReg; } protected VersionManager getVersionManager() { // check sanity of this instance sanityCheck(); return vMgr; } protected NodeId getRootNodeId() { // check sanity of this instance sanityCheck(); return rootNodeId; } /** * Returns the names of <i>all</i> workspaces in this repository. * * @return the names of all workspaces in this repository. * @see javax.jcr.Workspace#getAccessibleWorkspaceNames() */ protected String[] getWorkspaceNames() { synchronized (wspInfos) { return (String[]) wspInfos.keySet().toArray(new String[wspInfos.keySet().size()]); } } /** * Returns the {@link WorkspaceInfo} for the named workspace. * * @param workspaceName The name of the workspace whose {@link WorkspaceInfo} * is to be returned. This must not be <code>null</code>. * @return The {@link WorkspaceInfo} for the named workspace. This will * never be <code>null</code>. * @throws IllegalStateException If this repository has already been * shut down. * @throws NoSuchWorkspaceException If the named workspace does not exist. */ protected WorkspaceInfo getWorkspaceInfo(String workspaceName) throws IllegalStateException, NoSuchWorkspaceException { // check sanity of this instance sanityCheck(); WorkspaceInfo wspInfo; synchronized (wspInfos) { wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName); if (wspInfo == null) { throw new NoSuchWorkspaceException(workspaceName); } } try { initWorkspace(wspInfo); } catch (RepositoryException e) { log.error("Unable to initialize workspace '" + workspaceName + "'", e); throw new NoSuchWorkspaceException(workspaceName); } return wspInfo; } /** * Creates a workspace with the given name. * * @param workspaceName name of the new workspace * @throws RepositoryException if a workspace with the given name * already exists or if another error occurs * @see SessionImpl#createWorkspace(String) */ protected void createWorkspace(String workspaceName) throws RepositoryException { synchronized (wspInfos) { if (wspInfos.containsKey(workspaceName)) { throw new RepositoryException("workspace '" + workspaceName + "' already exists."); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?