View Javadoc

1   package org.apache.velocity.site.news.plugin;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.lang.reflect.Method;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.ResourceBundle;
28  
29  import org.apache.maven.doxia.parser.Parser;
30  import org.apache.maven.doxia.siterenderer.Renderer;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.reporting.AbstractMavenReport;
33  import org.apache.maven.reporting.MavenReport;
34  import org.apache.maven.reporting.MavenReportException;
35  import org.apache.velocity.site.news.NewsReport;
36  import org.apache.velocity.site.news.VelocityNewsException;
37  import org.apache.velocity.site.news.VelocityNewsUtils;
38  import org.apache.velocity.site.news.model.Item;
39  import org.apache.velocity.site.news.model.NewsManager;
40  import org.apache.velocity.site.news.model.NewsModel;
41  import org.apache.velocity.site.plexus.PlexusKludgeException;
42  import org.apache.velocity.site.plexus.PlexusKludgeUtils;
43  import org.codehaus.plexus.PlexusConstants;
44  import org.codehaus.plexus.PlexusContainer;
45  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
46  import org.codehaus.plexus.context.Context;
47  import org.codehaus.plexus.context.ContextException;
48  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
49  
50  /**
51   * Report news items for the site.
52   *
53   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
54   * @version $Revision: 526767 $
55   *
56   * @goal news
57   */
58  public class VelocityNewsReport extends AbstractMavenReport implements MavenReport, NewsReport, NewsFeed, Contextualizable
59  {
60  
61      /**
62       * @component
63       * @required
64       * @readonly
65       */
66      private Renderer siteRenderer;
67  
68      /**
69       * @component role-hint="apt"
70       * @required
71       * @readonly
72       */
73      private Parser aptParser;
74  
75      /**
76       * Specifies the directory where the report will be generated
77       *
78       * @parameter default-value="${project.reporting.outputDirectory}"
79       * @required
80       */
81      private File outputDirectory;
82  
83      /**
84       * The Maven Project Object
85       *
86       * @parameter default-value="${project}"
87       * @required
88       * @readonly
89       */
90      private MavenProject project;
91  
92      /**
93       * Configuration of the base URL of the feed
94       *
95       * @parameter expression="${baseUrl}"
96       * @required
97       */
98      private String baseUrl;
99  
100     protected PlexusContainer container = null;
101 
102     public void contextualize(final Context context) throws ContextException
103     {
104         this.container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY);
105     }
106 
107     /**
108      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
109      */
110     public String getOutputDirectory()
111     {
112         return outputDirectory.getAbsolutePath();
113     }
114 
115     /**
116      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
117      */
118     public MavenProject getProject()
119     {
120         return project;
121     }
122 
123     /**
124      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
125      */
126     protected Renderer getSiteRenderer()
127     {
128         return siteRenderer;
129     }
130 
131     /**
132      * @see org.apache.maven.reporting.MavenReport#getOutputName()
133      */
134     public String getOutputName()
135     {
136         try
137         {
138             Object newsManager = getNewsManager();
139             Method newsFileNameMethod = newsManager.getClass().getMethod(NewsManager.NEWS_FILE_NAME_METHOD, new Class[] {});
140             return (String) newsFileNameMethod.invoke(newsManager, new Object[] {});
141         }
142         catch (RuntimeException re)
143         {
144             throw re;
145         }
146         catch (Exception e)
147         {
148             throw new IllegalStateException("Could not retrieve page name from model!");
149         }
150     }
151 
152     public ResourceBundle getBundle(final Locale locale)
153     {
154         return ResourceBundle.getBundle("velocity-news-report", locale, VelocityNewsReport.class.getClassLoader());
155     }
156 
157     /**
158      * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
159      */
160     public String getDescription(final Locale locale)
161     {
162         return getBundle(locale).getString("report.news.description");
163     }
164 
165     /**
166      * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
167      */
168     public String getName(final Locale locale)
169     {
170         return getBundle(locale).getString("report.news.name");
171     }
172 
173     public String getBaseUrl()
174     {
175         return baseUrl;
176     }
177 
178     public Parser getAptParser()
179     {
180         return aptParser;
181     }
182 
183     protected void executeReport(final Locale locale) throws MavenReportException
184     {
185 
186         try
187         {
188 
189             Object newsManager = getNewsManager();
190             Method newsModelMethod = newsManager.getClass().getMethod(NewsManager.NEWS_MODEL_METHOD, new Class[] {});
191             NewsModel newsModel = (NewsModel) newsModelMethod.invoke(newsManager, new Object[] {});
192 
193             if (newsModel != null)
194             {
195                 generateNewsPage(newsModel, locale);
196                 generateNewsRssFeed(newsModel);
197             }
198 
199         }
200         catch (RuntimeException re)
201         {
202             throw re;
203         }
204         catch (Exception e)
205         {
206             throw new MavenReportException("While generating report: ", e);
207         }
208     }
209 
210     private void generateNewsPage(final NewsModel newsModel, final Locale locale) throws VelocityNewsException
211     {
212 
213         NewsPageGenerator newsPageGenerator = new NewsPageGenerator(this, locale);
214 
215         newsPageGenerator.openPage();
216         newsPageGenerator.displayHeader();
217 
218         List items = VelocityNewsUtils.sortItemsByReverseDate(newsModel.getItems());
219 
220         for (Iterator it = items.iterator(); it.hasNext();)
221         {
222             Item newsItem = (Item) it.next();
223             newsPageGenerator.displayItem(newsItem);
224         }
225 
226         newsPageGenerator.closePage();
227     }
228 
229     private void generateNewsRssFeed(final NewsModel newsModel) throws VelocityNewsException
230     {
231         String outputDirectory = getOutputDirectory();
232 
233         if (outputDirectory == null)
234         {
235             throw new VelocityNewsException("No output directory specified!");
236         }
237 
238         NewsFeedGenerator newsFeedGenerator = new NewsFeedGenerator(this);
239 
240         newsFeedGenerator.openFeed();
241 
242         List items = VelocityNewsUtils.sortItemsByReverseDate(newsModel.getItems());
243 
244         for (Iterator it = items.iterator(); it.hasNext();)
245         {
246             Item newsItem = (Item) it.next();
247             newsFeedGenerator.publishItem(newsItem);
248         }
249 
250         newsFeedGenerator.publishFeed();
251     }
252 
253     /**
254      * Returns the News Manager.
255      *
256      * It must be injected into an instance of this component.
257      *
258      * @return A {@link DoxiaVelocityContextFactory} instance that generates
259      *         {@link VelocityContext} objects for the render process.
260      */
261     private Object getNewsManager() throws ComponentLookupException
262     {
263         try
264         {
265             PlexusContainer velocityContainer = PlexusKludgeUtils.lookupContainer(container,
266                     VelocityNewsPlugin.CONTAINER_LOOKUP_NAME);
267             return velocityContainer.lookup(NewsManager.ROLE);
268         }
269         catch (PlexusKludgeException pke)
270         {
271             throw new ComponentLookupException("While retrieving News Manager: ", pke);
272         }
273     }
274 }