cos removed from the open posting system + misc. updates here and there
[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.Random;\r
36 \r
37 import mir.config.MirPropertiesConfiguration;\r
38 import mir.log.LoggerWrapper;\r
39 import mir.session.Request;\r
40 import mir.session.Session;\r
41 import mir.session.SessionHandler;\r
42 import mircoders.entity.EntityComment;\r
43 import mircoders.entity.EntityContent;\r
44 import mircoders.global.MirGlobal;\r
45 import mircoders.global.ProducerEngine;\r
46 import mircoders.localizer.MirLocalizerExc;\r
47 import mircoders.localizer.MirLocalizerFailure;\r
48 import mircoders.localizer.MirOpenPostingLocalizer;\r
49 \r
50 public class MirBasicOpenPostingLocalizer implements MirOpenPostingLocalizer {\r
51   private List afterContentProducerTasks;\r
52   private List afterCommentProducerTasks;\r
53   protected LoggerWrapper logger;\r
54   protected MirPropertiesConfiguration configuration;\r
55 \r
56 \r
57   public MirBasicOpenPostingLocalizer() throws MirLocalizerExc, MirLocalizerFailure {\r
58     logger = new LoggerWrapper("Localizer.Basic.OpenPosting");\r
59 \r
60     try {\r
61       configuration = MirPropertiesConfiguration.instance();\r
62     }\r
63     catch (Throwable e) {\r
64       throw new MirLocalizerFailure("Can't get configuration: " + e.getMessage(), e);\r
65     }\r
66 \r
67     try {\r
68       String contentProducers = MirGlobal.config().getString("Mir.Localizer.OpenPosting.ContentProducers");\r
69       String commentProducers = MirGlobal.config().getString("Mir.Localizer.OpenPosting.CommentProducers");\r
70 \r
71       afterContentProducerTasks = ProducerEngine.ProducerTask.parseProducerTaskList(contentProducers);\r
72       afterCommentProducerTasks = ProducerEngine.ProducerTask.parseProducerTaskList(commentProducers);\r
73     }\r
74     catch (Throwable t) {\r
75       logger.error("Setting up MirBasicOpenPostingLocalizer failed: " + t.getMessage());\r
76 \r
77       throw new MirLocalizerFailure(t);\r
78     }\r
79   }\r
80 \r
81   public SessionHandler getOpenSessionHandler(Request aRequest, Session aSession) throws MirLocalizerExc, MirLocalizerFailure {\r
82     if (aSession.getAttribute("handler")==null)\r
83       aSession.setAttribute("handler", new MirBasicCommentPostingSessionHandler());\r
84 \r
85     return (SessionHandler) aSession.getAttribute("handler");\r
86   }\r
87 \r
88   public void afterContentPosting() {\r
89     MirGlobal.producerEngine().addTasks(afterContentProducerTasks);\r
90   }\r
91 \r
92   public void afterContentPosting(EntityContent aContent) {\r
93     afterContentPosting();\r
94   }\r
95 \r
96   public void afterCommentPosting(EntityComment aComment) {\r
97     afterCommentPosting();\r
98   }\r
99 \r
100   public void afterCommentPosting() {\r
101     MirGlobal.producerEngine().addTasks(afterCommentProducerTasks);\r
102   }\r
103 \r
104   public String generateOnetimePassword() {\r
105     Random r = new Random();\r
106     int random = r.nextInt();\r
107 \r
108     long l = System.currentTimeMillis();\r
109 \r
110     l = (l*l*l*l)/random;\r
111     if (l<0)\r
112       l = l * -1;\r
113 \r
114     String returnString = ""+l;\r
115 \r
116     return returnString.substring(5);\r
117   }\r
118 \r
119 }\r