View Javadoc

1   package org.apache.velocity.site.news.model;
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.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.velocity.site.news.VelocityNewsException;
29  import org.apache.velocity.site.news.model.io.xpp3.NewsXpp3Reader;
30  import org.codehaus.plexus.logging.LogEnabled;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  
34  /**
35   * This is the container that keeps the news model for us.
36   *
37   * @plexus.component role="org.apache.velocity.site.news.model.NewsManager"
38   */
39  public class NewsManager implements LogEnabled
40  {
41  
42      public static final String ROLE = NewsManager.class.getName();
43  
44      public static final String NEWS_FILE_NAME_METHOD = "getNewsFileName";
45  
46      public static final String NEWS_SITE_DIRECTORY_METHOD = "getSiteDirectory";
47  
48      public static final String NEWS_MODEL_METHOD = "getNewsModel";
49  
50      private NewsModel newsModel = null;
51  
52      private Logger logger = null;
53  
54      private String newsFileName = null;
55  
56      private File siteDirectory = null;
57  
58      /**
59       * Plexus lifecycle.
60       */
61      public void enableLogging(final Logger logger)
62      {
63          this.logger = logger;
64      }
65  
66      public void load(final File siteDirectory, final String newsFileName) throws VelocityNewsException
67      {
68  
69          if (StringUtils.isEmpty(newsFileName))
70          {
71              throw new IllegalArgumentException("Filename can not be empty!");
72          }
73  
74          setNewsFileName(newsFileName);
75          setSiteDirectory(siteDirectory);
76  
77          File newsFile = new File(siteDirectory, newsFileName + ".xml");
78  
79          if (newsFile.exists() && newsFile.canRead())
80          {
81  
82              FileInputStream fis = null;
83              InputStreamReader isr = null;
84  
85              try
86              {
87                  fis = new FileInputStream(newsFile);
88                  isr = new InputStreamReader(fis);
89  
90                  newsModel = new NewsXpp3Reader().read(isr);
91              }
92              catch (XmlPullParserException xppe)
93              {
94                  throw new VelocityNewsException("While parsing news file: ", xppe);
95              }
96              catch (IOException ioe)
97              {
98                  throw new VelocityNewsException("While reading news file: ", ioe);
99              }
100             finally
101             {
102                 if (fis != null)
103                 {
104                     try
105                     {
106                         fis.close();
107                     }
108                     catch (IOException ioe)
109                     {
110                         logger.error("While closing news stream: ", ioe);
111                     }
112                 }
113                 if (isr != null)
114                 {
115                     try
116                     {
117                         isr.close();
118                     }
119                     catch (IOException ioe)
120                     {
121                         logger.error("While closing news reader: ", ioe);
122                     }
123                 }
124             }
125         }
126 
127         setNewsModel(newsModel);
128     }
129 
130     protected void setNewsFileName(final String newsFileName)
131     {
132         this.newsFileName = newsFileName;
133     }
134 
135     public String getNewsFileName()
136     {
137         return newsFileName;
138     }
139 
140     public File getSiteDirectory()
141     {
142         return this.siteDirectory;
143     }
144 
145     public void setSiteDirectory(final File siteDirectory)
146     {
147         this.siteDirectory = siteDirectory;
148     }
149 
150     public NewsModel getNewsModel()
151     {
152         return this.newsModel;
153     }
154 
155     protected void setNewsModel(final NewsModel newsModel)
156     {
157         this.newsModel = newsModel;
158     }
159 }