Search in sources :

Example 1 with WonNode

use of won.protocol.model.WonNode in project webofneeds by researchstudio-sat.

the class OwnerProtocolCommunicationServiceImpl method configureCamelEndpoint.

public final synchronized CamelConfiguration configureCamelEndpoint(URI wonNodeUri, String ownerId) throws Exception {
    CamelConfiguration camelConfiguration = new CamelConfiguration();
    URI brokerURI = activeMQService.getBrokerEndpoint(wonNodeUri);
    String ownerProtocolQueueName;
    List<WonNode> wonNodeList = wonNodeRepository.findByWonNodeURI(wonNodeUri);
    // OwnerProtocolCamelConfigurator ownerProtocolCamelConfigurator =
    // camelConfiguratorFactory.createCamelConfigurator(methodName);
    logger.debug("configuring camel endpoint");
    if (ownerProtocolCamelConfigurator.getBrokerComponentName(brokerURI) != null && ownerProtocolCamelConfigurator.getEndpoint(wonNodeUri) != null) {
        logger.debug("wonNode known");
        WonNode wonNode = wonNodeList.get(0);
        // brokerURI = wonNode.getBrokerURI();
        camelConfiguration.setEndpoint(wonNode.getOwnerProtocolEndpoint());
        if (ownerProtocolCamelConfigurator.getCamelContext().getComponent(wonNodeList.get(0).getBrokerComponent()) == null) {
            // camelConfiguration.setBrokerComponentName(ownerProtocolCamelConfigurator.addCamelComponentForWonNodeBroker
            // (wonNode.getWonNodeURI(),brokerURI,wonNode.getOwnerApplicationID()));
            ownerProtocolQueueName = activeMQService.getProtocolQueueNameWithResource(wonNodeUri);
            String endpoint = ownerProtocolCamelConfigurator.configureCamelEndpointForNodeURI(wonNodeUri, brokerURI, ownerProtocolQueueName);
            camelConfiguration.setBrokerComponentName(ownerProtocolCamelConfigurator.getBrokerComponentName(brokerURI));
            ownerProtocolCamelConfigurator.getCamelContext().getComponent(camelConfiguration.getBrokerComponentName()).createEndpoint(camelConfiguration.getEndpoint());
            if (ownerProtocolCamelConfigurator.getCamelContext().getRoute(wonNode.getStartingComponent()) == null)
                ownerProtocolCamelConfigurator.addRouteForEndpoint(null, wonNode.getWonNodeURI());
        }
    } else {
        // if unknown wonNode
        logger.debug("wonNode unknown");
        // TODO: brokerURI gets the node information already. so requesting node
        // information again for queuename would be duplicate
        ownerProtocolQueueName = activeMQService.getProtocolQueueNameWithResource(wonNodeUri);
        String endpoint = ownerProtocolCamelConfigurator.configureCamelEndpointForNodeURI(wonNodeUri, brokerURI, ownerProtocolQueueName);
        camelConfiguration.setEndpoint(endpoint);
        camelConfiguration.setBrokerComponentName(ownerProtocolCamelConfigurator.getBrokerComponentName(brokerURI));
        ownerProtocolCamelConfigurator.addRouteForEndpoint(null, wonNodeUri);
    }
    return camelConfiguration;
}
Also used : WonNode(won.protocol.model.WonNode) CamelConfiguration(won.protocol.jms.CamelConfiguration) URI(java.net.URI)

Example 2 with WonNode

use of won.protocol.model.WonNode in project webofneeds by researchstudio-sat.

the class OwnerProtocolCommunicationServiceImpl method isRegisteredWithWonNode.

public synchronized boolean isRegisteredWithWonNode(URI wonNodeURI) {
    try {
        String ownerApplicationId = calculateOwnerApplicationIdFromOwnerCertificate();
        logger.debug("using ownerApplicationId: {}", ownerApplicationId);
        WonNode wonNode = wonNodeRepository.findOneByWonNodeURIAndOwnerApplicationID(wonNodeURI, ownerApplicationId);
        return ownerProtocolCamelConfigurator.getCamelContext().getComponent(wonNode.getBrokerComponent()) != null;
    } catch (Exception e) {
        logger.info("error while checking if we are registered with WoN node " + wonNodeURI, e);
    }
    return false;
}
Also used : WonNode(won.protocol.model.WonNode) CamelConfigurationFailedException(won.protocol.exception.CamelConfigurationFailedException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with WonNode

use of won.protocol.model.WonNode in project webofneeds by researchstudio-sat.

the class OwnerProtocolCommunicationServiceImpl method storeWonNode.

/**
 * Stores the won node information, possibly overwriting existing data.
 *
 * @param ownerApplicationId
 * @param camelConfiguration
 * @param wonNodeURI
 * @return
 * @throws NoSuchConnectionException
 */
public WonNode storeWonNode(String ownerApplicationId, CamelConfiguration camelConfiguration, URI wonNodeURI) throws NoSuchConnectionException {
    WonNode wonNode = DataAccessUtils.loadWonNode(wonNodeRepository, wonNodeURI);
    if (wonNode == null) {
        wonNode = new WonNode();
    }
    wonNode.setOwnerApplicationID(ownerApplicationId);
    wonNode.setOwnerProtocolEndpoint(camelConfiguration.getEndpoint());
    wonNode.setWonNodeURI(wonNodeURI);
    wonNode.setBrokerURI(getBrokerUri(wonNodeURI));
    wonNode.setBrokerComponent(camelConfiguration.getBrokerComponentName());
    wonNode.setStartingComponent(getProtocolCamelConfigurator().getStartingEndpoint(wonNodeURI));
    wonNodeRepository.save(wonNode);
    logger.debug("setting starting component {}", wonNode.getStartingComponent());
    return wonNode;
}
Also used : WonNode(won.protocol.model.WonNode)

Example 4 with WonNode

use of won.protocol.model.WonNode in project webofneeds by researchstudio-sat.

the class OwnerProtocolCommunicationServiceImpl method register.

/**
 * Registers the owner application at a won node. Owner Id is typically his Key
 * ID (lower 64 bits of the owner public key fingerprint). Unless there is a
 * collision of owner ids on the node - then the owner can assign another id...
 *
 * @throws Exception
 */
public synchronized void register(URI wonNodeURI, MessagingService messagingService) throws Exception {
    CamelConfiguration camelConfiguration = null;
    logger.debug("setting up communication with won node {} ", wonNodeURI);
    String ownerApplicationId = calculateOwnerApplicationIdFromOwnerCertificate();
    logger.debug("using ownerApplicationId: {}", ownerApplicationId);
    WonNode wonNode = wonNodeRepository.findOneByWonNodeURIAndOwnerApplicationID(wonNodeURI, ownerApplicationId);
    if (wonNode != null) {
        // register further below
        try {
            logger.debug("we're already registered. Connecting with WoN node: " + wonNodeURI);
            configureCamelEndpoint(wonNodeURI, ownerApplicationId);
            configureRemoteEndpointForOwnerApplication(ownerApplicationId, getProtocolCamelConfigurator().getEndpoint(wonNodeURI));
            logger.debug("connected with WoN node: " + wonNodeURI);
            return;
        } catch (Exception e) {
            LoggingUtils.logMessageAsInfoAndStacktraceAsDebug(logger, e, "We thought we were already registerd, but connecting to {} failed With an exception. Trying to re-register. ", wonNodeURI);
        }
    // we'll try to re-register now, see below. This is necessary if the WoN node
    // forgets about us for whatever
    // reason.
    }
    logger.info("we're not yet registered. Registering with WoN node {} under ownerApplicationId {}", wonNodeURI, ownerApplicationId);
    String nodeGeneratedOwnerApplicationId = registrationClient.register(wonNodeURI.toString());
    if (!ownerApplicationId.equals(nodeGeneratedOwnerApplicationId)) {
        throw new java.lang.IllegalStateException("WoN node " + wonNodeURI + " generated an ownerApplicationId that differs from" + " ours. Node generated: " + nodeGeneratedOwnerApplicationId + ", we " + "generated: " + ownerApplicationId);
    }
    logger.debug("registered with WoN node: " + wonNodeURI + ",  ownerappID: " + ownerApplicationId);
    camelConfiguration = configureCamelEndpoint(wonNodeURI, ownerApplicationId);
    storeWonNode(ownerApplicationId, camelConfiguration, wonNodeURI);
    configureRemoteEndpointForOwnerApplication(ownerApplicationId, getProtocolCamelConfigurator().getEndpoint(wonNodeURI));
    logger.info("connected with WoN node: : " + wonNodeURI);
}
Also used : WonNode(won.protocol.model.WonNode) CamelConfiguration(won.protocol.jms.CamelConfiguration) CamelConfigurationFailedException(won.protocol.exception.CamelConfigurationFailedException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with WonNode

use of won.protocol.model.WonNode in project webofneeds by researchstudio-sat.

the class OwnerWonMessageSenderJMSBased method sendMessage.

/**
 * Signs the message, calculates its messageURI based on content and sends it.
 *
 * @param message
 * @return the updated, final message.
 * @throws WonMessageSenderException
 */
public void sendMessage(WonMessage wonMessage) {
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("sending this message: {}", RdfUtils.writeDatasetToString(wonMessage.getCompleteDataset(), Lang.TRIG));
        }
        URI msgUri = wonMessage.getMessageURIRequired();
        if (!WonMessageUtils.isValidMessageUri(msgUri)) {
            throw new WonMessageSenderException("Not a valid message uri: " + msgUri + ". Did you call prepareMessage(message) first?");
        }
        // ToDo (FS): change it to won node URI and create method in the MessageEvent
        // class
        URI wonNodeUri = wonMessage.getSenderNodeURI();
        if (wonNodeUri == null) {
            // obtain the sender won node from the sender atom
            throw new IllegalStateException("a message needs a SenderNodeUri otherwise we can't determine the won node " + "via which to send it");
        }
        // get the camel endpoint for talking to the WoN node
        String ep = ownerProtocolCommunicationServiceImpl.getProtocolCamelConfigurator().getEndpoint(wonNodeUri);
        if (ep == null) {
            // necessary
            if (!ownerProtocolCommunicationServiceImpl.isRegisteredWithWonNode(wonNodeUri)) {
                ownerProtocolCommunicationServiceImpl.register(wonNodeUri, messagingService);
            }
            // try again to get the endpoint
            ep = ownerProtocolCommunicationServiceImpl.getProtocolCamelConfigurator().getEndpoint(wonNodeUri);
            if (ep == null) {
                throw new Exception("could not obtain camel endpoint for WoN node " + wonNodeUri + " even after trying to re-register");
            }
        }
        List<WonNode> wonNodeList = wonNodeRepository.findByWonNodeURI(wonNodeUri);
        String ownerApplicationId = wonNodeList.get(0).getOwnerApplicationID();
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put(WonCamelConstants.OWNER_APPLICATION_ID_HEADER, ownerApplicationId);
        headerMap.put(WonCamelConstants.REMOTE_BROKER_ENDPOINT_HEADER, ep);
        messagingService.sendInOnlyMessage(null, headerMap, WonMessageEncoder.encode(wonMessage, Lang.TRIG), startingEndpoint);
    // camelContext.getShutdownStrategy().setSuppressLoggingOnTimeout(true);
    } catch (Exception e) {
        throw new RuntimeException("could not send message", e);
    }
}
Also used : WonNode(won.protocol.model.WonNode) HashMap(java.util.HashMap) URI(java.net.URI) WonMessageSenderException(won.protocol.message.sender.exception.WonMessageSenderException) WonMessageSenderException(won.protocol.message.sender.exception.WonMessageSenderException)

Aggregations

WonNode (won.protocol.model.WonNode)5 URI (java.net.URI)2 CertificateException (java.security.cert.CertificateException)2 ExecutionException (java.util.concurrent.ExecutionException)2 CamelConfigurationFailedException (won.protocol.exception.CamelConfigurationFailedException)2 NoSuchConnectionException (won.protocol.exception.NoSuchConnectionException)2 CamelConfiguration (won.protocol.jms.CamelConfiguration)2 HashMap (java.util.HashMap)1 WonMessageSenderException (won.protocol.message.sender.exception.WonMessageSenderException)1