All Downloads are FREE. Search and download functionalities are using the official Maven repository.

external.swagger.SwaggerApiTemplateLoader Maven / Gradle / Ivy

Go to download

Epikos is a Rest Serivce framework which can be extend to develop any other Rest API/Services. For more detail please checkout github (https://github.com/epikosrest/epikos.git)

There is a newer version: 0.0.7.8.1
Show newest version
package external.swagger;

import com.sun.jna.platform.FileUtils;
import core.lib.Utility;
import io.swagger.models.Swagger;
import javassist.ClassPool;
import net.openhft.compiler.CachedCompiler;
import net.openhft.compiler.CompilerUtils;

import javax.tools.*;
import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;

/**
 * Created by nitina on 3/4/17.
 * This class load a template class which is basically a Swagger API decorator
 * We will use OpenHFT Java Runtime Compiler to compile class on the fly and put it under target/classes
 * (or inside corresponding package if package has been included)
 * folder so that Swagger can load/read it later
 * For more info //Ref: https://github.com/OpenHFT/Java-Runtime-Compiler
 * Also an issue was raised which has been resolved //Ref: https://github.com/OpenHFT/Java-Runtime-Compiler/issues/23
 */
public class SwaggerApiTemplateLoader {

    public final static void loadTemplate() throws Exception{

        String javaCode = loadSwaggerApiTemplate();
        String fqdnClassName = "external.swagger.template.SwaggerApiDocumentation";
        loadTemplate(javaCode,fqdnClassName);

    }

    public final static void loadTemplate(String classToLoad,String fqdnClassName) throws Exception{
        //If the class SwaggerApiDocumentation suppose to be in package then construct fqdn string i.e. pakcageName.SwaggerApiDocumentation
        //In this case the class will get compile and put under target/classes/external/swagger/template.
        //String fqdnClassName = "external.swagger.template.SwaggerApiDocumentation";
        CachedCompiler JCC = new CachedCompiler(null, new File("", System.getProperty("user.dir") +
                System.getProperty("file.separator") +"target" + System.getProperty("file.separator") +"classes"));
        JCC.loadFromJava(fqdnClassName,classToLoad);
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
    }

    private static String loadSwaggerApiTemplate(){
        String templateString="";
        try {


            String resourceBannerFullPath = System.getProperty("user.dir") + System.getProperty("file.separator") +
                    "src" + System.getProperty("file.separator") +
                    "main" + System.getProperty("file.separator") +
                    "resources"+ System.getProperty("file.separator") +"SwaggerApiTemplate";
            templateString = new String(Files.readAllBytes(Paths.get(resourceBannerFullPath)));

        }catch (IOException exp){
            System.out.println("Panic : failed to load SwaggerApiTemplate !");
        }finally {
            return templateString;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy