class AbstractBinaryStore implements BinaryStore {

    @Override
    public final String getText( BinaryValue binary ) throws BinaryStoreException {
        // try and locate an already extracted text from the store
        if (binary instanceof StoredBinaryValue) {
            String extractedText = getExtractedText(binary);
            if (extractedText != null) {
                return extractedText;
            }
        }

        // there isn't extracted text stored, so try to extract text if extraction is enabled
        if (extractors == null || !extractors.extractionEnabled()) {
            return null;
        }

        if (binary instanceof InMemoryBinaryValue) {
            // The extracted text will never be stored, so try directly using the text extractors ...
            return extractors.extract((InMemoryBinaryValue)binary, new TextExtractorContext(detector()));
        }

        // there isn't any text available, so wait for a job to finish and then return the result
        try {
            CountDownLatch latch = extractors.getWorkerLatch(binary.getKey(), false);
            if (latch == null) {
                // There is no latch, so just compute the text here ...
                latch = extractors.extract(this, binary, new TextExtractorContext(detector()));
            }
            // There was a latch, so wait till the work is done ...
            if (latch != null && latch.await(DEFAULT_LATCH_WAIT_IN_SECONDS, TimeUnit.SECONDS)) {
                return getExtractedText(binary);
            }
            // Stopped waiting ...
            return null;
        } catch (InterruptedException e) {
            throw new BinaryStoreException(e);
        }
    }

}