Search in sources :

Example 1 with ListenerList

use of org.apache.pivot.util.ListenerList in project pivot by apache.

the class EventLogger method unregisterEventListeners.

/**
 * Unregisters event listeners on this event logger's source.
 */
private void unregisterEventListeners() {
    Method[] methods = source.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (ListenerList.class.isAssignableFrom(method.getReturnType()) && (method.getModifiers() & Modifier.STATIC) == 0) {
            ParameterizedType genericType = (ParameterizedType) method.getGenericReturnType();
            Type[] typeArguments = genericType.getActualTypeArguments();
            if (typeArguments.length == 1) {
                Type type = typeArguments[0];
                Class<?> listenerInterface;
                if (type instanceof ParameterizedType) {
                    ParameterizedType paramType = (ParameterizedType) type;
                    listenerInterface = (Class<?>) paramType.getRawType();
                } else {
                    listenerInterface = (Class<?>) type;
                }
                // Get the listener list
                Object listenerList;
                try {
                    listenerList = method.invoke(source);
                } catch (InvocationTargetException exception) {
                    throw new RuntimeException(exception);
                } catch (IllegalAccessException exception) {
                    throw new RuntimeException(exception);
                }
                // Get the listener for this interface
                Object listener = eventListenerProxies.get(listenerInterface);
                // Remove the listener
                Class<?> listenerListClass = listenerList.getClass();
                Method removeMethod;
                try {
                    removeMethod = listenerListClass.getMethod("remove", Object.class);
                } catch (NoSuchMethodException exception) {
                    throw new RuntimeException(exception);
                }
                try {
                    removeMethod.invoke(listenerList, listener);
                } catch (IllegalAccessException exception) {
                    throw new RuntimeException(exception);
                } catch (InvocationTargetException exception) {
                    throw new RuntimeException(exception);
                }
            }
        }
    }
}
Also used : ListenerList(org.apache.pivot.util.ListenerList) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 2 with ListenerList

use of org.apache.pivot.util.ListenerList in project pivot by apache.

the class BeanMonitor method invoke.

private void invoke(String methodName, boolean addProxy) {
    for (Method method : bean.getClass().getMethods()) {
        if (ListenerList.class.isAssignableFrom(method.getReturnType()) && (method.getModifiers() & Modifier.STATIC) == 0) {
            ParameterizedType genericType = (ParameterizedType) method.getGenericReturnType();
            Type[] typeArguments = genericType.getActualTypeArguments();
            if (typeArguments.length == 1) {
                Type type = typeArguments[0];
                Class<?> listenerInterface;
                if (type instanceof ParameterizedType) {
                    ParameterizedType paramType = (ParameterizedType) type;
                    listenerInterface = (Class<?>) paramType.getRawType();
                } else {
                    listenerInterface = (Class<?>) type;
                }
                // Get the listener list
                Object listenerList;
                try {
                    listenerList = method.invoke(bean);
                } catch (IllegalAccessException | InvocationTargetException exception) {
                    throw new RuntimeException(exception);
                }
                // Get the listener for this interface
                Object listener = beanListenerProxies.get(listenerInterface);
                if (listener == null) {
                    if (addProxy) {
                        listener = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { listenerInterface }, invocationHandler);
                        beanListenerProxies.put(listenerInterface, listener);
                    } else {
                        throw new IllegalStateException("Listener proxy is null.");
                    }
                }
                Class<?> listenerListClass = listenerList.getClass();
                Method classMethod;
                try {
                    classMethod = listenerListClass.getMethod(methodName, new Class<?>[] { Object.class });
                } catch (NoSuchMethodException exception) {
                    throw new RuntimeException(exception);
                }
                try {
                    classMethod.invoke(listenerList, new Object[] { listener });
                } catch (IllegalAccessException | InvocationTargetException exception) {
                    throw new RuntimeException(exception);
                }
            }
        }
    }
}
Also used : ListenerList(org.apache.pivot.util.ListenerList) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 3 with ListenerList

use of org.apache.pivot.util.ListenerList in project pivot by apache.

the class BXMLSerializer method processEndElement.

@SuppressWarnings("unchecked")
private void processEndElement() throws SerializationException {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    switch(element.type) {
        case INSTANCE:
        case INCLUDE:
        case REFERENCE:
            {
                // Apply attributes
                for (Attribute attribute : element.attributes) {
                    if (attribute.propertyClass == null) {
                        Dictionary<String, Object> dictionary;
                        if (element.value instanceof Dictionary<?, ?>) {
                            dictionary = (Dictionary<String, Object>) element.value;
                        } else {
                            dictionary = new BeanAdapter(element.value);
                        }
                        dictionary.put(attribute.name, attribute.value);
                    } else {
                        if (attribute.propertyClass.isInterface()) {
                            // The attribute represents an event listener
                            String listenerClassName = attribute.propertyClass.getName();
                            listenerClassName = listenerClassName.substring(listenerClassName.lastIndexOf('.') + 1);
                            String getListenerListMethodName = "get" + Character.toUpperCase(listenerClassName.charAt(0)) + listenerClassName.substring(1) + "s";
                            // Get the listener list
                            Method getListenerListMethod;
                            try {
                                Class<?> type = element.value.getClass();
                                getListenerListMethod = type.getMethod(getListenerListMethodName);
                            } catch (NoSuchMethodException exception) {
                                throw new SerializationException(exception);
                            }
                            Object listenerList;
                            try {
                                listenerList = getListenerListMethod.invoke(element.value);
                            } catch (InvocationTargetException exception) {
                                throw new SerializationException(exception);
                            } catch (IllegalAccessException exception) {
                                throw new SerializationException(exception);
                            }
                            // Create an invocation handler for this listener
                            AttributeInvocationHandler handler = new AttributeInvocationHandler(getEngineByName(language), attribute.name, (String) attribute.value);
                            Object listener = Proxy.newProxyInstance(classLoader, new Class<?>[] { attribute.propertyClass }, handler);
                            // Add the listener
                            Class<?> listenerListClass = listenerList.getClass();
                            Method addMethod;
                            try {
                                addMethod = listenerListClass.getMethod("add", Object.class);
                            } catch (NoSuchMethodException exception) {
                                throw new RuntimeException(exception);
                            }
                            try {
                                addMethod.invoke(listenerList, listener);
                            } catch (IllegalAccessException exception) {
                                throw new SerializationException(exception);
                            } catch (InvocationTargetException exception) {
                                throw new SerializationException(exception);
                            }
                        } else {
                            // The attribute represents a static setter
                            setStaticProperty(element.value, attribute.propertyClass, attribute.name, attribute.value);
                        }
                    }
                }
                if (element.parent != null) {
                    if (element.parent.type == Element.Type.WRITABLE_PROPERTY) {
                        // Set this as the property value; it will be applied
                        // later in the parent's closing tag
                        element.parent.value = element.value;
                    } else if (element.parent.value != null) {
                        // If the parent element has a default property, use it;
                        // otherwise, if the parent is a sequence, add the element to it.
                        Class<?> parentType = element.parent.value.getClass();
                        DefaultProperty defaultProperty = parentType.getAnnotation(DefaultProperty.class);
                        if (defaultProperty == null) {
                            if (element.parent.value instanceof Sequence<?>) {
                                Sequence<Object> sequence = (Sequence<Object>) element.parent.value;
                                sequence.add(element.value);
                            } else {
                                throw new SerializationException(element.parent.value.getClass() + " is not a sequence.");
                            }
                        } else {
                            String defaultPropertyName = defaultProperty.value();
                            BeanAdapter beanAdapter = new BeanAdapter(element.parent.value);
                            Object defaultPropertyValue = beanAdapter.get(defaultPropertyName);
                            if (defaultPropertyValue instanceof Sequence<?>) {
                                Sequence<Object> sequence = (Sequence<Object>) defaultPropertyValue;
                                try {
                                    sequence.add(element.value);
                                } catch (UnsupportedOperationException uoe) {
                                    beanAdapter.put(defaultPropertyName, element.value);
                                }
                            } else {
                                beanAdapter.put(defaultPropertyName, element.value);
                            }
                        }
                    }
                }
                break;
            }
        case READ_ONLY_PROPERTY:
            {
                Dictionary<String, Object> dictionary;
                if (element.value instanceof Dictionary<?, ?>) {
                    dictionary = (Dictionary<String, Object>) element.value;
                } else {
                    dictionary = new BeanAdapter(element.value);
                }
                // Process attributes looking for instance property setters
                for (Attribute attribute : element.attributes) {
                    if (attribute.propertyClass != null) {
                        throw new SerializationException("Static setters are not supported" + " for read-only properties.");
                    }
                    dictionary.put(attribute.name, attribute.value);
                }
                break;
            }
        case WRITABLE_PROPERTY:
            {
                if (element.propertyClass == null) {
                    Dictionary<String, Object> dictionary;
                    if (element.parent.value instanceof Dictionary) {
                        dictionary = (Dictionary<String, Object>) element.parent.value;
                    } else {
                        dictionary = new BeanAdapter(element.parent.value);
                    }
                    dictionary.put(element.name, element.value);
                } else {
                    if (element.parent == null) {
                        throw new SerializationException("Element does not have a parent.");
                    }
                    if (element.parent.value == null) {
                        throw new SerializationException("Parent value is null.");
                    }
                    setStaticProperty(element.parent.value, element.propertyClass, element.name, element.value);
                }
                break;
            }
        case LISTENER_LIST_PROPERTY:
            {
                // Evaluate the script
                String script = (String) element.value;
                // Get a new engine here in order to make the script function private to this object
                ScriptEngine scriptEngine = newEngineByName(language);
                try {
                    scriptEngine.eval(script);
                } catch (ScriptException exception) {
                    reportException(exception, script);
                    break;
                }
                // Create the listener and add it to the list
                BeanAdapter beanAdapter = new BeanAdapter(element.parent.value);
                ListenerList<?> listenerList = (ListenerList<?>) beanAdapter.get(element.name);
                Class<?> listenerListClass = listenerList.getClass();
                java.lang.reflect.Type[] genericInterfaces = listenerListClass.getGenericInterfaces();
                Class<?> listenerClass = (Class<?>) genericInterfaces[0];
                ElementInvocationHandler handler = new ElementInvocationHandler(scriptEngine);
                Method addMethod;
                try {
                    addMethod = listenerListClass.getMethod("add", Object.class);
                } catch (NoSuchMethodException exception) {
                    throw new RuntimeException(exception);
                }
                Object listener = Proxy.newProxyInstance(classLoader, new Class<?>[] { listenerClass }, handler);
                try {
                    addMethod.invoke(listenerList, listener);
                } catch (IllegalAccessException exception) {
                    throw new SerializationException(exception);
                } catch (InvocationTargetException exception) {
                    throw new SerializationException(exception);
                }
                break;
            }
        case SCRIPT:
            {
                String src = null;
                if (element.properties.containsKey(SCRIPT_SRC_ATTRIBUTE)) {
                    src = element.properties.get(SCRIPT_SRC_ATTRIBUTE);
                }
                if (src != null && src.charAt(0) == OBJECT_REFERENCE_PREFIX) {
                    src = src.substring(1);
                    if (src.length() > 0) {
                        if (!JSON.containsKey(namespace, src)) {
                            throw new SerializationException("Value \"" + src + "\" is not defined.");
                        }
                        String variableValue = JSON.get(namespace, src);
                        src = variableValue;
                    }
                }
                if (src != null) {
                    int i = src.lastIndexOf(".");
                    if (i == -1) {
                        throw new SerializationException("Cannot determine type of script \"" + src + "\".");
                    }
                    String extension = src.substring(i + 1);
                    ScriptEngine scriptEngine = getEngineByExtension(extension);
                    scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE);
                    try {
                        URL scriptLocation;
                        if (src.charAt(0) == SLASH_PREFIX) {
                            scriptLocation = classLoader.getResource(src.substring(1));
                            if (scriptLocation == null) {
                                // add a fallback
                                scriptLocation = new URL(location, src.substring(1));
                            }
                        } else {
                            scriptLocation = new URL(location, src);
                        }
                        BufferedReader scriptReader = null;
                        try {
                            scriptReader = new BufferedReader(new InputStreamReader(scriptLocation.openStream()));
                            scriptEngine.eval(NASHORN_COMPAT_SCRIPT);
                            scriptEngine.eval(scriptReader);
                        } catch (ScriptException exception) {
                            reportException(exception);
                        } finally {
                            if (scriptReader != null) {
                                scriptReader.close();
                            }
                        }
                    } catch (IOException exception) {
                        throw new SerializationException(exception);
                    }
                }
                if (element.value != null) {
                    // Evaluate the script
                    String script = (String) element.value;
                    ScriptEngine scriptEngine = getEngineByName(language);
                    scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE);
                    try {
                        scriptEngine.eval(NASHORN_COMPAT_SCRIPT);
                        scriptEngine.eval(script);
                    } catch (ScriptException exception) {
                        reportException(exception, script);
                    }
                }
                break;
            }
        case DEFINE:
            {
                // No-op
                break;
            }
        default:
            {
                break;
            }
    }
    // Move up the stack
    if (element.parent == null) {
        root = element.value;
    }
    element = element.parent;
}
Also used : ListenerList(org.apache.pivot.util.ListenerList) Dictionary(org.apache.pivot.collections.Dictionary) URL(java.net.URL) ScriptException(javax.script.ScriptException) SerializationException(org.apache.pivot.serialization.SerializationException) InputStreamReader(java.io.InputStreamReader) Method(java.lang.reflect.Method) Sequence(org.apache.pivot.collections.Sequence) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ScriptEngine(javax.script.ScriptEngine) BufferedReader(java.io.BufferedReader)

Example 4 with ListenerList

use of org.apache.pivot.util.ListenerList in project pivot by apache.

the class EventLogger method registerEventListeners.

/**
 * Registers event listeners on this event logger's source.
 */
private void registerEventListeners() {
    Method[] methods = source.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (ListenerList.class.isAssignableFrom(method.getReturnType()) && (method.getModifiers() & Modifier.STATIC) == 0) {
            ParameterizedType genericType = (ParameterizedType) method.getGenericReturnType();
            Type[] typeArguments = genericType.getActualTypeArguments();
            if (typeArguments.length == 1) {
                Type type = typeArguments[0];
                Class<?> listenerInterface;
                if (type instanceof ParameterizedType) {
                    ParameterizedType paramType = (ParameterizedType) type;
                    listenerInterface = (Class<?>) paramType.getRawType();
                } else {
                    listenerInterface = (Class<?>) type;
                }
                if (!listenerInterface.isInterface()) {
                    throw new RuntimeException(listenerInterface.getName() + " is not an interface.");
                }
                Method[] interfaceMethods = listenerInterface.getMethods();
                for (int j = 0; j < interfaceMethods.length; j++) {
                    Method interfaceMethod = interfaceMethods[j];
                    declaredEvents.add(interfaceMethod);
                }
                // Get the listener list
                Object listenerList;
                try {
                    listenerList = method.invoke(source);
                } catch (InvocationTargetException exception) {
                    throw new RuntimeException(exception);
                } catch (IllegalAccessException exception) {
                    throw new RuntimeException(exception);
                }
                // Get the listener for this interface
                Object listener = eventListenerProxies.get(listenerInterface);
                if (listener == null) {
                    listener = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { listenerInterface }, loggerInvocationHandler);
                    eventListenerProxies.put(listenerInterface, listener);
                }
                // Add the listener
                Class<?> listenerListClass = listenerList.getClass();
                Method addMethod;
                try {
                    addMethod = listenerListClass.getMethod("add", Object.class);
                } catch (NoSuchMethodException exception) {
                    throw new RuntimeException(exception);
                }
                try {
                    addMethod.invoke(listenerList, listener);
                } catch (IllegalAccessException exception) {
                    throw new RuntimeException(exception);
                } catch (InvocationTargetException exception) {
                    throw new RuntimeException(exception);
                }
            }
        }
    }
}
Also used : ListenerList(org.apache.pivot.util.ListenerList) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Method (java.lang.reflect.Method)4 ListenerList (org.apache.pivot.util.ListenerList)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptException (javax.script.ScriptException)1 Dictionary (org.apache.pivot.collections.Dictionary)1 Sequence (org.apache.pivot.collections.Sequence)1 SerializationException (org.apache.pivot.serialization.SerializationException)1