Search in sources :

Example 16 with TooManyListenersException

use of java.util.TooManyListenersException in project openhab1-addons by openhab.

the class Stick method initialize.

/**
     * Initialize this device and open the serial port
     *
     * @throws PlugwiseInitializationException if port can not be opened
     */
@SuppressWarnings("rawtypes")
private void initialize() throws PlugwiseInitializationException {
    // Flush the deviceCache
    plugwiseDeviceCache.clear();
    // parse ports and if the default port is found, initialized the reader
    Enumeration portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (id.getName().equals(port)) {
                logger.debug("Serial port '{}' has been found.", port);
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("openHAB", 2000);
        } catch (PortInUseException e) {
            throw new PlugwiseInitializationException(e);
        }
        try {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
            throw new PlugwiseInitializationException(e);
        }
        // activate the DATA_AVAILABLE notifier
        serialPort.notifyOnDataAvailable(true);
        try {
            // set port parameters
            serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            throw new PlugwiseInitializationException(e);
        }
        try {
            // get the output stream
            outputChannel = Channels.newChannel(serialPort.getOutputStream());
        } catch (IOException e) {
            throw new PlugwiseInitializationException(e);
        }
    } else {
        StringBuilder sb = new StringBuilder();
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new PlugwiseInitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb);
    }
    initialised = true;
    // initialise the Stick
    sendMessage(new InitialiseRequestMessage());
}
Also used : TooManyListenersException(java.util.TooManyListenersException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) Enumeration(java.util.Enumeration) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) InitialiseRequestMessage(org.openhab.binding.plugwise.protocol.InitialiseRequestMessage) IOException(java.io.IOException)

Example 17 with TooManyListenersException

use of java.util.TooManyListenersException in project openhab1-addons by openhab.

the class SerialDevice method initialize.

/**
     * Initialize this device and open the serial port
     * 
     * @throws InitializationException
     *             if port can not be opened
     */
public void initialize() throws InitializationException {
    // parse ports and if the default port is found, initialized the reader
    Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (id.getName().equals(port)) {
                logger.debug("Serial port '{}' has been found.", port);
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("openHAB-Smarthomatic", DEFAULT_PORT);
        } catch (PortInUseException e) {
            throw new InitializationException(e);
        }
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {
            throw new InitializationException(e);
        }
        try {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
            throw new InitializationException(e);
        }
        // activate the DATA_AVAILABLE notifier
        serialPort.notifyOnDataAvailable(true);
        try {
            // set port parameters
            serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            throw new InitializationException(e);
        }
        try {
            // get the output stream
            outputStream = serialPort.getOutputStream();
        } catch (IOException e) {
            throw new InitializationException(e);
        }
    } else {
        StringBuilder sb = new StringBuilder();
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : TooManyListenersException(java.util.TooManyListenersException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException)

Example 18 with TooManyListenersException

use of java.util.TooManyListenersException in project roboguice by roboguice.

the class CheckedProviderTest method testProviderMethodWithManyExceptions.

public void testProviderMethodWithManyExceptions() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(RemoteProvider.class)
            String foo() throws InterruptedException, RuntimeException, RemoteException, AccessException, TooManyListenersException, BindException, SubBindException {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        // The only two that should fail are Interrupted & TooManyListeners.. the rest are OK.
        List<Message> errors = ImmutableList.copyOf(ce.getErrorMessages());
        assertEquals(InterruptedException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(0).getMessage());
        assertEquals(TooManyListenersException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(1).getMessage());
        assertEquals(2, errors.size());
    }
}
Also used : BindException(java.net.BindException) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) TooManyListenersException(java.util.TooManyListenersException) AccessException(java.rmi.AccessException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RemoteException(java.rmi.RemoteException)

Example 19 with TooManyListenersException

use of java.util.TooManyListenersException in project intellij-community by JetBrains.

the class XalanSupport method init.

public static boolean init(Transformer transformer, LocalDebugger dbg) {
    if (transformer instanceof TransformerImpl) {
        try {
            System.out.println("XALAN: " + Class.forName("org.apache.xalan.Version", true, transformer.getClass().getClassLoader()).getMethod("getVersion").invoke(null));
            final TransformerImpl tr = (TransformerImpl) transformer;
            tr.setErrorListener(new DefaultErrorHandler(false) {

                @Override
                public void fatalError(TransformerException exception) throws TransformerException {
                    if (!(exception.getCause() instanceof DebuggerStoppedException)) {
                        super.fatalError(exception);
                    }
                }
            });
            try {
                tr.getTraceManager().addTraceListener(new XalanTraceListener(dbg, tr));
            } catch (TooManyListenersException e) {
                throw new AssertionError(e);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) TooManyListenersException(java.util.TooManyListenersException) DefaultErrorHandler(org.apache.xml.utils.DefaultErrorHandler) TransformerException(javax.xml.transform.TransformerException) TooManyListenersException(java.util.TooManyListenersException) TransformerException(javax.xml.transform.TransformerException) DebuggerStoppedException(org.intellij.plugins.xsltDebugger.rt.engine.DebuggerStoppedException) DebuggerStoppedException(org.intellij.plugins.xsltDebugger.rt.engine.DebuggerStoppedException)

Example 20 with TooManyListenersException

use of java.util.TooManyListenersException in project jdk8u_jdk by JetBrains.

the class TestDragSourceAdapter method main.

public static void main(String[] args) throws Exception {
    DragSource ds = new DragSource();
    TestDragSourceAdapter dsa1 = new TestDragSourceAdapter(1);
    TestDragSourceAdapter dsa2 = new TestDragSourceAdapter(2);
    Component c = new Button();
    DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(c, DnDConstants.ACTION_COPY, e -> e.startDrag(null, null));
    MouseEvent me = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, 0, InputEvent.CTRL_MASK, 100, 100, 0, false);
    DragGestureEvent dge = new DragGestureEvent(dgr, DnDConstants.ACTION_COPY, new Point(100, 100), Arrays.asList(me));
    DragSourceContext dsc = new DragSourceContext(Toolkit.getDefaultToolkit().createDragSourceContextPeer(dge), dge, new Cursor(Cursor.HAND_CURSOR), null, null, new StringSelection("TEXT"), null);
    ds.addDragSourceListener(dsa1);
    ds.addDragSourceListener(dsa2);
    ds.addDragSourceListener(dsa2);
    ds.addDragSourceMotionListener(dsa1);
    ds.addDragSourceMotionListener(dsa1);
    ds.addDragSourceMotionListener(dsa2);
    dsc.addDragSourceListener(dsa2);
    byte[] serialized;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
        oos.writeObject(dsc);
        serialized = bos.toByteArray();
    }
    DragSourceContext dsc_copy;
    try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
        dsc_copy = (DragSourceContext) ois.readObject();
    }
    try {
        dsc_copy.addDragSourceListener(dsa1);
        throw new RuntimeException("Test failed. Listener addition succeeded");
    } catch (TooManyListenersException ignored) {
    }
    try {
        dsc_copy.addDragSourceListener(dsa2);
        throw new RuntimeException("Test failed. Listener addition succeeded");
    } catch (TooManyListenersException ignored) {
    }
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
        oos.writeObject(ds);
        serialized = bos.toByteArray();
    }
    DragSource ds_copy;
    try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
        ds_copy = (DragSource) ois.readObject();
    }
    DragSourceListener[] dsls = ds_copy.getDragSourceListeners();
    assertEquals(3, dsls.length, "DragSourceListeners number");
    assertEquals(1, Stream.of(dsls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
    assertEquals(2, Stream.of(dsls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
    DragSourceMotionListener[] dsmls = ds_copy.getDragSourceMotionListeners();
    assertEquals(3, dsmls.length, "DragSourceMotionListeners number");
    assertEquals(2, Stream.of(dsmls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
    assertEquals(1, Stream.of(dsmls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
}
Also used : MouseEvent(java.awt.event.MouseEvent) DragGestureEvent(java.awt.dnd.DragGestureEvent) DragSource(java.awt.dnd.DragSource) Point(java.awt.Point) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DragGestureRecognizer(java.awt.dnd.DragGestureRecognizer) DragSourceContext(java.awt.dnd.DragSourceContext) Cursor(java.awt.Cursor) ObjectOutputStream(java.io.ObjectOutputStream) DragSourceListener(java.awt.dnd.DragSourceListener) StringSelection(java.awt.datatransfer.StringSelection) TooManyListenersException(java.util.TooManyListenersException) Button(java.awt.Button) ByteArrayInputStream(java.io.ByteArrayInputStream) DragSourceMotionListener(java.awt.dnd.DragSourceMotionListener) Component(java.awt.Component) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

TooManyListenersException (java.util.TooManyListenersException)24 PortInUseException (gnu.io.PortInUseException)15 CommPortIdentifier (gnu.io.CommPortIdentifier)14 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)12 IOException (java.io.IOException)11 NoSuchPortException (gnu.io.NoSuchPortException)6 ImmutableList (com.google.common.collect.ImmutableList)4 AbstractModule (com.google.inject.AbstractModule)4 CreationException (com.google.inject.CreationException)4 SerialPortEvent (gnu.io.SerialPortEvent)4 AccessException (java.rmi.AccessException)4 RemoteException (java.rmi.RemoteException)4 List (java.util.List)4 Enumeration (java.util.Enumeration)3 CommPort (gnu.io.CommPort)2 SerialPort (gnu.io.SerialPort)2 SerialPortEventListener (gnu.io.SerialPortEventListener)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 InputStreamReader (java.io.InputStreamReader)2