1 package org.apache.maven.archiva.reporting.store;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.archiva.reporting.model.Reporting;
20 import org.apache.maven.archiva.reporting.model.io.xpp3.ReportingXpp3Reader;
21 import org.apache.maven.archiva.reporting.model.io.xpp3.ReportingXpp3Writer;
22 import org.apache.maven.archiva.reporting.group.ReportGroup;
23 import org.apache.maven.archiva.reporting.store.ReportingStore;
24 import org.apache.maven.archiva.reporting.store.ReportingStoreException;
25 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.codehaus.plexus.logging.AbstractLogEnabled;
28 import org.apache.commons.io.IOUtils;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32 import java.io.FileNotFoundException;
33 import java.io.FileReader;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.util.HashMap;
40 * Load and store the reports. No synchronization is used, but it is unnecessary as the old object
41 * can continue to be used.
43 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
44 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
45 * @todo support other implementations than XML file
48 public class DefaultReportingStore
49 extends AbstractLogEnabled
50 implements ReportingStore
53 * The cached reports for given repositories.
55 private Map/*<String,ReportingDatabase>*/ reports = new HashMap();
57 public ReportingDatabase getReportsFromStore( ArtifactRepository repository, ReportGroup reportGroup )
58 throws ReportingStoreException
60 String key = getKey( repository, reportGroup );
61 ReportingDatabase database = (ReportingDatabase) reports.get( key );
63 if ( database == null )
65 ReportingXpp3Reader reader = new ReportingXpp3Reader();
67 File file = getReportFilename( repository, reportGroup );
69 FileReader fileReader = null;
72 fileReader = new FileReader( file );
74 catch ( FileNotFoundException e )
76 database = new ReportingDatabase( reportGroup, repository );
79 if ( database == null )
81 getLogger().info( "Reading report database from " + file );
84 Reporting reporting = reader.read( fileReader, false );
85 database = new ReportingDatabase( reportGroup, reporting, repository );
87 catch ( IOException e )
89 throw new ReportingStoreException( e.getMessage(), e );
91 catch ( XmlPullParserException e )
93 throw new ReportingStoreException( e.getMessage(), e );
97 IOUtils.closeQuietly( fileReader );
101 reports.put( key, database );
106 private static String getKey( ArtifactRepository repository, ReportGroup reportGroup )
108 return repository.getId() + "/" + reportGroup.getFilename();
111 private static File getReportFilename( ArtifactRepository repository, ReportGroup reportGroup )
113 return new File( repository.getBasedir(), ".reports/" + reportGroup.getFilename() );
116 public void storeReports( ReportingDatabase database, ArtifactRepository repository )
117 throws ReportingStoreException
119 database.updateTimings();
121 ReportingXpp3Writer writer = new ReportingXpp3Writer();
123 File file = getReportFilename( repository, database.getReportGroup() );
124 getLogger().info( "Writing reports to " + file );
125 FileWriter fileWriter = null;
128 file.getParentFile().mkdirs();
130 fileWriter = new FileWriter( file );
131 writer.write( fileWriter, database.getReporting() );
133 catch ( IOException e )
135 throw new ReportingStoreException( e.getMessage(), e );
139 IOUtils.closeQuietly( fileWriter );