comparison src/org/eclipse/jetty/server/handler/ContextHandler.java @ 989:900e5b8ccd19

remove useless listeners
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 17 Oct 2016 05:39:54 -0600
parents 13beba0c127f
children 688c39c50ee3
comparison
equal deleted inserted replaced
988:13beba0c127f 989:900e5b8ccd19
68 import org.eclipse.jetty.server.Handler; 68 import org.eclipse.jetty.server.Handler;
69 import org.eclipse.jetty.server.HandlerContainer; 69 import org.eclipse.jetty.server.HandlerContainer;
70 import org.eclipse.jetty.server.Request; 70 import org.eclipse.jetty.server.Request;
71 import org.eclipse.jetty.server.Server; 71 import org.eclipse.jetty.server.Server;
72 import org.eclipse.jetty.util.AttributesMap; 72 import org.eclipse.jetty.util.AttributesMap;
73 import org.eclipse.jetty.util.LazyList;
74 import org.eclipse.jetty.util.StringUtil; 73 import org.eclipse.jetty.util.StringUtil;
75 import org.eclipse.jetty.util.TypeUtil; 74 import org.eclipse.jetty.util.TypeUtil;
76 import org.eclipse.jetty.util.URIUtil; 75 import org.eclipse.jetty.util.URIUtil;
77 import org.eclipse.jetty.util.component.AggregateLifeCycle; 76 import org.eclipse.jetty.util.component.AggregateLifeCycle;
78 import org.eclipse.jetty.util.component.Dumpable; 77 import org.eclipse.jetty.util.component.Dumpable;
129 private Resource _baseResource; 128 private Resource _baseResource;
130 private MimeTypes _mimeTypes; 129 private MimeTypes _mimeTypes;
131 private Map<String, String> _localeEncodingMap; 130 private Map<String, String> _localeEncodingMap;
132 private String[] _welcomeFiles; 131 private String[] _welcomeFiles;
133 private String[] _vhosts; 132 private String[] _vhosts;
134 private EventListener[] _eventListeners;
135 private Logger _logger; 133 private Logger _logger;
136 private boolean _allowNullPathInfo; 134 private boolean _allowNullPathInfo;
137 private int _maxFormKeys = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormKeys",-1).intValue(); 135 private int _maxFormKeys = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormKeys",-1).intValue();
138 private int _maxFormContentSize = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormContentSize",-1).intValue(); 136 private int _maxFormContentSize = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormContentSize",-1).intValue();
139 private boolean _compactPath = false; 137 private boolean _compactPath = false;
140 private boolean _aliasesAllowed = false; 138 private boolean _aliasesAllowed = false;
141 139
142 private Object _contextListeners;
143 private Object _contextAttributeListeners;
144 private Object _requestListeners;
145 private Object _requestAttributeListeners;
146 private Object _durableListeners;
147 private Map<String, Object> _managedAttributes; 140 private Map<String, Object> _managedAttributes;
148 private String[] _protectedTargets; 141 private String[] _protectedTargets;
149 private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<ContextHandler.AliasCheck>(); 142 private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<ContextHandler.AliasCheck>();
150 143
151 private boolean _shutdown = false; 144 private boolean _shutdown = false;
437 * @see javax.servlet.ServletContext#getServletContextName() 430 * @see javax.servlet.ServletContext#getServletContextName()
438 */ 431 */
439 public String getDisplayName() 432 public String getDisplayName()
440 { 433 {
441 return _displayName; 434 return _displayName;
442 } 435 }
443
444 /* ------------------------------------------------------------ */
445 public EventListener[] getEventListeners()
446 {
447 return _eventListeners;
448 }
449
450 /* ------------------------------------------------------------ */
451 /**
452 * Set the context event listeners.
453 *
454 * @param eventListeners
455 * the event listeners
456 * @see ServletContextListener
457 * @see ServletContextAttributeListener
458 * @see ServletRequestListener
459 * @see ServletRequestAttributeListener
460 */
461 public void setEventListeners(EventListener[] eventListeners)
462 {
463 _contextListeners = null;
464 _contextAttributeListeners = null;
465 _requestListeners = null;
466 _requestAttributeListeners = null;
467
468 _eventListeners = eventListeners;
469
470 for (int i = 0; eventListeners != null && i < eventListeners.length; i++)
471 {
472 EventListener listener = _eventListeners[i];
473
474 if (listener instanceof ServletContextListener)
475 _contextListeners = LazyList.add(_contextListeners,listener);
476
477 if (listener instanceof ServletContextAttributeListener)
478 _contextAttributeListeners = LazyList.add(_contextAttributeListeners,listener);
479
480 if (listener instanceof ServletRequestListener)
481 _requestListeners = LazyList.add(_requestListeners,listener);
482
483 if (listener instanceof ServletRequestAttributeListener)
484 _requestAttributeListeners = LazyList.add(_requestAttributeListeners,listener);
485 }
486 }
487
488 /* ------------------------------------------------------------ */
489 /**
490 * Add a context event listeners.
491 *
492 * @see ServletContextListener
493 * @see ServletContextAttributeListener
494 * @see ServletRequestListener
495 * @see ServletRequestAttributeListener
496 */
497 public void addEventListener(EventListener listener)
498 {
499 //Only listeners added before the context starts last through a stop/start cycle
500 if (!(isStarted() || isStarting()))
501 _durableListeners = LazyList.add(_durableListeners, listener);
502
503 setEventListeners((EventListener[])LazyList.addToArray(getEventListeners(),listener,EventListener.class));
504 }
505
506
507 /**
508 * Apply any necessary restrictions on a programmatically added
509 * listener.
510 *
511 * Superclasses should implement.
512 *
513 * @param listener
514 */
515 public void restrictEventListener (EventListener listener)
516 {
517 }
518
519
520 436
521 /* ------------------------------------------------------------ */ 437 /* ------------------------------------------------------------ */
522 /** 438 /**
523 * @return true if this context is accepting new requests 439 * @return true if this context is accepting new requests
524 */ 440 */
663 checkManagedAttribute(name,value); 579 checkManagedAttribute(name,value);
664 } 580 }
665 } 581 }
666 582
667 super.doStart(); 583 super.doStart();
668
669 // Context listeners
670 if (_contextListeners != null)
671 {
672 ServletContextEvent event = new ServletContextEvent(_scontext);
673 for (int i = 0; i < LazyList.size(_contextListeners); i++)
674 {
675 callContextInitialized(((ServletContextListener)LazyList.get(_contextListeners, i)), event);
676 }
677 }
678 } 584 }
679 585
680 /* ------------------------------------------------------------ */ 586 /* ------------------------------------------------------------ */
681 public void callContextInitialized (ServletContextListener l, ServletContextEvent e) 587 public void callContextInitialized (ServletContextListener l, ServletContextEvent e)
682 { 588 {
712 old_classloader = current_thread.getContextClassLoader(); 618 old_classloader = current_thread.getContextClassLoader();
713 current_thread.setContextClassLoader(_classLoader); 619 current_thread.setContextClassLoader(_classLoader);
714 } 620 }
715 621
716 super.doStop(); 622 super.doStop();
717
718 // Context listeners
719 if (_contextListeners != null)
720 {
721 ServletContextEvent event = new ServletContextEvent(_scontext);
722 for (int i = LazyList.size(_contextListeners); i-- > 0;)
723 {
724 ((ServletContextListener)LazyList.get(_contextListeners,i)).contextDestroyed(event);
725 }
726 }
727
728 //remove all non-durable listeners
729 setEventListeners((EventListener[])LazyList.toArray(_durableListeners, EventListener.class));
730 _durableListeners = null;
731 623
732 Enumeration e = _scontext.getAttributeNames(); 624 Enumeration e = _scontext.getAttributeNames();
733 while (e.hasMoreElements()) 625 while (e.hasMoreElements())
734 { 626 {
735 String name = (String)e.nextElement(); 627 String name = (String)e.nextElement();
944 { 836 {
945 final DispatcherType dispatch = baseRequest.getDispatcherType(); 837 final DispatcherType dispatch = baseRequest.getDispatcherType();
946 final boolean new_context = baseRequest.takeNewContext(); 838 final boolean new_context = baseRequest.takeNewContext();
947 try 839 try
948 { 840 {
949 if (new_context)
950 {
951 // Handle the REALLY SILLY request events!
952 if (_requestAttributeListeners != null)
953 {
954 final int s = LazyList.size(_requestAttributeListeners);
955 for (int i = 0; i < s; i++)
956 baseRequest.addEventListener(((EventListener)LazyList.get(_requestAttributeListeners,i)));
957 }
958
959 if (_requestListeners != null)
960 {
961 final int s = LazyList.size(_requestListeners);
962 final ServletRequestEvent sre = new ServletRequestEvent(_scontext,request);
963 for (int i = 0; i < s; i++)
964 ((ServletRequestListener)LazyList.get(_requestListeners,i)).requestInitialized(sre);
965 }
966 }
967
968 if (DispatcherType.REQUEST.equals(dispatch) && isProtectedTarget(target)) 841 if (DispatcherType.REQUEST.equals(dispatch) && isProtectedTarget(target))
969 throw new HttpException(HttpServletResponse.SC_NOT_FOUND); 842 throw new HttpException(HttpServletResponse.SC_NOT_FOUND);
970 843
971 // start manual inline of nextHandle(target,baseRequest,request,response); 844 // start manual inline of nextHandle(target,baseRequest,request,response);
972 // noinspection ConstantIfStatement 845 // noinspection ConstantIfStatement
981 catch (HttpException e) 854 catch (HttpException e)
982 { 855 {
983 LOG.debug("",e); 856 LOG.debug("",e);
984 baseRequest.setHandled(true); 857 baseRequest.setHandled(true);
985 response.sendError(e.getStatus(),e.getReason()); 858 response.sendError(e.getStatus(),e.getReason());
986 }
987 finally
988 {
989 // Handle more REALLY SILLY request events!
990 if (new_context)
991 {
992 if (_requestListeners != null)
993 {
994 final ServletRequestEvent sre = new ServletRequestEvent(_scontext,request);
995 for (int i = LazyList.size(_requestListeners); i-- > 0;)
996 ((ServletRequestListener)LazyList.get(_requestListeners,i)).requestDestroyed(sre);
997 }
998
999 if (_requestAttributeListeners != null)
1000 {
1001 for (int i = LazyList.size(_requestAttributeListeners); i-- > 0;)
1002 baseRequest.removeEventListener(((EventListener)LazyList.get(_requestAttributeListeners,i)));
1003 }
1004 }
1005 } 859 }
1006 } 860 }
1007 861
1008 /* ------------------------------------------------------------ */ 862 /* ------------------------------------------------------------ */
1009 /* 863 /*
1880 1734
1881 if (value == null) 1735 if (value == null)
1882 _contextAttributes.removeAttribute(name); 1736 _contextAttributes.removeAttribute(name);
1883 else 1737 else
1884 _contextAttributes.setAttribute(name,value); 1738 _contextAttributes.setAttribute(name,value);
1885
1886 if (_contextAttributeListeners != null)
1887 {
1888 ServletContextAttributeEvent event = new ServletContextAttributeEvent(_scontext,name,old_value == null?value:old_value);
1889
1890 for (int i = 0; i < LazyList.size(_contextAttributeListeners); i++)
1891 {
1892 ServletContextAttributeListener l = (ServletContextAttributeListener)LazyList.get(_contextAttributeListeners,i);
1893
1894 if (old_value == null)
1895 l.attributeAdded(event);
1896 else if (value == null)
1897 l.attributeRemoved(event);
1898 else
1899 l.attributeReplaced(event);
1900 }
1901 }
1902 } 1739 }
1903 1740
1904 /* ------------------------------------------------------------ */ 1741 /* ------------------------------------------------------------ */
1905 /* 1742 /*
1906 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String) 1743 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
1914 { 1751 {
1915 // Set it on the handler 1752 // Set it on the handler
1916 return; 1753 return;
1917 } 1754 }
1918 1755
1919 Object old_value = _contextAttributes.getAttribute(name);
1920 _contextAttributes.removeAttribute(name); 1756 _contextAttributes.removeAttribute(name);
1921 if (old_value != null)
1922 {
1923 if (_contextAttributeListeners != null)
1924 {
1925 ServletContextAttributeEvent event = new ServletContextAttributeEvent(_scontext,name,old_value);
1926
1927 for (int i = 0; i < LazyList.size(_contextAttributeListeners); i++)
1928 ((ServletContextAttributeListener)LazyList.get(_contextAttributeListeners,i)).attributeRemoved(event);
1929 }
1930 }
1931 } 1757 }
1932 1758
1933 /* ------------------------------------------------------------ */ 1759 /* ------------------------------------------------------------ */
1934 /* 1760 /*
1935 * @see javax.servlet.ServletContext#getServletContextName() 1761 * @see javax.servlet.ServletContext#getServletContextName()