org.coos.messaging.COOS Maven / Gradle / Ivy
/**
* 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 .
*
* 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;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.coos.messaging.jmx.ManagedObject;
import org.coos.messaging.jmx.ManagementFactory;
import org.coos.messaging.routing.Router;
import org.coos.messaging.routing.RouterChannel;
import org.coos.messaging.routing.RouterSegment;
import org.coos.messaging.routing.RoutingAlgorithm;
import org.coos.messaging.serializer.ObjectJavaSerializer;
import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;
/**
* COOS class.
*
* Description needed.
*
* The COOS instance has a reference to the COOS-container in which it is running.
*
* @author Knut Eilif Husa, Tellu AS
* @author Robert Bjarum, Tellu AS
*
*/
public class COOS {
private static final String COOS_INSTANCE_KEY = "COOSInstance";
private static final Log LOG = LogFactory.getLog(COOS.class.getName());
private String name;
private Router router;
private Map transports = new ConcurrentHashMap();
private Map channels = new ConcurrentHashMap();
private Map processors = new ConcurrentHashMap();
private Map channelServers = new ConcurrentHashMap();
private Map segmentMap = new ConcurrentHashMap();
private Map routingAlgorithmMap = new ConcurrentHashMap();
private COContainer coosContainer;
private boolean started;
/*
* The object used for Monitoring and Management of the COOS instance (i.e. using JMX)
*/
private ManagedObject managedObject = null;
protected COOS() {
SerializerFactory.registerSerializer(Message.SERIALIZATION_METHOD_JAVA, new ObjectJavaSerializer());
/*
* We do not want to see the context of the other loggers on the same thread in this logger
*/
LOG.setInheritMDC(false);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
LOG.putMDC(COOS_INSTANCE_KEY, getName());
}
/**
* Return reference to the COOS Container in which this COOS instance is running.
*
* @return COOS Container (COContainer)
*/
public COContainer getCoosContainer() {
return coosContainer;
}
public void setCoosContainer(COContainer coosContainer) {
this.coosContainer = coosContainer;
for (Transport transport : transports.values()) {
transport.setCoContainer(coosContainer);
}
for (Channel channel : channels.values()) {
channel.setCoContainer(coosContainer);
}
for (Processor processor : processors.values()) {
processor.setCoContainer(coosContainer);
}
}
public boolean isStarted() {
return started;
}
public Router getRouter() {
return router;
}
public void setRouter(Router router) {
this.router = router;
this.router.setCOOS(this);
}
public void addTransport(String name, Transport transport) {
transports.put(name, transport);
transport.setCoContainer(coosContainer);
}
public Transport getTransport(String name) {
return transports.get(name);
}
public void addChannel(String name, RouterChannel channel) {
channels.put(name, channel);
channel.setCoContainer(coosContainer);
}
public void removeChannel(String name) {
channels.remove(name);
}
public RouterChannel getChannel(String name) {
return channels.get(name);
}
public void addProcessor(String name, Processor processor) {
processors.put(name, processor);
processor.setCoContainer(coosContainer);
}
public Processor getProcessor(String name) {
return processors.get(name);
}
public Map getTransports() {
return transports;
}
public Map getChannels() {
return channels;
}
public Map getProcessors() {
return processors;
}
public Map getChannelServers() {
return channelServers;
}
public void addChannelServer(String name, ChannelServer server) {
channelServers.put(name, server);
}
public ChannelServer getChannelServer(String name) {
return channelServers.get(name);
}
public Map getSegmentMap() {
return segmentMap;
}
public void setSegmentMap(Map segmentMap) {
this.segmentMap = segmentMap;
}
public void addSegment(RouterSegment routerSegment) {
segmentMap.put(routerSegment.getName(), routerSegment);
}
public RouterSegment getSegment(String segmentName){
return segmentMap.get(segmentName);
}
public void addRoutingAlgorithm(String algorithmName, RoutingAlgorithm routingAlgorithm) {
routingAlgorithmMap.put(algorithmName, routingAlgorithm);
}
public RoutingAlgorithm getRoutingAlgorithm(String algorithmName){
return routingAlgorithmMap.get(algorithmName);
}
public void start() throws Exception {
if (coosContainer == null) {
LOG.warn("The COOS container property (COContainer) has not been set.");
}
LOG.info("Starting COOS " + name);
LOG.info("Starting processors");
for (Processor processor : processors.values()) {
if (processor instanceof Service) {
((Service) processor).start();
}
}
LOG.info("Starting channel servers");
for (ChannelServer channelServer : channelServers.values()) {
if (channelServer instanceof Service) {
((Service) channelServer).start();
}
}
LOG.info("Initializing channels");
for (Channel channel : channels.values()) {
if (channel.getTransport() != null) {
channel.connect(router);
}
}
LOG.info("Starting Router");
// router.setLoggingEnabled(true);
router.start();
/*
* Register COOS for monitoring. Need to cast, since the method registerCoos() is not defined in interface.
*/
managedObject = ManagementFactory.getPlatformManagementService().registerCoos(this);
LOG.info("COOS " + name + " successfully started");
started = true;
}
public void stop() throws Exception {
LOG.info("Stopping COOS " + name);
LOG.info("Stopping Router");
// router.setLoggingEnabled(true);
router.stop();
LOG.info("Stopping channels");
for (Channel channel : channels.values()) {
channel.disconnect();
}
LOG.info("Stopping channel servers");
for (ChannelServer channelServer : channelServers.values()) {
channelServer.stop();
}
LOG.info("Stopping processors");
for (Processor processor : processors.values()) {
if (processor instanceof Service) {
((Service) processor).stop();
}
}
LOG.info("COOS " + name + " stopped");
started = false;
if (managedObject != null) {
ManagementFactory.getPlatformManagementService().unregister(managedObject);
}
}
}