use of javax.xml.bind.JAXBContext in project camel by apache.
the class RecordsUtil method createXMLFile.
public static void createXMLFile() {
File in = new File("target/in/records.xml");
if (in.exists()) {
return;
} else {
if (!in.getParentFile().exists() && !in.getParentFile().mkdirs()) {
throw new RuntimeException("can't create " + in.getParent());
}
}
Records records = new Records();
for (int i = 0; i < 10; i++) {
Record record = new Record();
record.setKey(Integer.toString(i));
record.setValue("#" + i);
records.getRecord().add(record);
}
Marshaller marshaller;
try {
JAXBContext jaxbCtx = JAXBContext.newInstance(Records.class.getPackage().getName());
marshaller = jaxbCtx.createMarshaller();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
FileWriter writer = null;
try {
writer = new FileWriter(in);
marshaller.marshal(records, writer);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JAXBException e) {
throw new RuntimeException(e);
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
// no-op
}
}
}
}
use of javax.xml.bind.JAXBContext in project camel by apache.
the class RouteInfoCommand method executeOnRoute.
@Override
public void executeOnRoute(CamelController camelController, String contextName, String routeId, PrintStream out, PrintStream err) throws Exception {
out.println(stringEscape.unescapeJava("[1mCamel Route " + routeId + "[0m"));
out.println(stringEscape.unescapeJava("\tCamel Context: " + contextName));
String xml = camelController.getRouteStatsAsXml(routeId, contextName, true, false);
if (xml != null) {
JAXBContext context = JAXBContext.newInstance(RouteStatDump.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
RouteStatDump route = (RouteStatDump) unmarshaller.unmarshal(new StringReader(xml));
out.println(stringEscape.unescapeJava("\tState: " + route.getState()));
out.println(stringEscape.unescapeJava("\tState: " + route.getState()));
out.println("");
out.println("");
out.println(stringEscape.unescapeJava("[1mStatistics[0m"));
long total = route.getExchangesCompleted() + route.getExchangesFailed();
out.println(stringEscape.unescapeJava("\tExchanges Total: " + total));
out.println(stringEscape.unescapeJava("\tExchanges Completed: " + route.getExchangesCompleted()));
out.println(stringEscape.unescapeJava("\tExchanges Failed: " + route.getExchangesFailed()));
out.println(stringEscape.unescapeJava("\tExchanges Inflight: " + route.getExchangesInflight()));
out.println(stringEscape.unescapeJava("\tMin Processing Time: " + route.getMinProcessingTime() + " ms"));
out.println(stringEscape.unescapeJava("\tMax Processing Time: " + route.getMaxProcessingTime() + " ms"));
out.println(stringEscape.unescapeJava("\tMean Processing Time: " + route.getMeanProcessingTime() + " ms"));
out.println(stringEscape.unescapeJava("\tTotal Processing Time: " + route.getTotalProcessingTime() + " ms"));
out.println(stringEscape.unescapeJava("\tLast Processing Time: " + route.getLastProcessingTime() + " ms"));
out.println(stringEscape.unescapeJava("\tDelta Processing Time: " + route.getDeltaProcessingTime() + " ms"));
if (isEmpty(route.getStartTimestamp())) {
// Print an empty value for scripting
out.println(stringEscape.unescapeJava("\tStart Statistics Date:"));
} else {
Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getStartTimestamp());
String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
out.println(stringEscape.unescapeJava("\tStart Statistics Date: " + text));
}
// Test for null to see if a any exchanges have been processed first to avoid NPE
if (isEmpty(route.getResetTimestamp())) {
// Print an empty value for scripting
out.println(stringEscape.unescapeJava("\tReset Statistics Date:"));
} else {
Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getResetTimestamp());
String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
out.println(stringEscape.unescapeJava("\tReset Statistics Date: " + text));
}
// Test for null to see if a any exchanges have been processed first to avoid NPE
if (isEmpty(route.getFirstExchangeCompletedTimestamp())) {
// Print an empty value for scripting
out.println(stringEscape.unescapeJava("\tFirst Exchange Date:"));
} else {
Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getFirstExchangeCompletedTimestamp());
String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
out.println(stringEscape.unescapeJava("\tFirst Exchange Date: " + text));
}
// Test for null to see if a any exchanges have been processed first to avoid NPE
if (isEmpty(route.getLastExchangeCompletedTimestamp())) {
// Print an empty value for scripting
out.println(stringEscape.unescapeJava("\tLast Exchange Date:"));
} else {
Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getLastExchangeCompletedTimestamp());
String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
out.println(stringEscape.unescapeJava("\tLast Exchange Date: " + text));
}
}
}
use of javax.xml.bind.JAXBContext in project springside4 by springside.
the class XmlMapper method createMarshaller.
/**
* 创建Marshaller并设定encoding(可为null).
* 线程不安全,需要每次创建或pooling。
*/
public static Marshaller createMarshaller(Class clazz, String encoding) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (StringUtils.isNotBlank(encoding)) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
} catch (JAXBException e) {
throw ExceptionUtil.unchecked(e);
}
}
use of javax.xml.bind.JAXBContext in project springside4 by springside.
the class XmlMapper method getJaxbContext.
protected static JAXBContext getJaxbContext(Class clazz) {
Validate.notNull(clazz, "'clazz' must not be null");
JAXBContext jaxbContext = jaxbContexts.get(clazz);
if (jaxbContext == null) {
try {
jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
jaxbContexts.putIfAbsent(clazz, jaxbContext);
} catch (JAXBException ex) {
throw new RuntimeException("Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
}
}
return jaxbContext;
}
use of javax.xml.bind.JAXBContext in project midpoint by Evolveum.
the class DescriptorLoader method loadData.
public void loadData(MidPointApplication application) {
LOGGER.debug("Loading data from descriptor files.");
try (InputStream baseInput = application.getServletContext().getResourceAsStream(baseFileName);
InputStream customInput = application.getServletContext().getResourceAsStream(customFileName)) {
if (baseInput == null) {
LOGGER.error("Couldn't find " + baseFileName + " file, can't load application menu and other stuff.");
}
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<DescriptorType> element = (JAXBElement) unmarshaller.unmarshal(baseInput);
DescriptorType descriptor = element.getValue();
LOGGER.debug("Loading menu bar from " + baseFileName + " .");
DescriptorType customDescriptor = null;
if (customInput != null) {
element = (JAXBElement) unmarshaller.unmarshal(customInput);
customDescriptor = element.getValue();
}
scanPackagesForPages(descriptor.getPackagesToScan(), application);
if (customDescriptor != null) {
scanPackagesForPages(customDescriptor.getPackagesToScan(), application);
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("loaded:\n{}", debugDump(1));
}
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't process application descriptor", ex);
}
}
Aggregations