Search in sources :

Example 56 with PrivilegedActionException

use of java.security.PrivilegedActionException in project stanbol by apache.

the class SolrYard method store.

@Override
public final Iterable<Representation> store(Iterable<Representation> representations) throws IllegalArgumentException, YardException {
    if (representations == null) {
        throw new IllegalArgumentException("The parsed Representations MUST NOT be NULL!");
    }
    Collection<Representation> added = new HashSet<Representation>();
    long start = System.currentTimeMillis();
    Collection<SolrInputDocument> inputDocs = new HashSet<SolrInputDocument>();
    for (Representation representation : representations) {
        if (representation != null) {
            inputDocs.add(createSolrInputDocument(representation));
            added.add(representation);
        }
    }
    if (inputDocs.isEmpty()) {
        //empty data sent ... nothing to do
        log.debug("strore called with empty collection of Representations");
        return representations;
    }
    long created = System.currentTimeMillis();
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    final UpdateRequest update = new UpdateRequest();
    if (!immediateCommit) {
        update.setCommitWithin(commitWithin);
    }
    update.add(inputDocs);
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws IOException, SolrServerException {
                update.process(server);
                if (immediateCommit) {
                    server.commit();
                }
                return null;
            }
        });
        long ready = System.currentTimeMillis();
        log.debug(String.format("Processed store request for %d documents in %dms (created %dms| stored%dms)", inputDocs.size(), ready - start, created - start, ready - created));
    } catch (PrivilegedActionException pae) {
        if (pae.getException() instanceof SolrServerException) {
            throw new YardException("Exception while adding Documents to the Solr Server!", pae.getException());
        } else if (pae.getException() instanceof IOException) {
            throw new YardException("Unable to access SolrServer", pae.getException());
        } else {
            throw RuntimeException.class.cast(pae.getException());
        }
    }
    return added;
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) PrivilegedActionException(java.security.PrivilegedActionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) HashSet(java.util.HashSet)

Example 57 with PrivilegedActionException

use of java.security.PrivilegedActionException in project stanbol by apache.

the class SolrYard method getSolrDocument.

protected final SolrDocument getSolrDocument(String uri, Collection<String> fields) throws SolrServerException, IOException {
    final SolrQuery solrQuery = new SolrQuery();
    if (fields == null || fields.isEmpty()) {
        // select all fields
        solrQuery.addField("*");
    } else {
        for (String field : fields) {
            if (field != null && !field.isEmpty()) {
                solrQuery.addField(field);
            }
        }
    }
    // we query for the id, there is only one result
    solrQuery.setRows(1);
    String queryString = String.format("%s:\"%s\"", fieldMapper.getDocumentIdField(), SolrUtil.escapeSolrSpecialChars(uri));
    solrQuery.setQuery(queryString);
    QueryResponse queryResponse;
    try {
        queryResponse = AccessController.doPrivileged(new PrivilegedExceptionAction<QueryResponse>() {

            public QueryResponse run() throws IOException, SolrServerException {
                return server.query(solrQuery, METHOD.POST);
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof SolrServerException) {
            throw (SolrServerException) e;
        } else if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
    if (queryResponse.getResults().isEmpty()) {
        return null;
    } else {
        return queryResponse.getResults().get(0);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) NoConverterException(org.apache.stanbol.entityhub.yard.solr.model.NoConverterException)

Example 58 with PrivilegedActionException

use of java.security.PrivilegedActionException in project stanbol by apache.

the class SolrYard method findReferences.

@Override
public final QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException {
    //create a clone of the query, because we need to refine it because the
    //query (as executed) needs to be included in the result set
    FieldQuery fieldQuery = parsedQuery.clone();
    final SolrQuery query = solrQueryFactoy.parseFieldQuery(fieldQuery, SELECT.ID);
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    QueryResponse response;
    try {
        response = AccessController.doPrivileged(new PrivilegedExceptionAction<QueryResponse>() {

            public QueryResponse run() throws IOException, SolrServerException {
                return server.query(query, METHOD.POST);
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof SolrServerException) {
            throw new YardException("Error while performing query on the SolrServer (query: " + query.getQuery() + ")!", e);
        } else if (e instanceof IOException) {
            throw new YardException("Unable to access SolrServer", e);
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
    // return a queryResultList
    return new QueryResultListImpl<String>(fieldQuery, // by adapting SolrDocuments to Representations
    new AdaptingIterator<SolrDocument, String>(response.getResults().iterator(), // inline Adapter Implementation
    new AdaptingIterator.Adapter<SolrDocument, String>() {

        @Override
        public String adapt(SolrDocument doc, Class<String> type) {
            // use this method for the conversion!
            return doc.getFirstValue(fieldMapper.getDocumentIdField()).toString();
        }
    }, String.class), String.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) PrivilegedActionException(java.security.PrivilegedActionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) NoConverterException(org.apache.stanbol.entityhub.yard.solr.model.NoConverterException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl)

Example 59 with PrivilegedActionException

use of java.security.PrivilegedActionException in project stanbol by apache.

the class SolrYard method update.

@Override
public final Iterable<Representation> update(Iterable<Representation> representations) throws YardException, IllegalArgumentException, NullPointerException {
    if (representations == null) {
        throw new IllegalArgumentException("The parsed Iterable over Representations MUST NOT be NULL!");
    }
    long start = System.currentTimeMillis();
    Set<String> ids = new HashSet<String>();
    for (Representation representation : representations) {
        if (representation != null) {
            ids.add(representation.getId());
        }
    }
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    // for debuging
    int numDocs = ids.size();
    try {
        // returns the ids found in the solrIndex
        ids = checkRepresentations(ids);
    } catch (SolrServerException e) {
        throw new YardException("Error while searching for alredy present documents " + "before executing the actual update for the parsed Representations", e);
    } catch (IOException e) {
        throw new YardException("Unable to access SolrServer", e);
    }
    long checked = System.currentTimeMillis();
    List<SolrInputDocument> inputDocs = new ArrayList<SolrInputDocument>(ids.size());
    List<Representation> updated = new ArrayList<Representation>();
    for (Representation representation : representations) {
        if (representation != null && ids.contains(representation.getId())) {
            // null parsed or not
            // already present
            inputDocs.add(createSolrInputDocument(representation));
            updated.add(representation);
        }
    }
    long created = System.currentTimeMillis();
    if (!inputDocs.isEmpty()) {
        try {
            final UpdateRequest update = new UpdateRequest();
            if (!immediateCommit) {
                update.setCommitWithin(commitWithin);
            }
            update.add(inputDocs);
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                public UpdateResponse run() throws IOException, SolrServerException {
                    update.process(server);
                    if (immediateCommit) {
                        server.commit();
                    }
                    return null;
                }
            });
        } catch (PrivilegedActionException pae) {
            if (pae.getException() instanceof SolrServerException) {
                throw new YardException("Error while adding updated Documents to the SolrServer", pae.getException());
            } else if (pae.getException() instanceof IOException) {
                throw new YardException("Unable to access SolrServer", pae.getException());
            } else {
                throw RuntimeException.class.cast(pae.getException());
            }
        }
    }
    long ready = System.currentTimeMillis();
    log.info(String.format("Processed updateRequest for %d documents (%d in index " + "| %d updated) in %dms (checked %dms|created %dms| stored%dms)", numDocs, ids.size(), updated.size(), ready - start, checked - start, created - checked, ready - created));
    return updated;
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) PrivilegedActionException(java.security.PrivilegedActionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) HashSet(java.util.HashSet)

Example 60 with PrivilegedActionException

use of java.security.PrivilegedActionException in project stanbol by apache.

the class SolrYard method store.

@Override
public final Representation store(Representation representation) throws YardException, IllegalArgumentException {
    log.debug("Store {}", representation != null ? representation.getId() : null);
    if (representation == null) {
        throw new IllegalArgumentException("The parsed Representation MUST NOT be NULL!");
    }
    long start = System.currentTimeMillis();
    final SolrInputDocument inputDocument = createSolrInputDocument(representation);
    long create = System.currentTimeMillis();
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    try {
        final UpdateRequest update = new UpdateRequest();
        if (!immediateCommit) {
            update.setCommitWithin(commitWithin);
        }
        update.add(inputDocument);
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws IOException, SolrServerException {
                update.process(server);
                if (immediateCommit) {
                    server.commit();
                }
                // nothing to return
                return null;
            }
        });
        long stored = System.currentTimeMillis();
        log.debug("  ... done [create={}ms|store={}ms|sum={}ms]", new Object[] { (create - start), (stored - create), (stored - start) });
    } catch (PrivilegedActionException pae) {
        if (pae.getException() instanceof SolrServerException) {
            throw new YardException(String.format("Exception while adding Document to Solr", representation.getId()), pae.getException());
        } else if (pae.getException() instanceof IOException) {
            throw new YardException("Unable to access SolrServer", pae.getException());
        } else {
            throw RuntimeException.class.cast(pae.getException());
        }
    }
    return representation;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) PrivilegedActionException(java.security.PrivilegedActionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Aggregations

PrivilegedActionException (java.security.PrivilegedActionException)135 IOException (java.io.IOException)58 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)56 Subject (javax.security.auth.Subject)23 LoginContext (javax.security.auth.login.LoginContext)14 LoginException (javax.security.auth.login.LoginException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 Method (java.lang.reflect.Method)11 URISyntaxException (java.net.URISyntaxException)11 HashSet (java.util.HashSet)11 ServletException (javax.servlet.ServletException)11 AccessControlContext (java.security.AccessControlContext)10 Principal (java.security.Principal)9 GSSException (org.ietf.jgss.GSSException)9 Field (java.lang.reflect.Field)8 SolrServerException (org.apache.solr.client.solrj.SolrServerException)7 GSSManager (org.ietf.jgss.GSSManager)7 MalformedURLException (java.net.MalformedURLException)6 ArrayList (java.util.ArrayList)6 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)6