nginx configuration file parsing java

Recent projects need to resolve nginx configuration files, several lookup to find nginx-java-parser tool, the project addresses on GitHub: https://github.com/odiszapc/nginx-java-parser

Nginx.conf parsing process, reference may README.md the project, do not know which method call to complete the function, you can view the source code can be found. Here are my resolve demo.

 2 
 3 import com.github.odiszapc.nginxparser.NgxBlock;
 4 import com.github.odiszapc.nginxparser.NgxConfig;
 5 import com.github.odiszapc.nginxparser.NgxEntry;
 6 import com.github.odiszapc.nginxparser.NgxParam;
 7 
 8 import java.io.File;
 9 import java.util.*;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12 
13 public class TestParase {
14 
15 
16     public static void main (String args []) {
 . 17          the try {
 18 is              // profile 1. Read in the specified folders **: \ Work \ Document \ nginxconf \ Test 
. 19              the ArrayList <File> filesArray = TestParase.getFiless ( " *: Work \\ \\ \\ nginxconf document Test \\ \\ " );
 20 is              // the conf configuration file folder 2. parsing loop 
21 is              for ( int I = 0; I <filesArray.size (); I ++ ) {
 22 is                  String filePath = filesArray.get (I) .getPath ();
 23 is                 TestParase.paraseNginx (filePath);
 24              }
 25  
26 is          } the catch (Exception E) {
 27             e.printStackTrace();
28         }
29 
30 
31     }
32 
33     public static void paraseNginx(String filePath) throws Exception{
34 
35         NgxConfig conf=NgxConfig.read(filePath);
36         // 解析ngxin配置文件中的params
37         NgxParam workers = conf.findParam("worker_processes");
38         workers.getValue();
39         System.out.println("workers:"+workers.getValue());
40 
41         // 获取http下面的upstream block
42         List<NgxEntry> rtmpServers = conf.findAll(NgxConfig.BLOCK, "http", "upstream");
43         Set<String> noRepeatRtmpServers=new HashSet<String>();
44         for (NgxEntry entry : rtmpServers) {
45             // 获取upstream下的所有的server
46             List<NgxEntry> entryParamList=((NgxBlock)entry).findAll(NgxConfig.PARAM,"server");
47             for(NgxEntry entryParam : entryParamList){
48                String  str=entryParam.toString();
49                 String[] enrryParamStrArr=entryParam.toString().split(" ");
50                 noRepeatRtmpServers.add(enrryParamStrArr[1]);
51             }
52         }
53         for(String noRepeatRtmpServer:noRepeatRtmpServers){
54             System.out.println(noRepeatRtmpServer);
55         }
56 
57         // 获取http下的server
58         List<NgxEntry> locationServers = conf.findAll(NgxConfig.BLOCK, "http", "server","location");
59         Set<String> noRepeatLocationServers=new HashSet<String>();
60         for (NgxEntry entry : locationServers) {
61             NgxParam locationParam=((NgxBlock)entry).findParam("proxy_pass");
62             if(locationParam==null){
63                 continue;
64             }
65             String[] enrryParamStrArr=locationParam.toString().split(" ");
66             String enrryParamStr=enrryParamStrArr[1];
67             String[] enrryParamStrArry=enrryParamStr.split("/");
68             noRepeatRtmpServers.add(enrryParamStrArry[2]);
69         }
70         //循环遍历 noRepeatRtmpServers
71         for(String noRepeatRtmpServer:noRepeatRtmpServers){
72             System.out.println(noRepeatRtmpServer);
73         }
74     }
75     public static ArrayList<File> getFiless(String path) throws Exception{
76         ArrayList<File> fileList=new ArrayList<File>();//目标集合fileList
77         File file =new File(path);
78         if(file.isDirectory()){
79             File[]files=file.listFiles();
80             for(File fileIndex:files){
81                 if(fileIndex.isDirectory ()) {
 82                      TestParase.getFiless (fileIndex.getPath ()); // if this file is a directory, then a recursive search 
83                  } the else {
 84                      fileList.add (FileIndex); // if the file is an ordinary file, the file handle into the collection 
85                  }
 86              }
 87          }
 88          return the fileList;
 89      }
 90  
91 is }

 

Guess you like

Origin www.cnblogs.com/zhangdanyang95/p/11322159.html