001/* 002 * Copyright 2012 Anyware Services 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 */ 016package org.ametys.runtime.request; 017 018import java.util.ArrayList; 019import java.util.Collection; 020 021import org.apache.avalon.framework.activity.Initializable; 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.context.ContextException; 024import org.apache.avalon.framework.context.Contextualizable; 025import org.apache.avalon.framework.thread.ThreadSafe; 026import org.apache.cocoon.Constants; 027import org.apache.cocoon.environment.Context; 028 029/** 030 * Avalon component responsible for registering <code>RequestListener</code>s 031 */ 032public class RequestListenerManager implements Contextualizable, ThreadSafe, Initializable, Component 033{ 034 /** Avalon Role */ 035 public static final String ROLE = RequestListenerManager.class.getName(); 036 037 /** ServletContext attribute id containing an ArrayList<RequestListener> */ 038 public static final String CONTEXT_ATTRIBUTE_REQUEST_LISTENERS = "runtime:requestListeners"; 039 040 private Context _context; 041 042 public void contextualize(org.apache.avalon.framework.context.Context context) throws ContextException 043 { 044 _context = (Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 045 } 046 047 public void initialize() throws Exception 048 { 049 _context.setAttribute(org.ametys.runtime.request.RequestListenerManager.CONTEXT_ATTRIBUTE_REQUEST_LISTENERS, new ArrayList<RequestListener>()); 050 } 051 052 /** 053 * Registers a RequestListener 054 * @param listener the listener being registered 055 */ 056 @SuppressWarnings("unchecked") 057 public void registerListener(RequestListener listener) 058 { 059 Collection<RequestListener> listeners = (Collection) _context.getAttribute(org.ametys.runtime.request.RequestListenerManager.CONTEXT_ATTRIBUTE_REQUEST_LISTENERS); 060 listeners.add(listener); 061 } 062 063 /** 064 * Unregisters a RequestListener 065 * @param listener the listener being unregistered 066 */ 067 @SuppressWarnings("unchecked") 068 public void unregisterListener(RequestListener listener) 069 { 070 Collection<RequestListener> listeners = (Collection) _context.getAttribute(org.ametys.runtime.request.RequestListenerManager.CONTEXT_ATTRIBUTE_REQUEST_LISTENERS); 071 listeners.remove(listener); 072 } 073}