001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.cdi; 018 019import javax.enterprise.event.Event; 020import javax.inject.Inject; 021 022import org.apache.wicket.MetaDataKey; 023import org.apache.wicket.request.IRequestHandler; 024import org.apache.wicket.request.cycle.IRequestCycleListener; 025import org.apache.wicket.request.cycle.RequestCycle; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029/** 030 * Request cycle listener that fires the {@link DetachEvent} event 031 * 032 * @author igor 033 * 034 */ 035public class DetachEventEmitter implements IRequestCycleListener 036{ 037 private static final Logger logger = LoggerFactory.getLogger(DetachEventEmitter.class); 038 039 private static final MetaDataKey<Boolean> DETACH_SCHEDULED_KEY = new MetaDataKey<>() 040 { 041 private static final long serialVersionUID = 1L; 042 }; 043 044 @Inject 045 Event<DetachEvent> detachEvent; 046 047 /** 048 * Constructor 049 */ 050 public DetachEventEmitter() 051 { 052 NonContextual.of(DetachEventEmitter.class).postConstruct(this); 053 } 054 055 @Override 056 public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) 057 { 058 // this is a wicket request, schedule detach event to be fired 059 060 cycle.setMetaData(DETACH_SCHEDULED_KEY, true); 061 } 062 063 @Override 064 public void onDetach(RequestCycle cycle) 065 { 066 if (Boolean.TRUE.equals(cycle.getMetaData(DETACH_SCHEDULED_KEY))) 067 { 068 logger.debug("Firing Detach event {}", cycle.getRequest().getUrl()); 069 070 detachEvent.fire(new DetachEvent()); 071 072 cycle.setMetaData(DETACH_SCHEDULED_KEY, null); 073 } 074 } 075}