001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.metamodel.facets.actions.event; 021 022import java.lang.reflect.Constructor; 023import java.lang.reflect.InvocationTargetException; 024 025import org.apache.isis.applib.Identifier; 026import org.apache.isis.applib.services.eventbus.ActionInvokedEvent; 027import org.apache.isis.core.metamodel.facets.actions.invoke.ActionInvocationFacet; 028 029/** 030 * Extends the mechanism by which the action should be invoked by sending an 031 * Event to the internal Event Bus after being invoked without throwing 032 * an Exception. 033 */ 034public interface PostsActionInvokedEventFacet extends ActionInvocationFacet { 035 036 public static class Util { 037 private Util(){} 038 039 @SuppressWarnings("unchecked") 040 public static <S> ActionInvokedEvent<S> newEvent( 041 final Class<? extends ActionInvokedEvent<S>> type, 042 final S source, 043 final Identifier identifier, 044 final Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { 045 final Constructor<?>[] constructors = type.getConstructors(); 046 for (final Constructor<?> constructor : constructors) { 047 final Class<?>[] parameterTypes = constructor.getParameterTypes(); 048 if(parameterTypes.length != 3) { 049 continue; 050 } 051 if(!parameterTypes[0].isAssignableFrom(source.getClass())) { 052 continue; 053 } 054 if(!parameterTypes[1].isAssignableFrom(Identifier.class)) { 055 continue; 056 } 057 if(!parameterTypes[2].isAssignableFrom(Object[].class)) { 058 continue; 059 } 060 final Object event = constructor.newInstance(source, identifier, arguments); 061 return (ActionInvokedEvent<S>) event; 062 } 063 throw new NoSuchMethodException(type.getName()+".<init>(? super " + source.getClass().getName() + ", " + Identifier.class.getName() + ", [Ljava.lang.Object;)"); 064 } 065 } 066}