1 25 package org.objectweb.speedo.j2eedo.web; 26 import java.io.IOException ; 27 import java.rmi.RemoteException ; 28 29 import javax.ejb.CreateException ; 30 import javax.jdo.JDOException; 31 import javax.jdo.PersistenceManager; 32 import javax.jdo.PersistenceManagerFactory; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import javax.servlet.ServletException ; 37 import javax.servlet.http.HttpServlet ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 41 import org.objectweb.speedo.j2eedo.common.PMHolder; 42 import org.objectweb.speedo.j2eedo.ejb.StoreServicesLocal; 43 import org.objectweb.speedo.j2eedo.ejb.StoreServicesLocalHome; 44 import org.objectweb.speedo.j2eedo.ejb.StoreServicesRemote; 45 import org.objectweb.speedo.j2eedo.ejb.StoreServicesRemoteHome; 46 import org.objectweb.util.monolog.Monolog; 47 import org.objectweb.util.monolog.api.BasicLevel; 48 import org.objectweb.util.monolog.api.Logger; 49 50 abstract class MainServlet extends HttpServlet { 51 57 public final static String WITH_TRANSACTION_PARAMETER = "withTrans"; 58 63 public final static String TYPE_CONTAINER_PARAMETER = "container"; 64 65 public final static String LOCAL_SESSION_BEAN_PARAMETER = "localsession"; 66 67 72 public final static String WITH_GETPM_PARAMETER = "withGetPM"; 73 74 private final static String PMF_JNDI_NAME = "speedo"; 75 76 protected static Logger logger = null; 77 78 protected PersistenceManagerFactory persistenceManagerFactory = null; 79 80 protected StoreServicesLocalHome lstoreServicesLH; 81 protected StoreServicesRemoteHome lstoreServicesRH; 82 83 94 public void service(HttpServletRequest req, HttpServletResponse resp) 95 throws ServletException , IOException { 96 boolean withTransaction = getParameterValue( 97 req, MainServlet.WITH_TRANSACTION_PARAMETER, false); 98 boolean withGetPM = getParameterValue( 99 req, MainServlet.WITH_GETPM_PARAMETER, withTransaction); 100 boolean useSessionBean = getParameterValue( 101 req, MainServlet.TYPE_CONTAINER_PARAMETER, false); 102 boolean localSessionBean = getParameterValue( 103 req, MainServlet.LOCAL_SESSION_BEAN_PARAMETER, false); 104 if (logger.isLoggable(BasicLevel.DEBUG)) { 105 logger.log(BasicLevel.DEBUG, "Invoke Servlet: " 106 +" \n\t-withGetPM=" + withGetPM 107 + "\n\t-withTransaction=" + withTransaction 108 + "\n\t-useSessionBean=" + useSessionBean 109 + "\n\t-localSessionBean=" + localSessionBean 110 ); 111 } 112 if (useSessionBean && withTransaction) { 114 withTransaction = false; 115 logger.log(BasicLevel.WARN, 116 "Use local transaction is forbiden when using an ejb session"); 117 } 118 if (withTransaction && !withGetPM) { 119 withGetPM = true; 121 logger.log(BasicLevel.WARN, 122 "Use of the persistence manager is required when using transaction"); 123 } 124 PMHolder pmh = new PMHolder(persistenceManagerFactory); 125 PersistenceManager pm = null; 126 javax.jdo.Transaction utx = null; 127 try { 128 if ((!useSessionBean || localSessionBean) && withGetPM) { 129 logger.log(BasicLevel.DEBUG, "GetPM"); 130 pm = pmh.getPersistenceManager(); 131 } 132 if ((!useSessionBean || localSessionBean) && withTransaction) { 133 utx = pm.currentTransaction(); 134 utx.begin(); 135 logger.log(BasicLevel.DEBUG, "Begin local Transaction"); 136 } 137 if (useSessionBean) { 138 if (localSessionBean) { 140 logger.log(BasicLevel.DEBUG, "Use local session bean"); 142 StoreServicesLocal lstoreServices = null; 143 try { 144 lstoreServices = lstoreServicesLH.create(); 145 } catch (CreateException e) { 146 logger.log(BasicLevel.ERROR, "Error during the creation of Local Session", e); 147 throw new ServletException (e); 148 } 149 try { 150 executeSessionBean(req, resp, lstoreServices, pmh); 151 } finally { 152 lstoreServices.remove(); 154 } 155 } else { 156 logger.log(BasicLevel.DEBUG, "Use remote session bean"); 158 StoreServicesRemote lstoreServices = null; 159 try { 160 lstoreServices = lstoreServicesRH.create(); 161 } catch (CreateException e) { 162 logger.log(BasicLevel.ERROR, "Error during the creation of Local Session", e); 163 throw new ServletException (e); 164 } 165 try { 166 executeSessionBean(req, resp, lstoreServices); 167 } finally { 168 lstoreServices.remove(); 170 } 171 } 172 } else { 173 logger.log(BasicLevel.DEBUG, "Use direct call"); 174 executeDirectCall(req, resp, pmh); 175 } 176 } catch (Exception e) { 177 if (utx != null && utx.isActive()) { 178 logger.log(BasicLevel.DEBUG, "Rollback the local Transaction due to an error: ", e); 179 utx.rollback(); 180 } else { 181 logger.log(BasicLevel.ERROR, "An error has occured: ", e); 182 } 183 throw new ServletException (e); 184 } finally { 185 if (utx != null && utx.isActive()) { 186 logger.log(BasicLevel.DEBUG, "Commit the local Transaction"); 187 utx.commit(); 188 } 189 if (pm != null && !pm.isClosed()) { 190 logger.log(BasicLevel.DEBUG, "Close local PM"); 191 pmh.closePersistenceManager(); 192 } 193 } 194 } 195 196 public void init() throws ServletException { 197 super.init(); 198 199 logger = Monolog.initialize().getLogger(getClass().getName()); 201 202 persistenceManagerFactory = (PersistenceManagerFactory) 204 getObjectFromContext(null, PMF_JNDI_NAME); 205 206 try { 207 getStoreServicesRemoteHome(); 208 } catch (ServletException e) { 209 logger.log(BasicLevel.WARN, "No StoreServicesRemoteHome found", e); 210 } 211 try { 212 getStoreServicesLocalHome(); 213 } catch (ServletException e) { 214 logger.log(BasicLevel.WARN, "No StoreServicesLocalHome found", e); 215 } 216 } 217 218 221 private StoreServicesLocalHome getStoreServicesLocalHome() throws ServletException { 222 if (lstoreServicesLH == null) { 223 lstoreServicesLH = (StoreServicesLocalHome) 224 getObjectFromContext(null, "java:comp/env/ejb/StoreServicesHomeLocal"); 225 } 226 return lstoreServicesLH; 227 } 228 229 232 private StoreServicesRemoteHome getStoreServicesRemoteHome() throws ServletException { 233 if (lstoreServicesRH == null) { 234 lstoreServicesRH = (StoreServicesRemoteHome) 235 getObjectFromContext(null, "ejb/storeService"); 236 } 237 return lstoreServicesRH; 238 } 239 240 248 private static Object getObjectFromContext(InitialContext ictx, String name) throws ServletException { 249 boolean hasToCloseContext = false; 250 if (ictx == null) { 251 try { 252 ictx = new InitialContext (); 253 hasToCloseContext = true; 254 } catch (NamingException e) { 255 logger.log(BasicLevel.ERROR, 256 "Impossible to get the InitialContext: ", e); 257 throw new ServletException ( 258 "Impossible to get the InitialContext: " + e.getMessage()); 259 } 260 } 261 try { 262 Object o = ictx.lookup(name); 263 if (o == null) { 264 throw new ServletException ("'" + name + "' has not been found from JNDI"); 265 } 266 return o; 267 } catch (ServletException e) { 268 throw e; 269 } catch (Exception e) { 270 logger.log(BasicLevel.ERROR, "Impossible to find " + name +": ", e); 271 return null; 272 } finally { 273 if (hasToCloseContext && ictx != null) { 274 try { 275 ictx.close(); 276 } catch (NamingException ne) { 277 } 278 } 279 } 280 } 281 282 289 private static boolean getParameterValue(HttpServletRequest req, 290 String paramName, 291 boolean defaultValue) { 292 try { 293 String val = req.getParameter(paramName); 294 return val != null && Boolean.valueOf(val).booleanValue(); 295 } catch (Exception e) { 296 return defaultValue; 297 } 298 } 299 300 311 protected abstract void executeSessionBean( 312 HttpServletRequest req, 313 HttpServletResponse resp, 314 StoreServicesRemote storeServices) 315 throws JDOException, RemoteException , Exception , IOException ; 316 317 protected abstract void executeSessionBean( 318 HttpServletRequest req, 319 HttpServletResponse resp, 320 StoreServicesLocal storeServices, 321 PMHolder persistenceManagerHolder) 322 throws JDOException, RemoteException , Exception , IOException ; 323 324 333 protected abstract void executeDirectCall( 334 HttpServletRequest req, 335 HttpServletResponse resp, 336 PMHolder persistenceManagerHolder) 337 throws JDOException, Exception , IOException ; 338 } 339 | Popular Tags |