Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.marmotta.platform.core.services.triplestore;
 
 
Offers access to the Sesame repository underlying this Apache Marmotta instance. The activation/deactivation methods of this service make sure the repository is properly initialised and shut down.

Usage: to access the triple store properly through Sesame, you should follow the following pattern:

     RespositoryConnection con = sesameService.getConnection();

     URI subject = con.getValueFactory().createURI(...);
     ...
     RepositoryResult<Statement> result = con.getStatemenrs(subject,predicate,object,inferred,context);
     while(result.hasNext()) {
         Statement triple = result.next();
         ...
     }

     con.close();
 

Will replace the existing TripleStore at some point.

Author: Sebastian Schaffert

 
 public class SesameServiceImpl implements SesameService {
 
     @Inject
     private Logger log;
 
     @Inject
 
     @Inject @BeforeCommit
 
     @Inject @AfterCommit
 
     @Inject @AfterRollback
     private Event<TransactionDataafterRollbackEvent;

    
triple store providers from backend modules
 
     @Inject
     private Instance<StoreProviderstoreProviders;

    
notifying sail providers from other modules
    @Inject
transactional sail providers from other modules
    @Inject
normal sail providers from other modules
    @Inject
garbage collectors for backends that support it
    @Inject
    private NotifyingSail store;
    private SailRepository repository;
    private ReentrantReadWriteLock restartLock = new ReentrantReadWriteLock();

    
Initialise the Sesame repository. Should be called on service activation.
    @Override
    public synchronized void initialise() {
        .writeLock().lock();
        try {
            .info("Apache Marmotta Sesame Repository Service starting up ...");
            if( != null) {
                .warn("RDF repository has already been initialized");
            }
            if(.isUnsatisfied()) {
                .error("no storage backend found in classpath; please add one of the marmotta-backend-XXX modules");
                throw new UnsatisfiedResolutionException("no storage backend found in classpath; please add one of the marmotta-backend-XXX modules");
            }
            if(.isAmbiguous()) {
                .warn("more than one storage backend in classpath, trying to select the most appropriate ...");
                StoreProvider candidate = null;
                Iterator<StoreProviderit = .iterator();
                while (it.hasNext()) {
                    StoreProvider next = it.next();
                    .warn("- candidate: {} (annotations: {})"next.getName(), next.getClass().getAnnotations());
                    if(candidate == null || !next.getClass().isAnnotationPresent(Fallback.class)) {
                        candidate = next;
                    }
                }
                .warn("selected storage backend: {}"candidate.getName());
                 = candidate.createStore();
            } else {
                 = .get().createStore();
            }
            NotifyingSail notifyingSail;
            .info("initialising repository plugins ...");
            // TODO: should also wrap a transactional sail in case there are observers on the classpath!
            if(   !.isUnsatisfied() || CDIContext.hasObservers(this"beforeCommitEvent")
               || CDIContext.hasObservers(this"afterCommitEvent") || CDIContext.hasObservers(this"afterRollbackEvent")) {
                .info("enabling transaction notification");
                KiWiTransactionalSail tsail = new KiWiTransactionalSail();
                // the CDI events should be triggered once all internal events have been handled, so register the transaction listener last
                tsail.addTransactionListener(new LMFTransactionEventProxy());
                // wrap all stackable transactional sails
                TransactionalSail transactionalSail = tsail;
                for(TransactionalSailProvider provider : ) {
                    if(provider.isEnabled()) {
                        .info("- transaction plugin: {}",provider.getName());
                        transactionalSail = provider.createSail(transactionalSail);
                    } else {
                        .info("- transaction plugin: {} (DISABLED)"provider.getName());
                    }
                }
                notifyingSail = transactionalSail;
            } else {
                .info("not enabling transaction notification, because no transaction observers are registered");
                notifyingSail = ;
            }
            // wrap all stackable notifying sails
            for(NotifyingSailProvider provider : ) {
                if(provider.isEnabled()) {
                    .info("- notifying plugin: {}",provider.getName());
                    notifyingSail = provider.createSail(notifyingSail);
                } else {
                    .info("- notifying plugin: {} (DISABLED)"provider.getName());
                }
            }
            // wrap all standard sails
            Sail standardSail = notifyingSail;
            for(StandardSailProvider provider : ) {
                if(provider.isEnabled()) {
                    .info("- standard plugin: {}",provider.getName());
                    standardSail = provider.createSail(standardSail);
                } else {
                    .info("- standard plugin: {} (DISABLED)"provider.getName());
                }
            }
             = .get().createRepository(standardSail);
            try {
                .initialize();
            } catch (RepositoryException e) {
                .error("error while initialising Apache Marmotta Sesame repository",e);
            }
        } finally {
            .writeLock().unlock();
        }
    }

    
Shutdown the Sesame repository. Should be called on service deactivation.
    @Override
    @PreDestroy
    public synchronized void shutdown() {
        .writeLock().lock();
        try {
            if( != null) {
                .info("Apache Marmotta Sesame Repository Service shutting down ...");
                try {
                    .shutDown();
                } catch (RepositoryException e) {
                    .error("error while shutting down Apache Marmotta Sesame repository",e);
                }
                 = null;
            }
        } finally {
            .writeLock().unlock();
        }
    }

    
Restart the Sesame Service.
    @Override
    public void restart() {
        .writeLock().lock();
        try {
            shutdown();
            initialise();
        } finally {
            .writeLock().unlock();
        }
    }

    
Reinit Sesame repository when the configuration has been initialised

Parameters:
e
    /*
    public void onConfigurationChange(@Observes ConfigurationChangedEvent e) {
        if(e.containsChangedKeyWithPrefix("database")) {
            shutdown();
            initialise();
        }
    }
    */

    
Return the Sesame Repository underlying this service. Callers should be careful with modifying this object directly.

Returns:
the Sesame Repository instance used by this service
    @Override
    @Produces
    public SailRepository getRepository() {
        return ;
    }

    
Return a Sesame RepositoryConnection to the underlying repository. The connection has auto-commit disabled, all transaction management must be performed explicitly.

Returns:
a RepositoryConnection to the underlying Sesame repository.
    @Override
    @Produces
        .readLock().lock();
        RepositoryConnection connection = .getConnection();
        return new RepositoryConnectionWrapper(,connection) {
            @Override
            public void close() throws RepositoryException {
                super.close();
                .readLock().unlock();
            }
        };
    }

    
Return a Sesame ValueFactory for creating new RDF objects.

Returns:
the Sesame ValueFactory belonging to the repository that is used by the service
    @Override
    @Produces
    public ValueFactory getValueFactory() {
        return .getValueFactory();
    }

    
Run the triple store garbage collector manually and clean up unreferenced nodes and triples.

    @Override
    public void garbageCollect() throws SailException {
        if( != null) {
            for(GarbageCollectionProvider p : ) {
                p.garbageCollect();
            }
        }
    }
    private class LMFTransactionEventProxy implements TransactionListener {

        
Called before a transaction commits. The transaction data will contain all changes done in the transaction since the last commit. This method should be used in case the transaction listener aims to perform additional activities in the same transaction, like inserting or updating database tables.

The implementation in lmf-core simply wraps the KiWi transaction data in a CDI transaction to notify other services in the system.

Parameters:
data
        @Override
        public void beforeCommit(TransactionData data) {
            .debug("transaction: before commit event");
            .fire(data);
        }

        
Called after a transaction has committed. The transaction data will contain all changes done in the transaction since the last commit. This method should be used in case the transaction listener aims to perform additional activities in a new transaction or outside the transaction management, e.g. notifying a server on the network, adding data to a cache, or similar.

The implementation in lmf-core simply wraps the KiWi transaction data in a CDI transaction to notify other services in the system.

Parameters:
data
        @Override
        public void afterCommit(TransactionData data) {
            .debug("transaction: after commit event");
            .fire(data);
        }

        
Called when a transaction rolls back.

The implementation in lmf-core simply wraps the KiWi transaction data in a CDI transaction to notify other services in the system.

        @Override
        public void rollback(TransactionData data) {
            .debug("transaction: rollback event");
            .fire(data);
        }
    }
New to GrepCode? Check out our FAQ X