/*
 * Copyright 2014 Sebastian Schaffert (wastl@wastl.net)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.wastl.rdfdot.render;

import net.wastl.rdfdot.config.GraphConfiguration;
import net.wastl.rdfdot.string.GraphvizSerializerString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of Graphviz Serializer using the Java JNI native library included in the source code (currently
 * works under Linux x64 only).
 *
 * @author Sebastian Schaffert (sschaffert@apache.org)
 */
public class GraphvizSerializerNative extends GraphvizSerializerString {
    private static Logger log = LoggerFactory.getLogger(GraphvizSerializerNative.class);

    public static boolean available = false;

    static {
        try {
            System.loadLibrary( "graphviz" );
            available = true;
        }
        catch( UnsatisfiedLinkError e ) {
            System.out.println( "Could not load native code for rendering." );
        }
    }



    private String filename;

    public GraphvizSerializerNative(GraphConfiguration configuration, String filename) {
        super(configuration);
        this.filename = filename;
    }

    @Override
    protected void finishSerialization() {
        log.info("rendering graph using native library call ...");
        long start = System.currentTimeMillis();
        render(getString(), filename);
        log.info("finished ({}ms)!", System.currentTimeMillis()-start);
    }

    private native void render(String data, String filename);
}

@@ -816,16 +816,66 @@
 ctory;%0A%0A
+import java.util.concurrent.locks.ReentrantLock;%0A%0A
 /**%0A * I
@@ -1538,16 +1538,83 @@
 %0A    %7D%0A%0A
+    private static ReentrantLock renderLock = new ReentrantLock();%0A
 %0A%0A    pr
@@ -1850,24 +1850,69 @@
 ization() %7B%0A
+        renderLock.lock();%0A        try %7B%0A    
         log.
@@ -1974,16 +1974,20 @@
         
+    
 long sta
@@ -2015,24 +2015,28 @@
 meMillis();%0A
+    
         rend
@@ -2062,32 +2062,36 @@
 ename);%0A        
+    
 log.info(%22finish
@@ -2138,16 +2138,79 @@
 start);%0A
+        %7D finally %7B%0A            renderLock.unlock();%0A        %7D%0A
     %7D%0A%0A 

add locking to avoid concurrency issues in JNI