1 package org.apache.archiva.rss.processor;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
29 import org.apache.archiva.rss.RssFeedEntry;
30 import org.apache.archiva.rss.RssFeedGenerator;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.maven.archiva.model.ArchivaArtifact;
35 * Process new artifacts in the repository and generate RSS feeds.
37 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
39 * @plexus.component role="org.apache.archiva.rss.processor.RssFeedProcessor" role-hint="new-artifacts"
41 public class NewArtifactsRssFeedProcessor
42 implements RssFeedProcessor
44 public static final String NEW_ARTIFACTS_IN_REPO = "New Artifacts in Repository ";
46 public static final String NEW_VERSIONS_OF_ARTIFACT = "New Versions of Artifact ";
51 private RssFeedGenerator generator;
54 * Process the newly discovered artifacts in the repository. Generate feeds for new artifacts in the repository and
55 * new versions of artifact.
57 public void process( List<ArchivaArtifact> data )
59 processNewArtifactsInRepo( data );
60 processNewVersionsOfArtifact( data );
63 private void processNewArtifactsInRepo( List<ArchivaArtifact> data )
65 List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
66 String repoId = getRepoId( data );
69 new RssFeedEntry( NEW_ARTIFACTS_IN_REPO + "\'" + repoId + "\'" + " as of " +
70 Calendar.getInstance().getTime(), "http://localhost:8080/archiva/repository/" + repoId );
71 String description = "These are the new artifacts found in repository " + "\'" + repoId + "\'" + ": \n";
73 for ( ArchivaArtifact artifact : data )
75 description = description + artifact.toString() + "\n";
77 entry.setDescription( description );
80 generateFeed( "new_artifacts_" + repoId + ".xml", NEW_ARTIFACTS_IN_REPO + "\'" + repoId + "\'",
81 "http://localhost:8080/archiva/repository/" + repoId, "New artifacts found in repository " +
82 "\'" + repoId + "\'" + " during repository scan.", entries );
85 private void processNewVersionsOfArtifact( List<ArchivaArtifact> data )
87 String repoId = getRepoId( data );
89 List<String> artifacts = new ArrayList<String>();
91 for ( ArchivaArtifact artifact : data )
93 artifacts.add( artifact.toString() );
96 Collections.sort( artifacts );
98 Map<String, String> artifactsMap = toMap( artifacts );
100 for ( String key : artifactsMap.keySet() )
102 List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
103 String artifactPath = getArtifactPath( key );
105 new RssFeedEntry( NEW_VERSIONS_OF_ARTIFACT + "\'" + key + "\'" + " as of " +
106 Calendar.getInstance().getTime(), "http://localhost:8080/archiva/repository/" + repoId + "/" +
110 "These are the new versions of artifact " + "\'" + key + "\'" + " in the repository: \n" +
111 StringUtils.replace( ( (String) artifactsMap.get( key ) ), "|", "\n" );
113 entry.setDescription( description );
114 entries.add( entry );
116 generateFeed( "new_versions_" + repoId + "_" + key + ".xml", NEW_VERSIONS_OF_ARTIFACT + "\'" + key + "\'",
117 "http://localhost:8080/archiva/repository/" + repoId + "/" + artifactPath,
118 "New versions of artifact " + "\'" + key + "\' found in repository " + "\'" + repoId + "\'" +
119 " during repository scan.", entries );
123 private String getRepoId( List<ArchivaArtifact> data )
126 if ( !data.isEmpty() )
128 repoId = ( (ArchivaArtifact) data.get( 0 ) ).getModel().getRepositoryId();
134 private void generateFeed( String filename, String title, String link, String description,
135 List<RssFeedEntry> dataEntries )
137 generator.generateFeed( title, link, description, dataEntries, filename );
140 private Map<String, String> toMap( List<String> artifacts )
142 Map<String, String> artifactsMap = new HashMap<String, String>();
143 for ( String id : artifacts )
145 String key = StringUtils.substringBefore( id, ":" );
146 key = key + ":" + StringUtils.substringBefore( StringUtils.substringAfter( id, ":" ), ":" );
148 String value = (String) artifactsMap.get( key );
151 value = value + "|" + id;
157 artifactsMap.put( key, value );
163 private String getArtifactPath( String key )
165 return StringUtils.replace( StringUtils.replace( key, ".", "/" ), ":", "/" );
168 public RssFeedGenerator getGenerator()
173 public void setGenerator( RssFeedGenerator generator )
175 this.generator = generator;