/*
 * Copyright 2010 Zyeeda Co. Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.zyeeda.framework.web;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.zyeeda.framework.GlobalConstants;
import com.zyeeda.framework.services.Server;
import com.zyeeda.framework.services.impl.PersistenceServiceImpl;
import com.zyeeda.framework.utils.JndiUtils;

/**
 * Open session in view servlet filter.
 * 
 * @author		Rui Tang
 * @version 	%I%, %G%
 * @since		1.0
 */
public class OpenSessionInViewFilter implements Filter {
	
	private FilterConfig config;
	
	@Override
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}
	
	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		PersistenceServiceImpl svc = null;
		
		try {
			String jndiServerKey = this.config.getServletContext().getInitParameter(GlobalConstants.SERVER_JNDI_NAME);
			
			Server server = (Server) JndiUtils.getObjectFromJndi(jndiServerKey);
			svc = server.getService(PersistenceServiceImpl.class);
			svc.openSession();
			chain.doFilter(request, response);
		} catch (NamingException e) {
			throw new ServletException(e);
		} finally {
			if (svc != null) {
				svc.closeSession();
			}
		}
	}

}

@@ -1068,13 +1068,8 @@
 ces.
-impl.
 Pers
@@ -1082,20 +1082,16 @@
 eService
-Impl
 ;%0D%0Aimpor
@@ -1308,20 +1308,22 @@
 lter %7B%0D%0A
-%09
 %0D%0A
-%09
+    
 private 
@@ -1344,20 +1344,22 @@
 onfig;%0D%0A
-%09
 %0D%0A
-%09
+    
 @Overrid
@@ -1353,33 +1353,36 @@
 %0A    @Override%0D%0A
-%09
+    
 public void init
@@ -1430,18 +1430,24 @@
 tion %7B%0D%0A
-%09%09
+        
 this.con
@@ -1465,16 +1465,21 @@
 g;%0D%0A
-%09
+    
 %7D%0D%0A
-%09
 %0D%0A
-%09
+    
 @Ove
@@ -1477,33 +1477,36 @@
 %0A    @Override%0D%0A
-%09
+    
 public void dest
@@ -1518,15 +1518,21 @@
  %7B%0D%0A
-%09
+    
 %7D%0D%0A%0D%0A
-%09
+    
 @Ove
@@ -1538,17 +1538,20 @@
 erride%0D%0A
-%09
+    
 public v
@@ -1672,18 +1672,24 @@
 tion %7B%0D%0A
-%09%09
+        
 Persiste
@@ -1698,20 +1698,16 @@
 eService
-Impl
  svc = n
@@ -1716,24 +1716,37 @@
 l;%0D%0A
-%09%09%0D%0A%09%09try %7B%0D%0A%09%09%09
+%0D%0A        try %7B%0D%0A            
 Stri
@@ -1853,16 +1853,22 @@
 );%0D%0A
-%09%09%09%0D%0A%09%09%09
+%0D%0A            
 Serv
@@ -1933,19 +1933,28 @@
 rKey);%0D%0A
-%09%09%09
+            
 svc = se
@@ -1991,12 +1991,8 @@
 vice
-Impl
 .cla
@@ -1997,19 +1997,28 @@
 lass);%0D%0A
-%09%09%09
+            
 svc.open
@@ -2029,19 +2029,28 @@
 ion();%0D%0A
-%09%09%09
+            
 chain.do
@@ -2077,18 +2077,24 @@
 onse);%0D%0A
-%09%09
+        
 %7D catch 
@@ -2116,19 +2116,28 @@
 n e) %7B%0D%0A
-%09%09%09
+            
 throw ne
@@ -2160,18 +2160,24 @@
 on(e);%0D%0A
-%09%09
+        
 %7D finall
@@ -2181,19 +2181,28 @@
 ally %7B%0D%0A
-%09%09%09
+            
 if (svc 
@@ -2217,12 +2217,24 @@
  %7B%0D%0A
-%09%09%09%09
+                
 svc.
@@ -2254,20 +2254,38 @@
 );%0D%0A
-%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09
+            %7D%0D%0A        %7D%0D%0A    
 %7D%0D%0A%0D

fix open session in view filter error.