Author: pero Date: Sun Jul 4 10:50:53 2010 New Revision: 960318 URL: http://svn.apache.org/viewvc?rev=960318&view=rev Log: no functional change. reformating and fix some checkstyle warnings
Modified: tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java Modified: tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java?rev=960318&r1=960317&r2=960318&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java Sun Jul 4 10:50:53 2010 @@ -1,13 +1,13 @@ -/* +/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. @@ -40,19 +40,21 @@ import org.apache.catalina.connector.Req import org.apache.coyote.ActionCode; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; + /** - * + * * @author fhanik * */ public class AsyncContextImpl implements AsyncContext { - + public static enum AsyncState { - NOT_STARTED, STARTED, DISPATCHING, DISPATCHED, COMPLETING, TIMING_OUT, ERROR_DISPATCHING + NOT_STARTED, STARTED, DISPATCHING, + DISPATCHED, COMPLETING, TIMING_OUT, ERROR_DISPATCHING } - + private static final Log log = LogFactory.getLog(AsyncContextImpl.class); - + private ServletRequest servletRequest = null; private ServletResponse servletResponse = null; private List<AsyncListenerWrapper> listeners = new ArrayList<AsyncListenerWrapper>(); @@ -62,12 +64,15 @@ public class AsyncContextImpl implements private AtomicReference<AsyncState> state = new AtomicReference<AsyncState>(AsyncState.NOT_STARTED); private long timeout = -1; private AsyncEvent event = null; - + private Request request; - + public AsyncContextImpl(Request request) { if (log.isDebugEnabled()) { - log.debug("AsyncContext created["+request.getRequestURI()+"?"+request.getQueryString()+"]", new DebugException()); + log.debug("AsyncContext created[" + + request.getRequestURI() + + "?" + request.getQueryString() + "]", + new DebugException()); } //TODO SERVLET3 - async this.request = request; @@ -76,81 +81,102 @@ public class AsyncContextImpl implements @Override public void complete() { if (log.isDebugEnabled()) { - log.debug("AsyncContext Complete Called["+state.get()+"; "+request.getRequestURI()+"?"+request.getQueryString()+"]", new DebugException()); + log.debug("AsyncContext Complete Called[" + + state.get() + "; " + + request.getRequestURI() + + "?" + request.getQueryString() + "]", + new DebugException()); } - if (state.get()==AsyncState.COMPLETING) { + if (state.get() == AsyncState.COMPLETING) { //do nothing - } else if (state.compareAndSet(AsyncState.DISPATCHED, AsyncState.COMPLETING) || - state.compareAndSet(AsyncState.STARTED, AsyncState.COMPLETING)) { + } else if (state.compareAndSet(AsyncState.DISPATCHED, AsyncState.COMPLETING) + || state.compareAndSet(AsyncState.STARTED, AsyncState.COMPLETING)) { // TODO SERVLET3 - async AtomicBoolean dispatched = new AtomicBoolean(false); - request.getCoyoteRequest().action(ActionCode.ACTION_ASYNC_COMPLETE,dispatched); - if (!dispatched.get()) doInternalComplete(false); + request.getCoyoteRequest().action( + ActionCode.ACTION_ASYNC_COMPLETE, dispatched); + if (!dispatched.get()) { + doInternalComplete(false); + } } else { - throw new IllegalStateException("Complete not allowed. Invalid state:"+state.get()); + throw new IllegalStateException( + "Complete not allowed. Invalid state:" + state.get()); } - + } @Override public void dispatch() { - HttpServletRequest sr = (HttpServletRequest)getServletRequest(); + HttpServletRequest sr = (HttpServletRequest) getServletRequest(); String path = sr.getRequestURI(); - String cpath = sr.getContextPath(); - if (cpath.length()>1) path = path.substring(cpath.length()); + final String cpath = sr.getContextPath(); + if (cpath.length() > 1) { + path = path.substring(cpath.length()); + } dispatch(path); } @Override - public void dispatch(String path) { - dispatch(request.getServletContext(),path); + public void dispatch(final String path) { + dispatch(request.getServletContext(), path); } @Override - public void dispatch(ServletContext context, String path) { + public void dispatch(final ServletContext dispachContext, + final String path) { if (log.isDebugEnabled()) { - log.debug("AsyncContext Dispatch Called["+state.get()+"; "+path+"; "+request.getRequestURI()+"?"+request.getQueryString()+"]", new DebugException()); + log.debug("AsyncContext Dispatch Called[" + + state.get() + "; " + + path + "; " + + request.getRequestURI() + + "?" + request.getQueryString() + "]", + new DebugException()); } // TODO SERVLET3 - async - if (state.compareAndSet(AsyncState.STARTED, AsyncState.DISPATCHING) || - state.compareAndSet(AsyncState.DISPATCHED, AsyncState.DISPATCHING)) { + if (state.compareAndSet(AsyncState.STARTED, AsyncState.DISPATCHING) + || state.compareAndSet(AsyncState.DISPATCHED, AsyncState.DISPATCHING)) { - if (request.getAttribute(ASYNC_REQUEST_URI)==null) { - request.setAttribute(ASYNC_REQUEST_URI, request.getRequestURI()+"?"+request.getQueryString()); - request.setAttribute(ASYNC_CONTEXT_PATH, request.getContextPath()); - request.setAttribute(ASYNC_SERVLET_PATH, request.getServletPath()); - request.setAttribute(ASYNC_QUERY_STRING, request.getQueryString()); - } - final RequestDispatcher requestDispatcher = context.getRequestDispatcher(path); - final HttpServletRequest servletRequest = (HttpServletRequest)getRequest(); - final HttpServletResponse servletResponse = (HttpServletResponse)getResponse(); + if (request.getAttribute(ASYNC_REQUEST_URI) == null) { + request.setAttribute(ASYNC_REQUEST_URI, + request.getRequestURI() + "?" + request.getQueryString()); + request.setAttribute(ASYNC_CONTEXT_PATH, + request.getContextPath()); + request.setAttribute(ASYNC_SERVLET_PATH, + request.getServletPath()); + request.setAttribute(ASYNC_QUERY_STRING, + request.getQueryString()); + } + final RequestDispatcher requestDispatcher = dispachContext.getRequestDispatcher(path); + final HttpServletRequest sRequest = (HttpServletRequest) getRequest(); + final HttpServletResponse sResponse = (HttpServletResponse) getResponse(); Runnable run = new Runnable() { public void run() { - DispatcherType type = (DispatcherType)request.getAttribute(Globals.DISPATCHER_TYPE_ATTR); + DispatcherType type = (DispatcherType) request.getAttribute(Globals.DISPATCHER_TYPE_ATTR); try { //piggy back on the request dispatcher to ensure that filters etc get called. //TODO SERVLET3 - async should this be include/forward or a new dispatch type //javadoc suggests include with the type of DispatcherType.ASYNC request.setAttribute(Globals.DISPATCHER_TYPE_ATTR, DispatcherType.ASYNC); - requestDispatcher.include(servletRequest, servletResponse); - }catch (Exception x) { + requestDispatcher.include(sRequest, sResponse); + } catch (Exception x) { //log.error("Async.dispatch",x); throw new RuntimeException(x); - }finally { + } finally { request.setAttribute(Globals.DISPATCHER_TYPE_ATTR, type); } } }; this.dispatch = run; AtomicBoolean dispatched = new AtomicBoolean(false); - request.getCoyoteRequest().action(ActionCode.ACTION_ASYNC_DISPATCH, dispatched ); + request.getCoyoteRequest().action( + ActionCode.ACTION_ASYNC_DISPATCH, dispatched); if (!dispatched.get()) { try { doInternalDispatch(); - }catch (ServletException sx) { + } catch (ServletException sx) { throw new RuntimeException(sx); - }catch (IOException ix) { + } catch (IOException ix) { throw new RuntimeException(ix); } } @@ -158,7 +184,8 @@ public class AsyncContextImpl implements complete(); } } else { - throw new IllegalStateException("Dispatch not allowed. Invalid state:"+state.get()); + throw new IllegalStateException( + "Dispatch not allowed. Invalid state:" + state.get()); } } @@ -175,51 +202,59 @@ public class AsyncContextImpl implements @Override public void start(final Runnable run) { if (log.isDebugEnabled()) { - log.debug("AsyncContext Start Called["+state.get()+"; "+request.getRequestURI()+"?"+request.getQueryString()+"]", new DebugException()); + log.debug("AsyncContext Start Called[" + + state.get() + "; " + + request.getRequestURI() + + "?" + request.getQueryString() + "]", + new DebugException()); } - if (state.compareAndSet(AsyncState.STARTED, AsyncState.DISPATCHING) || - state.compareAndSet(AsyncState.DISPATCHED, AsyncState.DISPATCHING)) { + if (state.compareAndSet(AsyncState.STARTED, AsyncState.DISPATCHING) + || state.compareAndSet(AsyncState.DISPATCHED, AsyncState.DISPATCHING)) { // TODO SERVLET3 - async final ServletContext sctx = getServletRequest().getServletContext(); Runnable r = new Runnable() { public void run() { - //TODO SERVLET3 - async - set context class loader when running the task. + //TODO SERVLET3 - + //async - set context class loader when running the task. try { - + run.run(); - }catch (Exception x) { - log.error("Unable to run async task.",x); + } catch (Exception x) { + log.error("Unable to run async task.", x); } } }; this.dispatch = r; AtomicBoolean dispatched = new AtomicBoolean(false); - request.getCoyoteRequest().action(ActionCode.ACTION_ASYNC_DISPATCH, dispatched ); + request.getCoyoteRequest().action( + ActionCode.ACTION_ASYNC_DISPATCH, dispatched); if (!dispatched.get()) { try { doInternalDispatch(); - }catch (ServletException sx) { + } catch (ServletException sx) { throw new RuntimeException(sx); - }catch (IOException ix) { + } catch (IOException ix) { throw new RuntimeException(ix); } } } else { - throw new IllegalStateException("Dispatch not allowed. Invalid state:"+state.get()); + throw new IllegalStateException( + "Dispatch not allowed. Invalid state:" + state.get()); } } - + @Override - public void addListener(AsyncListener listener) { + public void addListener(final AsyncListener listener) { AsyncListenerWrapper wrapper = new AsyncListenerWrapper(); wrapper.setListener(listener); listeners.add(wrapper); } @Override - public void addListener(AsyncListener listener, ServletRequest servletRequest, - ServletResponse servletResponse) { + public void addListener(final AsyncListener listener, + final ServletRequest sRequest, + final ServletResponse sResponse) { AsyncListenerWrapper wrapper = new AsyncListenerWrapper(); wrapper.setListener(listener); listeners.add(wrapper); @@ -240,7 +275,7 @@ public class AsyncContextImpl implements } return listener; } - + public void recycle() { servletRequest = null; servletResponse = null; @@ -253,15 +288,18 @@ public class AsyncContextImpl implements } public boolean isStarted() { - return (state.get() == AsyncState.STARTED || state.get() == AsyncState.DISPATCHING || state.get() == AsyncState.DISPATCHED); + return (state.get() == AsyncState.STARTED || + state.get() == AsyncState.DISPATCHING || + state.get() == AsyncState.DISPATCHED); } public void setStarted(Context context) { if (state.compareAndSet(AsyncState.NOT_STARTED, AsyncState.STARTED) || - state.compareAndSet(AsyncState.DISPATCHED, AsyncState.STARTED)) { + state.compareAndSet(AsyncState.DISPATCHED, AsyncState.STARTED)) { this.context = context; } else { - throw new IllegalStateException("Start illegal. Invalid state: "+state.get()); + throw new IllegalStateException("Start illegal. Invalid state: " + + state.get()); } } @@ -278,7 +316,8 @@ public class AsyncContextImpl implements return hasOriginalRequestAndResponse; } - public void setHasOriginalRequestAndResponse(boolean hasOriginalRequestAndResponse) { + public void setHasOriginalRequestAndResponse( + boolean hasOriginalRequestAndResponse) { this.hasOriginalRequestAndResponse = hasOriginalRequestAndResponse; } @@ -289,11 +328,12 @@ public class AsyncContextImpl implements public void setCompleted() { this.state.set(AsyncState.NOT_STARTED); } - + public void doInternalDispatch() throws ServletException, IOException { - if (this.state.compareAndSet(AsyncState.TIMING_OUT, AsyncState.COMPLETING)) { - if( log.isDebugEnabled()) - log.debug("TIMING OUT!"); + if (this.state.compareAndSet( + AsyncState.TIMING_OUT, AsyncState.COMPLETING)) { + if (log.isDebugEnabled()) + log.debug("TIMING OUT!"); boolean listenerInvoked = false; for (AsyncListenerWrapper listener : listeners) { listener.fireOnTimeout(event); @@ -303,18 +343,19 @@ public class AsyncContextImpl implements ((HttpServletResponse)servletResponse).setStatus(500); } doInternalComplete(true); - } else if (this.state.compareAndSet(AsyncState.ERROR_DISPATCHING, AsyncState.COMPLETING)) { - if( log.isDebugEnabled()) + } else if (this.state.compareAndSet( + AsyncState.ERROR_DISPATCHING, AsyncState.COMPLETING)) { + if (log.isDebugEnabled()) log.debug("ON ERROR!"); boolean listenerInvoked = false; for (AsyncListenerWrapper listener : listeners) { try { listener.fireOnError(event); }catch (IllegalStateException x) { - if( log.isDebugEnabled()) + if (log.isDebugEnabled()) log.debug("Listener invoked invalid state.",x); }catch (Exception x) { - if(log.isDebugEnabled()) + if (log.isDebugEnabled()) log.debug("Exception during onError.",x); } listenerInvoked = true; @@ -323,15 +364,18 @@ public class AsyncContextImpl implements ((HttpServletResponse)servletResponse).setStatus(500); } doInternalComplete(true); - - } else if (this.state.compareAndSet(AsyncState.DISPATCHING, AsyncState.DISPATCHED)) { + + } else if (this.state.compareAndSet( + AsyncState.DISPATCHING, AsyncState.DISPATCHED)) { if (this.dispatch!=null) { try { dispatch.run(); } catch (RuntimeException x) { doInternalComplete(true); - if (x.getCause() instanceof ServletException) throw (ServletException)x.getCause(); - if (x.getCause() instanceof IOException) throw (IOException)x.getCause(); + if (x.getCause() instanceof ServletException) + throw (ServletException)x.getCause(); + if (x.getCause() instanceof IOException) + throw (IOException)x.getCause(); else throw new ServletException(x); } finally { dispatch = null; @@ -340,23 +384,26 @@ public class AsyncContextImpl implements } else if (this.state.get()==AsyncState.COMPLETING) { doInternalComplete(false); } else { - throw new IllegalStateException("Dispatch illegal. Invalid state: "+state.get()); + throw new IllegalStateException( + "Dispatch illegal. Invalid state: " + + state.get()); } } - + public void doInternalComplete(boolean error) { if (isCompleted()) return; if (state.compareAndSet(AsyncState.STARTED, AsyncState.NOT_STARTED)) { //this is the same as //request.startAsync().complete(); recycle(); - } else if (state.compareAndSet(AsyncState.COMPLETING, AsyncState.NOT_STARTED)) { + } else if (state.compareAndSet( + AsyncState.COMPLETING, AsyncState.NOT_STARTED)) { for (AsyncListenerWrapper wrapper : listeners) { try { wrapper.fireOnComplete(event); }catch (IOException x) { //how does this propagate, or should it? - //TODO SERVLET3 - async + //TODO SERVLET3 - async log.error("",x); } } @@ -366,42 +413,46 @@ public class AsyncContextImpl implements log.error("",x); } recycle(); - - } else { - throw new IllegalStateException("Complete illegal. Invalid state:"+state.get()); + + } else { + throw new IllegalStateException("Complete illegal. Invalid state:" + + state.get()); } } - + public AsyncState getState() { return state.get(); } - + protected void setState(AsyncState st) { state.set(st); } - + public long getTimeout() { return timeout; } - + public void setTimeout(long timeout) { this.timeout = timeout; - request.getCoyoteRequest().action(ActionCode.ACTION_ASYNC_SETTIMEOUT,new Long(timeout)); + request.getCoyoteRequest().action(ActionCode.ACTION_ASYNC_SETTIMEOUT, + new Long(timeout)); } - + public void setTimeoutState() { state.set(AsyncState.TIMING_OUT); } - + public void setErrorState(Throwable t) { if (t!=null) request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t); state.set(AsyncState.ERROR_DISPATCHING); } - + public void init(ServletRequest request, ServletResponse response) { this.servletRequest = request; this.servletResponse = response; - event = new AsyncEvent(this, request, response); + event = new AsyncEvent(this, request, response); } + + @SuppressWarnings("serial") public static class DebugException extends Exception {} } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org