Example usage for org.springframework.core.io Resource getDescription

List of usage examples for org.springframework.core.io Resource getDescription

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getDescription.

Prototype

String getDescription();

Source Link

Document

Return a description for this resource, to be used for error output when working with the resource.

Usage

From source file:de.codecentric.boot.admin.web.servlet.resource.ConcatenatingResourceResolver.java

private String buildDescription(Collection<? extends Resource> resources) {
    StringBuilder sb = new StringBuilder("(");
    for (Resource resource : resources) {
        sb.append(resource.getDescription()).append(", ");
    }/*  w  ww. jav  a2  s .c o  m*/
    sb.replace(sb.length() - 2, sb.length(), ")");
    return sb.toString();
}

From source file:com.bluexml.side.framework.alfresco.commons.configurations.RepositoryPropertiesConfiguration.java

public Map<String, String> getDictionary() {
    dictionary = new HashMap<String, String>();
    try {//from  w w  w  .  ja va  2 s .c o  m
        for (Resource r : getResources()) {
            logger.info("Loading resource " + r.getDescription());
            loadResource(r);
        }
    } catch (Exception e) {
        logger.error("error when traying to reload configuration", e);
    }
    logger.debug("getDictionary() " + dictionary);
    return Collections.unmodifiableMap(dictionary);
}

From source file:com.github.mrstampy.gameboot.GameBootDependencyWriter.java

private void writeResource(Resource resource) throws IOException {
    String desc = resource.getDescription();
    log.debug("Creating file from {}", desc);

    if (!resource.exists()) {
        log.warn("No resource for {}", desc);
        return;/* w w  w  .  j  a  v  a 2  s.com*/
    }

    int first = desc.indexOf("[");
    int last = desc.indexOf("]");

    desc = desc.substring(first + 1, last);

    File f = new File(".", desc);

    try (BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(f))) {
        InputStream in = resource.getInputStream();
        byte[] b = new byte[in.available()];
        in.read(b);

        bis.write(b);
        bis.flush();
    }
}

From source file:de.langmi.spring.batch.examples.readers.file.zip.ZipMultiResourceItemReader.java

/**
 * Tries to extract all files in the archives and adds them as resources to
 * the normal MultiResourceItemReader. Overwrites the Comparator from
 * the super class to get it working with itemstreams.
 *
 * @param executionContext/*from   w w w  .j  a  va  2 s.co m*/
 * @throws ItemStreamException 
 */
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
    // really used with archives?
    if (archives != null) {
        // overwrite the comparator to use description
        // instead of filename, the itemStream can only
        // have that description
        this.setComparator(new Comparator<Resource>() {

            /** Compares resource descriptions. */
            @Override
            public int compare(Resource r1, Resource r2) {
                return r1.getDescription().compareTo(r2.getDescription());
            }
        });
        // get the inputStreams from all files inside the archives
        zipFiles = new ZipFile[archives.length];
        List<Resource> extractedResources = new ArrayList<Resource>();
        try {
            for (int i = 0; i < archives.length; i++) {
                // find files inside the current zip resource
                zipFiles[i] = new ZipFile(archives[i].getFile());
                extractFiles(zipFiles[i], extractedResources);
            }
        } catch (Exception ex) {
            throw new ItemStreamException(ex);
        }
        // propagate extracted resources
        this.setResources(extractedResources.toArray(new Resource[extractedResources.size()]));
    }
    super.open(executionContext);
}

From source file:org.openlmis.fulfillment.Resource2Db.java

Pair<List<String>, List<Object[]>> resourceCsvToBatchedPair(final Resource resource) throws IOException {
    XLOGGER.entry(resource.getDescription());

    // parse CSV/*from w  w w  .ja v a2  s .  c o  m*/
    try (InputStreamReader isReader = new InputStreamReader(
            new BOMInputStream(resource.getInputStream(), ByteOrderMark.UTF_8))) {
        CSVParser parser = CSVFormat.DEFAULT.withHeader().withNullString("").parse(isReader);

        // read header row
        MutablePair<List<String>, List<Object[]>> readData = new MutablePair<>();
        readData.setLeft(new ArrayList<>(parser.getHeaderMap().keySet()));
        XLOGGER.info("Read header: " + readData.getLeft());

        // read data rows
        List<Object[]> rows = new ArrayList<>();
        for (CSVRecord record : parser.getRecords()) {
            if (!record.isConsistent()) {
                throw new IllegalArgumentException("CSV record inconsistent: " + record);
            }

            List theRow = IteratorUtils.toList(record.iterator());
            rows.add(theRow.toArray());
        }
        readData.setRight(rows);

        XLOGGER.exit("Records read: " + readData.getRight().size());
        return readData;
    }
}

From source file:fr.acxio.tools.agia.io.IdentityResourceAwareItemReaderItemStreamTest.java

@Test
public void testRead() throws Exception {
    IdentityResourceAwareItemReaderItemStream aReader = new IdentityResourceAwareItemReaderItemStream();
    aReader.setName("testReader");

    Resource aResource = mock(Resource.class);
    when(aResource.getFilename()).thenReturn("file1");
    when(aResource.getDescription()).thenReturn("file1");
    when(aResource.exists()).thenReturn(true);

    aReader.setResource(aResource);// ww  w. j  av  a2  s  . c  o m

    aReader.open(new ExecutionContext());
    assertEquals(aResource, aReader.read());
    assertNull(aReader.read());
    aReader.close();
}

From source file:fr.acxio.tools.agia.io.IdentityResourceAwareItemReaderItemStreamTest.java

@Test
public void testReadNotExists() throws Exception {
    IdentityResourceAwareItemReaderItemStream aReader = new IdentityResourceAwareItemReaderItemStream();
    aReader.setName("testReader");
    aReader.setStrict(false);/*from  ww  w . j  a  va 2  s .  c o m*/

    Resource aResource = mock(Resource.class);
    when(aResource.getFilename()).thenReturn("file1");
    when(aResource.getDescription()).thenReturn("file1");
    when(aResource.exists()).thenReturn(false);

    aReader.setResource(aResource);

    aReader.open(new ExecutionContext());
    assertNull(aReader.read());
    aReader.close();
}

From source file:fr.acxio.tools.agia.io.IdentityResourceAwareItemReaderItemStreamTest.java

@Test
public void testReadNotExistsStrict() throws Exception {
    exception.expect(ItemStreamException.class);
    IdentityResourceAwareItemReaderItemStream aReader = new IdentityResourceAwareItemReaderItemStream();
    aReader.setName("testReader");
    aReader.setStrict(true);/*  www  .j av  a2 s. c o m*/

    Resource aResource = mock(Resource.class);
    when(aResource.getFilename()).thenReturn("file1");
    when(aResource.getDescription()).thenReturn("file1");
    when(aResource.exists()).thenReturn(false);

    aReader.setResource(aResource);

    aReader.open(new ExecutionContext());
    assertNull(aReader.read());
    aReader.close();
}

From source file:de.langmi.spring.batch.examples.readers.file.archive.ArchiveMultiResourceItemReader.java

/**
 * Tries to extract all files in the archives and adds them as resources to
 * the normal MultiResourceItemReader. Overwrites the Comparator from
 * the super class to get it working with itemstreams.
 *
 * @param executionContext//from   w w w. j  av a  2  s .c om
 * @throws ItemStreamException 
 */
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
    // really used with archives?
    if (archives != null) {
        // overwrite the comparator to use description instead of filename
        this.setComparator(new Comparator<Resource>() {

            /** Compares resource descriptions. */
            @Override
            public int compare(Resource r1, Resource r2) {
                return r1.getDescription().compareTo(r2.getDescription());
            }
        });
        // get the inputStreams from all files inside the archives
        wrappedArchives = new TFile[archives.length];
        List<Resource> extractedResources = new ArrayList<Resource>();
        try {
            for (int i = 0; i < archives.length; i++) {
                wrappedArchives[i] = new TFile(archives[i].getFile());
                // iterate over each TFile and get the file list                
                // extract only the files, ignore directories
                List<TFile> fileList = new ArrayList<TFile>();
                runNestedDirs(wrappedArchives[i], fileList, filenameFilter);
                for (TFile tFile : fileList) {
                    extractedResources
                            .add(new InputStreamResource(new TFileInputStream(tFile), tFile.getName()));
                    LOG.info("using extracted file:" + tFile.getName());
                }
            }
        } catch (Exception ex) {
            throw new ItemStreamException(ex);
        }
        // propagate extracted resources
        this.setResources(extractedResources.toArray(new Resource[extractedResources.size()]));
    }
    super.open(executionContext);
}

From source file:org.springmodules.jcr.jackrabbit.JackrabbitSessionFactory.java

protected void registerNodeTypes() throws Exception {
    if (!ObjectUtils.isEmpty(nodeDefinitions)) {
        Workspace ws = getSession().getWorkspace();

        // Get the NodeTypeManager from the Workspace.
        // Note that it must be cast from the generic JCR NodeTypeManager to
        // the//from   w w  w.  j a v  a2s . co  m
        // Jackrabbit-specific implementation.
        JackrabbitNodeTypeManager nodeTypeManager = (JackrabbitNodeTypeManager) ws.getNodeTypeManager();

        boolean debug = log.isDebugEnabled();
        for (int i = 0; i < nodeDefinitions.length; i++) {
            Resource resource = nodeDefinitions[i];
            if (debug)
                log.debug("adding node type definitions from " + resource.getDescription());

            nodeTypeManager.registerNodeTypes(resource.getInputStream(), contentType);
        }
    }
}