1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start.builders;
20
21 import java.io.BufferedReader;
22 import java.io.BufferedWriter;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.StandardOpenOption;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 import org.eclipse.jetty.start.BaseBuilder;
33 import org.eclipse.jetty.start.BaseHome;
34 import org.eclipse.jetty.start.Module;
35 import org.eclipse.jetty.start.Props;
36 import org.eclipse.jetty.start.StartLog;
37
38
39
40
41
42
43 public class StartIniBuilder implements BaseBuilder.Config
44 {
45 private final BaseHome baseHome;
46 private final Path startIni;
47
48
49 private Set<String> modulesPresent = new HashSet<>();
50
51
52 private Set<String> propsPresent = new HashSet<>();
53
54 public StartIniBuilder(BaseBuilder baseBuilder) throws IOException
55 {
56 this.baseHome = baseBuilder.getBaseHome();
57 this.startIni = baseHome.getBasePath("start.ini");
58
59 if (Files.exists(startIni))
60 {
61 parseIni();
62 }
63 }
64
65 private void parseIni() throws IOException
66 {
67 try (BufferedReader reader = Files.newBufferedReader(startIni,StandardCharsets.UTF_8))
68 {
69 String line;
70 while ((line = reader.readLine()) != null)
71 {
72 line = line.trim();
73 if (line.startsWith("--module="))
74 {
75 List<String> moduleNames = Props.getValues(line);
76 this.modulesPresent.addAll(moduleNames);
77 }
78 else if (!line.startsWith("-") && line.contains("="))
79 {
80 String key = line.substring(0,line.indexOf('='));
81 this.propsPresent.add(key);
82 }
83 }
84 }
85 }
86
87 @Override
88 public String addModule(Module module, Props props) throws IOException
89 {
90 if (modulesPresent.contains(module.getName()))
91 {
92 StartLog.info("%-15s already initialised in %s",module.getName(),baseHome.toShortForm(startIni));
93
94 return null;
95 }
96
97 if (module.isDynamic())
98 {
99 if (module.hasIniTemplate())
100 {
101
102 StartLog.warn("%-15s not adding [ini-template] from dynamic module",module.getName());
103 }
104 return null;
105 }
106
107 if (module.hasIniTemplate() || !module.isTransitive())
108 {
109
110 try (BufferedWriter writer = Files.newBufferedWriter(startIni,StandardCharsets.UTF_8,StandardOpenOption.APPEND,StandardOpenOption.CREATE))
111 {
112 module.writeIniSection(writer, props);
113 }
114 return baseHome.toShortForm(startIni);
115 }
116
117 return null;
118 }
119 }