001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.webengine.jaxrs.servlet.config; 020 021import java.util.ArrayList; 022 023import javax.servlet.ServletConfig; 024import javax.servlet.ServletContextEvent; 025import javax.servlet.ServletContextListener; 026 027import org.nuxeo.common.xmap.annotation.XNodeList; 028import org.nuxeo.common.xmap.annotation.XObject; 029import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException; 030 031/** 032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 033 */ 034@XObject("listeners") 035public class ListenerSetDescriptor { 036 037 @XNodeList(value = "listener", type = ArrayList.class, componentType = ListenerDescriptor.class, trim = true, nullByDefault = false) 038 protected ArrayList<ListenerDescriptor> listenerDescriptors; 039 040 private ServletContextListener[] listeners; 041 042 private ServletContextEvent event; 043 044 public synchronized boolean isInitialized() { 045 return event != null; 046 } 047 048 public synchronized void init(ServletConfig config) throws ReflectiveOperationException, BundleNotFoundException { 049 if (event == null && !listenerDescriptors.isEmpty()) { 050 event = new ServletContextEvent(config.getServletContext()); 051 listeners = new ServletContextListener[listenerDescriptors.size()]; 052 for (int i = 0; i < listeners.length; i++) { 053 ListenerDescriptor ld = listenerDescriptors.get(i); 054 listeners[i] = ld.getListener(); 055 listeners[i].contextInitialized(event); 056 } 057 } 058 } 059 060 public synchronized boolean destroy() { 061 if (event != null) { 062 if (listeners != null) { 063 try { 064 for (ServletContextListener listener : listeners) { 065 listener.contextDestroyed(event); 066 } 067 } finally { 068 event = null; 069 listeners = null; 070 } 071 return true; 072 } 073 } 074 return false; 075 } 076 077 @Override 078 public String toString() { 079 return listenerDescriptors.toString(); 080 } 081 082}