View Javadoc

1   package net.sf.mavenhist;
2   
3   import java.io.FileNotFoundException;
4   import java.util.ArrayList;
5   
6   import net.sf.mavenhist.extractor.IValueExtractor;
7   import net.sf.mavenhist.extractor.MultiMetric;
8   import net.sf.mavenhist.extractor.util.DocumentReader;
9   import net.sf.mavenhist.extractor.util.MultimetricArrayToStringUtil;
10  import net.sf.mavenhist.maven1.extractormapping.ExtractorInfo;
11  import net.sf.mavenhist.maven1.extractormapping.ExtractorPathMappingFactory;
12  import net.sf.mavenhist.persistency.IPersistency;
13  import net.sf.mavenhist.persistency.Metric;
14  import net.sf.mavenhist.persistency.StringMetric;
15  import net.sf.mavenhist.util.ParamCheckUtil;
16  
17  import org.apache.commons.lang.StringUtils;
18  import org.apache.log4j.Logger;
19  
20  /**
21   * Base class to extract the value from specified reports with extractors. To be able to work this
22   * class must be filled with setter methods before the use. If <code>storeAllValues</code> is called
23   * before all settings are set. An exception is thrown.
24   */
25  public class HistorisationBase {
26    private String persistencyClass;
27    private IPersistency persistency;
28    private String[] extractors = new String[0];
29    private String mavenBuildDir;
30    private String mavenDocsDest;
31    private String date;
32    private String groupId;
33    private String artifactId;
34    private String buildNumber;
35    
36    /**
37     * Extracts and stores the results of all extractors given in the <code>setExtractorNames</code>
38     * method.
39     *
40     * @throws Exception will be thrown if not all needed parameters are set before calling this
41     * method.
42     */
43    public void storeAllValues() throws Exception {
44      // will throw exception when not all needed parameters are set.
45      isNecessaryParamSet();
46      ExtractorPathMappingFactory factory =
47        new ExtractorPathMappingFactory(getMavenBuildDir(), getMavenDocsDest());
48      ArrayList extractedMetrics = new ArrayList(extractors.length);
49      
50      IPersistency persistable = getCurrentPersistency();
51      
52      for (int i = 0; i < extractors.length; i++) {
53        ExtractorInfo extractorInfo = factory.getExtractorMapping(extractors[i]);
54        
55        if (extractorInfo == null) {
56          // probably the user mistyped a extractorname.
57          Logger.getLogger(this.getClass()).error(
58              "The specified extractor is not available: " + extractors[i]);
59        } else {
60          IValueExtractor extractor = extractorInfo.getExtractor();
61          try {
62            MultiMetric[] result = extractor.extract(
63                DocumentReader.getInputStream(extractorInfo.getPath()));
64            Logger.getLogger(this.getClass()).info(
65                "Extractor: " + extractor.getName() + " got " + result.length
66                + " results with the values: " + MultimetricArrayToStringUtil.toString(result));
67            extractedMetrics.addAll(getMetricsList(
68                extractor.getName(),
69                result,
70                getDate(),
71                getGroupId(),
72                getArtifactId(),
73                getBuildNumber()));
74          } catch (FileNotFoundException e) {
75            Logger.getLogger(this.getClass()).warn(
76                "The specified file is not available: " + extractorInfo.getPath(), e);
77          }
78        }
79      }
80      persistable.storeMetrics((Metric[]) extractedMetrics.toArray(new Metric[0]));
81    }
82    
83    /**
84     * Initialises the persistency layer. This is needed before any data are stored in the db.
85     * 
86     * @throws Exception if something failed.
87     */
88    public void initialisePersistency() throws Exception {
89      getPersistency().initializeMetricsDataBase();
90    }
91    
92    /**
93     * Creates the persistency class from the given persistency string. Or, if it is set, uses just
94     * the given persistency object.
95     * 
96     * @return persitable class.
97     * @throws ClassNotFoundException May happen on instantiation of the persistency class.
98     * @throws InstantiationException May happen on instantiation of the persistency class.
99     * @throws IllegalAccessException May happen on instantiation of the persistency class.
100    */
101   private IPersistency getCurrentPersistency() 
102   throws ClassNotFoundException, InstantiationException, IllegalAccessException {
103     IPersistency persistable = getPersistency();
104     if (persistable == null) {
105       Class clazz = Class.forName(getPersistencyClass());
106       persistable = (IPersistency) clazz.newInstance();
107       setPersistency(persistable);
108     }
109     return persistable;    
110   }
111   
112   /**
113    * Checks that the necessairy parameters are set in the {@link HistorisationBase} that it can
114    * store values successful. Throws <code>IllegalStateException</code> when a parameter is not set.
115    *
116    * @return are all needed parameter set.
117    */
118   private boolean isNecessaryParamSet() {
119     StringBuffer messageBuffer = new StringBuffer();
120     boolean paramsOk = ParamCheckUtil.checkParam(messageBuffer, getArtifactId(), "ArtifactId")
121     & ParamCheckUtil.checkParam(messageBuffer, getGroupId(), "GroupId")
122     & ParamCheckUtil.checkParam(messageBuffer, getMavenBuildDir(), "MavenBuildDir")
123     & ParamCheckUtil.checkParam(messageBuffer, getMavenDocsDest(), "MavenDocsDest")
124     & ParamCheckUtil.checkParamOr(
125         messageBuffer,
126         getPersistencyClass(),
127         "PersistencyClassName",
128         getPersistency(),
129     "Persistency")
130     & ParamCheckUtil.checkParam(messageBuffer, extractors, "Extractors");
131     if (!paramsOk) {
132       throw new IllegalStateException(messageBuffer.toString());
133     }
134     return true;
135   }
136   
137   /**
138    * Wraps the {@link MultiMetric} entities in simple {@link Metric} objects and adds the
139    * necessairy data in these objects.
140    * 
141    * @param extractorName for the metric.
142    * @param multiMetrics that came from the extractor.
143    * @param date when the metric was created.
144    * @param groupId of the measured project.
145    * @param artifactId of the project.
146    * @param buildNumber of the project.
147    * @return a list of {@link StringMetric}.
148    */
149   private ArrayList getMetricsList(
150       String extractorName,
151       MultiMetric[] multiMetrics,
152       String date,
153       String groupId,
154       String artifactId,
155       String buildNumber) {
156     ArrayList metrics = new ArrayList(multiMetrics.length);
157     for (int i = 0; i < multiMetrics.length; i++) {
158       metrics.add(new StringMetric(
159           extractorName,
160           multiMetrics[i].getValue(),
161           date,
162           groupId,
163           artifactId,
164           multiMetrics[i].getLocation(),
165           buildNumber));
166     }
167     return metrics;
168   }
169 
170   /**
171    * @return the persistencyClass
172    */
173   public String getPersistencyClass() {
174     return persistencyClass;
175   }
176   
177   /**
178    * @param persistencyClass the persistencyClass to set
179    */
180   public void setPersistencyClass(String persistencyClass) {
181     this.persistencyClass = persistencyClass;
182   }
183   
184   /**
185    * Sets a comma-separated list of extractors.
186    * @param extractorNames comma-separated list.
187    */
188   public void setExtractorNames(String extractorNames) {
189     if (extractorNames != null) {
190       extractors = StringUtils.split(extractorNames, ',');
191     }
192   }
193   
194 
195   /**
196    * @return the mavenBuildDir
197    */
198   public String getMavenBuildDir() {
199     return mavenBuildDir;
200   }
201   /**
202    * @param mavenBuildDir the mavenBuildDir to set
203    */
204   public void setMavenBuildDir(String mavenBuildDir) {
205     this.mavenBuildDir = mavenBuildDir;
206   }
207   /**
208    * @return the artifactId
209    */
210   public String getArtifactId() {
211     return artifactId;
212   }
213   /**
214    * @param artifactId the artifactId to set
215    */
216   public void setArtifactId(String artifactId) {
217     this.artifactId = artifactId;
218   }
219   /**
220    * @return the buildNumber
221    */
222   public String getBuildNumber() {
223     return buildNumber;
224   }
225   /**
226    * @param buildNumber the buildNumber to set
227    */
228   public void setBuildNumber(String buildNumber) {
229     this.buildNumber = buildNumber;
230   }
231   /**
232    * @return the date
233    */
234   public String getDate() {
235     return date;
236   }
237   /**
238    * @param date the date to set
239    */
240   public void setDate(String date) {
241     this.date = date;
242   }
243   /**
244    * @return the groupId
245    */
246   public String getGroupId() {
247     return groupId;
248   }
249   /**
250    * @param groupId the groupId to set
251    */
252   public void setGroupId(String groupId) {
253     this.groupId = groupId;
254   }
255 
256   /**
257    * @return the persistency
258    */
259   public IPersistency getPersistency() {
260     return persistency;
261   }
262 
263   /**
264    * @param persistency the persistency to set
265    */
266   public void setPersistency(IPersistency persistency) {
267     this.persistency = persistency;
268   }
269 
270   /**
271    * @return the mavenDocsDest
272    */
273   public String getMavenDocsDest() {
274     return mavenDocsDest;
275   }
276 
277   /**
278    * @param mavenDocsDest the mavenDocsDest to set
279    */
280   public void setMavenDocsDest(String mavenDocsDest) {
281     this.mavenDocsDest = mavenDocsDest;
282   }
283 }