View Javadoc

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