org.jboss.arquillian.warp.spi.LifecycleManager Maven / Gradle / Ivy
/**
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.warp.spi;
import org.jboss.arquillian.warp.Inspection;
import org.jboss.arquillian.warp.spi.exception.ObjectAlreadyAssociatedException;
import org.jboss.arquillian.warp.spi.exception.ObjectNotAssociatedException;
import org.jboss.arquillian.warp.spi.exception.StoreHasAssociatedObjectsException;
/**
*
* Provides ability of executing events in Warp lifecycle from outside of Arquillian extensions.
*
*
*
* Lifecycle manager can execute either:
*
*
*
* - {@link WarpLifecycleEvent} events - these will cause executing lifecycle verification methods in all associated
* {@link Inspection}s
* - any other Arquillian event
*
*
*
*
* LifecycleManager can be retrieved from arbitrary objects associated with current request using {@link LifecycleManagerStore}.
*
*
*
* Example:
*
*
*
*
* // having reference to request
* ServletRequest request = ...;
*
* LifecycleManager manager = LifecycleManagerStore.get(ServletRequest.class, request);
* manager.fireEvent(new CustomLifecycleEvent());
*
*
*
*
* with following implementation of CustomLifecycleEvent
*
*
*
*
* public class CustomLifecycleEvent extends WarpLifecycleEvent {
*
* public Annotation getAnnotation() {
* return new CustomAnnotation() {
* public Class extends Annotation> annotationType() {
* return CustomAnnotation.class;
* }
* };
* }
*
* }
*
*
*
*
* and following implementation of CustomAnnotation
*
*
*
*
* {@literal @}Retention(RUNTIME)
* {@literal @}Target(ElementType.METHOD)
* public {@literal @}interface CustomAnnotation {
* }
*
*
*
* This will cause executing following method in associated user-defined {@link Inspection}:
*
*
*
* {@literal @}CustomAnnotation
* public void verifyCustomBehavior() {
* ...
* }
*
*
*
*
* For more details how to implement CustomLifecycleEvent, see {@link WarpLifecycleEvent}.
*
*
* For more info on how to retrieve {@link LifecycleManagerStore}, see {@link LifecycleManagerStore#get(Class, Object)}.
*
*
* @author Lukas Fryc
*/
public abstract class LifecycleManager {
/**
*
*
* Executes either:
*
*
*
* - {@link WarpLifecycleEvent} events - these will cause executing lifecycle verification methods in all associated
* {@link Inspection}s
* - any other Arquillian event
*
*
* @param event any Arquillian event or specifically {@link WarpLifecycleEvent}
*/
public abstract void fireEvent(Object event);
/**
* Binds the current {@link LifecycleManager} with given object of given class.
*
* @param clazz the class to be bound
* @param object the object to be bound
* @throws ObjectAlreadyAssociatedException when there is already object bound with {@link LifecycleManager} for given
* class.
*/
public final void bindTo(Class clazz, T object) throws ObjectAlreadyAssociatedException {
LifecycleManagerStore.getCurrentStore().bind(this, clazz, object);
}
/**
* Unbinds the {@link LifecycleManager} from given class and given object.
*
* @param clazz the bound class
* @param object the bound class
* @throws ObjectNotAssociatedException when no object bound with {@link LifecycleManager}.
*/
public final void unbindFrom(Class clazz, T object) throws ObjectNotAssociatedException {
LifecycleManagerStore.getCurrentStore().unbind(this, clazz, object);
}
/**
* Verifies that there is no object bound with this {@link LifecycleManager}.
*
* @throws StoreHasAssociatedObjectsException when there is object bound with this {@link LifecycleManager}.
*/
public final void checkUnbound() throws StoreHasAssociatedObjectsException {
LifecycleManagerStore.getCurrentStore().checkUnbound(this);
}
}