changeset 177:28f536a049ec

8085816: Simplify native event handling framework Summary: Single event handler is introduces as substitution for UART*, GPIO* handlers Reviewed-by: alkonsta
author snazarki
date Thu, 04 Jun 2015 17:04:43 +0300
parents 3e30ad0fc940
children 481953419516
files src/se/classes/com/oracle/dio/gpio/impl/GPIOEventHandler.java src/se/classes/com/oracle/dio/gpio/impl/GPIOPinEventHandler.java src/se/classes/com/oracle/dio/gpio/impl/GPIOPortEventHandler.java src/se/classes/com/oracle/dio/impl/Event.java src/se/classes/com/oracle/dio/impl/EventQueue.java src/se/classes/com/oracle/dio/impl/EventQueueManager.java src/se/classes/com/oracle/dio/uart/impl/ModemSignalDispatcher.java src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java src/se/native/com/oracle/dio/dio_event_queue.cpp src/se/native/com/oracle/dio/dio_event_queue.h src/se/native/com/oracle/dio/gpio/impl/jni_gpio.cpp src/se/native/com/oracle/dio/uart/impl/jni_signal_dispatcher.cpp src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp src/share/classes/com/oracle/dio/gpio/impl/GPIOPinImpl.java src/share/classes/com/oracle/dio/gpio/impl/GPIOPortImpl.java src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java src/share/native/com/oracle/dio/javacall_uart.h
diffstat 18 files changed, 225 insertions(+), 582 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/gpio/impl/GPIOEventHandler.java	Tue Jun 02 18:05:59 2015 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,79 +0,0 @@
-/*
- * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package com.oracle.dio.gpio.impl;
-
-import java.util.Enumeration;
-import java.util.Hashtable;
-import jdk.dio.Device;
-import com.oracle.dio.impl.AbstractPeripheral;
-import com.oracle.dio.impl.Event;
-import com.oracle.dio.impl.EventHandler;
-import com.oracle.dio.impl.EventQueue;
-
-abstract class GPIOEventHandler<T extends AbstractPeripheral> implements EventHandler {
-
-    private final Hashtable<T, Object> listeners = new Hashtable<>();
-    private static final int QUEUE_BUFFER_SIZE = 4096;
-    protected final EventQueue queue = EventQueue.createEventQueue(QUEUE_BUFFER_SIZE);
-
-    protected <U extends GPIOEvent> GPIOEventHandler(Class<U> eventClass) {
-        queue.registerForEvent(eventClass, this);
-    }
-
-    synchronized void setEventListener(T peripheral, Object listener) {
-        listeners.put(peripheral, listener);
-    }
-
-    synchronized void removeEventListener(T peripheral) {
-        listeners.remove(peripheral);
-    }
-
-    public synchronized boolean handleEvent(Event event) {
-        GPIOEvent e = (GPIOEvent)event;
-        Enumeration<T> peripherals = listeners.keys();
-        while (peripherals.hasMoreElements()) {
-            T p = peripherals.nextElement();
-            if (p.getHandle().getNativeHandle() == e.getNativeHandle()) {
-                Object listener = listeners.get(p);
-                handleGPIOEvent(listener, p, e);
-            }
-        }
-        return true;
-    }
-
-    protected abstract void handleGPIOEvent(Object listener, T peripheral, GPIOEvent event);
-
-    protected static abstract class GPIOEvent extends Event {
-        int getNativeHandle() {
-            byte[] payload = getPayload();
-            int handle = ((0x00ff & payload[0]) << 24) |
-                         ((0x00ff & payload[1]) << 16) |
-                         ((0x00ff & payload[2]) << 8)  |
-                         ((0x00ff & payload[3]));
-            return handle;
-        }
-    }
-}
--- a/src/se/classes/com/oracle/dio/gpio/impl/GPIOPinEventHandler.java	Tue Jun 02 18:05:59 2015 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,66 +0,0 @@
-/*
- * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package com.oracle.dio.gpio.impl;
-
-import jdk.dio.gpio.PinEvent;
-import jdk.dio.gpio.PinListener;
-import java.nio.Buffer;
-
-final class GPIOPinEventHandler extends GPIOEventHandler<GPIOPinImpl> {
-
-    private static final GPIOPinEventHandler instance = new GPIOPinEventHandler();
-
-    private GPIOPinEventHandler() {
-        super(InternalEvent.class);
-    }
-
-    static GPIOPinEventHandler getInstance() {
-        return instance;
-    }
-
-    static {
-        setNativeEntries(instance.queue.getNativeBuffer(), InternalEvent.class);
-    }
-
-    protected void handleGPIOEvent(Object listener, GPIOPinImpl pin, GPIOEvent event) {
-        boolean value = ((InternalEvent)event).getValue();
-        PinListener pinListener = (PinListener)listener;
-        pinListener.valueChanged(new PinEvent(pin, value));
-    }
-
-    public static class InternalEvent extends GPIOEvent {
-        boolean getValue() {
-            byte[] payload = getPayload();
-            int value = ((int)(0x00ff & payload[4]) << 24) |
-                        ((int)(0x00ff & payload[5]) << 16) |
-                        ((int)(0x00ff & payload[6]) << 8)  |
-                        ((int)(0x00ff & payload[7]));
-            return (value != 0);
-        }
-    }
-
-    private static native void setNativeEntries(Buffer buffer, Class<InternalEvent> eventClass);
-}
--- a/src/se/classes/com/oracle/dio/gpio/impl/GPIOPortEventHandler.java	Tue Jun 02 18:05:59 2015 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,66 +0,0 @@
-/*
- * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package com.oracle.dio.gpio.impl;
-
-import jdk.dio.gpio.PortEvent;
-import jdk.dio.gpio.PortListener;
-import java.nio.Buffer;
-
-final class GPIOPortEventHandler extends GPIOEventHandler<GPIOPortImpl> {
-
-    private static final GPIOPortEventHandler instance = new GPIOPortEventHandler();
-
-    private GPIOPortEventHandler() {
-        super(InternalEvent.class);
-    }
-
-    static GPIOPortEventHandler getInstance() {
-        return instance;
-    }
-
-    static {
-        setNativeEntries(instance.queue.getNativeBuffer(), InternalEvent.class);
-    }
-
-    protected void handleGPIOEvent(Object listener, GPIOPortImpl port, GPIOEvent event) {
-        int value = ((InternalEvent)event).getValue();
-        PortListener portListener = (PortListener)listener;
-        portListener.valueChanged(new PortEvent(port, value));
-    }
-
-    class InternalEvent extends GPIOEvent {
-        int getValue() {
-            byte[] payload = getPayload();
-            int value = ((int)(0x00ff & payload[4]) << 24) |
-                        ((int)(0x00ff & payload[5]) << 16) |
-                        ((int)(0x00ff & payload[6]) << 8)  |
-                        ((int)(0x00ff & payload[7]));
-            return value;
-        }
-    }
-
-    private static native void setNativeEntries(Buffer buffer, Class<InternalEvent> eventClass);
-}
--- a/src/se/classes/com/oracle/dio/impl/Event.java	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/classes/com/oracle/dio/impl/Event.java	Thu Jun 04 17:04:43 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,26 +24,22 @@
  */
 
 package com.oracle.dio.impl;
+import jdk.dio.Device;
 
 /**
  * Base class for all events.
  */
 public class Event {
     private byte[] payload;
-
+    private Class<? extends Device> eventType;
     /**
      * Creates event object with payload.
      * @param payload event payload
+     * @param clazz  device class that describes event type
      */
-    public Event(byte[] payload) {
+    public Event(Class<? extends Device> clazz, byte[] payload) {
         this.payload = payload;
-    }
-
-    /**
-     * Creates event object with empty payload.
-     */
-    public Event() {
-        payload = null;
+        eventType = clazz;
     }
 
     /**
@@ -55,8 +51,9 @@
         return payload;
     }
 
-    void setPayload(byte[] payload) {
-        this.payload = payload;
+    /** Returns type of this event. */
+    public Class<? extends Device> getType() {
+        return eventType;
     }
 
     @Override
--- a/src/se/classes/com/oracle/dio/impl/EventQueue.java	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/classes/com/oracle/dio/impl/EventQueue.java	Thu Jun 04 17:04:43 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,8 @@
 import java.util.ArrayList;
 import java.util.LinkedList;
 
+import jdk.dio.Device;
+
 /**
  * Event queue implementation.
  */
@@ -55,15 +57,7 @@
         buffer = ByteBuffer.allocateDirect(bufferSize);
         buffer.position(0).limit(0);
         startQueue();
-    }
-
-    /**
-     * This method creates new event queue.
-     * @param bufferSize size of the native buffer in bytes
-     * @return event queue
-     */
-    public static EventQueue createEventQueue(int bufferSize) {
-        return new EventQueue(bufferSize);
+        setNativeEntries(buffer);
     }
 
     /**
@@ -82,22 +76,15 @@
                 try {
                     while (true) {
                         while (buffer.hasRemaining()) {
-                            Class<? extends Event> eventClass = getEventClass(buffer, buffer.position());
+                            Class<? extends Device> eventType = getEventType(buffer);
                             byte[] payload = null;
-                            byte hibyte = buffer.get();
-                            byte lobyte = buffer.get();
-                            short len = (short)((0xff00 & (hibyte << 8)) | (lobyte & 0xff));
+                            short len = buffer.getShort();
                             if (len > 0) {
                                 payload = new byte[len];
                                 buffer.get(payload);
                             }
-                            try {
-                                Event e = eventClass.newInstance();
-                                e.setPayload(payload);
-                                postEvent(e);
-                            } catch (InstantiationException | IllegalAccessException ex) {
-                                // do nothing, just skip
-                            }
+                            Event e = new Event(eventType, payload);
+                            postEvent(e);
                         }
                         buffer.position(0).limit(0);
                         buffer.wait();
@@ -155,7 +142,7 @@
      * @param evtClass class object of event class
      * @param handler listener
      */
-    public <T extends Event> void registerForEvent(Class<T> evtClass, EventHandler handler) {
+    public <T extends Device> void registerForEvent(Class<T> evtClass, EventHandler handler) {
         if (evtClass == null || handler == null) {
             throw new IllegalArgumentException();
         }
@@ -186,15 +173,19 @@
         eventDispatchThread.start();
     }
 
-    /**
-     * Returns native buffer. This is intended solely for components with native
-     * event generating. Java components must not call any methods of the
-     * returned buffer object.
-     * @return native buffer
-     */
-    public Buffer getNativeBuffer() {
-        return buffer;
+    private Class<? extends Device> getEventType(ByteBuffer buffer) {
+        char c;
+        StringBuilder sb = new StringBuilder();
+        while (0 != (c = (char)buffer.get())) {
+            sb.append(c);
+        }
+        try {
+            return (Class<? extends Device>)Class.forName(sb.toString());
+        } catch (ClassNotFoundException cnfe) {
+        }
+
+        return Device.class;
     }
 
-    private static native Class<? extends Event> getEventClass(ByteBuffer buffer, int position);
+    private static native void setNativeEntries(ByteBuffer buffer);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/se/classes/com/oracle/dio/impl/EventQueueManager.java	Thu Jun 04 17:04:43 2015 +0300
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+
+package com.oracle.dio.impl;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.IntBuffer;
+import java.util.Hashtable;
+
+import com.oracle.dio.impl.AbstractPeripheral;
+
+import jdk.dio.Device;
+import jdk.dio.DeviceEvent;
+
+public final class EventQueueManager implements EventHandler {
+
+    private final Hashtable<Integer, AbstractPeripheral> listenerRegistry = new Hashtable();
+
+    private static final EventQueueManager instance = new EventQueueManager();
+
+    private final EventQueue queue = EventQueue.getSharedEventQueue();
+
+    public static EventQueueManager getInstance() {
+        return instance;
+    }
+
+    private EventQueueManager() {
+    }
+
+    private int getHash(int deviceType, int eventType, int nativeHandle) {
+        return deviceType * (deviceType + deviceType * eventType) + nativeHandle;
+    }
+
+    public void setEventListener(Class<? extends Device> clazz, int eventSubType, AbstractPeripheral listener) {
+        final int eventType = clazz.hashCode();
+        final int nativeHandle = listener.getHandle().getNativeHandle();
+        queue.registerForEvent(clazz, this);
+        listenerRegistry.put(getHash(eventType, eventSubType, nativeHandle), listener);
+    }
+
+    public void removeEventListener(Class<? extends Device> clazz, int eventSubType, AbstractPeripheral listener) {
+        final int eventType = clazz.hashCode();
+        final int nativeHandle = listener.getHandle().getNativeHandle();
+        listenerRegistry.remove(getHash(eventType, eventSubType, nativeHandle));
+    }
+
+
+    /**
+     * This method is called by EventQueue.dispatch(). Each call is made on a
+     * separate thread.
+     * @param event a previously queued event to handle
+     */
+    public boolean handleEvent(Event event) {
+        IntBuffer payload = ByteBuffer.wrap(event.getPayload()).asIntBuffer();
+        // all data are stored in big endia format
+        int handle = payload.get();
+        int subEvent  = payload.get();
+        int data   = payload.get();
+        AbstractPeripheral handler = listenerRegistry.get(getHash(event.getType().hashCode(), subEvent, handle));
+        if (handler != null) {
+            handler.processNativeEvent(subEvent, data);
+        }
+        return true;
+    }
+
+    public boolean isDispatchThread() {
+        return queue.getEventDispatchThread().equals(Thread.currentThread());
+    }
+}
--- a/src/se/classes/com/oracle/dio/uart/impl/ModemSignalDispatcher.java	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/classes/com/oracle/dio/uart/impl/ModemSignalDispatcher.java	Thu Jun 04 17:04:43 2015 +0300
@@ -24,12 +24,15 @@
  */
 package com.oracle.dio.uart.impl;
 
+import java.nio.Buffer;
+import java.util.*;
+
+import com.oracle.dio.impl.Event;
+import com.oracle.dio.impl.EventHandler;
+import com.oracle.dio.impl.EventQueue;
 import com.oracle.dio.utils.Constants;
-import com.oracle.dio.impl.EventQueue;
-import com.oracle.dio.impl.EventHandler;
-import com.oracle.dio.impl.Event;
-import java.util.*;
-import java.nio.Buffer;
+
+import jdk.dio.uart.ModemUART;
 
 /**
  * Serial signal proxy
@@ -61,11 +64,10 @@
 
     private static ModemSignalDispatcher instance;
     private static final int QUEUE_BUFFER_SIZE = 4096;
-    private final EventQueue queue =
-                             EventQueue.createEventQueue(QUEUE_BUFFER_SIZE);
+    private final EventQueue queue = EventQueue.getSharedEventQueue();
 
     private ModemSignalDispatcher() {
-        queue.registerForEvent(SignalEvent.class, this);
+        queue.registerForEvent(ModemUART.class, this);
     }
 
     private static class SerialContext {
@@ -150,6 +152,11 @@
     }
 
     private static class SignalEvent extends Event {
+
+        SignalEvent(byte[] payload) {
+            super(ModemUART.class, payload);
+        }
+
         int getHandler() {
             byte[] payload = getPayload();
             int handler = (((int)(0x00ff & payload[0])) << 24) | (((int)(0x00ff & payload[1])) << 16) |
@@ -175,7 +182,7 @@
      * @param event a previously queued event to handle
      */
     public boolean handleEvent(Event event) {
-        SignalEvent e = (SignalEvent)event;
+        SignalEvent e = new SignalEvent(event.getPayload());
 
         int serialHandler = e.getHandler();
         int signalLine = e.getLine();
@@ -191,13 +198,6 @@
         return true;
     }
 
-    static {
-        setNativeEntries(instance.queue.getNativeBuffer(), SignalEvent.class);
-    }
-
-    private static native void setNativeEntries(Buffer buffer,
-                                                Class<SignalEvent> eventClass);
-
     /**
      * Starts serial signal listening
      *
--- a/src/se/classes/com/oracle/dio/uart/impl/UARTEventHandler.java	Tue Jun 02 18:05:59 2015 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,151 +0,0 @@
-/*
- * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.dio.uart.impl;
-
-import com.oracle.dio.impl.EventQueue;
-import com.oracle.dio.impl.EventHandler;
-import com.oracle.dio.impl.Event;
-import java.util.Hashtable;
-import java.nio.Buffer;
-
-class UARTEventHandler implements EventHandler {
-
-    private static class UARTHash {
-        final int port;
-        final int eventType;
-        final int hash;
-
-        UARTHash(int port, int eventType) {
-            this.port = port;
-            this.eventType = eventType;
-            long lHash = 17 + port;
-            lHash = 17 * lHash + eventType;
-            hash = (int)lHash;
-        }
-
-        @Override
-        public int hashCode() {
-            return hash;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (obj instanceof UARTHash) {
-                return (((UARTHash)obj).port == port &&
-                        ((UARTHash)obj).eventType == eventType);
-            } else {
-                return super.equals(obj);
-            }
-        }
-    }
-
-    private final Hashtable<UARTHash, UARTImpl> listenerRegistry = new Hashtable();
-    private static final UARTEventHandler instance = new UARTEventHandler();
-    private static final int QUEUE_BUFFER_SIZE = 4096;
-    private final EventQueue queue =
-                             EventQueue.createEventQueue(QUEUE_BUFFER_SIZE);
-
-    static UARTEventHandler getInstance() {
-        return instance;
-    }
-
-    private UARTEventHandler() {
-        queue.registerForEvent(UARTEvent.class, this);
-    }
-
-    void addEventListener(int eventType, UARTImpl uart) {
-        if (null == uart) {
-            throw new NullPointerException("uart == null");
-        }
-        int port = uart.getHandle().getNativeHandle();
-        listenerRegistry.put(new UARTHash(port, eventType), uart);
-    }
-
-    UARTImpl getEventListener(int port, int eventType) {
-        return listenerRegistry.get(new UARTHash(port, eventType));
-    }
-
-    void removeEventListener(int eventType, UARTImpl uart) {
-        int port = uart.getHandle().getNativeHandle();
-        listenerRegistry.remove(new UARTHash(port, eventType));
-    }
-
-    protected static class UARTEvent extends Event {
-        UARTEvent(byte[] payload) {
-            super(payload);
-        }
-
-        public UARTEvent() {
-            super();
-        }
-
-        int getPort() {
-            byte[] payload = getPayload();
-            int port = (((int)(0x00ff & payload[0])) << 24) | (((int)(0x00ff & payload[1])) << 16) |
-                       (((int)(0x00ff & payload[2])) << 8 ) | (((int)(0x00ff & payload[3])));
-            return port;
-        }
-        int getEventType() {
-            byte[] payload = getPayload();
-            int type = (((int)(0x00ff & payload[4])) << 24) | (((int)(0x00ff & payload[5])) << 16) |
-                       (((int)(0x00ff & payload[6]) << 8 ) | (((int)(0x00ff & payload[7]))));
-            return type;
-        }
-        int getBytesProcessed() {
-            byte[] payload = getPayload();
-            int bytes = (((int)(0x00ff & payload[8]))  << 24) | (((int)(0x00ff & payload[9]) << 16)) |
-                        (((int)(0x00ff & payload[10])) << 8 ) | (((int)(0x00ff & payload[11])));
-            return bytes;
-        }
-    }
-
-    /**
-     * This method is called by EventQueue.dispatch(). Each call is made on a
-     * separate thread.
-     * @param event a previously queued event to handle
-     */
-    public boolean handleEvent(Event event) {
-        UARTEvent e = (UARTEvent)event;
-        int port = e.getPort();
-        int type = e.getEventType();
-        int bytesProcessed = e.getBytesProcessed();
-        UARTImpl uart = listenerRegistry.get(new UARTHash(port, type));
-        if (uart != null) {
-            uart.processEvent(type, bytesProcessed);
-        }
-        return true;
-    }
-
-    static {
-        setNativeEntries(instance.queue.getNativeBuffer(), UARTEvent.class);
-    }
-
-    private static native void setNativeEntries(Buffer buffer,
-                                                Class<UARTEvent> eventClass);
-
-    public boolean isDispatchThread() {
-        return queue.getEventDispatchThread().equals(Thread.currentThread());
-    }
-}
--- a/src/se/native/com/oracle/dio/dio_event_queue.cpp	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/native/com/oracle/dio/dio_event_queue.cpp	Thu Jun 04 17:04:43 2015 +0300
@@ -26,30 +26,17 @@
 #include "dio_event_queue.h"
 #include <string.h>
 
-static const int JCLASS_SIZE = sizeof(jclass);
+/* Entities required for sending notifications */
+static jobject eventBuffer = NULL;
 
 extern "C" {
 
-JNIEXPORT jclass JNICALL Java_com_oracle_dio_impl_EventQueue_getEventClass
-  (JNIEnv* env, jclass clazz, jobject buffer, jint currPos) {
-    // required methods references
-    jclass bufferClass = env->GetObjectClass(buffer);
-
-    jmethodID setPositionID = bufferClass ? env->GetMethodID(bufferClass, "position", "(I)Ljava/nio/Buffer;") :
-                                            NULL;
-
-    // Not necessary to synchronize on buffer, it's always called from protected section
-    jbyte* buf = setPositionID ? (jbyte*)env->GetDirectBufferAddress(buffer) :
-                                 NULL;
-
-    jclass eventClass = NULL;
-    if (buf) {
-        memcpy(&eventClass, buf + currPos, JCLASS_SIZE);
-        // update buffer position
-        env->CallObjectMethod(buffer, setPositionID, currPos + JCLASS_SIZE);
+JNIEXPORT void JNICALL Java_com_oracle_dio_impl_EventQueue_setNativeEntries
+  (JNIEnv* env, jobject buffer) {
+    if (eventBuffer) {
+        env->DeleteGlobalRef(eventBuffer);
     }
-
-    return eventClass;
+    eventBuffer = env->NewGlobalRef(buffer);
 }
 
 } /* extern "C */
@@ -60,11 +47,11 @@
  * of the com.oracle.dio.impl.EventQueue class for details.
  */
 void event_queue_put_native_event
-  (JavaVM* cachedJVM, jobject eventBufferRef, jclass eventClass, const char *payload, int payload_size) {
+  (JavaVM* cachedJVM, const char* device_type, const char *payload, int payload_size) {
     JNIEnv* env;
     cachedJVM->AttachCurrentThread((void**)&env, NULL);
 
-    jclass bufferClass = env->GetObjectClass(eventBufferRef);
+    jclass bufferClass = env->GetObjectClass(eventBuffer);
     jmethodID notifyID = bufferClass ? env->GetMethodID(bufferClass, "notify", "()V") :
                                        NULL;
     jmethodID limitID = notifyID ? env->GetMethodID(bufferClass, "limit", "()I") :
@@ -72,34 +59,35 @@
     jmethodID setLimitID = limitID ? env->GetMethodID(bufferClass, "limit", "(I)Ljava/nio/Buffer;") :
                                      NULL;
 
+    const int device_type_len = strlen(device_type) + 1;
     if (setLimitID) {
-        env->MonitorEnter(eventBufferRef);
+        env->MonitorEnter(eventBuffer);
 
         if (env->ExceptionCheck() != JNI_TRUE) {
             // check enough space in direct buffer
-            jlong capacity = env->GetDirectBufferCapacity(eventBufferRef);
-            jint limit = env->CallIntMethod(eventBufferRef, limitID);
+            jlong capacity = env->GetDirectBufferCapacity(eventBuffer);
+            jint limit = env->CallIntMethod(eventBuffer, limitID);
 
-            jint newLimit = limit + JCLASS_SIZE + payload_size + 2;
+            jint newLimit = limit + device_type_len + payload_size + 2;
 
             if (newLimit < capacity) {
-                jbyte* buf = (jbyte*)env->GetDirectBufferAddress(eventBufferRef);
+                jbyte* buf = (jbyte*)env->GetDirectBufferAddress(eventBuffer);
 
                 buf += limit;
 
-                memcpy(buf, &eventClass, JCLASS_SIZE);
-                buf += JCLASS_SIZE;
+                memcpy(buf, device_type, device_type_len);
+                buf += device_type_len;
 
                 // payload
                 *buf++ = (jbyte)((payload_size & 0xFF00) >> 8); // high byte
                 *buf++ = (jbyte)(payload_size & 0xFF);          // then low byte
                 memcpy(buf, payload, payload_size);
 
-                env->CallObjectMethod(eventBufferRef, setLimitID, newLimit);
-                env->CallVoidMethod(eventBufferRef, notifyID);
+                env->CallObjectMethod(eventBuffer, setLimitID, newLimit);
+                env->CallVoidMethod(eventBuffer, notifyID);
             }
 
-            env->MonitorExit(eventBufferRef);
+            env->MonitorExit(eventBuffer);
         }
     }
     cachedJVM->DetachCurrentThread();
--- a/src/se/native/com/oracle/dio/dio_event_queue.h	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/native/com/oracle/dio/dio_event_queue.h	Thu Jun 04 17:04:43 2015 +0300
@@ -34,6 +34,6 @@
  * of the com.oracle.dio.impl.EventQueue class for details.
  */
 void event_queue_put_native_event
-  (JavaVM* cachedJVM, jobject eventBufferRef, jclass eventClass, const char* payload, int payload_size);
+  (JavaVM* cachedJVM, const char* device_type, const char *payload, int payload_size);
 
 #endif /* __DEVICEACCESS_EVENT_QUEUE_H */
--- a/src/se/native/com/oracle/dio/gpio/impl/jni_gpio.cpp	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/native/com/oracle/dio/gpio/impl/jni_gpio.cpp	Thu Jun 04 17:04:43 2015 +0300
@@ -358,47 +358,6 @@
 }
 
 
-/* Entities required for sending GPIO pin notifications */
-static jobject pinEventBuffer = NULL;
-static jclass pinEventClass = NULL;
-
-/*
- * Sets references to the Java entities required for sending notifications.
- * Class:     com_oracle_dio_gpio_impl_GPIOPinEventHandler
- * Method:    setNativeEntries
- */
-JNIEXPORT void JNICALL Java_com_oracle_dio_gpio_impl_GPIOPinEventHandler_setNativeEntries
-  (JNIEnv* env, jclass clazz, jobject buffer, jclass event) {
-    if (pinEventBuffer) {
-        env->DeleteGlobalRef(pinEventBuffer);
-    }
-    pinEventBuffer = env->NewGlobalRef(buffer);
-    if (pinEventClass) {
-        env->DeleteGlobalRef(pinEventClass);
-    }
-    pinEventClass = (jclass)env->NewGlobalRef(event);
-}
-
-/* Entities required for sending GPIO port notifications */
-static jobject portEventBuffer = NULL;
-static jclass portEventClass = NULL;
-
-/*
- * Sets references to the Java entities required for sending notifications.
- * Class:     com_oracle_dio_gpio_impl_GPIOPinEventHandler
- * Method:    setNativeEntries
- */
-JNIEXPORT void JNICALL Java_com_oracle_dio_gpio_impl_GPIOPortEventHandler_setNativeEntries
-  (JNIEnv* env, jclass clazz, jobject buffer, jclass event) {
-    if (portEventBuffer) {
-        env->DeleteGlobalRef(portEventBuffer);
-    }
-    portEventBuffer = env->NewGlobalRef(buffer);
-    if (portEventClass) {
-        env->DeleteGlobalRef(portEventClass);
-    }
-    portEventClass = (jclass)env->NewGlobalRef(event);
-}
 
 /*
  * Sends a pin value change event.
@@ -407,9 +366,6 @@
  */
 void javanotify_gpio_pin_value_changed
   (const javacall_handle handle, const javacall_int32 value) {
-    if (pinEventBuffer == NULL || pinEventClass == NULL) {
-        return;
-    }
     device_reference device = getDeviceReference(handle);
     if (device == INVALID_DEVICE_REFERENCE) {
         return;
@@ -421,7 +377,7 @@
     payload[4] = value >> 24, payload[5] = value >> 16;
     payload[6] = value >> 8,  payload[7] = value;
     JavaVM* vm = getGlobalJavaVM();
-    event_queue_put_native_event(vm, pinEventBuffer, pinEventClass, payload, size);
+    event_queue_put_native_event(vm, "jdk.dio.gpio.GPIOPin", payload, size);
 }
 
 /*
@@ -431,9 +387,6 @@
  */
 void javanotify_gpio_port_value_changed
   (const javacall_handle handle, const javacall_int32 value) {
-    if (portEventBuffer == NULL || portEventClass == NULL) {
-        return;
-    }
     device_reference device = getDeviceReference(handle);
     if (device == INVALID_DEVICE_REFERENCE) {
         return;
@@ -445,7 +398,7 @@
     payload[4] = value >> 24, payload[5] = value >> 16;
     payload[6] = value >> 8,  payload[7] = value;
     JavaVM* vm = getGlobalJavaVM();
-    event_queue_put_native_event(vm, portEventBuffer, portEventClass, payload, size);
+    event_queue_put_native_event(vm, "jdk.dio.gpio.GPIOPort", payload, size);
 }
 
 } // extern "C"
--- a/src/se/native/com/oracle/dio/uart/impl/jni_signal_dispatcher.cpp	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/native/com/oracle/dio/uart/impl/jni_signal_dispatcher.cpp	Thu Jun 04 17:04:43 2015 +0300
@@ -31,27 +31,6 @@
 
 extern "C" {
 
-/* Entities required for sending modem signal notifications */
-static jobject eventBuffer = NULL;
-static jclass eventClass = NULL;
-
-/*
- * Class      com_oracle_dio_uart_impl_ModemSignalDispatcher
- * Method:    setNativeEntries
- * Signature: (Ljava/nio/Buffer;Ljava/lang/Class;)V
- */
-JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_ModemSignalDispatcher_setNativeEntries
-  (JNIEnv* env, jclass clazz, jobject buffer, jclass event) {
-    if (eventBuffer) {
-        env->DeleteGlobalRef(eventBuffer);
-    }
-    eventBuffer = env->NewGlobalRef(buffer);
-    if (eventClass) {
-        env->DeleteGlobalRef(eventClass);
-    }
-    eventClass = (jclass)env->NewGlobalRef(event);
-}
-
 /*
  * Class:     com_oracle_dio_uart_impl_ModemSignalDispatcher
  * Method:    startListening0
@@ -93,9 +72,6 @@
  */
 void javanotify_serial_signal
   (javacall_handle port, javacall_handle target, javacall_serial_signal_type signal, javacall_bool value) {
-    if (eventBuffer == NULL || eventClass == NULL) {
-        return;
-    }
 
     device_reference device = getDeviceReference(port);
     if (device == INVALID_DEVICE_REFERENCE) {
@@ -103,10 +79,10 @@
     }
 
     // reserve 4 bytes for the port handler, 4 bytes for the signal line,
-    // and 1 byte for the value
-    const int size = 9;
+    // and 4 byte for the value
+    const int size = 12;
     char payload[size];
-
+    memset(payload, 0, size);
     payload[0] = (jint)device >> 24, payload[1] = (jint)device >> 16;
     payload[2] = (jint)device >> 8,  payload[3] = (jint)device >> 0;
     payload[4] = (jint)signal >> 24, payload[5] = (jint)signal >> 16;
@@ -114,7 +90,7 @@
     payload[8] = (value != JAVACALL_FALSE ? 1 : 0);
 
     JavaVM* vm = getGlobalJavaVM();
-    event_queue_put_native_event(vm, eventBuffer, eventClass, payload, size);
+    event_queue_put_native_event(vm, "jdk.dio.uart.ModemUART", payload, size);
 }
 
 } // extern "C"
--- a/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Thu Jun 04 17:04:43 2015 +0300
@@ -33,30 +33,11 @@
 
 extern "C" {
 
-/* Entities required for sending uart notifications */
-static jobject eventBuffer = NULL;
-static jclass eventClass = NULL;
-
 /* Cleanup function */
 static javacall_dio_result uart_cleanup(javacall_handle handle) {
     return javacall_uart_close_start(handle, NULL);
 }
 
-/*
- * Class      com_oracle_dio_uart_impl_UARTEventHandler
- * Method:    setNativeEntries
- */
-JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_UARTEventHandler_setNativeEntries
-  (JNIEnv* env, jclass clazz, jobject buffer, jclass event) {
-    if (eventBuffer) {
-        env->DeleteGlobalRef(eventBuffer);
-    }
-    eventBuffer = env->NewGlobalRef(buffer);
-    if (eventClass) {
-        env->DeleteGlobalRef(eventClass);
-    }
-    eventClass = (jclass)env->NewGlobalRef(event);
-}
 
 /*
  * Class:     com_oracle_dio_uart_impl_UARTImpl
@@ -586,9 +567,6 @@
 void javanotify_uart_event
   (javacall_uart_event_type type, javacall_handle port, javacall_int32 bytes,
    javacall_result result) {
-    if (eventBuffer == NULL || eventClass == NULL) {
-        return;
-    }
 
     device_reference device = getDeviceReference(port);
     if (device == INVALID_DEVICE_REFERENCE) {
@@ -608,7 +586,7 @@
     payload[10] = (jint)bytes >> 8,  payload[11] = (jint)bytes >> 0;
 
     JavaVM* vm = getGlobalJavaVM();
-    event_queue_put_native_event(vm, eventBuffer, eventClass, payload, size);
+    event_queue_put_native_event(vm, "jdk.dio.uart.UART", payload, size);
 }
 
 /**
--- a/src/share/classes/com/oracle/dio/gpio/impl/GPIOPinImpl.java	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/share/classes/com/oracle/dio/gpio/impl/GPIOPinImpl.java	Thu Jun 04 17:04:43 2015 +0300
@@ -29,15 +29,17 @@
 import java.io.IOException;
 import java.security.AccessController;
 
+import com.oracle.dio.impl.EventQueueManager;
+import com.oracle.dio.impl.Handle;
 import com.oracle.dio.power.impl.PowerManagedBase;
 import com.oracle.dio.utils.Constants;
 import com.oracle.dio.utils.ExceptionMessage;
-import com.oracle.dio.impl.Handle;
 
 import jdk.dio.*;
 import jdk.dio.gpio.GPIOPin;
 import jdk.dio.gpio.GPIOPinConfig;
 import jdk.dio.gpio.GPIOPinPermission;
+import jdk.dio.gpio.PinEvent;
 import jdk.dio.gpio.PinListener;
 
 import romizer.*;
@@ -155,6 +157,13 @@
         return(getOutputMode0() ? OUTPUT : INPUT );
     }
 
+    public void processNativeEvent(int event, int data) {
+        PinListener listener = this.listener;
+        if (null != listener) {
+            listener.valueChanged(new PinEvent(this, data > 0));
+        }
+    }
+
     public synchronized void setInputListener(PinListener listener) throws java.io.IOException,
     UnavailableDeviceException,
     ClosedDeviceException{
@@ -168,7 +177,7 @@
         }
 
         if (null == listener) {
-            GPIOPinEventHandler.getInstance().removeEventListener(this);
+            EventQueueManager.getInstance().removeEventListener(GPIOPin.class, 0, this);
             if (null != this.listener) {
                 try {
                     stopNoti0();
@@ -178,17 +187,16 @@
             }
             this.listener = null;
         } else if (this.listener == null) {
-            GPIOPinEventHandler.getInstance().setEventListener(this, listener);
             this.listener = listener;
             try {
                 startNoti0();
             } catch (IOException ex) {
-                GPIOPinEventHandler.getInstance().removeEventListener(this);
                 this.listener = null;
                 throw new UnsupportedOperationException(
                     ExceptionMessage.format(ExceptionMessage.GPIO_CANNOT_START_NOTIFICATION)
                 );
             }
+            EventQueueManager.getInstance().setEventListener(GPIOPin.class, 0, this);
         } else {
             throw new IllegalStateException (
                 ExceptionMessage.format(ExceptionMessage.GPIO_LISTENER_ALREADY_ASSIGNED)
--- a/src/share/classes/com/oracle/dio/gpio/impl/GPIOPortImpl.java	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/share/classes/com/oracle/dio/gpio/impl/GPIOPortImpl.java	Thu Jun 04 17:04:43 2015 +0300
@@ -27,24 +27,27 @@
 
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.security.AccessController;
+
+import com.oracle.dio.impl.AbstractPeripheral;
+import com.oracle.dio.impl.EventQueueManager;
+import com.oracle.dio.utils.Constants;
+import com.oracle.dio.utils.ExceptionMessage;
 
 import jdk.dio.ClosedDeviceException;
-import jdk.dio.InvalidDeviceConfigException;
 import jdk.dio.DeviceDescriptor;
 import jdk.dio.DeviceManager;
 import jdk.dio.DeviceNotFoundException;
+import jdk.dio.InvalidDeviceConfigException;
+import jdk.dio.UnavailableDeviceException;
 import jdk.dio.UnsupportedDeviceTypeException;
-import jdk.dio.UnavailableDeviceException;
 import jdk.dio.gpio.GPIOPin;
 import jdk.dio.gpio.GPIOPinConfig;
 import jdk.dio.gpio.GPIOPort;
 import jdk.dio.gpio.GPIOPortConfig;
+import jdk.dio.gpio.GPIOPortPermission;
+import jdk.dio.gpio.PortEvent;
 import jdk.dio.gpio.PortListener;
-import com.oracle.dio.impl.AbstractPeripheral;
-import com.oracle.dio.utils.Constants;
-import com.oracle.dio.utils.ExceptionMessage;
-import jdk.dio.gpio.GPIOPortPermission;
-import java.security.AccessController;
 
 import romizer.*;
 
@@ -163,6 +166,13 @@
         setOutputMode0(((OUTPUT == direction)?true:false) );
     }
 
+    public void processNativeEvent(int event, int data) {
+        PortListener listener = this.listener;
+        if (null != listener) {
+            listener.valueChanged(new PortEvent(this, data));
+        }
+    }
+
     @Override
     public synchronized void setInputListener(PortListener listener) throws java.io.IOException,
                          UnavailableDeviceException,
@@ -176,7 +186,7 @@
         }
 
         if(null == listener){
-            GPIOPortEventHandler.getInstance().removeEventListener(this);
+            EventQueueManager.getInstance().removeEventListener(GPIOPort.class, 0, this);
             if(null != this.listener){
                 try {
                     stopNoti0();
@@ -186,18 +196,16 @@
             this.listener = null;
 
         }else if (null == this.listener) {
-            GPIOPortEventHandler.getInstance().setEventListener(this, listener);
             this.listener = listener;
             try {
                 startNoti0();
             } catch (IOException ex) {
-                GPIOPortEventHandler.getInstance().removeEventListener(this);
                 this.listener = null;
                 throw new UnsupportedOperationException (
                     ExceptionMessage.format(ExceptionMessage.GPIO_CANNOT_START_NOTIFICATION)
                 );
             }
-
+            EventQueueManager.getInstance().setEventListener(GPIOPort.class, 0, this);
         }else{
             throw new IllegalStateException (
                 ExceptionMessage.format(ExceptionMessage.GPIO_LISTENER_ALREADY_ASSIGNED)
--- a/src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/share/classes/com/oracle/dio/impl/AbstractPeripheral.java	Thu Jun 04 17:04:43 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,17 +25,19 @@
 
 package com.oracle.dio.impl;
 import java.io.IOException;
+import java.nio.Buffer;
 import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import com.oracle.dio.utils.Constants;
+import com.oracle.dio.utils.ExceptionMessage;
 
 import jdk.dio.ClosedDeviceException;
 import jdk.dio.Device;
 import jdk.dio.DeviceDescriptor;
+import jdk.dio.DeviceEvent;
 import jdk.dio.DeviceManager;
 import jdk.dio.UnavailableDeviceException;
-import com.oracle.dio.utils.Constants;
-import com.oracle.dio.utils.ExceptionMessage;
-import java.nio.Buffer;
-import java.nio.ByteOrder;
 
 /* It is recommended to synchronize subclass native operation on {@code handle} lock.
    @see {@link #unlock()} for the reason
@@ -206,4 +208,12 @@
     public ByteOrder getByteOrder() throws IOException, UnavailableDeviceException, ClosedDeviceException {
         return ByteOrder.LITTLE_ENDIAN;
     }
+
+    /* --------- functions to access from EventQueueManager --------------- */
+    /* Need to be overriden at childs */
+    protected void processDeviceEvent(DeviceEvent event) {
+    }
+
+    protected void processNativeEvent(int type, int data) {
+    }
 }
--- a/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/share/classes/com/oracle/dio/uart/impl/UARTImpl.java	Thu Jun 04 17:04:43 2015 +0300
@@ -34,6 +34,7 @@
 import java.util.Timer;
 import java.util.TimerTask;
 
+import com.oracle.dio.impl.EventQueueManager;
 import com.oracle.dio.power.impl.PowerManagedBase;
 import com.oracle.dio.utils.Configuration;
 import com.oracle.dio.utils.Constants;
@@ -194,7 +195,8 @@
         AccessController.checkPermission(new UARTPermission(getSecurityName(), DevicePermission.POWER_MANAGE));
     }
 
-    protected void processEvent(int event, int bytesProcessed){
+    @Override
+    protected void processNativeEvent(int event, int bytesProcessed) {
         UARTEventListener listener = eventListeners.get(event);
         if (listener != null){
             try{
@@ -321,6 +323,8 @@
         }//switch(event)
     }
 
+
+
     /**
      * Gets the current baud rate. If the baud rate was not set previously using {@link #setBaudRate(int)} the
      * peripheral configuration-specific default value is returned.
@@ -376,9 +380,7 @@
     @Override
     public synchronized void setBaudRate(int baudRate) throws IOException, UnavailableDeviceException, ClosedDeviceException{
         checkPowerState();
-        if(baudRate <= 0){
-            throw new IllegalArgumentException();
-        }
+        new UARTConfig.Builder().setBaudRate(baudRate);
         setBaudRate0( baudRate);
     }
 
@@ -389,6 +391,7 @@
     @Override
     public synchronized void setDataBits(int dataBits) throws IOException, UnavailableDeviceException, ClosedDeviceException{
         checkPowerState();
+        new UARTConfig.Builder().setDataBits(dataBits);
         setDataBits0( dataBits);
     }
 
@@ -425,7 +428,7 @@
     }
 
     private void subscribe(int eventId){
-        UARTEventHandler.getInstance().addEventListener(eventId, this);
+        EventQueueManager.getInstance().setEventListener(UART.class, eventId, this);
         setEventListener0(eventId);
     }
 
@@ -440,7 +443,7 @@
             }
         }
 
-        UARTEventHandler.getInstance().removeEventListener(eventId, this);
+        EventQueueManager.getInstance().removeEventListener(UART.class, eventId, this);
         removeEventListener0(eventId);
     }
 
@@ -672,7 +675,7 @@
                 int readRes = read0(dst);
                 shiftBufferPosition(dst, ret + readRes);
                 if ((0 == receiveTriggerLevel || readRes < receiveTriggerLevel) && dst.hasRemaining()) {
-                    if (!UARTEventHandler.getInstance().isDispatchThread()) {
+                    if (!EventQueueManager.getInstance().isDispatchThread()) {
                         /*
                          * the user calls read() from the event callback, or inputTimeout is 0
                          * exit immediatelly,
--- a/src/share/native/com/oracle/dio/javacall_uart.h	Tue Jun 02 18:05:59 2015 +0300
+++ b/src/share/native/com/oracle/dio/javacall_uart.h	Thu Jun 04 17:04:43 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it