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.io.xpp3.ReportingXpp3Reader;
20 import org.apache.maven.archiva.reporting.model.io.xpp3.ReportingXpp3Writer;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.codehaus.plexus.logging.AbstractLogEnabled;
23 import org.codehaus.plexus.util.IOUtil;
24 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
27 import java.io.FileNotFoundException;
28 import java.io.FileReader;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.util.HashMap;
35 * Load and store the reports. No synchronization is used, but it is unnecessary as the old object
36 * can continue to be used.
38 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
40 * @todo support other implementations than XML file
43 public class DefaultReportingStore
44 extends AbstractLogEnabled
45 implements ReportingStore
48 * The cached reports for given repositories.
50 private Map/*<ArtifactRepository,ReportingDatabase>*/ reports = new HashMap();
52 public ReportingDatabase getReportsFromStore( ArtifactRepository repository )
53 throws ReportingStoreException
55 ReportingDatabase database = (ReportingDatabase) reports.get( repository );
57 if ( database == null )
59 ReportingXpp3Reader reader = new ReportingXpp3Reader();
61 File file = new File( repository.getBasedir(), "report-database.xml" );
63 FileReader fileReader = null;
66 fileReader = new FileReader( file );
68 catch ( FileNotFoundException e )
70 database = new ReportingDatabase();
73 if ( database == null )
75 getLogger().info( "Reading report database from " + file );
78 database = new ReportingDatabase( reader.read( fileReader, false ) );
80 catch ( IOException e )
82 throw new ReportingStoreException( e.getMessage(), e );
84 catch ( XmlPullParserException e )
86 throw new ReportingStoreException( e.getMessage(), e );
90 IOUtil.close( fileReader );
94 reports.put( repository, database );
99 public void storeReports( ReportingDatabase database, ArtifactRepository repository )
100 throws ReportingStoreException
102 ReportingXpp3Writer writer = new ReportingXpp3Writer();
104 File file = new File( repository.getBasedir(), "report-database.xml" );
105 getLogger().info( "Writing reports to " + file );
106 FileWriter fileWriter = null;
109 file.getParentFile().mkdirs();
111 fileWriter = new FileWriter( file );
112 writer.write( fileWriter, database.getReporting() );
114 catch ( IOException e )
116 throw new ReportingStoreException( e.getMessage(), e );
120 IOUtil.close( fileWriter );