Package org.apache.stanbol.ontologymanager.servicesapi.session

Examples of org.apache.stanbol.ontologymanager.servicesapi.session.Session


    @Override
    public Session createSession() throws SessionLimitException {
        checkSessionLimit();
        Set<String> exclude = getRegisteredSessionIDs();
        Session session = null;
        while (session == null)
            try {
                session = createSession(idgen.createSessionID(exclude));
            } catch (DuplicateSessionIDException e) {
                exclude.add(e.getDuplicateID());
View Full Code Here


         * before creating a new one.
         */
        if (sessionsByID.containsKey(sessionID)) throw new DuplicateSessionIDException(sessionID.toString());
        checkSessionLimit();
        IRI ns = IRI.create(getDefaultNamespace() + getID() + "/");
        Session session = new SessionImpl(sessionID, ns, ontologyProvider);

        // Have the ontology provider listen to ontology events
        if (ontologyProvider instanceof OntologyCollectorListener) session
                .addOntologyCollectorListener((OntologyCollectorListener) ontologyProvider);
        if (ontologyProvider instanceof SessionListener) session
                .addSessionListener((SessionListener) ontologyProvider);

        Multiplexer multiplexer = ontologyProvider.getOntologyNetworkDescriptor();
        session.addOntologyCollectorListener(multiplexer);
        session.addSessionListener(multiplexer);

        ConnectivityPolicy policy;
        try {
            policy = ConnectivityPolicy.valueOf(connectivityPolicyString);
        } catch (IllegalArgumentException e) {
            log.warn("The value {}", connectivityPolicyString);
            log.warn(" -- configured as default ConnectivityPolicy does not match any value of the Enumeration!");
            log.warn(" -- Setting the default policy as defined by the {}.", ConnectivityPolicy.class);
            policy = ConnectivityPolicy.valueOf(_CONNECTIVITY_POLICY_DEFAULT);
        }
        session.setConnectivityPolicy(policy);

        addSession(session);
        fireSessionCreated(session);
        return session;
    }
View Full Code Here

    }

    @Override
    public synchronized void destroySession(String sessionID) {
        try {
            Session ses = sessionsByID.get(sessionID);
            if (ses == null) log.warn(
                "Tried to destroy nonexisting session {} . Could it have been previously destroyed?",
                sessionID);
            else {
                ses.close();
                if (ses instanceof SessionImpl) ((SessionImpl) ses).state = State.ZOMBIE;
                // Make session no longer referenceable
                removeSession(ses);
                fireSessionDestroyed(ses);
            }
View Full Code Here

        }
        OntologyNetworkConfiguration struct = ontologyProvider.getOntologyNetworkConfiguration();
        for (String sessionId : struct.getSessionIDs()) {
            long before = System.currentTimeMillis();
            log.debug("Rebuilding session with ID \"{}\"", sessionId);
            Session session;
            try {
                session = createSession(sessionId);
            } catch (DuplicateSessionIDException e) {
                log.warn("Session \"{}\" already exists and will be reused.", sessionId);
                session = getSession(sessionId);
            } catch (SessionLimitException e) {
                log.error("Cannot create session {}. Session limit of {} reached.", sessionId,
                    getActiveSessionLimit());
                break;
            }
            // Register even if some ontologies were to fail to be restored afterwards.
            sessionsByID.put(sessionId, session);
            session.setActive(false); // Restored sessions are inactive at first.
            for (OWLOntologyID key : struct.getOntologyKeysForSession(sessionId))
                try {
                    session.addOntology(new StoredOntologySource(key));
                } catch (MissingOntologyException ex) {
                    log.error(
                        "Could not find an ontology with public key {} to be managed by session \"{}\". Proceeding to next ontology.",
                        key, sessionId);
                    continue;
                } catch (Exception ex) {
                    log.error("Exception caught while trying to add ontology with public key " + key
                              + " to rebuilt session \"" + sessionId + "\". Proceeding to next ontology.", ex);
                    continue;
                }
            for (String scopeId : struct.getAttachedScopes(sessionId)) {
                /*
                 * The scope is attached by reference, so we won't have to bother checking if the scope has
                 * been rebuilt by then (which could not happen if the SessionManager is being activated
                 * first).
                 */
                session.attachScope(scopeId);
            }
            log.info("Session \"{}\" rebuilt in {} ms.", sessionId, System.currentTimeMillis() - before);
        }
    }
View Full Code Here

        }
    }

    protected synchronized void removeSession(Session session) {
        String id = session.getID();
        Session s2 = sessionsByID.get(id);
        if (session == s2) sessionsByID.remove(id);
    }
View Full Code Here

    }

    @Test
    public void testRegisterSession() throws Exception {
        int before = sessionManager.getRegisteredSessionIDs().size();
        Session ses = sessionManager.createSession();
        assertNotNull(ses);
        assertEquals(before + 1, sessionManager.getRegisteredSessionIDs().size());
    }
View Full Code Here

        int initialSize = sessionManager.getRegisteredSessionIDs().size();
        Set<Session> sessions = new HashSet<Session>();
        // Create and open many sessions.
        synchronized (sessionManager) {
            for (int i = 0; i < size; i++) {
                Session ses = sessionManager.createSession();
                ses.open();
                sessions.add(ses);
            }
            // Check that 500 sessions have been created
            assertEquals(initialSize + size, sessionManager.getRegisteredSessionIDs().size());
        }
        boolean open = true;
        for (Session ses : sessions)
            open &= ses.getSessionState() == State.ACTIVE;
        // Check that all created sessions have been opened
        assertTrue(open);
        // Kill 'em all, to quote Metallica
        synchronized (sessionManager) {
            for (Session ses : sessions)
                sessionManager.destroySession(ses.getID());
            assertEquals(initialSize, sessionManager.getRegisteredSessionIDs().size());
        }
        // Check that they are all zombies
        boolean zombi = true;
        for (Session ses : sessions)
            zombi &= ses.getSessionState() == State.ZOMBIE;
        assertTrue(zombi);
        // Try to resurrect them (hopefully failing)
        boolean resurrect = false;
        for (Session ses : sessions)
            try {
                ses.open();
                resurrect |= true;
            } catch (NonReferenceableSessionException e) {
                resurrect |= false;
                continue;
            }
View Full Code Here

        assertFalse(resurrect);
    }

    // @Test
    public void zombieSessionClearsContents() throws Exception {
        Session ses = sessionManager.createSession();
        ses.addOntology(new RootOntologySource((IRI
                .create(getClass().getResource("/ontologies/mockfoaf.rdf")))));
        OWLOntologyID expectedKey = new OWLOntologyID(IRI.create("http://xmlns.com/foaf/0.1/"));
        assertTrue(ontologyProvider.hasOntology(expectedKey));
        sessionManager.destroySession(ses.getID());
    }
View Full Code Here

    }

    @Test
    public void sessionPreservesManagedOntologies() throws Exception {
        String id = "12345"; // The kind of thing an idiot would have on his luggage.
        Session session = sessionManager.createSession(id);
        // Anonymous ontologies must preserve their public keys!
        session.addOntology(new GraphContentInputSource(getClass().getResourceAsStream(
            "/ontologies/nameless_ontology.owl")));
        // Same for named ontologies...
        session.addOntology(new GraphContentInputSource(getClass().getResourceAsStream(
            "/ontologies/nonexistentcharacters.owl")));
        // ... and versioned ontologies too.
        session.addOntology(new GraphContentInputSource(getClass().getResourceAsStream(
            "/ontologies/versiontest_v1.owl")));
        session.addOntology(new GraphContentInputSource(getClass().getResourceAsStream(
            "/ontologies/versiontest_v2.owl")));
        Collection<OWLOntologyID> managed = session.listManagedOntologies();
        assertEquals(4, managed.size());

        // Simulate Stanbol going down.
        log.info("Stanbol going down...");
        resetOntologyProvider(); // but keep the TcProvider
        resetManagers();

        Session ses = sessionManager.getSession(id);
        assertNotNull(ses);
        assertEquals(managed, ses.listManagedOntologies());
        assertEquals(session, ses); // XXX Remember that only weak equality is implemented.
    }
View Full Code Here

        Scope scope2 = onManager.createOntologyScope(id2);
        assertNotNull(scope2);
        onManager.deregisterScope(scope1);

        // A session with a system ID
        Session ses1 = sessionManager.createSession();
        String sid1 = ses1.getID();
        assertNotNull(ses1);
        assertNotNull(sid1);
        assertFalse(sid1.isEmpty());
        // A session with an ID chosen manually
        Session ses2 = sessionManager.createSession(sid2);
        assertNotNull(ses2);
        assertNotNull(ses2.getID());
        assertEquals(sid2, ses2.getID());

        log.info("Stanbol going down...");
        resetOntologyProvider(); // but keep the TcProvider
        resetManagers();
View Full Code Here

TOP

Related Classes of org.apache.stanbol.ontologymanager.servicesapi.session.Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.