Search in sources :

Example 1 with IRegistrationService

use of org.jumpmind.symmetric.service.IRegistrationService in project symmetric-ds by JumpMind.

the class SymmetricEngineHolder method install.

public ISymmetricEngine install(Properties passedInProperties) throws Exception {
    TypedProperties properties = new TypedProperties(passedInProperties);
    String password = properties.getProperty(BasicDataSourcePropertyConstants.DB_POOL_PASSWORD);
    if (StringUtils.isNotBlank(password) && !password.startsWith(SecurityConstants.PREFIX_ENC)) {
        try {
            ISecurityService service = SecurityServiceFactory.create(SecurityServiceType.CLIENT, properties);
            properties.setProperty(BasicDataSourcePropertyConstants.DB_POOL_PASSWORD, SecurityConstants.PREFIX_ENC + service.encrypt(password));
        } catch (Exception ex) {
            log.warn("Could not encrypt password", ex);
        }
    }
    String engineName = validateRequiredProperties(properties);
    passedInProperties.setProperty(ParameterConstants.ENGINE_NAME, engineName);
    if (engines.get(engineName) != null) {
        try {
            engines.get(engineName).stop();
        } catch (Exception e) {
            log.error("", e);
        }
        engines.remove(engineName);
    }
    File enginesDir = new File(AbstractCommandLauncher.getEnginesDir());
    File symmetricProperties = new File(enginesDir, engineName + ".properties");
    FileOutputStream fileOs = null;
    try {
        fileOs = new FileOutputStream(symmetricProperties);
        properties.store(fileOs, "Updated by SymmetricDS Pro");
    } catch (IOException ex) {
        throw new RuntimeException("Failed to write symmetric.properties to engine directory", ex);
    } finally {
        IOUtils.closeQuietly(fileOs);
    }
    ISymmetricEngine engine = null;
    try {
        String registrationUrl = properties.getProperty(ParameterConstants.REGISTRATION_URL);
        if (StringUtils.isNotBlank(registrationUrl)) {
            Collection<ServerSymmetricEngine> all = getEngines().values();
            for (ISymmetricEngine currentEngine : all) {
                if (currentEngine.getParameterService().getSyncUrl().equals(registrationUrl)) {
                    String serverNodeGroupId = currentEngine.getParameterService().getNodeGroupId();
                    String clientNodeGroupId = properties.getProperty(ParameterConstants.NODE_GROUP_ID);
                    String externalId = properties.getProperty(ParameterConstants.EXTERNAL_ID);
                    IConfigurationService configurationService = currentEngine.getConfigurationService();
                    ITriggerRouterService triggerRouterService = currentEngine.getTriggerRouterService();
                    List<NodeGroup> groups = configurationService.getNodeGroups();
                    boolean foundGroup = false;
                    for (NodeGroup nodeGroup : groups) {
                        if (nodeGroup.getNodeGroupId().equals(clientNodeGroupId)) {
                            foundGroup = true;
                        }
                    }
                    if (!foundGroup) {
                        configurationService.saveNodeGroup(new NodeGroup(clientNodeGroupId));
                    }
                    boolean foundLink = false;
                    List<NodeGroupLink> links = configurationService.getNodeGroupLinksFor(serverNodeGroupId, false);
                    for (NodeGroupLink nodeGroupLink : links) {
                        if (nodeGroupLink.getTargetNodeGroupId().equals(clientNodeGroupId)) {
                            foundLink = true;
                        }
                    }
                    if (!foundLink) {
                        configurationService.saveNodeGroupLink(new NodeGroupLink(serverNodeGroupId, clientNodeGroupId, NodeGroupLinkAction.W));
                        triggerRouterService.syncTriggers();
                    }
                    IRegistrationService registrationService = currentEngine.getRegistrationService();
                    if (!registrationService.isAutoRegistration() && !registrationService.isRegistrationOpen(clientNodeGroupId, externalId)) {
                        Node node = new Node(properties);
                        registrationService.openRegistration(node);
                    }
                }
            }
        }
        engine = create(symmetricProperties.getAbsolutePath());
        if (engine != null) {
            engineCount++;
            engine.start();
        } else {
            FileUtils.deleteQuietly(symmetricProperties);
            log.warn("The engine could not be created.  It will not be started");
        }
        return engine;
    } catch (RuntimeException ex) {
        if (engine != null) {
            engine.destroy();
        }
        FileUtils.deleteQuietly(symmetricProperties);
        throw ex;
    }
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) IRegistrationService(org.jumpmind.symmetric.service.IRegistrationService) Node(org.jumpmind.symmetric.model.Node) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) IConfigurationService(org.jumpmind.symmetric.service.IConfigurationService) IOException(java.io.IOException) TypedProperties(org.jumpmind.properties.TypedProperties) IOException(java.io.IOException) ISecurityService(org.jumpmind.security.ISecurityService) FileOutputStream(java.io.FileOutputStream) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) File(java.io.File) NodeGroup(org.jumpmind.symmetric.model.NodeGroup)

Example 2 with IRegistrationService

use of org.jumpmind.symmetric.service.IRegistrationService in project symmetric-ds by JumpMind.

the class SymmetricAdmin method openRegistration.

private void openRegistration(CommandLine line, List<String> args) {
    String nodeGroupId = popArg(args, "Node Group ID");
    String externalId = popArg(args, "External ID");
    IRegistrationService registrationService = getSymmetricEngine().getRegistrationService();
    registrationService.openRegistration(nodeGroupId, externalId);
    System.out.println(String.format("Opened registration for node group of '%s' external ID of '%s'", nodeGroupId, externalId));
}
Also used : IRegistrationService(org.jumpmind.symmetric.service.IRegistrationService)

Example 3 with IRegistrationService

use of org.jumpmind.symmetric.service.IRegistrationService in project symmetric-ds by JumpMind.

the class AcknowledgeService method ack.

public BatchAckResult ack(final BatchAck batch) {
    IRegistrationService registrationService = engine.getRegistrationService();
    IOutgoingBatchService outgoingBatchService = engine.getOutgoingBatchService();
    BatchAckResult result = new BatchAckResult(batch);
    for (IAcknowledgeEventListener listener : engine.getExtensionService().getExtensionPointList(IAcknowledgeEventListener.class)) {
        listener.onAcknowledgeEvent(batch);
    }
    if (batch.getBatchId() == Constants.VIRTUAL_BATCH_FOR_REGISTRATION) {
        if (batch.isOk()) {
            registrationService.markNodeAsRegistered(batch.getNodeId());
        }
    } else {
        OutgoingBatch outgoingBatch = outgoingBatchService.findOutgoingBatch(batch.getBatchId(), batch.getNodeId());
        Status status = batch.isOk() ? Status.OK : batch.isResend() ? Status.RS : Status.ER;
        if (outgoingBatch != null) {
            // is OK.
            if (outgoingBatch.getStatus() != Status.OK && outgoingBatch.getStatus() != Status.IG) {
                outgoingBatch.setStatus(status);
                outgoingBatch.setErrorFlag(!batch.isOk());
            } else {
                // clearing the error flag in case the user set the batch
                // status to OK
                Status oldStatus = outgoingBatch.getStatus();
                outgoingBatch.setStatus(Status.OK);
                outgoingBatch.setErrorFlag(false);
                log.info("Batch {} for {} was set to {}.  Updating the status to OK", new Object[] { batch.getBatchId(), batch.getNodeId(), oldStatus.name() });
            }
            if (batch.isIgnored()) {
                outgoingBatch.incrementIgnoreCount();
            }
            outgoingBatch.setNetworkMillis(batch.getNetworkMillis());
            outgoingBatch.setFilterMillis(batch.getFilterMillis());
            outgoingBatch.setLoadMillis(batch.getDatabaseMillis());
            outgoingBatch.setSqlCode(batch.getSqlCode());
            outgoingBatch.setSqlState(batch.getSqlState());
            outgoingBatch.setSqlMessage(batch.getSqlMessage());
            boolean isNewError = false;
            if (!batch.isOk() && batch.getErrorLine() != 0) {
                List<Number> ids = sqlTemplateDirty.query(getSql("selectDataIdSql"), new NumberMapper(), outgoingBatch.getBatchId());
                if (ids.size() >= batch.getErrorLine()) {
                    long failedDataId = ids.get((int) batch.getErrorLine() - 1).longValue();
                    if (outgoingBatch.getFailedDataId() == 0 || outgoingBatch.getFailedDataId() != failedDataId) {
                        isNewError = true;
                    }
                    outgoingBatch.setFailedDataId(failedDataId);
                }
            }
            if (status == Status.ER) {
                log.error("The outgoing batch {} failed: {}{}", outgoingBatch.getNodeBatchId(), (batch.getSqlCode() != 0 ? "[" + batch.getSqlState() + "," + batch.getSqlCode() + "] " : ""), batch.getSqlMessage());
                RouterStats routerStats = engine.getStatisticManager().getRouterStatsByBatch(batch.getBatchId());
                if (routerStats != null) {
                    log.info("Router stats for batch " + outgoingBatch.getBatchId() + ": " + routerStats.toString());
                }
                if (isNewError && outgoingBatch.getSqlCode() == ErrorConstants.FK_VIOLATION_CODE && parameterService.is(ParameterConstants.AUTO_RESOLVE_FOREIGN_KEY_VIOLATION)) {
                    Channel channel = engine.getConfigurationService().getChannel(outgoingBatch.getChannelId());
                    if (channel != null && !channel.isReloadFlag()) {
                        engine.getDataService().reloadMissingForeignKeyRows(outgoingBatch.getNodeId(), outgoingBatch.getFailedDataId());
                    }
                }
            } else if (status == Status.RS) {
                log.info("The outgoing batch {} received resend request", outgoingBatch.getNodeBatchId());
            }
            outgoingBatchService.updateOutgoingBatch(outgoingBatch);
            if (status == Status.OK) {
                Channel channel = engine.getConfigurationService().getChannel(outgoingBatch.getChannelId());
                if (channel != null && channel.isFileSyncFlag()) {
                    /* Acknowledge the file_sync in case the file needs deleted. */
                    engine.getFileSyncService().acknowledgeFiles(outgoingBatch);
                }
                engine.getStatisticManager().removeRouterStatsByBatch(batch.getBatchId());
            }
        } else {
            log.error("Could not find batch {}-{} to acknowledge as {}", new Object[] { batch.getNodeId(), batch.getBatchId(), status.name() });
            result.setOk(false);
        }
    }
    return result;
}
Also used : Status(org.jumpmind.symmetric.model.OutgoingBatch.Status) NumberMapper(org.jumpmind.db.sql.mapper.NumberMapper) IAcknowledgeEventListener(org.jumpmind.symmetric.transport.IAcknowledgeEventListener) IRegistrationService(org.jumpmind.symmetric.service.IRegistrationService) Channel(org.jumpmind.symmetric.model.Channel) BatchAckResult(org.jumpmind.symmetric.model.BatchAckResult) RouterStats(org.jumpmind.symmetric.statistic.RouterStats) IOutgoingBatchService(org.jumpmind.symmetric.service.IOutgoingBatchService) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch)

Example 4 with IRegistrationService

use of org.jumpmind.symmetric.service.IRegistrationService in project symmetric-ds by JumpMind.

the class CopyNodeUriHandler method handle.

public void handle(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    IRegistrationService registrationService = engine.getRegistrationService();
    IOutgoingBatchService outgoingBatchService = engine.getOutgoingBatchService();
    INodeService nodeService = engine.getNodeService();
    IConfigurationService configurationService = engine.getConfigurationService();
    String identityNodeId = nodeService.findIdentityNodeId();
    String copyFromNodeId = req.getParameter(WebConstants.NODE_ID);
    String newExternalId = req.getParameter(WebConstants.EXTERNAL_ID);
    String newGroupId = req.getParameter(WebConstants.NODE_GROUP_ID);
    String newNodeId = registrationService.openRegistration(newGroupId, newExternalId);
    log.info("Received a copy request.  New external_id={}, new node_group_id={}, old node_id={}, new node_id={}", new Object[] { newExternalId, newGroupId, copyFromNodeId, newNodeId });
    Set<String> channelIds = configurationService.getChannels(false).keySet();
    for (String channelId : channelIds) {
        String batchId = req.getParameter(channelId + "-" + identityNodeId);
        if (isNotBlank(batchId)) {
            outgoingBatchService.copyOutgoingBatches(channelId, NumberUtils.toLong(batchId.trim()), copyFromNodeId, newNodeId);
        }
    }
}
Also used : IRegistrationService(org.jumpmind.symmetric.service.IRegistrationService) INodeService(org.jumpmind.symmetric.service.INodeService) IConfigurationService(org.jumpmind.symmetric.service.IConfigurationService) IOutgoingBatchService(org.jumpmind.symmetric.service.IOutgoingBatchService)

Example 5 with IRegistrationService

use of org.jumpmind.symmetric.service.IRegistrationService in project symmetric-ds by JumpMind.

the class RestService method postRegisterNode.

@ApiOperation(value = "Register the specified node for the specified engine")
@RequestMapping(value = "/engine/{engine}/registernode", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final RegistrationInfo postRegisterNode(@PathVariable("engine") String engineName, @RequestParam(value = "externalId") String externalId, @RequestParam(value = "nodeGroupId") String nodeGroupId, @RequestParam(value = "databaseType") String databaseType, @RequestParam(value = "databaseVersion") String databaseVersion, @RequestParam(value = "hostName") String hostName) {
    ISymmetricEngine engine = getSymmetricEngine(engineName);
    IRegistrationService registrationService = engine.getRegistrationService();
    INodeService nodeService = engine.getNodeService();
    RegistrationInfo regInfo = new org.jumpmind.symmetric.web.rest.model.RegistrationInfo();
    try {
        org.jumpmind.symmetric.model.Node processedNode = registrationService.registerPullOnlyNode(externalId, nodeGroupId, databaseType, databaseVersion);
        regInfo.setRegistered(processedNode.isSyncEnabled());
        if (regInfo.isRegistered()) {
            regInfo.setNodeId(processedNode.getNodeId());
            NodeSecurity nodeSecurity = nodeService.findNodeSecurity(processedNode.getNodeId());
            regInfo.setNodePassword(nodeSecurity.getNodePassword());
            org.jumpmind.symmetric.model.Node modelNode = nodeService.findIdentity();
            regInfo.setSyncUrl(modelNode.getSyncUrl());
            // do an initial heartbeat
            Heartbeat heartbeat = new Heartbeat();
            heartbeat.setNodeId(regInfo.getNodeId());
            heartbeat.setHostName(hostName);
            Date now = new Date();
            heartbeat.setCreateTime(now);
            heartbeat.setLastRestartTime(now);
            heartbeat.setHeartbeatTime(now);
            this.heartbeatImpl(engine, heartbeat);
        }
    // TODO: Catch a RegistrationRedirectException and redirect.
    } catch (IOException e) {
        throw new IoException(e);
    }
    return regInfo;
}
Also used : RegistrationInfo(org.jumpmind.symmetric.web.rest.model.RegistrationInfo) NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) IRegistrationService(org.jumpmind.symmetric.service.IRegistrationService) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) IOException(java.io.IOException) Date(java.util.Date) INodeService(org.jumpmind.symmetric.service.INodeService) Heartbeat(org.jumpmind.symmetric.web.rest.model.Heartbeat) IoException(org.jumpmind.exception.IoException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

IRegistrationService (org.jumpmind.symmetric.service.IRegistrationService)5 IOException (java.io.IOException)2 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)2 IConfigurationService (org.jumpmind.symmetric.service.IConfigurationService)2 INodeService (org.jumpmind.symmetric.service.INodeService)2 IOutgoingBatchService (org.jumpmind.symmetric.service.IOutgoingBatchService)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Date (java.util.Date)1 NumberMapper (org.jumpmind.db.sql.mapper.NumberMapper)1 IoException (org.jumpmind.exception.IoException)1 TypedProperties (org.jumpmind.properties.TypedProperties)1 ISecurityService (org.jumpmind.security.ISecurityService)1 BatchAckResult (org.jumpmind.symmetric.model.BatchAckResult)1 Channel (org.jumpmind.symmetric.model.Channel)1 Node (org.jumpmind.symmetric.model.Node)1 NodeGroup (org.jumpmind.symmetric.model.NodeGroup)1 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)1 NodeSecurity (org.jumpmind.symmetric.model.NodeSecurity)1