Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
COOS - Connected Objects Operating System (www.connectedobjects.org). Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved. DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. You may also contact one of the following for additional information: Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no) Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 
 package org.coos.messaging.plugin;
 
 
A channel connecting plugins to coos instances (router nodes)

Author(s):
Knut Eilif Husa, Tellu AS
 
 public class PluginChannel extends DefaultChannel {
 
     private static final String PROPERTY_CONNECTION_TIMEOUT = "connectionTimeout";
     private static final String PROPERTY_STARTUP_ORDERED = "startupOrdered";
 
     private int connectionTimeout = 10000; // Default value
     private boolean startupOrdered = true// Default value
 
     private String connectionErrorCause = null;
 
     public PluginChannel() {
     }
 
     public long getConnectionTimeout() {
         return ;
     }
 
     public void setConnectionTimeout(int connectionTimeout) {
         this. = connectionTimeout;
     }
 
     public boolean isStartupOrdered() {
         return ;
     }
 
     public void setStartupOrdered(boolean startupOrdered) {
         this. = startupOrdered;
     }
 
     public void addInFilter(Processor filter) {
         .addFilterProcessor(filter);
     }
 
     public void addOutFilter(Processor filter) {
         .addFilterProcessor(filter);
     }
 
     public void setProperties(Hashtable properties) {
         super.setProperties(properties);
 
         if (properties.get() != null) {
              = Integer.valueOf((Stringproperties.get(
                         )).intValue();
         }
 
         /*String startupOrderedStr = (String) properties.get(PROPERTY_STARTUP_ORDERED);
         if (startupOrderedStr != null && startupOrderedStr.equals("false")) {
                 startupOrdered = false;
         }*/
 
    }
    public synchronized void connect(Connectable connectablethrows ConnectingException {
        if (!(connectable instanceof Endpoint)) {
            throw new ConnectingException("This channel can only be connected to Endpoints.");
        }
        if () {
            throw new ConnectingException("This channel is already connected.");
        }
        this. = connectable;
        final Endpoint endpoint = (Endpointconnectable;
        try {
            if ( == null) {
                String className = "org.coos.messaging.transport.JvmTransport";
                Class transportClass = Class.forName(className);
                 = (TransporttransportClass.newInstance();
                .setChannel(this);
            }
            .setChainedProcessor(new DefaultProcessor() {
                    public void processMessage(Message msgthrows ProcessorException {
                        synchronized (PluginChannel.this) {
                            if (msg.getType().equals(.)) {
                                endpoint.setEndpointState(.);
                                 = msg.getHeader(.);
                            } else if (msg.getName().equals()) {
                                String allocUuid = msg.getHeader();
                                String routerUuid = msg.getHeader();
                                .setChainedProcessor();
                                .setDestinationUuid(routerUuid);
                                if (allocUuid != null) {
                                    .setDestinationUuid(allocUuid);
                                    endpoint.setEndpointUuid(allocUuid);
                                    if (endpoint.getEndpointUri() == null) {
                                        endpoint.setName(allocUuid);
                                        endpoint.setEndpointUri("coos://" + allocUuid);
                                    }
                                    //set the segment here if not allocated
                                    if (.equals("")) {
                                         = UuidHelper.getSegmentFromSegmentOrEndpointUuid(
                                                allocUuid);
                                    }
                                }
                                 = true;
                            }
                            PluginChannel.this.notifyAll();
                            /*new Thread(new Runnable() {
                                    public void run() {
                                        synchronized (PluginChannel.this) {
                                            System.out.println("Notifying waitthread");
                                            PluginChannel.this.notifyAll();
                                        }
                                    }
                                }).start();*/
                            if (! && !) {
                                throwConnectingException();
                            }
                        }
                    }
                });
            DefaultMessage msg = new DefaultMessage(.);
            if (endpoint.getEndpointUuid() == null) {
                // must allocate a uuid
                msg.setHeader();
            } else {
                // has predefined uuid
                msg.setHeader(endpoint.getEndpointUuid());
            }
            // Set the aliases of the endpoint as a property in the body of
            // the message
            Hashtable props = new Hashtable();
            props.put(.endpoint.getAliases());
            msg.setBody(props);
            .setChainedProcessor(endpoint);
            for (int i = 0; i < .size(); i++) {
                endpoint.addLink((String.elementAt(i), );
            }
            .setChainedProcessor();
            .start();
            .start();
            .start();
            .processMessage(msg);
            if () {
                if (!) {
                    this.wait();
                }
                if (!) {
                    throwConnectingException();
                }
            }
        } catch (Exception e) {
            disconnect();
            throw new ConnectingException("Connecting channel: " +  + " failed, cause: " +
                e.getClass().getName() + ": " + e.getMessage(), e);
        }
    }
    private void throwConnectingException() throws ConnectingException {
        if ( == null) {
            throw new ConnectingException("Timeout, no response from connecting coos router");
        } else {
            throw new ConnectingException();
        }
    }
    public synchronized void disconnect() {
        if (!) {
            return;
        }
        try {
             = false;
            if ( != null) {
                .removeLinkById(.getLinkId());
            }
            .stop();
            .stop();
            if ( != null) {
                .stop();
            }
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).warn("Exception when diconnecting"e);
        }
    }
    public boolean isConnected() {
        return ;
    }
New to GrepCode? Check out our FAQ X