View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.creadur.whisker.app;
20  
21  import static org.apache.creadur.whisker.app.LicenseConfiguration.*;
22  import static org.apache.creadur.whisker.app.ConfigurationBuilder.*;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Collection;
27  
28  import org.apache.creadur.whisker.app.analysis.LicenseAnalyst;
29  import org.apache.creadur.whisker.fromxml.JDomBuilder;
30  import org.apache.creadur.whisker.model.Descriptor;
31  import org.apache.creadur.whisker.scan.Directory;
32  import org.apache.creadur.whisker.scan.FromFileSystem;
33  
34  
35  /**
36   * High level application, configured as a bean.
37   */
38  public class Whisker {
39  
40      /** Operation to be performed. */
41      private Act act;
42      /** The source on which the operation is to be performed. */
43      private String source;
44      /** The meta-data. */
45      private StreamableResource licenseDescriptor;
46      /** Pluggable report writer. */
47      private ResultWriterFactory writerFactory;
48      /** Pluggable templating. */
49      private AbstractEngine engine;
50      /** Configuration options for license rendering */
51      private LicenseConfiguration licenseConfiguration = DEFAULT_LICENSE_CONFIGURATION;
52  
53      /**
54       * Gets the configuration options for license rendering.
55       * @return not null
56       */
57      public LicenseConfiguration getLicenseConfiguration() {
58          return licenseConfiguration;
59      }
60  
61      /**
62       * Sets the configuration options for license rendering.
63       * @param licenseConfiguration not null
64       * @return this, not null
65       */
66      public Whisker setLicenseConfiguration(final LicenseConfiguration licenseConfiguration) {
67          this.licenseConfiguration = notNull(licenseConfiguration);
68          return this;
69      }
70  
71      /**
72       * Gets the factory that builds product {@link java.io.Writer}s.
73       * @return factory
74       */
75      public final ResultWriterFactory getWriterFactory() {
76          return writerFactory;
77      }
78  
79      /**
80       * Sets the factory that builds product {@link java.io.Writer}s.
81       * @param writerFactory not null
82       * @return this, not null
83       */
84      public final Whisker setWriterFactory(final ResultWriterFactory writerFactory) {
85          this.writerFactory = writerFactory;
86          return this;
87      }
88  
89      /**
90       * Gets the reporting engine.
91       * @return not null
92       */
93      public final AbstractEngine getEngine() {
94          return engine;
95      }
96  
97      /**
98       * Sets the reporting engine.
99       * @param engine not null
100      * @return this, not null
101      */
102     public final Whisker setEngine(final AbstractEngine engine) {
103         this.engine = engine;
104         return this;
105     }
106 
107     /**
108      * Gets the source on which the operation will be performed.
109      * @return the base, not null
110      */
111     public final String getSource() {
112         return source;
113     }
114 
115     /**
116      * Sets the source
117      * @param source the base to set
118      * @return this, not null
119      */
120     public final Whisker setSource(final String source) {
121         this.source = source;
122         return this;
123     }
124 
125     /**
126      * @return the licenseDescriptor
127      */
128     public final StreamableResource getLicenseDescriptor() {
129         return licenseDescriptor;
130     }
131 
132     /**
133      * Sets meta-data describing the source licensing.
134      * @param licenseDescriptor the licenseDescriptor to set
135      * @return this, not null
136      */
137     public final Whisker setLicenseDescriptor(StreamableResource licenseDescriptor) {
138         this.licenseDescriptor = licenseDescriptor;
139         return this;
140     }
141 
142     /**
143      * Gets the operation to be performed.
144      * @return the act
145      */
146     public final Act getAct() {
147         return act;
148     }
149 
150     /**
151      * Sets the operation to be performed.
152      * @param act the act to set
153      * @return this, not null
154      */
155     public final Whisker setAct(Act act) {
156         this.act = act;
157         return this;
158     }
159 
160     /**
161      * Performs the operation set.
162      * @return this, not null
163      * @throws Exception when the operation fails
164      */
165     public Whisker act() throws Exception {
166         switch (act) {
167             case REPORT:
168                 return report();
169             case AUDIT:
170                 return validate();
171             case SKELETON:
172                 return skeleton();
173             case GENERATE:
174             default:
175                 return generate();
176         }
177     }
178 
179     /**
180      * Creates a template to help create meta-data.
181      * @throws Exception when the report creation fails
182      * @return this, not null
183      */
184     private Whisker skeleton() throws Exception {
185         engine.skeleton(directories(), getWriterFactory(), configuration());
186         return this;
187     }
188 
189     /**
190      * Builds a populated configuration.
191      * @return not null
192      */
193     public Configuration configuration() {
194         return aConfiguration().with(licenseConfiguration).build();
195     }
196 
197     /**
198      * Writes a report on the directories in the source.
199      * @return this, not null
200      * @throws Exception when the report creation fails
201      */
202     private Whisker report() throws Exception {
203         engine.report(directories(), getWriterFactory(), configuration());
204         return this;
205     }
206 
207     /**
208      * Generates legal documentation.
209      * @return this, not null
210      * @throws Exception when the generation fails
211      */
212     private Whisker generate() throws Exception {
213         engine.generate(new LicenseAnalyst().validate(load(getLicenseDescriptor())), getWriterFactory(), configuration());
214         return this;
215     }
216 
217     /**
218      * Writes a validation report.
219      * @return this, not null
220      * @throws Exception when the validation fails
221      */
222     private Whisker validate() throws Exception {
223         engine.validate(new LicenseAnalyst(directories()).analyse(load(getLicenseDescriptor())), getWriterFactory(), configuration());
224         return this;
225     }
226 
227     /**
228      * Describes the directories within the source.
229      * @return not null
230      * @throws IOException when reading the source fails
231      */
232     private Collection<Directory> directories() throws IOException {
233         return new FromFileSystem().withBase(getSource());
234     }
235 
236     /**
237      * Describes the state suitable for logging.
238      * @return something suitable for logging
239      */
240     @Override
241     public String toString() {
242         return "Whisker [act=" + act + ", base=" + source
243                 + ", licenseDescriptor=" + licenseDescriptor + "]";
244     }
245 
246 
247     /**
248      * Opens a resource as a stream.
249      * @param resource not null
250      * @return not null
251      * @throws IOException
252      */
253     private InputStream resourceAsStream(final StreamableResource resource) throws IOException {
254         return resource.open();
255     }
256 
257 
258     /**
259      * Reads meta-data from the given source.
260      * @param resource not null
261      * @return not null
262      * @throws Exception when meta-data cannot be loaded
263      */
264     private Descriptor load(final StreamableResource resource) throws Exception {
265         final InputStream resourceAsStream = resourceAsStream(resource);
266         if (resourceAsStream == null) {
267             throw new IllegalArgumentException("Cannot load " + resource);
268         }
269         return new JDomBuilder().build(resourceAsStream);
270     }
271 }