KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > my > ListLogsAction


1 /*
2  * $Id: ListLogsAction.java,v 1.5 2005/01/17 21:35:45 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.my;
27
28 import java.io.File JavaDoc;
29 import java.io.FileFilter JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Comparator JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.commons.lang.StringUtils;
37
38 import com.j2biz.blogunity.BlogunityManager;
39 import com.j2biz.blogunity.dao.BlogDAO;
40 import com.j2biz.blogunity.exception.BlogunityException;
41 import com.j2biz.blogunity.i18n.I18N;
42 import com.j2biz.blogunity.i18n.I18NStatusFactory;
43 import com.j2biz.blogunity.pojo.Blog;
44 import com.j2biz.blogunity.web.ActionResultFactory;
45 import com.j2biz.blogunity.web.IActionResult;
46
47 /**
48  * @author michelson
49  * @version $$
50  * @since 0.1
51  *
52  *
53  */

54 public class ListLogsAction extends MyAbstractAction {
55
56     private static final IActionResult LOGS_FORWARD = ActionResultFactory
57             .buildForward("/jsp/my/listLogs.jsp");
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
63      * javax.servlet.http.HttpServletResponse)
64      */

65     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
66             throws BlogunityException {
67
68         String JavaDoc blogId = request.getParameter("id");
69
70         if (StringUtils.isEmpty(blogId))
71                 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ID_NOT_SETTED,
72                         "Blog"));
73
74         Blog blog = (new BlogDAO()).getBlogByID(new Long JavaDoc(blogId));
75
76         if (user.getId().longValue() != blog.getFounder().getId().longValue()
77                 && !user.isAdministrator()) { throw new BlogunityException(I18NStatusFactory
78                 .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION)); }
79
80         File JavaDoc blogsDir = BlogunityManager.getSystemConfiguration().getBlogsDirectory();
81         File JavaDoc blogLogsDir = new File JavaDoc(blogsDir, blog.getUrlName() + "/logs");
82
83         if (!blogLogsDir.exists()) {
84             // try to create empty "logs"-directory
85
boolean result = blogLogsDir.mkdir();
86             if (!result) { throw new BlogunityException(I18NStatusFactory.create(
87                     I18N.ERRORS.CREATE_DIRECTORY, blogLogsDir.getAbsolutePath())); }
88
89         } else if (!blogLogsDir.isDirectory() || !blogLogsDir.canRead() || !blogLogsDir.canWrite()) { throw new BlogunityException(
90                 I18NStatusFactory.create(I18N.ERRORS.READ_DIRECTORY, blogLogsDir.getAbsolutePath())); }
91
92         File JavaDoc[] logFiles = blogLogsDir.listFiles(new FileFilter JavaDoc() {
93             public boolean accept(File JavaDoc f) {
94                 String JavaDoc name = f.getName();
95                 if (f.isFile() && (name.endsWith(".log"))) return true;
96                 return false;
97             }
98         });
99
100         Arrays.sort(logFiles, new Comparator JavaDoc() {
101             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
102                 File JavaDoc f1 = (File JavaDoc) o1;
103                 File JavaDoc f2 = (File JavaDoc) o2;
104
105                 return (int) (f2.lastModified() - f1.lastModified());
106             }
107         });
108
109         request.setAttribute("blog", blog);
110         request.setAttribute("logFiles", logFiles);
111
112         navigationStack.push(ActionResultFactory.buildRedirect(I18N.MESSAGES.NAVI_LIST_LOGS,
113                 currentActionPath));
114
115         return LOGS_FORWARD;
116     }
117
118 }
Popular Tags