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.Enumeration;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Properties;
31 import org.apache.archiva.rss.RssFeedEntry;
32 import org.apache.archiva.rss.RssFeedGenerator;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.maven.archiva.model.ArchivaArtifact;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Process new artifacts in the repository and generate RSS feeds.
41 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
43 * @plexus.component role="org.apache.archiva.rss.processor.RssFeedProcessor" role-hint="new-artifacts"
45 public class NewArtifactsRssFeedProcessor
46 implements RssFeedProcessor
48 public static final String NEW_ARTIFACTS_IN_REPO = "New Artifacts in Repository ";
50 public static final String NEW_VERSIONS_OF_ARTIFACT = "New Versions of Artifact ";
55 private RssFeedGenerator generator;
57 private Logger log = LoggerFactory.getLogger( NewArtifactsRssFeedProcessor.class );
60 * The hostname that will be used in the urls for the feed links.
62 private String host = "localhost";
65 * The port that will be used in the urls for the feed links.
67 private String port = "8080";
70 * Process the newly discovered artifacts in the repository. Generate feeds for new artifacts in the repository and
71 * new versions of artifact.
73 public void process( List<ArchivaArtifact> data )
75 log.debug( "Process new artifacts into rss feeds." );
77 if ( System.getProperty( "jetty.host" ) != null )
79 host = System.getProperty( "jetty.host" );
82 if ( System.getProperty( "jetty.port" ) != null )
84 port = System.getProperty( "jetty.port" );
87 processNewArtifactsInRepo( data );
88 processNewVersionsOfArtifact( data );
91 private void processNewArtifactsInRepo( List<ArchivaArtifact> data )
93 List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
94 String repoId = getRepoId( data );
97 new RssFeedEntry( NEW_ARTIFACTS_IN_REPO + "\'" + repoId + "\'" + " as of " +
98 Calendar.getInstance().getTime(), getBaseUrl() + "/archiva/rss/new_artifacts_" + repoId + ".xml" );
99 String description = "These are the new artifacts found in repository " + "\'" + repoId + "\'" + ": \n";
101 for ( ArchivaArtifact artifact : data )
103 description = description + artifact.toString() + " | ";
105 entry.setDescription( description );
106 entries.add( entry );
108 generateFeed( "new_artifacts_" + repoId + ".xml", NEW_ARTIFACTS_IN_REPO + "\'" + repoId + "\'",
109 getBaseUrl() + "/archiva/repository/rss/new_artifacts_" + repoId + ".xml",
110 "New artifacts found in repository " + "\'" + repoId + "\'" + " during repository scan.", entries );
113 private void processNewVersionsOfArtifact( List<ArchivaArtifact> data )
115 String repoId = getRepoId( data );
117 List<String> artifacts = new ArrayList<String>();
119 for ( ArchivaArtifact artifact : data )
121 artifacts.add( artifact.toString() );
124 Collections.sort( artifacts );
126 Map<String, String> artifactsMap = toMap( artifacts );
128 for ( String key : artifactsMap.keySet() )
130 List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
132 new RssFeedEntry( NEW_VERSIONS_OF_ARTIFACT + "\'" + key + "\'" + " as of " +
133 Calendar.getInstance().getTime(), getBaseUrl() + "/archiva/rss/new_versions_" + key + ".xml" );
136 "These are the new versions of artifact " + "\'" + key + "\'" + " in the repository: \n" +
137 ( (String) artifactsMap.get( key ) );
139 entry.setDescription( description );
140 entries.add( entry );
142 generateFeed( "new_versions_" + key + ".xml", NEW_VERSIONS_OF_ARTIFACT + "\'" + key + "\'",
143 getBaseUrl() + "/archiva/rss/new_versions_" + key + ".xml",
144 "New versions of artifact " + "\'" + key + "\' found in repository " + "\'" + repoId + "\'" +
145 " during repository scan.", entries );
149 private String getRepoId( List<ArchivaArtifact> data )
152 if ( !data.isEmpty() )
154 repoId = ( (ArchivaArtifact) data.get( 0 ) ).getModel().getRepositoryId();
160 private void generateFeed( String filename, String title, String link, String description,
161 List<RssFeedEntry> dataEntries )
163 generator.generateFeed( title, link, description, dataEntries, filename );
166 private Map<String, String> toMap( List<String> artifacts )
168 Map<String, String> artifactsMap = new HashMap<String, String>();
169 for ( String id : artifacts )
171 String key = StringUtils.substringBefore( id, ":" );
172 key = key + ":" + StringUtils.substringBefore( StringUtils.substringAfter( id, ":" ), ":" );
174 String value = (String) artifactsMap.get( key );
177 value = value + " | " + id;
183 artifactsMap.put( key, value );
189 public RssFeedGenerator getGenerator()
194 public void setGenerator( RssFeedGenerator generator )
196 this.generator = generator;
199 private String getBaseUrl()
201 String baseUrl = "http://" + host;
203 if( port != null && !"".equals( port ) )
205 baseUrl = baseUrl + ":" + port;