/** * Handle the {@code ProducerSwitchCommand} command. * * @param command */ public ConsumerSwitchResult execute(ConsumerSwitchCommand command) { // query if (command.getStatus() == null) { return new ConsumerSwitchResult(command.getId(), Boolean.TRUE, consumer.getSwitch().getStatus().getValue()); } else if (command.getStatus().equals(SwitchStatus.ON)) { // turn on the switch consumer.getSwitch().turnOn(); // persist the change persist(command.getStatus()); return new ConsumerSwitchResult(command.getId(), Boolean.TRUE, Boolean.TRUE); } else if (command.getStatus().equals(SwitchStatus.OFF)) { // turn on the switch consumer.getSwitch().turnOff(); // persist the change persist(command.getStatus()); return new ConsumerSwitchResult(command.getId(), Boolean.TRUE, Boolean.FALSE); } else { return new ConsumerSwitchResult(command.getId(), Boolean.FALSE, consumer.getSwitch().getStatus().getValue()); } }
/** * Execute a producer switch command. * * @param command the producer switch command to execute. * @return the result of the command execution. */ @Override public ProducerSwitchResult execute(ProducerSwitchCommand command) { // query if (command.getStatus() == null) { return new ProducerSwitchResult(command.getId(), Boolean.TRUE, producer.getSwitch().getStatus().getValue()); } else if (command.getStatus().equals(SwitchStatus.ON)) { // turn on the switch producer.getSwitch().turnOn(); // persist the change persist(command.getStatus()); return new ProducerSwitchResult(command.getId(), Boolean.TRUE, Boolean.TRUE); } else if (command.getStatus().equals(SwitchStatus.OFF)) { // turn off the switch producer.getSwitch().turnOff(); // persist the change persist(command.getStatus()); return new ProducerSwitchResult(command.getId(), Boolean.TRUE, Boolean.FALSE); } else { return new ProducerSwitchResult(command.getId(), Boolean.FALSE, producer.getSwitch().getStatus().getValue()); } }
@Override public ManageGroupResult execute(ManageGroupCommand command) { ManageGroupResult result = new ManageGroupResult(command.getId()); ManageGroupAction action = command.getAction(); String targetGroupName = command.getGroupName(); if (ManageGroupAction.JOIN.equals(action)) { joinGroup(targetGroupName); } else if (ManageGroupAction.QUIT.equals(action)) { quitGroup(targetGroupName); if (groupManager.listLocalGroups().isEmpty()) { joinGroup(Configurations.DEFAULT_GROUP_NAME); } } else if (ManageGroupAction.PURGE.equals(action)) { purgeGroups(); joinGroup(Configurations.DEFAULT_GROUP_NAME); } else if (ManageGroupAction.SET.equals(action)) { Group localGroup = groupManager.listLocalGroups().iterator().next(); quitGroup(localGroup.getName()); joinGroup(targetGroupName); } addGroupListToResult(result); return result; }
/** * Consume a cluster event. * * @param event the cluster event. */ @Override public void consume(E event) { if (event != null && (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce())) { dispatcher.dispatch(event); } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, cluster event is not consumed", SWITCH_ID); } } }
@Override public void shutdown(boolean poweroff) throws Exception { ShutdownCommand command = new ShutdownCommand(clusterManager.generateId()); Set<Node> nodes = clusterManager.listNodes(); command.setDestination(nodes); command.setHalt(poweroff); executionContext.execute(command); }
@Override public ShutdownResult execute(ShutdownCommand command) { try { if (command.isHalt()) { bundleContext.getBundle(0).stop(); } else { ServiceReference<FeaturesService> ref = bundleContext.getServiceReference(FeaturesService.class); FeaturesService featuresService = bundleContext.getService(ref); featuresService.uninstallFeature("cellar"); bundleContext.ungetService(ref); } } catch (Exception e) { // nothing to do } return new ShutdownResult(command.getId()); }
/** * Consume a cluster event. * * @param event the cluster event. */ @Override public void consume(E event) { if (event != null && (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce())) { dispatcher.dispatch(event); } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, cluster event is not consumed", SWITCH_ID); } } }
/** * Handle the {@code ProducerSwitchCommand} command. * * @param command */ public ConsumerSwitchResult execute(ConsumerSwitchCommand command) { // query if (command.getStatus() == null) { return new ConsumerSwitchResult(command.getId(), Boolean.TRUE, consumer.getSwitch().getStatus().getValue()); } else if (command.getStatus().equals(SwitchStatus.ON)) { // turn on the switch consumer.getSwitch().turnOn(); // persist the change persist(command.getStatus()); return new ConsumerSwitchResult(command.getId(), Boolean.TRUE, Boolean.TRUE); } else if (command.getStatus().equals(SwitchStatus.OFF)) { // turn on the switch consumer.getSwitch().turnOff(); // persist the change persist(command.getStatus()); return new ConsumerSwitchResult(command.getId(), Boolean.TRUE, Boolean.FALSE); } else { return new ConsumerSwitchResult(command.getId(), Boolean.FALSE, consumer.getSwitch().getStatus().getValue()); } }
/** * Execute a producer switch command. * * @param command the producer switch command to execute. * @return the result of the command execution. */ @Override public ProducerSwitchResult execute(ProducerSwitchCommand command) { // query if (command.getStatus() == null) { return new ProducerSwitchResult(command.getId(), Boolean.TRUE, producer.getSwitch().getStatus().getValue()); } else if (command.getStatus().equals(SwitchStatus.ON)) { // turn on the switch producer.getSwitch().turnOn(); // persist the change persist(command.getStatus()); return new ProducerSwitchResult(command.getId(), Boolean.TRUE, Boolean.TRUE); } else if (command.getStatus().equals(SwitchStatus.OFF)) { // turn off the switch producer.getSwitch().turnOff(); // persist the change persist(command.getStatus()); return new ProducerSwitchResult(command.getId(), Boolean.TRUE, Boolean.FALSE); } else { return new ProducerSwitchResult(command.getId(), Boolean.FALSE, producer.getSwitch().getStatus().getValue()); } }
@Override public ManageGroupResult execute(ManageGroupCommand command) { ManageGroupResult result = new ManageGroupResult(command.getId()); ManageGroupAction action = command.getAction(); String targetGroupName = command.getGroupName(); if (ManageGroupAction.JOIN.equals(action)) { joinGroup(targetGroupName); } else if (ManageGroupAction.QUIT.equals(action)) { quitGroup(targetGroupName); if (groupManager.listLocalGroups().isEmpty()) { joinGroup(Configurations.DEFAULT_GROUP_NAME); } } else if (ManageGroupAction.PURGE.equals(action)) { purgeGroups(); joinGroup(Configurations.DEFAULT_GROUP_NAME); } else if (ManageGroupAction.SET.equals(action)) { Group localGroup = groupManager.listLocalGroups().iterator().next(); quitGroup(localGroup.getName()); joinGroup(targetGroupName); } addGroupListToResult(result); return result; }
@Override public void shutdown(boolean poweroff) throws Exception { ShutdownCommand command = new ShutdownCommand(clusterManager.generateId()); Set<Node> nodes = clusterManager.listNodes(); command.setDestination(nodes); command.setHalt(poweroff); executionContext.execute(command); }
@Override public ShutdownResult execute(ShutdownCommand command) { try { if (command.isHalt()) { bundleContext.getBundle(0).stop(); } else { ServiceReference<FeaturesService> ref = bundleContext.getServiceReference(FeaturesService.class); FeaturesService featuresService = bundleContext.getService(ref); featuresService.uninstallFeature("cellar"); bundleContext.ungetService(ref); } } catch (Exception e) { // nothing to do } return new ShutdownResult(command.getId()); }
@Override public void produce(E event) { if (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce() || event instanceof Result) { if (event instanceof Result || event instanceof Command) event.setSourceNode(node); topic.publish(event); } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, don't produce the cluster event", SWITCH_ID); } } }
@Override public void produce(E event) { if (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce() || event instanceof Result) { if (event instanceof Result || event instanceof Command) event.setSourceNode(node); topic.publish(event); } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, don't produce the cluster event", SWITCH_ID); } } }
@Override public void produce(E event) { if (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce() || event instanceof Result) { if (event instanceof Result || event instanceof Command) event.setSourceNode(node); try { queue.put(event); } catch (InterruptedException e) { LOGGER.error("CELLAR HAZELCAST: queue producer interrupted", e); } } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, don't produce the cluster event", SWITCH_ID); } } }
@Override public void produce(E event) { if (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce() || event instanceof Result) { if (event instanceof Result || event instanceof Command) event.setSourceNode(node); try { queue.put(event); } catch (InterruptedException e) { LOGGER.error("CELLAR HAZELCAST: queue producer interrupted", e); } } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, don't produce the cluster event", SWITCH_ID); } } }
@Override public void consume(E event) { // check if event has a specified destination. if ((event.getDestination() == null || event.getDestination().contains(node)) && (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce())) { dispatcher.dispatch(event); } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, cluster event is not consumed", SWITCH_ID); } } }
@Override public void consume(E event) { // check if event has a specified destination. if ((event.getDestination() == null || event.getDestination().contains(node)) && (this.getSwitch().getStatus().equals(SwitchStatus.ON) || event.getForce())) { dispatcher.dispatch(event); } else { if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) { LOGGER.debug("CELLAR HAZELCAST: {} switch is OFF, cluster event is not consumed", SWITCH_ID); } } }
/** * Retrieve the correlated command from the store and set the result on the command object. * * @param result the cluster result event. */ @Override public void handle(R result) { if (commandStore != null && commandStore.getPending() != null) { String id = result.getId(); Command command = commandStore.getPending().get(id); if (command != null && handlerSwitch.getStatus().equals(SwitchStatus.ON)) { command.addResults(result); } } }
/** * Retrieve the correlated command from the store and set the result on the command object. * * @param result the cluster result event. */ @Override public void handle(R result) { if (commandStore != null && commandStore.getPending() != null) { String id = result.getId(); Command command = commandStore.getPending().get(id); if (command != null && handlerSwitch.getStatus().equals(SwitchStatus.ON)) { command.addResults(result); } } }