001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.operations.daemon.messages;
019
020import java.util.logging.Level;
021
022import org.syncany.operations.daemon.messages.api.ManagementRequest;
023import org.syncany.operations.daemon.messages.api.ManagementRequestHandler;
024import org.syncany.operations.daemon.messages.api.Response;
025import org.syncany.operations.plugin.PluginOperation;
026import org.syncany.operations.plugin.PluginOperationResult;
027
028public class PluginManagementRequestHandler extends ManagementRequestHandler {
029        public PluginManagementRequestHandler() {
030                // Nothing
031        }
032
033        @Override
034        public Response handleRequest(final ManagementRequest request) {
035                final PluginManagementRequest concreteRequest = (PluginManagementRequest) request;              
036                logger.log(Level.SEVERE, "Executing PluginOperation for action " + concreteRequest.getOptions().getAction() + " ...");
037
038                Thread pluginThread = new Thread(new Runnable() {
039                        @Override
040                        public void run() {
041                                try {                                   
042                                        PluginOperation pluginOperation = new PluginOperation(null, concreteRequest.getOptions());
043                                        PluginOperationResult operationResult = pluginOperation.execute();
044                                        
045                                        switch (operationResult.getResultCode()) {
046                                        case OK:
047                                                eventBus.post(new PluginManagementResponse(PluginManagementResponse.OK, operationResult, request.getId()));
048                                                break;
049
050                                        case NOK:
051                                                eventBus.post(new PluginManagementResponse(PluginManagementResponse.NOK_FAILED_UNKNOWN, operationResult, request.getId()));
052                                                break;
053                                        }
054                                }
055                                catch (Exception e) {
056                                        logger.log(Level.WARNING, "Error executing plugin management request.", e);
057                                        eventBus.post(new PluginManagementResponse(PluginManagementResponse.NOK_OPERATION_FAILED, new PluginOperationResult(), request.getId()));
058                                }
059                        }
060                }, "PlugRq/" + concreteRequest.getOptions().getAction());
061
062                pluginThread.start();
063                
064                return null;
065        }
066}