/core-java-modules/pre-jpms/src/main/java/com/baeldung/prejpms/App.java

https://github.com/yelanggufeng/tutorials · Java · 77 lines · 62 code · 15 blank · 0 comment · 2 complexity · 250936edb241a19b537e506e2858f92d MD5 · raw file

  1. package com.baeldung.prejpms;
  2. import java.io.StringWriter;
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import com.sun.crypto.provider.SunJCE;
  9. import sun.misc.BASE64Encoder;
  10. import sun.reflect.Reflection;
  11. public class App {
  12. private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
  13. public static void main(String[] args) throws Exception {
  14. getCrytpographyProviderName();
  15. getCallStackClassNames();
  16. getXmlFromObject(new Book(100, "Java Modules Architecture"));
  17. getBase64EncodedString("Java");
  18. }
  19. private static void getCrytpographyProviderName() {
  20. try {
  21. LOGGER.info("1. JCE Provider Name: {}\n", new SunJCE().getName());
  22. } catch (Throwable e) {
  23. LOGGER.error(e.toString());
  24. }
  25. }
  26. private static void getCallStackClassNames() {
  27. try {
  28. StringBuffer sbStack = new StringBuffer();
  29. int i = 0;
  30. Class<?> caller = Reflection.getCallerClass(i++);
  31. do {
  32. sbStack.append(i + ".")
  33. .append(caller.getName())
  34. .append("\n");
  35. caller = Reflection.getCallerClass(i++);
  36. } while (caller != null);
  37. LOGGER.info("2. Call Stack:\n{}", sbStack);
  38. } catch (Throwable e) {
  39. LOGGER.error(e.toString());
  40. }
  41. }
  42. private static void getXmlFromObject(Book book) {
  43. try {
  44. Marshaller marshallerObj = JAXBContext.newInstance(Book.class)
  45. .createMarshaller();
  46. marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  47. StringWriter sw = new StringWriter();
  48. marshallerObj.marshal(book, sw);
  49. LOGGER.info("3. Xml for Book object:\n{}", sw);
  50. } catch (Throwable e) {
  51. LOGGER.error(e.toString());
  52. }
  53. }
  54. private static void getBase64EncodedString(String inputString) {
  55. try {
  56. String encodedString = new BASE64Encoder().encode(inputString.getBytes());
  57. LOGGER.info("4. Base Encoded String: {}", encodedString);
  58. } catch (Throwable e) {
  59. LOGGER.error(e.toString());
  60. }
  61. }
  62. }