View Javadoc
1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * Management of the <code>${jetty.base}/start.ini</code> based configuration.
40   * <p>
41   * Implementation of the <code>--add-to-start=[name]</code> command line behavior
42   */
43  public class StartIniBuilder implements BaseBuilder.Config
44  {
45      private final BaseHome baseHome;
46      private final Path startIni;
47  
48      /* List of modules already present in start.ini */
49      private Set<String> modulesPresent = new HashSet<>();
50  
51      /* List of properties (keys only) already present in start.ini */
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              // skip, already present
94              return null;
95          }
96  
97          if (module.isDynamic())
98          {
99              if (module.hasIniTemplate())
100             {
101                 // warn
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             // Append to start.ini
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 }