1 2 23 package com.geinuke.servlet; 24 25 26 import java.util.Enumeration ; 27 import java.util.Locale ; 28 import javax.servlet.ServletConfig ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 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 nukePath=null; 55 protected static ILogger logger=null; 56 public static GeiServlet se=null; 57 58 public static void home(HttpServletResponse res) throws Exception { 59 res.sendRedirect("geinuke.jhtm"); 60 } 61 62 63 public static void fill(Context ctx){ 64 65 66 MultiProps res=null; 67 Locale lo=(Locale )ctx.get("LOCALE"); 68 try{ 69 res=GeiServlet.loadResource(lo.getLanguage()); 70 String key=null; 71 String value=null; 72 Enumeration keys=res.keys(); 73 while(keys.hasMoreElements()){ 74 key=(String )keys.nextElement(); 75 value=(String )res.get(key); 76 ctx.put(key,value); 78 } 79 }catch(Exception e){ 80 GeiServlet.intLog(ILogger.CRITICAL,"GeiServlet.fill(...), EXCEPTION "+e); 81 } 83 84 } 85 86 87 public static MultiProps loadResource(String locale)throws Exception { 88 MultiProps res=null; 89 String ePath=""; 90 String 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 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 if(! NukeResource.existsFile(fn)){ 102 GeiServlet.intLog(ILogger.DEBUG,"GeiServlet.loadResource(..), C"); 103 fn=GeiServlet.getNukePath()+"/WEB-INF/templates/resources.txt"; 104 } 106 mc.setFileName(fn); 107 res=mc.getConfiguration(); 108 109 return res; 110 } 111 112 113 protected String getModuleByURL(HttpServletRequest req){ 114 String 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 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 getBL(String blName){ 129 130 159 return MiddleFactory.getBL(blName); 160 161 } 162 163 public String getModule(HttpServletRequest req){ 164 return this.getModuleByURL(req); 166 } 167 168 public static UserI getUser(HttpServletRequest req)throws BLException{ 169 HttpSession 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 187 public static String getNukePath(){ 188 189 return nukePath; 190 } 191 192 public static void fixStyle(HttpSession ses,String newStyle){ 193 194 ses.setAttribute("style",newStyle);; 195 ses.setAttribute("layout","themes/"+newStyle+"/template.vm"); 196 } 197 198 public static void intLog(String msg){ 199 intLog(ILogger.LOG,msg); 200 201 } 202 203 204 protected static void initLogger(){ 205 logger=LoggerFactory.getLogger("geinuke",LoggerFactory.LOG4J); 207 } 208 public static void intLog(int cat,String msg){ 209 if(logger==null){ 210 initLogger(); 211 } 212 logger.log(cat,msg); 213 } 214 215 public static void errLog(String msg,Throwable t){ 216 217 intLog(ILogger.CRITICAL,msg+" "+t.getMessage()); 218 219 } 220 221 222 public void init(ServletConfig sc) throws ServletException { 223 224 super.init(sc); 225 try{ 226 GeiServlet.se=this; 228 nukePath=this.getServletContext().getRealPath("/"); 229 this.gc=new GlobalCFG(GeiServlet.getNukePath()); 231 232 }catch(Exception e){ 234 e.printStackTrace(); 235 throw new ServletException (); 236 } 237 } 238 239 240 public GlobalConfigurationI getGConfiguration(HttpServletRequest req){ 241 return this.gc; 242 } 243 244 245 public static void fixStyle(HttpServletRequest req,Context ctx)throws BLException{ 246 String newStyle=null; 247 newStyle=req.getParameter("THEStyle"); 248 HttpSession 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 )ses.getAttribute("style"); 256 GeiServlet.fixStyle(ses,newStyle); 257 258 }else{ 259 UserI user=getUser(req); 260 261 newStyle=user.getDefStyle(); 262 GeiServlet.fixStyle(ses,newStyle); 264 265 } 266 ctx.put("themeDir","themes/"+newStyle+"/"); 267 } 268 269 public String getModuleTemplateFile(HttpServletRequest req){ 270 String res=null; 271 HttpSession ses=req.getSession(true); 272 String styleName=(String )ses.getAttribute("style"); 273 res="themes/"+styleName+"/moduleBox.vm"; 274 return res; 275 } 276 277 278 protected String getTemplateString( 279 Context context, 280 HttpServletRequest req, 281 HttpServletResponse res) throws Exception { 282 String ris=null; 283 String name=this.getRelativePath(req); 284 log(name); 285 286 String className=(String )this.initParameter.get("GeiAction"); 287 Class c=Class.forName(className); 288 Object 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 chooseTemplateString( 303 Context context, 304 HttpServletRequest req, 305 HttpServletResponse res) throws Exception { 306 long start=System.currentTimeMillis(); 307 String ris=null; 308 309 ris=this.getTemplateString(context,req,res); 310 Locale 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 |