1 package org.apache.maven.archiva.reporting;
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.artifact.repository.ArtifactRepository;
23 import org.codehaus.plexus.logging.AbstractLogEnabled;
24 import org.codehaus.plexus.util.IOUtil;
25 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28 import java.io.FileNotFoundException;
29 import java.io.FileReader;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.util.HashMap;
36 * Load and store the reports. No synchronization is used, but it is unnecessary as the old object
37 * can continue to be used.
39 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
41 * @todo support other implementations than XML file
44 public class DefaultReportingStore
45 extends AbstractLogEnabled
46 implements ReportingStore
49 * The cached reports for given repositories.
51 private Map/*<String,ReportingDatabase>*/ reports = new HashMap();
53 public ReportingDatabase getReportsFromStore( ArtifactRepository repository, ReportGroup reportGroup )
54 throws ReportingStoreException
56 String key = getKey( repository, reportGroup );
57 ReportingDatabase database = (ReportingDatabase) reports.get( key );
59 if ( database == null )
61 ReportingXpp3Reader reader = new ReportingXpp3Reader();
63 File file = getReportFilename( repository, reportGroup );
65 FileReader fileReader = null;
68 fileReader = new FileReader( file );
70 catch ( FileNotFoundException e )
72 database = new ReportingDatabase( reportGroup, repository );
75 if ( database == null )
77 getLogger().info( "Reading report database from " + file );
80 Reporting reporting = reader.read( fileReader, false );
81 database = new ReportingDatabase( reportGroup, reporting, repository );
83 catch ( IOException e )
85 throw new ReportingStoreException( e.getMessage(), e );
87 catch ( XmlPullParserException e )
89 throw new ReportingStoreException( e.getMessage(), e );
93 IOUtil.close( fileReader );
97 reports.put( key, database );
102 private static String getKey( ArtifactRepository repository, ReportGroup reportGroup )
104 return repository.getId() + "/" + reportGroup.getFilename();
107 private static File getReportFilename( ArtifactRepository repository, ReportGroup reportGroup )
109 return new File( repository.getBasedir(), ".reports/" + reportGroup.getFilename() );
112 public void storeReports( ReportingDatabase database, ArtifactRepository repository )
113 throws ReportingStoreException
115 database.updateTimings();
117 ReportingXpp3Writer writer = new ReportingXpp3Writer();
119 File file = getReportFilename( repository, database.getReportGroup() );
120 getLogger().info( "Writing reports to " + file );
121 FileWriter fileWriter = null;
124 file.getParentFile().mkdirs();
126 fileWriter = new FileWriter( file );
127 writer.write( fileWriter, database.getReporting() );
129 catch ( IOException e )
131 throw new ReportingStoreException( e.getMessage(), e );
135 IOUtil.close( fileWriter );