Example usage for javax.xml.bind JAXB unmarshal

List of usage examples for javax.xml.bind JAXB unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind JAXB unmarshal.

Prototype

public static <T> T unmarshal(Source xml, Class<T> type) 

Source Link

Document

Reads in a Java object tree from the given XML input.

Usage

From source file:org.metaservice.core.injection.ManagerConfigProvider.java

@Override
public ManagerConfig get() {
    if (managerConfig == null) {
        File file = new File(MANAGERCONFIG_XML);
        if (!file.exists()) {
            try {
                IOUtils.copy(ManagerConfigProvider.class.getResourceAsStream("/metaservice-default-config.xml"),
                        new FileOutputStream(file));
            } catch (IOException e) {
                LOGGER.error("Error loading default ", e);
                throw new RuntimeException(e);
            }/*from  w w  w .  ja v  a 2s. c om*/
        }
        managerConfig = JAXB.unmarshal(file, ManagerConfig.class);
        LOGGER.info("Loaded Configuration:");
        LOGGER.info(String.valueOf(managerConfig.getConfig()));
    }
    return managerConfig;
}

From source file:org.opennms.features.jmxconfiggenerator.graphs.JmxConfigReader.java

protected Collection<Report> generateReportsByJmxDatacollectionConfig(InputStream inputConfigStream) {
    return generateReportsByJmxDatacollectionConfig(
            JAXB.unmarshal(inputConfigStream, JmxDatacollectionConfig.class));
}

From source file:org.opennms.features.jmxconfiggenerator.graphs.JmxConfigReader.java

public Collection<Report> generateReportsByJmxDatacollectionConfig(String inputConfigFileName) {
    return generateReportsByJmxDatacollectionConfig(
            JAXB.unmarshal(new File(inputConfigFileName), JmxDatacollectionConfig.class));
}

From source file:org.opennms.jmxconfiggenerator.graphs.JmxConfigReader.java

public Collection<Report> generateReportsByJmxDatacollectionConfig(String jmxDatacollectionConfig) {
    Collection<Report> reports = new ArrayList<Report>();
    JmxDatacollectionConfig inputConfig = JAXB.unmarshal(new File(jmxDatacollectionConfig),
            JmxDatacollectionConfig.class);

    for (JmxCollection jmxCollection : inputConfig.getJmxCollection()) {
        logger.debug("jmxCollection: '{}'", jmxCollection.getName());
        Mbeans mbeans = jmxCollection.getMbeans();

        for (Mbean mbean : mbeans.getMbean()) {
            reports.addAll(generateMbeanReportsByMBean(mbean));
            reports.addAll(generateAttributeReporsByMBean(mbean));

            reports.addAll(generateCompositeReportsByMBean(mbean));
            reports.addAll(generateCompositeMemberReportsByMBean(mbean));
        }//from w  w  w.ja v  a  2  s . c o m
    }
    return reports;
}

From source file:org.opennms.tools.jmxconfiggenerator.graphs.SnmpGraphConfigGenerator.java

public static void generateGraphs(String serviceName, String inputFile, String outFile) throws IOException {
    JmxDatacollectionConfig inputConfig = JAXB.unmarshal(new File(inputFile), JmxDatacollectionConfig.class);

    for (JmxCollection jmxCollection : inputConfig.getJmxCollection()) {
        logger.debug("jmxCollection: '{}'", jmxCollection.getName());
        Mbeans mbeans = jmxCollection.getMbeans();
        int i = 0;
        for (Mbean mBean : mbeans.getMbean()) {
            logger.debug("\t" + "mBean: '{}'", mBean.getObjectname());
            String reportName = "jsr160" + "." + serviceName + "." + "combo" + i;
            graphList = graphList + reportName + ", \\" + "\n";
            i++;/*from  w w  w  . j  a  v  a2 s .  com*/
            graphBodies = graphBodies + "\n" + generateGraphCombo(jmxCollection, mBean, reportName);
            for (Attrib attrib : mBean.getAttrib()) {
                logger.debug("\t\t" + "attrib: '{}'", attrib.getAlias());
                graphList = graphList + "jsr160" + "." + serviceName + "."
                        + StringUtils.substring(attrib.getAlias(), 0, 19) + ", \\" + "\n";
                graphBodies = graphBodies + "\n" + generateGraph(jmxCollection, mBean, attrib);
            }
        }
    }
    graphList = StringUtils.substringBeforeLast(graphList, ", \\\n") + "\n";
    output = graphList + "\n" + graphBodies;
    output = StringUtils.substringBeforeLast(output, " \\" + "\n") + "\n";
    FileUtils.writeStringToFile(new File(outFile), output, "UTF-8");
}

From source file:org.opennms.web.controller.trend.TrendBoxController.java

public TrendConfiguration getConfiguration() {
    return JAXB.unmarshal(CONFIG_FILE, TrendConfiguration.class);
}

From source file:org.opennms.web.rest.CategoryRestServiceTest.java

@Test
@JUnitTemporaryDatabase//from   w w w .  ja  va2 s. c o  m
public void testCategories() throws Exception {
    // get initial categories
    String xml = sendRequest("GET", "/categories", 200);
    assertNotNull(xml);
    OnmsCategoryCollection categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    int initialSize = categories.size();
    assertNotNull(categories);
    assertEquals(initialSize, categories.size());

    // add category
    createCategory("testCategory");
    xml = sendRequest("GET", "/categories", 200);
    categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    assertEquals(initialSize + 1, categories.size());
    assertTrue(xml.contains("name=\"testCategory\""));

    // add again (should fail)
    sendData("POST", MediaType.APPLICATION_XML, "/categories",
            JaxbUtils.marshal(new OnmsCategory("testCategory")), 400);
    xml = sendRequest("GET", "/categories", 200);
    categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    assertEquals(initialSize + 1, categories.size());
    assertTrue(xml.contains("name=\"testCategory\""));

    // delete
    sendRequest("DELETE", "/categories/testCategory", 303);
    xml = sendRequest("GET", "/categories", 200);
    categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    assertEquals(initialSize, categories.size());
    assertFalse(xml.contains("name=\"testCategory\""));
}

From source file:org.opennms.web.rest.CategoryRestServiceTest.java

@Test
@JUnitTemporaryDatabase/*from  w w w  .j  a  v a2 s  .  c  o  m*/
public void testAddCategory() throws Exception {
    // add with description
    OnmsCategory createMe = new OnmsCategory();
    createMe.setDescription("This is a description");
    createMe.setName("myName");
    createCategory(createMe);

    // verify
    String xml = sendRequest("GET", "/categories/myName", 200);
    OnmsCategory category = JAXB.unmarshal(new StringReader(xml), OnmsCategory.class);
    assertNotNull(category.getId());
    createMe.setId(category.getId());
    assertTrue(category.getId().equals(createMe.getId()));
    assertEquals(createMe, category);
}

From source file:org.opennms.web.rest.GroupRestServiceTest.java

@Test
public void testCategories() throws Exception {

    createGroup("testGroup");
    String xml = sendRequest(GET, "/groups/testGroup/categories", 200);
    assertNotNull(xml);//from ww w .ja v  a  2s . com
    OnmsCategoryCollection categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    assertNotNull(categories);
    assertTrue(categories.getCategories().isEmpty());

    // add category to group
    sendRequest(PUT, "/groups/testGroup/categories/testCategory", 400); // fails, because Category is not there
    createCategory("testCategory"); // create category
    sendRequest(PUT, "/groups/testGroup/categories/testCategory", 200); // should not fail, because Category is now there
    xml = sendRequest(GET, "/groups/testGroup/categories/testCategory", 200); // get data
    assertNotNull(xml);
    OnmsCategory category = JAXB.unmarshal(new StringReader(xml), OnmsCategory.class);
    assertNotNull(category);
    assertEquals("testCategory", category.getName());

    // add again (fails)
    sendRequest(PUT, "/groups/testGroup/categories/testCategory", 400); // should fail, because Category is already there

    // remove category from group
    sendRequest(DELETE, "/groups/testGroup/categories/testCategory", 200); // should not fail
    sendRequest(DELETE, "/groups/testGroup/categories/testCategory", 400); // should fail, because category is already removed

    // test that all categories for group "testGroup" have been removed
    xml = sendRequest(GET, "/groups/testGroup/categories", 200);
    assertNotNull(xml);
    categories = JaxbUtils.unmarshal(OnmsCategoryCollection.class, xml);
    assertNotNull(categories);
    assertTrue(categories.getCategories().isEmpty());
}

From source file:org.opennms.web.rest.v1.CategoryRestServiceIT.java

@Test
@JUnitTemporaryDatabase/*from www  . ja  v a 2  s.  c o m*/
public void testCategories() throws Exception {
    // get initial categories
    String xml = sendRequest("GET", "/categories", 200);
    assertNotNull(xml);
    OnmsCategoryCollection categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    int initialSize = categories.size();
    assertNotNull(categories);
    assertEquals(initialSize, categories.size());

    // add category
    createCategory("testCategory");
    xml = sendRequest("GET", "/categories", 200);
    categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    assertEquals(initialSize + 1, categories.size());
    assertTrue(xml.contains("name=\"testCategory\""));

    // add again (should fail)
    sendData("POST", MediaType.APPLICATION_XML, "/categories",
            JaxbUtils.marshal(new OnmsCategory("testCategory")), 400);
    xml = sendRequest("GET", "/categories", 200);
    categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    assertEquals(initialSize + 1, categories.size());
    assertTrue(xml.contains("name=\"testCategory\""));

    // delete
    sendRequest("DELETE", "/categories/testCategory", 204);
    xml = sendRequest("GET", "/categories", 200);
    categories = JAXB.unmarshal(new StringReader(xml), OnmsCategoryCollection.class);
    assertEquals(initialSize, categories.size());
    assertFalse(xml.contains("name=\"testCategory\""));
}