|
| 1 | +package com.sky.test1; |
| 2 | + |
| 3 | +import com.sky.test1.controller.Test1Controller; |
| 4 | +import org.apache.commons.exec.CommandLine; |
| 5 | +import org.apache.commons.exec.DefaultExecutor; |
| 6 | +import org.apache.commons.exec.ExecuteException; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.boot.SpringApplication; |
| 10 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 11 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 12 | +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; |
| 13 | + |
| 14 | +import javax.annotation.PostConstruct; |
| 15 | +import java.io.BufferedReader; |
| 16 | +import java.io.File; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStreamReader; |
| 19 | + |
| 20 | +/* |
| 21 | +* Application to invoke python script from Java |
| 22 | +* */ |
| 23 | +@SpringBootApplication |
| 24 | +public class Test1Application extends SpringBootServletInitializer { |
| 25 | + |
| 26 | + private static final Logger logger = LoggerFactory.getLogger(Test1Controller.class); |
| 27 | + |
| 28 | + private static Process p = null; |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + SpringApplication.run(Test1Application.class, args); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { |
| 36 | + return builder.sources(Test1Application.class); |
| 37 | + } |
| 38 | + |
| 39 | + /*@PostConstruct |
| 40 | + public void init() throws IOException { |
| 41 | + logger.info("Test1Application - Inside Post Construct"); |
| 42 | + System.out.println("Inside post construct \n Application started, now invoking python script"); |
| 43 | + logger.info("Test1Application - Application started, now invoking python script......."); |
| 44 | + if(p == null) { |
| 45 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 46 | + File file = new File(classLoader.getResource("api.py").getFile()); |
| 47 | + String fileName = file.getAbsolutePath(); |
| 48 | + logger.info("Test1Application - "+fileName); |
| 49 | +
|
| 50 | + String[] cmd = { |
| 51 | + "/bin/bash", |
| 52 | + "-c", |
| 53 | + "python3 "+ fileName |
| 54 | + }; |
| 55 | + p = Runtime.getRuntime().exec(cmd); |
| 56 | + //p.waitFor(); |
| 57 | + } |
| 58 | + else |
| 59 | + logger.info("TestApplication Python process already running..."); |
| 60 | +
|
| 61 | + logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct"); |
| 62 | + }*/ |
| 63 | + |
| 64 | + /*@PostConstruct |
| 65 | + public void init() throws IOException, InterruptedException { |
| 66 | + logger.info("Test1Application - Inside Post Construct"); |
| 67 | + System.out.println("Inside post construct \n Application started, now invoking python script"); |
| 68 | + logger.info("Test1Application - Application started, now invoking python script......."); |
| 69 | + if(p == null) { |
| 70 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 71 | + *//*File file = new File(classLoader.getResource("api.py").getFile()); |
| 72 | + String fileName = file.getAbsolutePath(); |
| 73 | + logger.info("Test1Application - "+fileName);*//* |
| 74 | +
|
| 75 | + File file2 = new File(classLoader.getResource("perm.sh").getFile()); |
| 76 | + File parentDir = file2.getParentFile(); |
| 77 | + System.out.println(parentDir); |
| 78 | + String initFileName = file2.getAbsolutePath(); |
| 79 | + logger.info("Test1Application - "+initFileName); |
| 80 | +
|
| 81 | + String[] cmd = { |
| 82 | + "/bin/bash", |
| 83 | + "-c", |
| 84 | + "cd "+parentDir, |
| 85 | + "./ "+initFileName |
| 86 | + }; |
| 87 | +
|
| 88 | + //String cmd = "python3 "+initFileName; |
| 89 | + p = Runtime.getRuntime().exec(cmd); |
| 90 | + p.waitFor(); |
| 91 | + String line; |
| 92 | + BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 93 | + while ((line = input.readLine()) != null) { |
| 94 | + System.out.println(line); |
| 95 | + } |
| 96 | + input.close(); |
| 97 | + } |
| 98 | + else |
| 99 | + logger.info("TestApplication Python process already running..."); |
| 100 | +
|
| 101 | + logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct"); |
| 102 | + }*/ |
| 103 | + |
| 104 | + /*@PostConstruct |
| 105 | + public void init() throws IOException, InterruptedException { |
| 106 | + logger.info("Test1Application - Inside Post Construct"); |
| 107 | + System.out.println("Inside post construct \n Application started, now invoking python script"); |
| 108 | + logger.info("Test1Application - Application started, now invoking python script......."); |
| 109 | + if(p == null) { |
| 110 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 111 | + *//*File file = new File(classLoader.getResource("api.py").getFile()); |
| 112 | + String fileName = file.getAbsolutePath(); |
| 113 | + logger.info("Test1Application - "+fileName);*//* |
| 114 | +
|
| 115 | + File file2 = new File(classLoader.getResource("api.py").getFile()); |
| 116 | + File parentDir = file2.getParentFile(); |
| 117 | + System.out.println(parentDir); |
| 118 | + String initFileName = file2.getAbsolutePath(); |
| 119 | + logger.info("Test1Application - "+initFileName); |
| 120 | +
|
| 121 | + ProcessBuilder processBuilder = new ProcessBuilder(); |
| 122 | + //processBuilder.command("bash", "-c", "ls /Users/sky/"); |
| 123 | + processBuilder.command("bash", "-c", "python3 "+initFileName); |
| 124 | +
|
| 125 | + try { |
| 126 | +
|
| 127 | + Process process = processBuilder.start(); |
| 128 | +
|
| 129 | + StringBuilder output = new StringBuilder(); |
| 130 | +
|
| 131 | + BufferedReader reader = new BufferedReader( |
| 132 | + new InputStreamReader(process.getInputStream())); |
| 133 | +
|
| 134 | + String line; |
| 135 | + while ((line = reader.readLine()) != null) { |
| 136 | + output.append(line + "\n"); |
| 137 | + } |
| 138 | +
|
| 139 | + int exitVal = process.waitFor(); |
| 140 | + if (exitVal == 0) { |
| 141 | + System.out.println("Success!"); |
| 142 | + System.out.println(output); |
| 143 | + System.exit(0); |
| 144 | + } else { |
| 145 | + System.out.println("abnormal......"); |
| 146 | + } |
| 147 | +
|
| 148 | + } catch (IOException e) { |
| 149 | + e.printStackTrace(); |
| 150 | + } catch (InterruptedException e) { |
| 151 | + e.printStackTrace(); |
| 152 | + } |
| 153 | + } |
| 154 | + else |
| 155 | + logger.info("TestApplication Python process already running..."); |
| 156 | +
|
| 157 | + logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct"); |
| 158 | + }*/ |
| 159 | + |
| 160 | + @PostConstruct |
| 161 | + public void init() { |
| 162 | + logger.info("Test1Application - Inside Post Construct"); |
| 163 | + System.out.println("Inside post construct \n Application started, now invoking python script"); |
| 164 | + logger.info("Test1Application - Application started, now invoking python script......."); |
| 165 | + if(p == null) { |
| 166 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 167 | + /*File file = new File(classLoader.getResource("api.py").getFile()); |
| 168 | + String fileName = file.getAbsolutePath(); |
| 169 | + logger.info("Test1Application - "+fileName);*/ |
| 170 | + |
| 171 | + File file2 = new File(classLoader.getResource("perm.sh").getFile()); |
| 172 | + File parentDir = file2.getParentFile(); |
| 173 | + System.out.println(parentDir); |
| 174 | + String initFileName = file2.getAbsolutePath(); |
| 175 | + logger.info("Test1Application - "+initFileName); |
| 176 | + |
| 177 | + String sCommandString = "sh "+initFileName; |
| 178 | + CommandLine oCmdLine = CommandLine.parse(sCommandString); |
| 179 | + DefaultExecutor oDefaultExecutor = new DefaultExecutor(); |
| 180 | + oDefaultExecutor.setExitValue(0); |
| 181 | + |
| 182 | + Thread commandLineThread = new Thread(() -> { |
| 183 | + try { |
| 184 | + int iExitValue = oDefaultExecutor.execute(oCmdLine); |
| 185 | + System.out.println("-------" + iExitValue); |
| 186 | + } catch (ExecuteException e) { |
| 187 | + System.err.println("Execution failed."); |
| 188 | + e.printStackTrace(); |
| 189 | + } catch (IOException e) { |
| 190 | + System.err.println("permission denied."); |
| 191 | + e.printStackTrace(); |
| 192 | + } |
| 193 | + }); |
| 194 | + commandLineThread.setDaemon(true); |
| 195 | + commandLineThread.start(); |
| 196 | + |
| 197 | + } |
| 198 | + else |
| 199 | + logger.info("TestApplication Python process already running..."); |
| 200 | + |
| 201 | + logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct"); |
| 202 | + } |
| 203 | +} |
0 commit comments