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.config;
20  
21  import static org.eclipse.jetty.start.UsageException.ERR_BAD_ARG;
22  
23  import java.io.IOException;
24  import java.nio.file.DirectoryStream;
25  import java.nio.file.Files;
26  import java.nio.file.NoSuchFileException;
27  import java.nio.file.Path;
28  import java.nio.file.PathMatcher;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  
33  import org.eclipse.jetty.start.FS;
34  import org.eclipse.jetty.start.NaturalSort;
35  import org.eclipse.jetty.start.PathMatchers;
36  import org.eclipse.jetty.start.Props;
37  import org.eclipse.jetty.start.Props.Prop;
38  import org.eclipse.jetty.start.RawArgs;
39  import org.eclipse.jetty.start.StartIni;
40  import org.eclipse.jetty.start.StartLog;
41  import org.eclipse.jetty.start.UsageException;
42  
43  /**
44   * A Directory based {@link ConfigSource}.
45   * <p>
46   * Such as <code>${jetty.base}</code> or and <code>--include-jetty-dir=[path]</code> sources.
47   */
48  public class DirConfigSource implements ConfigSource
49  {
50      private static final List<String> BANNED_ARGS;
51  
52      static
53      {
54          // Arguments that are not allowed to be in start.ini or start.d/{name}.ini files
55          BANNED_ARGS = new ArrayList<>();
56          BANNED_ARGS.add("--help");
57          BANNED_ARGS.add("-?");
58          BANNED_ARGS.add("--stop");
59          BANNED_ARGS.add("--dry-run");
60          BANNED_ARGS.add("--exec-print");
61          BANNED_ARGS.add("--list-config");
62          BANNED_ARGS.add("--list-classpath");
63          BANNED_ARGS.add("--list-modules");
64          BANNED_ARGS.add("--write-module-graph");
65          BANNED_ARGS.add("--version");
66          BANNED_ARGS.add("-v");
67          BANNED_ARGS.add("--download");
68          BANNED_ARGS.add("--create-files");
69          BANNED_ARGS.add("--add-to-startd");
70          BANNED_ARGS.add("--add-to-start");
71      }
72  
73      private final String id;
74      private final Path dir;
75      private final int weight;
76      private final RawArgs args;
77      private final Props props;
78  
79      /**
80       * Create DirConfigSource with specified identifier and directory.
81       * 
82       * @param id
83       *            the identifier for this {@link ConfigSource}
84       * @param dir
85       *            the directory for this {@link ConfigSource}
86       * @param weight
87       *            the configuration weight (used for search order)
88       * @param canHaveArgs
89       *            true if this directory can have start.ini or start.d entries. (false for directories like ${jetty.home}, for example)
90       * @throws IOException
91       *             if unable to load the configuration args
92       */
93      public DirConfigSource(String id, Path dir, int weight, boolean canHaveArgs) throws IOException
94      {
95          this.id = id;
96          this.dir = dir.toAbsolutePath();
97          this.weight = weight;
98          this.props = new Props();
99  
100         this.args = new RawArgs();
101 
102         if (canHaveArgs)
103         {
104             Path iniFile = dir.resolve("start.ini").normalize().toAbsolutePath();
105             
106             try
107             {
108                 iniFile = iniFile.toRealPath();
109                 if (FS.canReadFile(iniFile))
110                 {
111                     StartIni ini = new StartIni(iniFile);
112                     args.addAll(ini.getLines(),iniFile);
113                     parseAllArgs(ini.getLines(),iniFile.toString());
114                 }
115             }
116             catch (NoSuchFileException ignore)
117             {
118                 // ignore
119             }
120 
121             Path startDdir = dir.resolve("start.d");
122 
123             if (FS.canReadDirectory(startDdir))
124             {
125                 DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>()
126                 {
127                     PathMatcher iniMatcher = PathMatchers.getMatcher("glob:**/start.d/*.ini");
128 
129                     @Override
130                     public boolean accept(Path entry) throws IOException
131                     {
132                         return iniMatcher.matches(entry);
133                     }
134                 };
135 
136                 List<Path> paths = new ArrayList<>();
137 
138                 for (Path diniFile : Files.newDirectoryStream(startDdir,filter))
139                 {
140                     if (FS.canReadFile(diniFile))
141                     {
142                         paths.add(diniFile);
143                     }
144                 }
145 
146                 Collections.sort(paths,new NaturalSort.Paths());
147 
148                 for (Path diniFile : paths)
149                 {
150                     StartLog.debug("Reading %s/start.d/%s - %s",id,diniFile.getFileName(),diniFile);
151                     StartIni ini = new StartIni(diniFile);
152                     args.addAll(ini.getLines(),diniFile);
153                     parseAllArgs(ini.getLines(),diniFile.toString());
154                 }
155             }
156         }
157     }
158 
159     private void parseAllArgs(List<String> lines, String origin)
160     {
161         for (String line : lines)
162         {
163             String arg = line;
164             int idx = line.indexOf('=');
165             if (idx > 0)
166             {
167                 arg = line.substring(0,idx);
168             }
169             if (BANNED_ARGS.contains(arg))
170             {
171                 throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,origin);
172             }
173             this.props.addPossibleProperty(line,origin);
174         }
175     }
176 
177     @Override
178     public boolean equals(Object obj)
179     {
180         if (this == obj)
181         {
182             return true;
183         }
184         if (obj == null)
185         {
186             return false;
187         }
188         if (getClass() != obj.getClass())
189         {
190             return false;
191         }
192         DirConfigSource other = (DirConfigSource)obj;
193         if (dir == null)
194         {
195             if (other.dir != null)
196             {
197                 return false;
198             }
199         }
200         else if (!dir.equals(other.dir))
201         {
202             return false;
203         }
204         return true;
205     }
206 
207     @Override
208     public RawArgs getArgs()
209     {
210         return args;
211     }
212 
213     public Path getDir()
214     {
215         return dir;
216     }
217 
218     @Override
219     public String getId()
220     {
221         return id;
222     }
223 
224     @Override
225     public String getProperty(String key)
226     {
227         Prop prop = props.getProp(key,false);
228         if (prop == null)
229         {
230             return null;
231         }
232         return prop.value;
233     }
234 
235     @Override
236     public Props getProps()
237     {
238         return props;
239     }
240 
241     @Override
242     public int getWeight()
243     {
244         return weight;
245     }
246 
247     @Override
248     public int hashCode()
249     {
250         final int prime = 31;
251         int result = 1;
252         result = (prime * result) + ((dir == null)?0:dir.hashCode());
253         return result;
254     }
255 
256     public boolean isPropertyBased()
257     {
258         return id.contains("${");
259     }
260 
261     @Override
262     public String toString()
263     {
264         return String.format("%s[%s,%s,args.length=%d]",this.getClass().getSimpleName(),id,dir,getArgs().size());
265     }
266 }