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.BufferedWriter;
22  import java.io.IOException;
23  import java.io.PrintWriter;
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  
29  import org.eclipse.jetty.start.BaseBuilder;
30  import org.eclipse.jetty.start.BaseHome;
31  import org.eclipse.jetty.start.FS;
32  import org.eclipse.jetty.start.Module;
33  import org.eclipse.jetty.start.StartLog;
34  import org.eclipse.jetty.start.graph.OnlyTransitivePredicate;
35  
36  /**
37   * Management of the <code>${jetty.base}/start.d/</code> based configuration.
38   * <p>
39   * Implementation of the <code>--add-to-startd=[name]</code> command line behavior
40   */
41  public class StartDirBuilder implements BaseBuilder.Config
42  {
43      private final BaseHome baseHome;
44      private final Path startDir;
45  
46      public StartDirBuilder(BaseBuilder baseBuilder) throws IOException
47      {
48          this.baseHome = baseBuilder.getBaseHome();
49          this.startDir = baseHome.getBasePath("start.d");
50          FS.ensureDirectoryExists(startDir);
51      }
52  
53      @Override
54      public boolean addModule(Module module) throws IOException
55      {
56          if (module.isDynamic())
57          {
58              if (module.hasIniTemplate())
59              {
60                  // warn
61                  StartLog.warn("%-15s not adding [ini-template] from dynamic module",module.getName());
62              }
63              return false;
64          }
65  
66          String mode = "";
67          boolean isTransitive = module.matches(OnlyTransitivePredicate.INSTANCE);
68          if (isTransitive)
69          {
70              mode = "(transitively) ";
71          }
72  
73          if (module.hasIniTemplate() || !isTransitive)
74          {
75              // Create start.d/{name}.ini
76              Path ini = startDir.resolve(module.getName() + ".ini");
77              StartLog.info("%-15s initialised %sin %s",module.getName(),mode,baseHome.toShortForm(ini));
78  
79              try (BufferedWriter writer = Files.newBufferedWriter(ini,StandardCharsets.UTF_8,StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING))
80              {
81                  writeModuleSection(writer,module);
82              }
83              return true;
84          }
85  
86          return false;
87      }
88  
89      protected void writeModuleSection(BufferedWriter writer, Module module)
90      {
91          PrintWriter out = new PrintWriter(writer);
92  
93          out.println("# --------------------------------------- ");
94          out.println("# Module: " + module.getName());
95          out.println("--module=" + module.getName());
96          out.println();
97  
98          for (String line : module.getIniTemplate())
99          {
100             out.println(line);
101         }
102 
103         out.println();
104         out.flush();
105     }
106 }