f4b5d5c7185bbf194c4cb379bb1f730b9f907a74
[mir.git] / source / mircoders / localizer / basic / MirBasicOpenPostingLocalizer.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mircoders.localizer.basic;\r
33 \r
34 import java.util.List;\r
35 import java.util.Locale;\r
36 import java.util.*;\r
37 import javax.servlet.http.HttpServletRequest;\r
38 \r
39 import mir.log.LoggerWrapper;\r
40 import mir.servlet.*;\r
41 import mir.config.*;\r
42 import mir.session.Request;\r
43 import mir.session.Response;\r
44 import mir.session.*;\r
45 \r
46 import mircoders.entity.EntityComment;\r
47 import mircoders.entity.EntityContent;\r
48 import mircoders.global.MirGlobal;\r
49 import mircoders.global.ProducerEngine;\r
50 import mircoders.localizer.*;\r
51 \r
52 public class MirBasicOpenPostingLocalizer implements MirOpenPostingLocalizer {\r
53   private List afterContentProducerTasks;\r
54   private List afterCommentProducerTasks;\r
55   protected LoggerWrapper logger;\r
56   protected MirPropertiesConfiguration configuration;\r
57 \r
58 \r
59   public MirBasicOpenPostingLocalizer() throws MirLocalizerExc, MirLocalizerFailure {\r
60     logger = new LoggerWrapper("Localizer.Basic.OpenPosting");\r
61 \r
62     try {\r
63       configuration = MirPropertiesConfiguration.instance();\r
64     }\r
65     catch (Throwable e) {\r
66       throw new MirLocalizerFailure("Can't get configuration: " + e.getMessage(), e);\r
67     }\r
68 \r
69     try {\r
70       String contentProducers = MirGlobal.config().getString("Mir.Localizer.OpenPosting.ContentProducers");\r
71       String commentProducers = MirGlobal.config().getString("Mir.Localizer.OpenPosting.CommentProducers");\r
72 \r
73       afterContentProducerTasks = ProducerEngine.ProducerTask.parseProducerTaskList(contentProducers);\r
74       afterCommentProducerTasks = ProducerEngine.ProducerTask.parseProducerTaskList(commentProducers);\r
75     }\r
76     catch (Throwable t) {\r
77       logger.error("Setting up MirBasicOpenPostingLocalizer failed: " + t.getMessage());\r
78 \r
79       throw new MirLocalizerFailure(t);\r
80     }\r
81   }\r
82 \r
83   public SessionHandler getOpenSessionHandler(Request aRequest, Session aSession) throws MirLocalizerExc, MirLocalizerFailure {\r
84     if (aSession.getAttribute("handler")==null)\r
85       aSession.setAttribute("handler", new MirBasicCommentPostingSessionHandler());\r
86 \r
87     return (SessionHandler) aSession.getAttribute("handler");\r
88   }\r
89 \r
90   public void afterContentPosting() {\r
91     MirGlobal.producerEngine().addTasks(afterContentProducerTasks);\r
92   }\r
93 \r
94   public void afterContentPosting(EntityContent aContent) {\r
95     afterContentPosting();\r
96   }\r
97 \r
98   public void afterCommentPosting(EntityComment aComment) {\r
99     afterCommentPosting();\r
100   }\r
101 \r
102   public void afterCommentPosting() {\r
103     MirGlobal.producerEngine().addTasks(afterCommentProducerTasks);\r
104   }\r
105 \r
106   public String generateOnetimePassword() {\r
107     Random r = new Random();\r
108     int random = r.nextInt();\r
109 \r
110     long l = System.currentTimeMillis();\r
111 \r
112     l = (l*l*l*l)/random;\r
113     if (l<0)\r
114       l = l * -1;\r
115 \r
116     String returnString = ""+l;\r
117 \r
118     return returnString.substring(5);\r
119   }\r
120 \r
121 }\r