diff options
| author | Michael DeHaan <mdehaan@redhat.com> | 2008-07-11 16:53:26 -0400 |
|---|---|---|
| committer | Michael DeHaan <mdehaan@redhat.com> | 2008-07-11 16:53:26 -0400 |
| commit | 210249ab81e5b4cad1575f6c2a21d06b360531b9 (patch) | |
| tree | d6a768f7532fa42cffd9d65f05699b922469e112 /bindings/func-java/src | |
| parent | 5c713b017d2af83da11f7b77e188fd0c2686b3c3 (diff) | |
| download | func-210249ab81e5b4cad1575f6c2a21d06b360531b9.tar.gz func-210249ab81e5b4cad1575f6c2a21d06b360531b9.tar.xz func-210249ab81e5b4cad1575f6c2a21d06b360531b9.zip | |
Merging Marco's func bindings
Diffstat (limited to 'bindings/func-java/src')
8 files changed, 508 insertions, 0 deletions
diff --git a/bindings/func-java/src/main/groovy/org/func/FuncFactory.groovy b/bindings/func-java/src/main/groovy/org/func/FuncFactory.groovy new file mode 100644 index 0000000..84d3d7b --- /dev/null +++ b/bindings/func-java/src/main/groovy/org/func/FuncFactory.groovy @@ -0,0 +1,48 @@ +package org.func +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Date: Jul 11, 2008 + * Time: 10:03:20 AM + * + * @author Marco Mornati + * @e-mail mmornati@byte-code.com + * @version 1.0 + */ +class FuncFactory { + + /** + * Factory class method. You need to call this method to obtain an instance of Func classes + * @return Func instances. To use for func invokation. + */ + public static Func getFunc() { + Func funcImplementation = new FuncImpl() + + return funcImplementation + } + + /** + * Factory class method. You need to call this method to obtain an instance of Func classes + * @param funcTransmit You can specify folder where your func-transmit is located. By default it must be in your path. + * @return Func instances. To use for func invokation. + */ + public static Func getFunc(String funcTransmit) { + Func funcImplementation = new FuncImpl(funcTransmit) + + return funcImplementation + } + +}
\ No newline at end of file diff --git a/bindings/func-java/src/main/groovy/org/func/FuncImpl.groovy b/bindings/func-java/src/main/groovy/org/func/FuncImpl.groovy new file mode 100644 index 0000000..332f72c --- /dev/null +++ b/bindings/func-java/src/main/groovy/org/func/FuncImpl.groovy @@ -0,0 +1,135 @@ +package org.func + +import org.func.exceptions.FuncCommunicationException +import org.jvyaml.YAML +import org.func.exceptions.CertmasterException + +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Date: Jul 10, 2008 + * Time: 5:38:02 PM + * + * @author Marco Mornati + * @e-mail mmornati@byte-code.com + * @version 1.0 + */ +class FuncImpl implements Func { + + private funcTransmit = "func-transmit" + private func = "func" + + public FuncImpl() { + + } + + public FuncImpl(String transmit) { + this.funcTransmit = transmit + } + + public Map call(String client, String module, String method) throws FuncCommunicationException { + this.call([client], module, method, null) + } + + public Map call(List clients, String module, String method) throws FuncCommunicationException { + this.call(clients, module, method, null) + } + + public Map call(String client, String module, String method, String parameter) throws FuncCommunicationException { + this.call([client], module, method, [parameter]) + } + + public Map call(List clients, String module, String method, List parameters) throws FuncCommunicationException { + return funcCall(clients, module, method, parameters) + } + + public Map listModules(String client) { + return this.listModules([client]) + } + + public Map listModuleMethods(String client, String method) { + return this.listModuleMethods([client], method) + } + + public Map listModules(List clients) { + return funcCall(clients, "system", "list_modules", null) + } + + public Map listModuleMethods(List clients, String module) { + return funcCall(clients, module, "list_methods", null) + } + + + public List listMinions() { + def minions = [] + def commandToExecute = [func, "*", "list_minions"] + def proc = commandToExecute.execute() + proc.waitFor() + def procAnswer = proc.text + if (procAnswer) { + minions = procAnswer.split("\n") + } else { + throw new CertmasterException("Error communicating with certmaster") + } + return minions; + } + + + private Map funcCall(List clients, String module, String method, List parameters) throws FuncCommunicationException { + def values = [clients: clientsCallPatch(clients), module: module, method: method, parameters: parameters] + def yamlDump = YAML.dump(values) + def commandToExecute = [funcTransmit] + def proc = commandToExecute.execute() + proc.withWriter {writer -> writer << yamlDump } + proc.waitFor() + def procAnswer = proc.text + Map funcResponse = null + if (procAnswer) { + def response = YAML.load(procAnswer) + if (response instanceof Map) { + funcResponse = (Map) response + } + } else { + throw new FuncCommunicationException(procAnswer + " - Error reading answer from Func-Transmit Process!") + } + return funcResponse + } + + + private String clientsCallPatch(List clients) { + //TODO: Now Func does not accept a LIST of client (you can send a list with a string formatted like client1;client2;client + //TODO: Change this patch when func will be ready + //===== PATCH BEGINS + String clientToSend = "" + clients?.each { + if (!clientToSend.equals("")) { + clientToSend += ";" + } + clientToSend += "${it}" + } + return clientToSend + //===== PATCH ENDS + } + + public void setFuncTransmit(String funcTransmit) { + this.funcTransmit = funcTransmit + } + + public String getFuncTransmit() { + return funcTransmit + } + +}
\ No newline at end of file diff --git a/bindings/func-java/src/main/java/org/func/Func.java b/bindings/func-java/src/main/java/org/func/Func.java new file mode 100644 index 0000000..d0d09a2 --- /dev/null +++ b/bindings/func-java/src/main/java/org/func/Func.java @@ -0,0 +1,123 @@ +package org.func; + +import org.func.exceptions.FuncCommunicationException; + +import java.util.List; +import java.util.Map; + +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * <p/> + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * <p/> + * 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 General Public License for more details. + * <p/> + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * <p/> + * Date: Jul 10, 2008 + * Time: 5:35:26 PM + * + * @author Marco Mornati + * @e-mail mmornati@byte-code.com + * @version 1.0 + */ +public interface Func { + + /** + * Function invoked to optain the list of func client (machine that you can control in your java application using this API). + * @return List of String with machine hostname + */ + public List listMinions(); + + /** + * Caller for func-transmit with single client and no parameter + * @param client Func client name. To invoke all defined clients use "*" + * @param module Name of func module (use listModules to obtain the available modules) + * @param method Name of method to invoke + * @return Map with client name as key and func response as value + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map call (String client, String module, String method) throws FuncCommunicationException; + + /** + * Caller for func-transmit with list of clients and no parameter + * @param clients List of Func clients. + * @param module Name of func module (use listModules to obtain the available modules) + * @param method Name of method to invoke + * @return Map with responses divided for each client: client name as key and func response as value + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map call (List clients, String module, String method) throws FuncCommunicationException; + + /** + * Caller for func-transmit with single client and single parameter + * @param client List of Func clients. + * @param module Name of func module (use listModules to obtain the available modules) + * @param method Name of method to invoke + * @param parameter parameter to use with your method. Passed during func-transmit invokation + * @return Map with responses divided for each client: client name as key and func response as value + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map call (String client, String module, String method, String parameter) throws FuncCommunicationException; + + /** + * Func-Transmit Caller. It generates a Process to invoke func-transmit (using Yaml as default message) and then + * it parses the output provided from func to generate a Map returned to your application. + * + * The Map content is something like this: + * + * [client_name1: func response for this client (could be a simple string, another map, ...), + * client_name2: func response] + * + * @param clients List of Func clients. + * @param module Name of func module (use listModules to obtain the available modules) + * @param method Name of method to invoke + * @param parameters list of parameters to use with your method. Passed during func-transmit invokation + * @return Map with responses divided for each client: client name as key and func response as value + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map call (List clients, String module, String method, List parameters) throws FuncCommunicationException; + + /** + * Invoked to obtain for specified client the list of func module installed + * + * @param client Name of the client to use with func. To invoke all defined clients use "*" + * @return Map with clientName as key and list of all installed func module as object + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map listModules (String client) throws FuncCommunicationException; + + /** + * Invoked to obtain for specified list of clients, the list of func module installed + * @param clients List of func clients names. + * @return Map with response divided for each client: clientName as key and list of all installed func module as value + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map listModules (List clients) throws FuncCommunicationException; + + /** + * Invoked to obtain the list of methods for provided module and client + * @param client client Name of the client to use with func. To invoke all defined clients use "*" + * @param module name of func module for which you need a list of methods + * @return Map with client name as key and list of module's methods as value + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map listModuleMethods (String client, String module) throws FuncCommunicationException; + + /** + * Invoked to obtain the list of methods for provided module and list of clients + * @param clients List of func clients names. + * @param module name of func module for which you need a list of methods + * @return Map with response divided for each client: clientName as key and list of all module's methods as value + * @throws FuncCommunicationException thrown if there is any kind of error in func invokation + */ + public Map listModuleMethods (List clients, String module) throws FuncCommunicationException; + +} diff --git a/bindings/func-java/src/main/java/org/func/exceptions/CertmasterException.java b/bindings/func-java/src/main/java/org/func/exceptions/CertmasterException.java new file mode 100644 index 0000000..dcab042 --- /dev/null +++ b/bindings/func-java/src/main/java/org/func/exceptions/CertmasterException.java @@ -0,0 +1,35 @@ +package org.func.exceptions; + +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * <p/> + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * <p/> + * 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 General Public License for more details. + * <p/> + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * <p/> + * Date: Jul 11, 2008 + * Time: 3:21:04 PM + * + * @author Marco Mornati + * @e-mail mmornati@byte-code.com + * @version 1.0 + */ +public class CertmasterException extends Exception { + + public CertmasterException(Exception e) { + super(e); + } + + public CertmasterException(String message) { + super(message); + } +} diff --git a/bindings/func-java/src/main/java/org/func/exceptions/FuncCommunicationException.java b/bindings/func-java/src/main/java/org/func/exceptions/FuncCommunicationException.java new file mode 100644 index 0000000..ccee91f --- /dev/null +++ b/bindings/func-java/src/main/java/org/func/exceptions/FuncCommunicationException.java @@ -0,0 +1,35 @@ +package org.func.exceptions; + +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * <p/> + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * <p/> + * 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 General Public License for more details. + * <p/> + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * <p/> + * Date: Jul 11, 2008 + * Time: 10:44:46 AM + * + * @author Marco Mornati + * @e-mail mmornati@byte-code.com + * @version 1.0 + */ +public class FuncCommunicationException extends Exception { + + public FuncCommunicationException(Exception e) { + super(e); + } + + public FuncCommunicationException(String message) { + super(message); + } +} diff --git a/bindings/func-java/src/test/groovy/FuncFactoryTest.groovy b/bindings/func-java/src/test/groovy/FuncFactoryTest.groovy new file mode 100644 index 0000000..fd0ab86 --- /dev/null +++ b/bindings/func-java/src/test/groovy/FuncFactoryTest.groovy @@ -0,0 +1,32 @@ +import org.func.FuncFactory +import org.func.Func + +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Date: Jul 11, 2008 + * Time: 10:06:03 AM + */ +class FuncFactoryTest extends GroovyTestCase { + + void testFuncFactory() { + def func = FuncFactory.getFunc() + + assertNotNull ("Func Factory returns a null Func Implementation", func) + assertTrue ("Func Factory does not return a correct Func object", func instanceof Func) + } + +}
\ No newline at end of file diff --git a/bindings/func-java/src/test/groovy/org/func/FuncFactoryTest.groovy b/bindings/func-java/src/test/groovy/org/func/FuncFactoryTest.groovy new file mode 100644 index 0000000..0cbc0f7 --- /dev/null +++ b/bindings/func-java/src/test/groovy/org/func/FuncFactoryTest.groovy @@ -0,0 +1,38 @@ +package org.func + +import org.func.FuncFactory +import org.func.Func + +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Date: Jul 11, 2008 + * Time: 10:06:03 AM + * + * @author Marco Mornati + * @e-mail mmornati@byte-code.com + * @version 1.0 + */ +class FuncFactoryTest extends GroovyTestCase { + + void testFuncFactory() { + def func = FuncFactory.getFunc() + + assertNotNull("Func Factory returns a null Func Implementation", func) + assertTrue("Func Factory does not return a correct Func object", func instanceof Func) + } + +}
\ No newline at end of file diff --git a/bindings/func-java/src/test/groovy/org/func/FuncImplTest.groovy b/bindings/func-java/src/test/groovy/org/func/FuncImplTest.groovy new file mode 100644 index 0000000..cd9ce60 --- /dev/null +++ b/bindings/func-java/src/test/groovy/org/func/FuncImplTest.groovy @@ -0,0 +1,62 @@ +package org.func + +import org.jvyaml.YAML + +/** + * Copyright (C) 2008, Byte-Code srl <http://www.byte-code.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Date: Jul 11, 2008 + * Time: 10:30:47 AM + * + * @author Marco Mornati + * @e-mail mmornati@byte-code.com + * @version 1.0 + * + */ +class FuncImplTest extends GroovyTestCase { + + void testCall() { + Func func = FuncFactory.getFunc("/home/mmornati/projects/func/scripts/func-transmit") + def response = func.call ("*", "hardware", "info") + assertTrue ("Func Response is not a Map", response instanceof Map) + } + + void testListModules() { + Func func = FuncFactory.getFunc("/home/mmornati/projects/func/scripts/func-transmit") + def response = func.listModules("bcmmornati") + assertTrue ("Func Response is not a Map", response instanceof Map) + } + + void testListModuleMethods() { + Func func = FuncFactory.getFunc("/home/mmornati/projects/func/scripts/func-transmit") + def response = func.listModuleMethods("bcmmornati", "hardware") + assertTrue ("Func Response is not a Map", response instanceof Map) + } + + void testClientPatch() { + def clients = ["client1", "client2"] + FuncImpl func = FuncFactory.getFunc() + def returned = func.clientsCallPatch(clients) + assertEquals ("Error in ClientsPatch generation", returned, "client1;client2") + } + + void testListMinions() { + Func func = FuncFactory.getFunc() + def response = func.listMinions() + assertTrue ("Error in calling listMinions", response instanceof List) + } + +}
\ No newline at end of file |
