Example usage for org.apache.commons.lang3.reflect FieldUtils readField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils readField

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils readField.

Prototype

public static Object readField(final Object target, final String fieldName, final boolean forceAccess)
        throws IllegalAccessException 

Source Link

Document

Reads the named Field .

Usage

From source file:org.silverpeas.core.viewer.service.ViewServiceConcurrencyDemonstrationIT.java

@Test
public void demonstrateConcurrencyManagementWithThreeDifferentConversionsAtSameTime() throws Exception {
    if (canPerformViewConversionTest()) {
        final List<Throwable> throwables = new ArrayList<>();

        final ConcurrentMap serviceCache = (ConcurrentMap) FieldUtils.readField(viewService, "cache", true);
        assertThat(serviceCache.size(), is(0));

        final int NB_VIEW_CALLS = 100;

        for (int i = 0; i < NB_VIEW_CALLS; i++) {
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override//from  ww w .  j  a v  a  2 s.c om
                public void run() {
                    try {
                        viewService.getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.odp")));
                    } catch (Exception ignore) {
                    }
                }
            }));
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        viewService.getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.pdf")));
                    } catch (Exception ignore) {
                    }
                }
            }));
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        viewService.getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.odt")));
                    } catch (Exception ignore) {
                    }
                }
            }));
        }

        SubThreadManager.addAndStart(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);

                    // Technical verification
                    assertThat(getTemporaryPath().listFiles(), arrayWithSize(6));

                } catch (Throwable e) {
                    throwables.add(e);
                    SubThreadManager.killAll();
                }
            }
        }));

        // Waiting for all thread ends
        SubThreadManager.joinAll(60000);

        for (Throwable throwable : throwables) {
            throw new RuntimeException(throwable);
        }

        assertThat(serviceCache.size(), is(0));
        assertThat(getTemporaryPath().listFiles(), arrayWithSize(6));
    }
}

From source file:org.silverpeas.core.viewer.service.ViewServiceConcurrencyDemonstrationTest.java

@Test
public void demonstrateConcurrencyManagement() throws Exception {
    if (canPerformViewConversionTest()) {
        final List<Throwable> throwables = new ArrayList<>();

        final ConcurrentMap serviceCache = (ConcurrentMap) FieldUtils.readField(viewService, "cache", true);
        assertThat(serviceCache.size(), is(0));

        final long startTime = System.currentTimeMillis();
        final int NB_VIEW_CALLS = 100;
        final int LAST_REQUEST_INDEX = NB_VIEW_CALLS - 1;
        final long durationToIncreaseChancesToPerformThreadBefore = 50;

        final long[] durationTimes = new long[NB_VIEW_CALLS];
        final long[] endTimes = new long[NB_VIEW_CALLS];
        final DocumentView[] results = new DocumentView[NB_VIEW_CALLS];

        for (int i = 0; i < LAST_REQUEST_INDEX; i++) {
            final int index = i;
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override//  w ww .  java 2 s.c  om
                public void run() {
                    try {
                        final long startThreadTime = System.currentTimeMillis();
                        final DocumentView viewFirstRequest = viewService
                                .getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.ppt")));
                        final long endThreadTime = System.currentTimeMillis();
                        durationTimes[index] = endThreadTime - startThreadTime;
                        endTimes[index] = endThreadTime;
                        results[index] = viewFirstRequest;
                    } catch (Exception e) {
                        throwables.add(e);
                        SubThreadManager.killAll();
                    }
                }
            }));
        }

        SubThreadManager.addAndStart(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);

                    // Technical verification
                    assertThat("At this level, the cache has 2 elements", serviceCache.size(), is(2));
                    assertThat(getTemporaryPath().listFiles(), arrayWithSize(2));

                } catch (Throwable e) {
                    throwables.add(e);
                    SubThreadManager.killAll();
                }
            }
        }));

        Thread.sleep(durationToIncreaseChancesToPerformThreadBefore);

        final long startLastRequestTime = System.currentTimeMillis();
        final DocumentView viewLastRequest = viewService
                .getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.ppt")));
        final long endLastRequestTime = System.currentTimeMillis();
        durationTimes[LAST_REQUEST_INDEX] = endLastRequestTime - startLastRequestTime;
        endTimes[LAST_REQUEST_INDEX] = endLastRequestTime;
        results[LAST_REQUEST_INDEX] = viewLastRequest;

        // Waiting for all thread ends
        SubThreadManager.joinAll(60000);

        for (Throwable throwable : throwables) {
            throw new RuntimeException(throwable);
        }

        for (long endTime : endTimes) {
            assertThat(endTime, greaterThan(startTime));
        }

        long minEndTime = Long.MAX_VALUE;
        long maxEndTime = 0;
        for (long endTime : endTimes) {
            assertThat(endTime, greaterThan(startTime));
            minEndTime = Math.min(minEndTime, endTime);
            maxEndTime = Math.max(maxEndTime, endTime);
        }
        assertThat((maxEndTime - minEndTime), lessThan(250l));
        for (DocumentView documentView : results) {
            assertThat(documentView, notNullValue());
        }

        for (int i = 0; i < LAST_REQUEST_INDEX; i++) {
            assertThat(results[i].getPhysicalFile(), is(viewLastRequest.getPhysicalFile()));
            assertThat(results[i].getNbPages(), is(viewLastRequest.getNbPages()));
            assertThat(results[i].getWidth(), is(viewLastRequest.getWidth()));
            assertThat(results[i].getHeight(), is(viewLastRequest.getHeight()));
            assertThat(results[i].getDisplayLicenseKey(), is(viewLastRequest.getDisplayLicenseKey()));
            assertThat(results[i].getOriginalFileName(), is(viewLastRequest.getOriginalFileName()));
            assertThat(results[i].getURLAsString(), is(viewLastRequest.getURLAsString()));
            assertThat(results[i].isDocumentSplit(), is(viewLastRequest.isDocumentSplit()));
            assertThat(results[i].areSearchDataComputed(), is(viewLastRequest.areSearchDataComputed()));
        }

        assertThat(serviceCache.size(), is(0));
        assertThat(getTemporaryPath().listFiles(), arrayWithSize(2));
    }
}

From source file:org.silverpeas.core.viewer.service.ViewServiceConcurrencyDemonstrationTest.java

@Test
public void demonstrateConcurrencyManagementWithTwoDifferentConversionsAtSameTime() throws Exception {
    if (canPerformViewConversionTest()) {
        final List<Throwable> throwables = new ArrayList<>();

        final ConcurrentMap serviceCache = (ConcurrentMap) FieldUtils.readField(viewService, "cache", true);
        assertThat(serviceCache.size(), is(0));

        final int NB_VIEW_CALLS = 100;

        for (int i = 0; i < NB_VIEW_CALLS; i++) {
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override// w  w  w.  ja v  a2 s .  c o  m
                public void run() {
                    try {
                        viewService.getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.odp")));
                    } catch (Exception ignore) {
                    }
                }
            }));
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        viewService.getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.pdf")));
                    } catch (Exception ignore) {
                    }
                }
            }));
        }

        SubThreadManager.addAndStart(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);

                    // Technical verification
                    assertThat(getTemporaryPath().listFiles(), arrayWithSize(4));

                } catch (Throwable e) {
                    throwables.add(e);
                    SubThreadManager.killAll();
                }
            }
        }));

        // Waiting for all thread ends
        SubThreadManager.joinAll(60000);

        for (Throwable throwable : throwables) {
            throw new RuntimeException(throwable);
        }

        assertThat(serviceCache.size(), is(0));
        assertThat(getTemporaryPath().listFiles(), arrayWithSize(4));
    }
}

From source file:org.treeingwalker.LessCompilerTest.java

@Test
public void testNewLessCompiler() throws Exception {
    assertEquals(LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.7.0.js"),
            FieldUtils.readField(lessCompiler, "lessJs", true));
    assertEquals(LessCompiler.class.getClassLoader().getResource("META-INF/lessc-rhino-1.7.0.js"),
            FieldUtils.readField(lessCompiler, "lesscJs", true));
    assertEquals(Collections.EMPTY_LIST, FieldUtils.readField(lessCompiler, "customJs", true));
}

From source file:org.treeingwalker.LessSourceTest.java

private String readLessSourceWithEncoding(String encoding) throws IOException, IllegalAccessException {
    URL sourceUrl = getClass().getResource("/compatibility/utf8-content.less");
    File sourceFile = new File(sourceUrl.getFile());
    LessSource lessSource = new LessSource(new FileResource(sourceFile), Charset.forName(encoding));
    return (String) FieldUtils.readField(lessSource, "content", true);
}

From source file:org.wso2.carbon.apimgt.core.TestUtil.java

public static <T> String printDiff(T obj1, T obj2) throws IllegalAccessException {
    Field[] fields = FieldUtils.getAllFields(obj1.getClass());

    for (Field field : fields) {
        Object obj1Value = FieldUtils.readField(field, obj1, true);
        Object obj2Value = FieldUtils.readField(field, obj2, true);

        String obj1ValueString = "null";
        String obj2ValueString = "null";
        if (obj1Value != null) {
            obj1ValueString = obj1Value.toString();
        }// ww w  .  j  a  v a  2 s  .  c o  m
        if (obj2Value != null) {
            obj2ValueString = obj2Value.toString();
        }

        if (!Objects.equals(obj1Value, obj2Value)) {
            return "Diff detected for '" + field.getName() + "' " + System.lineSeparator()
                    + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LHS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                    + System.lineSeparator() + obj1ValueString + System.lineSeparator()
                    + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LHS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                    + System.lineSeparator()
                    + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< RHS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                    + System.lineSeparator() + obj2ValueString + System.lineSeparator()
                    + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< RHS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                    + System.lineSeparator();
        }
    }

    return null;
}

From source file:org.xwiki.extension.xar.internal.script.SafeConflictQuestion.java

/**
 * @since 4.1//  w  w w. j a  v  a2s  . c  o  m
 */
public void setCustomDocument(Document customDocument) {
    try {
        XWikiDocument document = (XWikiDocument) FieldUtils.readField(customDocument, "doc", true);

        this.question.setCustomDocument(document);
    } catch (IllegalAccessException e) {
        // TODO: log something ?
    }
}

From source file:pl.wasat.smarthma.utils.xml.OpmlBuilder.java

/**
 * Build xml string./*  w w w.jav a  2 s  .  c  o  m*/
 *
 * @param opml the opml
 * @return the string
 */
public String buildXml(Opml opml) {
    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    List<Outline> outlines = opml.getBody().getOutline();
    try {
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", Opml.class.getSimpleName());
        serializer.attribute("", "version", opml.getVersion());

        serializer.startTag("", Head.class.getSimpleName());
        for (Field headField : opml.getHead().getClass().getDeclaredFields()) {
            serializer.startTag("", headField.getName());
            String value = String.valueOf(FieldUtils.readField(opml.getHead(), headField.getName(), true));
            serializer.text(value);
            serializer.endTag("", headField.getName());
        }
        serializer.endTag("", Head.class.getSimpleName());

        serializer.startTag("", Body.class.getSimpleName());
        for (Outline outline : outlines) {
            serializer.startTag("", Outline.class.getSimpleName());
            for (Field field : Outline.class.getDeclaredFields()) {
                String value = String.valueOf(FieldUtils.readField(outline, field.getName(), true));
                serializer.attribute("", field.getName(), value);
            }
            serializer.endTag("", Outline.class.getSimpleName());
        }
        serializer.endTag("", Body.class.getSimpleName());
        serializer.endTag("", Opml.class.getSimpleName());
        serializer.endDocument();
        //String xml = writer.toString();
        //Log.i("XML", xml);
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:prototypes.ws.proxy.soap.configuration.CaptureExpression.java

public String capture(Object object) {
    try {//from  w  w w .  j av a  2s.co m
        Object targetField = FieldUtils.readField(object, this.objectField, true);
        if (targetField != null) {
            if (targetField instanceof byte[]) {
                return capture(new String((byte[]) targetField));
            }
            return capture(targetField.toString());
        }
    } catch (IllegalArgumentException ex) {
        logger.warn(UNKNOWN_FIELD_OF_CLASS_2P, this.objectField, object.getClass().getName());
        logger.debug(Messages.MSG_ERROR_DETAILS, ex);
    } catch (IllegalAccessException ex) {
        logger.warn(CANT_ACCESS_FIELD_OF_CLASS_2P, this.objectField, object.getClass().getName());
        logger.debug(Messages.MSG_ERROR_DETAILS, ex);
    }
    return "";
}

From source file:prototypes.ws.proxy.soap.configuration.CaptureExpression.java

public boolean match(Object object) {
    try {//  w  w w. j a  va2 s .co  m
        Object targetField = FieldUtils.readField(object, this.objectField, true);
        if (targetField != null) {
            if (targetField instanceof byte[]) {
                return match(new String((byte[]) targetField));
            }
            return match(targetField.toString());
        }
    } catch (IllegalArgumentException ex) {
        logger.warn(UNKNOWN_FIELD_OF_CLASS_2P, this.objectField, object.getClass().getName());
        logger.debug(Messages.MSG_ERROR_DETAILS, ex);
    } catch (IllegalAccessException ex) {
        logger.warn(CANT_ACCESS_FIELD_OF_CLASS_2P, this.objectField, object.getClass().getName());
        logger.debug(Messages.MSG_ERROR_DETAILS, ex);
    }
    return false;
}