Add META-INF to Private-Packages to add the persistence.xml
authorChristian Schneider <chris@die-schneider.net>
Tue, 26 Apr 2016 12:14:07 +0000 (14:14 +0200)
committerChristian Schneider <chris@die-schneider.net>
Tue, 26 Apr 2016 12:14:07 +0000 (14:14 +0200)
samples/jpa/pom.xml
starters/karaf-boot-starter-jpa/src/main/java/org/apache/karaf/boot/jpa/impl/JpaProcessor.java

index 1c51404..f745ab2 100644 (file)
@@ -26,7 +26,7 @@
             <version>${project.version}</version>
         </dependency>
     </dependencies>
-
+    
     <build>
         <plugins>
             <plugin>
index f257238..f160fec 100644 (file)
@@ -1,6 +1,9 @@
 package org.apache.karaf.boot.jpa.impl;
 
+import java.io.CharArrayWriter;
 import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Reader;
 import java.io.Writer;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -48,13 +51,18 @@ public class JpaProcessor extends AbstractProcessor {
         }
         if (!units.isEmpty()) {
             try {
-                FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
+                FileObject o = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT,
                                                                        "", "META-INF/persistence.xml");
                 process(o.openWriter(), units);
                 processingEnv.getMessager().printMessage(Kind.NOTE, "Generated META-INF/persistence.xml");
             } catch (Exception e) {
                 processingEnv.getMessager().printMessage(Kind.ERROR, "Error: " + e.getMessage());
             }
+            try (PrintWriter w = appendResource("META-INF/org.apache.karaf.boot.bnd")) {
+                w.println("Private-Package: META-INF");
+            } catch (Exception e) {
+                processingEnv.getMessager().printMessage(Kind.ERROR, "Error writing to META-INF/org.apache.karaf.boot.bnd: " + e.getMessage());
+            }
         }
         return true;
     }
@@ -168,4 +176,33 @@ public class JpaProcessor extends AbstractProcessor {
         }
     }
 
+    private PrintWriter appendResource(String resource) throws IOException {
+        try {
+            FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "",
+                                                                   resource);
+            return new PrintWriter(o.openWriter());
+        } catch (Exception e) {
+            try {
+                FileObject o = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "",
+                                                                    resource);
+                CharArrayWriter baos = new CharArrayWriter();
+                try (Reader r = o.openReader(true)) {
+                    char[] buf = new char[4096];
+                    int l;
+                    while ((l = r.read(buf)) > 0) {
+                        baos.write(buf, 0, l);
+                    }
+                }
+                o.delete();
+                o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", resource);
+                Writer w = o.openWriter();
+                w.write(baos.toCharArray());
+                return new PrintWriter(w);
+            } catch (Exception e2) {
+                e2.addSuppressed(e);
+                e2.printStackTrace();
+                throw e2;
+            }
+        }
+    }
 }