KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > servlet > GeiServlet


1
2  /*
3  -- GeiNuke --
4 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
5  http://www.hostingjava.it/-geinuke/
6
7 This file is part of GeiNuke.
8
9     GeiNuke is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     GeiNuke is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with GeiNuke; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */

23 package com.geinuke.servlet;
24
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Locale JavaDoc;
28 import javax.servlet.ServletConfig JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33 import org.apache.velocity.context.Context;
34 import org.apache.velocity.tools.generic.RenderTool;
35
36 import com.geinuke.bizlogic.BLException;
37 import com.geinuke.common.GlobalCFG;
38 import com.geinuke.common.GlobalConfigurationI;
39 import com.geinuke.common.UserI;
40 import com.geinuke.middle.IUserBL;
41 import com.geinuke.middle.MiddleFactory;
42
43 import com.geinuke.util.NukeResource;
44 import com.geinuke.util.log.*;
45 import com.magic.servlet.ControllerMagicServlet;
46 import com.magic.servlet.action.MagicAction;
47 import com.magic.util.conf.MagicConfiguratorIfc;
48 import com.magic.util.props.MultiProps;
49
50
51 public class GeiServlet extends ControllerMagicServlet{
52     
53     protected GlobalConfigurationI gc=null;
54     protected static String JavaDoc nukePath=null;
55     protected static ILogger logger=null;
56     public static GeiServlet se=null;
57     
58     public static void home(HttpServletResponse JavaDoc res) throws Exception JavaDoc{
59         res.sendRedirect("geinuke.jhtm");
60     }
61     
62     
63     public static void fill(Context ctx){
64         
65                 
66         MultiProps res=null;
67         Locale JavaDoc lo=(Locale JavaDoc)ctx.get("LOCALE");
68         try{
69             res=GeiServlet.loadResource(lo.getLanguage());
70             String JavaDoc key=null;
71             String JavaDoc value=null;
72             Enumeration JavaDoc keys=res.keys();
73             while(keys.hasMoreElements()){
74                 key=(String JavaDoc)keys.nextElement();
75                 value=(String JavaDoc)res.get(key);
76                 //System.out.println("GeiServlet.fill(...), key "+key+" value "+value);
77
ctx.put(key,value);
78             }
79         }catch(Exception JavaDoc e){
80             GeiServlet.intLog(ILogger.CRITICAL,"GeiServlet.fill(...), EXCEPTION "+e);
81             //e.printStackTrace();
82
}
83         
84     }
85     
86     
87     public static MultiProps loadResource(String JavaDoc locale)throws Exception JavaDoc{
88         MultiProps res=null;
89         String JavaDoc ePath="";
90         String JavaDoc ext="";
91         if(locale!=null && !locale.equals("")){
92             ePath="/i18n";
93             ext="_"+locale;
94         }
95         MagicConfiguratorIfc mc=(MagicConfiguratorIfc)Class.forName("com.magic.util.conf.PropsFileConfigurator").newInstance();
96         String JavaDoc fn=null;
97         GeiServlet.intLog(ILogger.DEBUG,"GeiServlet.loadResource(..), A");
98         fn=GeiServlet.getNukePath()+"/WEB-INF/templates"+ePath+"/resources"+ext+".txt";
99         GeiServlet.intLog(ILogger.DEBUG,"GeiServlet.loadResource(..), file="+fn);
100         //System.out.println("GeiServlet.loadResource(...), A resource="+fn);
101
if(! NukeResource.existsFile(fn)){
102             GeiServlet.intLog(ILogger.DEBUG,"GeiServlet.loadResource(..), C");
103             fn=GeiServlet.getNukePath()+"/WEB-INF/templates/resources.txt";
104             //System.out.println("GeiServlet.loadResource(...), b resource="+fn);
105
}
106         mc.setFileName(fn);
107         res=mc.getConfiguration();
108          
109         return res;
110     }
111     
112     
113     protected String JavaDoc getModuleByURL(HttpServletRequest JavaDoc req){
114         String JavaDoc res=null;
115         
116         res=req.getRequestURI();
117         if(res.startsWith("/")){
118             res=res.substring(1);
119             int ind=res.indexOf("/");
120             res=res.substring(ind+1);
121         }
122         String JavaDoc ex=null;
123         ex=res.substring(res.lastIndexOf("."),res.length());
124         res=res.substring(0,res.lastIndexOf(ex));
125         return res;
126     }
127     
128     public static Object JavaDoc getBL(String JavaDoc blName){
129         
130         /*
131         if(blName.equals("IBlockBL")){
132             return BlockBL.singleton();
133         }else if(blName.equals("IBlogBL")){
134             return BlogBL.singleton();
135         }else if(blName.equals("ICategoryBL")){
136             return CategoryBL.singleton();
137         }else if(blName.equals("ICommentBL")){
138             return CommentBL.singleton();
139         }else if(blName.equals("IForumBL")){
140             return ForumBL.singleton();
141         }else if(blName.equals("IModuleBL")){
142             return ModuleBL.singleton();
143         }else if(blName.equals("INewsBL")){
144             return NewsBL.singleton();
145         }else if(blName.equals("IPortalCFGBL")){
146             return PortalCFGBL.singleton();
147         }else if(blName.equals("ISessionBL")){
148             return SessionBL.singleton();
149         }else if(blName.equals("ITopicBL")){
150             return TopicBL.singleton();
151         }else if(blName.equals("IUserBL")){
152             return UserBL.singleton();
153         }else if(blName.equals("IWikiBL")){
154             return WikiBL.singleton();
155         }else if(blName.equals("IUploadFileBL")){
156             return UploadFileBL.singleton();
157         }
158         */

159         return MiddleFactory.getBL(blName);
160         
161     }
162     
163     public String JavaDoc getModule(HttpServletRequest JavaDoc req){
164         //return req.getParameter("module");
165
return this.getModuleByURL(req);
166     }
167     
168     public static UserI getUser(HttpServletRequest JavaDoc req)throws BLException{
169         HttpSession JavaDoc ses=req.getSession(true);
170         UserI user=(UserI)ses.getAttribute("user");
171         
172         if(user==null){
173             
174             IUserBL ubl=(IUserBL)GeiServlet.getBL("IUserBL");
175             user=ubl.getDefUser();
176                         
177             ses.setAttribute("user",user);
178         }
179         
180         return user;
181     }
182     
183     /**
184      *
185      * @return <webapp>/WEB-INF/nukeroot/ absolute path
186      */

187     public static String JavaDoc getNukePath(){
188         
189         return nukePath;
190     }
191     
192     public static void fixStyle(HttpSession JavaDoc ses,String JavaDoc newStyle){
193         
194         ses.setAttribute("style",newStyle);;
195         ses.setAttribute("layout","themes/"+newStyle+"/template.vm");
196     }
197     
198     public static void intLog(String JavaDoc msg){
199         intLog(ILogger.LOG,msg);
200         
201     }
202     
203     
204     protected static void initLogger(){
205         //logger=LoggerFactory.getLogger(GeiServlet.class,LoggerFactory.OUT);
206
logger=LoggerFactory.getLogger("geinuke",LoggerFactory.LOG4J);
207     }
208     public static void intLog(int cat,String JavaDoc msg){
209         if(logger==null){
210             initLogger();
211         }
212         logger.log(cat,msg);
213     }
214     
215     public static void errLog(String JavaDoc msg,Throwable JavaDoc t){
216         
217         intLog(ILogger.CRITICAL,msg+" "+t.getMessage());
218         
219     }
220     
221     
222     public void init(ServletConfig JavaDoc sc) throws ServletException JavaDoc {
223         
224         super.init(sc);
225         try{
226             //intLog("GeiServlet.init(...), START OGGGI");
227
GeiServlet.se=this;
228             nukePath=this.getServletContext().getRealPath("/");
229             //intLog("GeiServlet.init(...), nukePath="+nukePath);
230
this.gc=new GlobalCFG(GeiServlet.getNukePath());
231             
232             //intLog("GeiServlet.init(...), END");
233
}catch(Exception JavaDoc e){
234             e.printStackTrace();
235             throw new ServletException JavaDoc();
236         }
237     }
238     
239     
240     public GlobalConfigurationI getGConfiguration(HttpServletRequest JavaDoc req){
241         return this.gc;
242     }
243     
244     
245     public static void fixStyle(HttpServletRequest JavaDoc req,Context ctx)throws BLException{
246         String JavaDoc newStyle=null;
247         newStyle=req.getParameter("THEStyle");
248         HttpSession JavaDoc ses=req.getSession(true);
249         if(newStyle!=null){
250             GeiServlet.fixStyle(ses,newStyle);
251             
252         }else if(ses.getAttribute("style")!=null){
253             
254             
255             newStyle=(String JavaDoc)ses.getAttribute("style");
256             GeiServlet.fixStyle(ses,newStyle);
257             
258         }else{
259             UserI user=getUser(req);
260             
261             newStyle=user.getDefStyle();
262             //newStyle="NewStyle";//TODO
263
GeiServlet.fixStyle(ses,newStyle);
264             
265         }
266         ctx.put("themeDir","themes/"+newStyle+"/");
267     }
268     
269     public String JavaDoc getModuleTemplateFile(HttpServletRequest JavaDoc req){
270         String JavaDoc res=null;
271         HttpSession JavaDoc ses=req.getSession(true);
272         String JavaDoc styleName=(String JavaDoc)ses.getAttribute("style");
273         res="themes/"+styleName+"/moduleBox.vm";
274         return res;
275     }
276     
277     
278     protected String JavaDoc getTemplateString(
279             Context context,
280             HttpServletRequest JavaDoc req,
281             HttpServletResponse JavaDoc res) throws Exception JavaDoc{
282         String JavaDoc ris=null;
283         String JavaDoc name=this.getRelativePath(req);
284         log(name);
285         
286         String JavaDoc className=(String JavaDoc)this.initParameter.get("GeiAction");
287         Class JavaDoc c=Class.forName(className);
288         Object JavaDoc o=c.newInstance();
289         MagicAction ac=(MagicAction)(o);
290         ac.setRefName(name);
291         ac.setServlet(this);
292         ris=ac.handleAction(req,res,context);
293         
294         
295         return ris;
296     
297     
298     }
299     
300     
301     
302     protected String JavaDoc chooseTemplateString(
303             Context context,
304             HttpServletRequest JavaDoc req,
305             HttpServletResponse JavaDoc res) throws Exception JavaDoc{
306         long start=System.currentTimeMillis();
307         String JavaDoc ris=null;
308         
309         ris=this.getTemplateString(context,req,res);
310         Locale JavaDoc lo=NukeResource.getLocale(req,res);
311         context.put("LOCALE",lo);
312         GeiServlet.fill(context);
313         context.put("render",new RenderTool());
314         context.put("context",context);
315         long end=System.currentTimeMillis();
316         float time=(end-start)/1000f;
317         context.put("DELAY",""+time);
318         return ris;
319     }
320 }
321
Popular Tags