org.asteriskjava.manager.internal.EventBuilderImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of asterisk-java Show documentation
Show all versions of asterisk-java Show documentation
The free Java library for Asterisk PBX integration.
/*
* Copyright 2004-2006 Stefan Reuter
*
* 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.asteriskjava.manager.internal;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.asteriskjava.manager.event.ManagerEvent;
import org.asteriskjava.manager.event.PeerEntryEvent;
import org.asteriskjava.manager.event.PeersEvent;
import org.asteriskjava.manager.event.ResponseEvent;
import org.asteriskjava.manager.event.UserEvent;
import org.asteriskjava.util.Log;
import org.asteriskjava.util.LogFactory;
import org.reflections.Reflections;
/**
* Default implementation of the EventBuilder interface.
*
* @author srt
* @version $Id$
* @see org.asteriskjava.manager.event.ManagerEvent
*/
class EventBuilderImpl extends AbstractBuilder implements EventBuilder
{
private static final Set ignoredAttributes = new HashSet<>(Arrays.asList("event"));
private Map> registeredEventClasses;
private final Set eventClassNegativeCache = new HashSet<>();
private static final Log logger = LogFactory.getLog(EventBuilderImpl.class);
private final static Set> knownManagerEventClasses = new Reflections(
"org.asteriskjava.manager.event").getSubTypesOf(ManagerEvent.class);
EventBuilderImpl()
{
this.registeredEventClasses = new HashMap<>();
registerBuiltinEventClasses();
}
/**
* register the known ManagerEventClasses for this builder
*/
private void registerBuiltinEventClasses()
{
for (Class< ? extends ManagerEvent> managerEventClass : knownManagerEventClasses)
{
if (!Modifier.isAbstract(managerEventClass.getModifiers()))
{
registerEventClass(managerEventClass);
}
}
}
public final void registerEventClass(Class< ? extends ManagerEvent> clazz) throws IllegalArgumentException
{
String className;
String eventType;
className = clazz.getName();
eventType = className.substring(className.lastIndexOf('.') + 1).toLowerCase(Locale.ENGLISH);
if (eventType.endsWith("event"))
{
eventType = eventType.substring(0, eventType.length() - "event".length());
}
if (UserEvent.class.isAssignableFrom(clazz) && !eventType.startsWith("userevent"))
{
eventType = "userevent" + eventType;
}
registerEventClass(eventType, clazz);
}
/**
* Registers a new event class for the event given by eventType.
*
* @param eventType the name of the event to register the class for. For
* example "Join".
* @param clazz the event class to register, must extend
* {@link ManagerEvent}.
* @throws IllegalArgumentException if clazz is not a valid event class.
*/
public final void registerEventClass(String eventType, Class< ? extends ManagerEvent> clazz)
throws IllegalArgumentException
{
Constructor< ? > defaultConstructor;
if (Modifier.isAbstract(clazz.getModifiers()))
{
throw new IllegalArgumentException(clazz + " is abstract");
}
if (clazz.isInterface())
{
throw new IllegalArgumentException(clazz + " is abstract");
}
try
{
defaultConstructor = clazz.getConstructor(Object.class);
if (!Modifier.isPublic(defaultConstructor.getModifiers()))
{
throw new IllegalArgumentException(clazz + " has no public default constructor");
}
}
catch (NoSuchMethodException ex)
{
throw new IllegalArgumentException(clazz + " has no usable constructor");
}
registeredEventClasses.put(eventType.toLowerCase(Locale.US), clazz);
logger.debug("Registered event type '" + eventType + "' (" + clazz + ")");
}
@SuppressWarnings("unchecked")
public ManagerEvent buildEvent(Object source, Map attributes)
{
ManagerEvent event;
String eventType = null;
Class< ? > eventClass;
Constructor< ? > constructor;
if (attributes.get("event") == null)
{
logger.error("No event type in properties");
return null;
}
if (attributes.get("event") instanceof List)
{
List< ? > eventNames = (List< ? >) attributes.get("event");
if (!eventNames.isEmpty() && "PeerEntry".equals(eventNames.get(0)))
{
// List of PeerEntry events was received (AJ-329)
// Convert map of lists to list of maps - one map for each
// PeerEntry event
int peersAmount = attributes.get("listitems") != null
? Integer.parseInt((String) attributes.get("listitems"))
: eventNames.size() - 1; // Last event is
// PeerlistComplete
List