sessionimpl.java

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

JAVA
1,456
字号
        return new ImportHandler(importer, getNamespaceResolver(), rep.getNamespaceRegistry());    }    /**     * {@inheritDoc}     */    public void importXML(String parentAbsPath, InputStream in,                          int uuidBehavior)            throws IOException, PathNotFoundException, ItemExistsException,            ConstraintViolationException, VersionException,            InvalidSerializedDataException, LockException, RepositoryException {        // check sanity of this session        sanityCheck();        ImportHandler handler = (ImportHandler)                getImportContentHandler(parentAbsPath, uuidBehavior);        try {            SAXParserFactory factory = SAXParserFactory.newInstance();            factory.setNamespaceAware(true);            factory.setFeature(                    "http://xml.org/sax/features/namespace-prefixes", false);            SAXParser parser = factory.newSAXParser();            parser.parse(new InputSource(in), handler);        } catch (SAXException se) {            // check for wrapped repository exception            Exception e = se.getException();            if (e != null && e instanceof RepositoryException) {                throw (RepositoryException) e;            } else {                String msg = "failed to parse XML stream";                log.debug(msg);                throw new InvalidSerializedDataException(msg, se);            }        } catch (ParserConfigurationException e) {            throw new RepositoryException("SAX parser configuration error", e);        }    }    /**     * {@inheritDoc}     */    public void exportDocumentView(String absPath, ContentHandler contentHandler,                                   boolean skipBinary, boolean noRecurse)            throws PathNotFoundException, SAXException, RepositoryException {        // check sanity of this session        sanityCheck();        Item item = getItem(absPath);        if (!item.isNode()) {            // there's a property, though not a node at the specified path            throw new PathNotFoundException(absPath);        }        new DocViewSAXEventGenerator((Node) item, noRecurse, skipBinary,                contentHandler).serialize();    }    /**     * {@inheritDoc}     */    public void exportDocumentView(String absPath, OutputStream out,                                   boolean skipBinary, boolean noRecurse)            throws IOException, PathNotFoundException, RepositoryException {        SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();        try {            TransformerHandler th = stf.newTransformerHandler();            th.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");            th.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");            th.getTransformer().setOutputProperty(OutputKeys.INDENT, "no");            th.setResult(new StreamResult(out));            exportDocumentView(absPath, th, skipBinary, noRecurse);        } catch (TransformerException te) {            throw new RepositoryException(te);        } catch (SAXException se) {            throw new RepositoryException(se);        }    }    /**     * {@inheritDoc}     */    public void exportSystemView(String absPath, ContentHandler contentHandler,                                 boolean skipBinary, boolean noRecurse)            throws PathNotFoundException, SAXException, RepositoryException {        // check sanity of this session        sanityCheck();        Item item = getItem(absPath);        if (!item.isNode()) {            // there's a property, though not a node at the specified path            throw new PathNotFoundException(absPath);        }        new SysViewSAXEventGenerator((Node) item, noRecurse, skipBinary,                contentHandler).serialize();    }    /**     * {@inheritDoc}     */    public void exportSystemView(String absPath, OutputStream out,                                 boolean skipBinary, boolean noRecurse)            throws IOException, PathNotFoundException, RepositoryException {        SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();        try {            TransformerHandler th = stf.newTransformerHandler();            th.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");            th.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");            th.getTransformer().setOutputProperty(OutputKeys.INDENT, "no");            th.setResult(new StreamResult(out));            exportSystemView(absPath, th, skipBinary, noRecurse);        } catch (TransformerException te) {            throw new RepositoryException(te);        } catch (SAXException se) {            throw new RepositoryException(se);        }    }    /**     * {@inheritDoc}     */    public boolean isLive() {        return alive;    }    /**     * Utility method that removes all registered event listeners.     */    private void removeRegisteredEventListeners() {        try {            ObservationManager manager = getWorkspace().getObservationManager();            // Use a copy to avoid modifying the set of registered listeners            // while iterating over it            Collection listeners =                IteratorUtils.toList(manager.getRegisteredEventListeners());            Iterator iterator = listeners.iterator();            while (iterator.hasNext()) {                EventListener listener = (EventListener) iterator.next();                try {                    manager.removeEventListener(listener);                } catch (RepositoryException e) {                    log.warn("Error removing event listener: " + listener, e);                }            }        } catch (RepositoryException e) {            log.warn("Error removing event listeners", e);        }    }    /**     * {@inheritDoc}     */    public synchronized void logout() {        if (!alive) {            // ignore            return;        }        // JCR-798: Remove all registered event listeners to avoid concurrent        // access to session internals by the event delivery or even listeners        removeRegisteredEventListeners();        // discard any pending changes first as those might        // interfere with subsequent operations        itemStateMgr.disposeAllTransientItemStates();        // notify listeners that session is about to be closed        notifyLoggingOut();        // dispose name resolver        nsMappings.dispose();        // dispose session item state manager        itemStateMgr.dispose();        // dispose item manager        itemMgr.dispose();        // dispose workspace        wsp.dispose();        // invalidate session        alive = false;        // logout JAAS subject        if (loginContext != null) {            try {                loginContext.logout();            } catch (javax.security.auth.login.LoginException le) {                log.warn("failed to logout current subject: " + le.getMessage());            }            loginContext = null;        }        try {            accessMgr.close();        } catch (Exception e) {            log.warn("error while closing AccessManager", e);        }        // finally notify listeners that session has been closed        notifyLoggedOut();    }    /**     * {@inheritDoc}     */    public Repository getRepository() {        return rep;    }    /**     * {@inheritDoc}     */    public ValueFactory getValueFactory()            throws UnsupportedRepositoryOperationException, RepositoryException {        if (valueFactory == null) {            valueFactory = ValueFactoryImpl.getInstance();        }        return valueFactory;    }    /**     * {@inheritDoc}     */    public String getUserID() {        return userId;    }    /**     * {@inheritDoc}     */    public Object getAttribute(String name) {        return attributes.get(name);    }    /**     * {@inheritDoc}     */    public String[] getAttributeNames() {        return (String[]) attributes.keySet().toArray(new String[attributes.size()]);    }    /**     * {@inheritDoc}     */    public void setNamespacePrefix(String prefix, String uri)            throws NamespaceException, RepositoryException {        nsMappings.setNamespacePrefix(prefix, uri);    }    /**     * {@inheritDoc}     */    public String[] getNamespacePrefixes()            throws NamespaceException, RepositoryException {        return nsMappings.getPrefixes();    }    /**     * {@inheritDoc}     */    public String getNamespaceURI(String prefix)            throws NamespaceException, RepositoryException {        return nsMappings.getURI(prefix);    }    /**     * {@inheritDoc}     */    public String getNamespacePrefix(String uri)            throws NamespaceException, RepositoryException {        return nsMappings.getPrefix(uri);    }    //------------------------------------------------------< locking support >    /**     * {@inheritDoc}     */    public void addLockToken(String lt) {        addLockToken(lt, true);    }    /**     * Internal implementation of {@link #addLockToken(String)}. Additionally     * takes a parameter indicating whether the lock manager needs to be     * informed.     */    public void addLockToken(String lt, boolean notify) {        synchronized (lockTokens) {            if (lockTokens.add(lt) && notify) {                try {                    getLockManager().lockTokenAdded(this, lt);                } catch (RepositoryException e) {                    log.error("Lock manager not available.", e);                }            }        }    }    /**     * {@inheritDoc}     */    public String[] getLockTokens() {        synchronized (lockTokens) {            String[] result = new String[lockTokens.size()];            lockTokens.toArray(result);            return result;        }    }    /**     * {@inheritDoc}     */    public void removeLockToken(String lt) {        removeLockToken(lt, true);    }    /**     * Internal implementation of {@link #removeLockToken(String)}. Additionally     * takes a parameter indicating whether the lock manager needs to be     * informed.     */    public void removeLockToken(String lt, boolean notify) {        synchronized (lockTokens) {            if (lockTokens.remove(lt) && notify) {                try {                    getLockManager().lockTokenRemoved(this, lt);                } catch (RepositoryException e) {                    log.error("Lock manager not available.", e);                }            }        }    }    /**     * Return the lock manager for this session.     * @return lock manager for this session     */    public LockManager getLockManager() throws RepositoryException {        return wsp.getLockManager();    }    //-------------------------------------------------------------< Dumpable >    /**     * {@inheritDoc}     */    public void dump(PrintStream ps) {        ps.print("Session: ");        if (userId == null) {            ps.print("unknown");        } else {            ps.print(userId);        }        ps.println(" (" + this + ")");        ps.println();        itemMgr.dump(ps);        ps.println();        itemStateMgr.dump(ps);    }}

⌨️ 快捷键说明

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