From 7d87f2963c2fbc727d488f0b4c0b66bf12f4199d Mon Sep 17 00:00:00 2001 From: macroscopic64 Date: Mon, 25 Mar 2019 23:04:50 +0530 Subject: [PATCH] [BAEL-2073] Java 9 Migration Issues and Resolution --- prejpms/pom.xml | 73 +++++++++++++++++ .../main/java/com/baeldung/prejpms/App.java | 79 +++++++++++++++++++ .../main/java/com/baeldung/prejpms/Book.java | 39 +++++++++ prejpms/src/main/resources/logback.xml | 10 +++ 4 files changed, 201 insertions(+) create mode 100644 prejpms/pom.xml create mode 100644 prejpms/src/main/java/com/baeldung/prejpms/App.java create mode 100644 prejpms/src/main/java/com/baeldung/prejpms/Book.java create mode 100644 prejpms/src/main/resources/logback.xml diff --git a/prejpms/pom.xml b/prejpms/pom.xml new file mode 100644 index 0000000000..0f0216682e --- /dev/null +++ b/prejpms/pom.xml @@ -0,0 +1,73 @@ + + 4.0.0 + prejpms + 0.0.1-SNAPSHOT + jar + pre-jpms + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + + org.slf4j + slf4j-api + 1.7.25 + + + + pre-jpms + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.1.1 + + + copy-dependencies + package + + copy-dependencies + + + + ${project.build.directory}/dependency-jars/ + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + com.baeldung.prejpms.App + true + dependency-jars/ + + + + + + + + + UTF-8 + + diff --git a/prejpms/src/main/java/com/baeldung/prejpms/App.java b/prejpms/src/main/java/com/baeldung/prejpms/App.java new file mode 100644 index 0000000000..0b38201302 --- /dev/null +++ b/prejpms/src/main/java/com/baeldung/prejpms/App.java @@ -0,0 +1,79 @@ +package com.baeldung.prejpms; + +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.sun.crypto.provider.SunJCE; + +import sun.misc.BASE64Encoder; +import sun.reflect.Reflection; + +public class App { + + private static final Logger logger = LoggerFactory.getLogger(App.class); + + public static void main(String[] args) throws Exception { + + getCrytpographyProviderName(); + getCallStackClassNames(); + getXmlFromObject(new Book(100, "Java Modules Architecture")); + getBase64EncodedString("Java"); + } + + private static void getCrytpographyProviderName() { + try { + logger.info("1. Java Cryptography Extension - Provider Name: " + new SunJCE().getName() + "\n"); + } catch (Throwable e) { + logger.error(e.toString()); + } + } + + private static void getCallStackClassNames() { + try { + int i = 0; + StringBuffer sbStack = new StringBuffer(); + while (true) { + Class caller = Reflection.getCallerClass(i++); + if (caller == null) { + break; + } else { + sbStack.append(caller.getName()) + .append("\n"); + } + } + logger.info("2. Call Stack Class Names:\n" + sbStack.toString()); + } catch (Throwable e) { + logger.error(e.toString()); + } + } + + private static void getXmlFromObject(Book book) { + try { + Marshaller marshallerObj = JAXBContext.newInstance(Book.class) + .createMarshaller(); + marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + + StringWriter sw = new StringWriter(); + marshallerObj.marshal(book, sw); + logger.info("3. Xml for Book object:\n" + sw); + } catch (Throwable e) { + logger.error(e.toString()); + } + + } + + private static void getBase64EncodedString(String inputString) { + try { + String encodedString = new BASE64Encoder().encode(inputString.getBytes()); + logger.info("4. Base Encoded String: " + encodedString); + } catch (Throwable e) { + logger.error(e.toString()); + } + } +} diff --git a/prejpms/src/main/java/com/baeldung/prejpms/Book.java b/prejpms/src/main/java/com/baeldung/prejpms/Book.java new file mode 100644 index 0000000000..57b19bf3c1 --- /dev/null +++ b/prejpms/src/main/java/com/baeldung/prejpms/Book.java @@ -0,0 +1,39 @@ +package com.baeldung.prejpms; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "book") +public class Book { + private long id; + private String name; + + public Book() { + + } + + public Book(long id, String name) { + this.id = id; + this.name = name; + } + + @XmlAttribute + public void setId(Long id) { + this.id = id; + } + + @XmlElement(name = "title") + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public Long getId() { + return id; + } + +} diff --git a/prejpms/src/main/resources/logback.xml b/prejpms/src/main/resources/logback.xml new file mode 100644 index 0000000000..7c5914e58e --- /dev/null +++ b/prejpms/src/main/resources/logback.xml @@ -0,0 +1,10 @@ + + + + [%level] %msg%n + + + + + + \ No newline at end of file