Mercurial > hg > release > thermostat-1.0
changeset 1342:e81352f71de2
Drop webservice command entirely
reviewed-by: omajid
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008727.html
author | Jon VanAlten <jon.vanalten@redhat.com> |
---|---|
date | Fri, 15 Nov 2013 16:15:18 -0700 |
parents | fb33ffad4da1 |
children | 082514261a09 |
files | distribution/config/commands/webservice.properties distribution/pom.xml distribution/scripts/thermostat-webservice web/cmd/pom.xml web/cmd/src/main/java/com/redhat/thermostat/web/cmd/Activator.java web/cmd/src/main/java/com/redhat/thermostat/web/cmd/LocaleResources.java web/cmd/src/main/java/com/redhat/thermostat/web/cmd/WebServiceCommand.java web/cmd/src/main/java/com/redhat/thermostat/web/cmd/WebServiceLauncher.java web/cmd/src/main/resources/com/redhat/thermostat/web/cmd/strings.properties web/cmd/src/test/java/com/redhat/thermostat/web/cmd/LocaleResourcesTest.java web/cmd/src/test/java/com/redhat/thermostat/web/cmd/WebServiceCommandTest.java web/cmd/src/test/java/com/redhat/thermostat/web/cmd/WebServiceLauncherTest.java web/pom.xml |
diffstat | 13 files changed, 211 insertions(+), 879 deletions(-) [+] |
line wrap: on
line diff
--- a/distribution/config/commands/webservice.properties Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -# Needs at least one implementing storage back-end (e.g. mongodb), -# since it builds on top of this implementing back-end. -bundles = com.redhat.thermostat.web.cmd=${project.version}, \ - com.redhat.thermostat.web.server=${project.version}, \ - com.redhat.thermostat.web.common=${project.version}, \ - com.redhat.thermostat.storage.mongodb=${project.version}, \ - org.mongodb.mongo-java-driver=${mongo-driver.osgi-version}, \ - org.apache.commons.beanutils=${commons-beanutils.version}, \ - org.apache.commons.codec=${commons-codec.osgi-version}, \ - org.apache.commons.collections=${commons-collections.version}, \ - org.apache.commons.logging=${commons-logging.version}, \ - org.apache.commons.fileupload=${commons-fileupload.version}, \ - org.apache.commons.io=${commons-io.version}, \ - com.google.gson=${gson.version}, \ - org.eclipse.jetty.continuation=${jetty.version}, \ - org.eclipse.jetty.http=${jetty.version}, \ - org.eclipse.jetty.io=${jetty.version}, \ - org.eclipse.jetty.security=${jetty.version}, \ - org.eclipse.jetty.server=${jetty.version}, \ - org.eclipse.jetty.servlet=${jetty.version}, \ - org.eclipse.jetty.util=${jetty.version}, \ - org.eclipse.jetty.webapp=${jetty.version}, \ - org.eclipse.jetty.xml=${jetty.version}, \ - org.eclipse.jetty.plus=${jetty.version}, \ - org.eclipse.jetty.jndi=${jetty.version}, \ - - -description = starts and stops the thermostat web service - -usage = webservice -d <url> -b <addrs> [-l <level>] - -options = AUTO_DB_OPTIONS, dbUrl, bindAddrs, AUTO_LOG_OPTION - -dbUrl.required = true - -bindAddrs.long = bindAddrs -bindAddrs.short = b -bindAddrs.hasarg = true -bindAddrs.required = true -bindAddrs.description = A comma separated list of IP address, \ -port pairs used for binding (e.g. 127.0.0.1:8888,127.0.0.1:8889) - -environments = cli
--- a/distribution/pom.xml Fri Nov 15 15:50:46 2013 -0700 +++ b/distribution/pom.xml Fri Nov 15 16:15:18 2013 -0700 @@ -359,11 +359,6 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-web-cmd</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-web-server</artifactId> <version>${project.version}</version> </dependency>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/distribution/scripts/thermostat-webservice Fri Nov 15 16:15:18 2013 -0700 @@ -0,0 +1,211 @@ +#!/bin/bash +# +# Copyright 2012, 2013 Red Hat, Inc. +# +# This file is part of Thermostat. +# +# Thermostat 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 2, or (at your +# option) any later version. +# +# Thermostat 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 Thermostat; see the file COPYING. If not see +# <http://www.gnu.org/licenses/>. +# +# Linking this code with other modules is making a combined work +# based on this code. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this code give +# you permission to link this code with independent modules to +# produce an executable, regardless of the license terms of these +# independent modules, and to copy and distribute the resulting +# executable under terms of your choice, provided that you also +# meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module +# which is not derived from or based on this code. If you modify +# this code, you may extend this exception to your version of the +# library, but you are not obligated to do so. If you do not wish +# to do so, delete this exception statement from your version. +# +##################################################################### +# +read -d '' help_text <<"END_OF_HELP" +thermostat-webservice: A convenience script for starting and stopping +tomcat with thermostat web storage application deployed. + +This script is intended to be an aid for developers to start up +tomcat and deploy thermostat web storage app. It is assumed that +there is already a backing storage running that the web app can +connect to, and that the web.xml file from the build image is +correctly configured for this backing storage connection. See +the file: + + web/war/target/thermostat-web-war-0.16.0-SNAPSHOT/WEB-INF/web.xml + +In addition, an appropriate user and role configuration must be +present in the build image. See the files: + + distribution/target/image/etc/thermostat-users.properties + distribution/target/image/etc/thermostat-roles.properties + +An agent requires authentication configuration in order to connect +to secured storage. See the file: + + ~/.thermostat/etc/agent.auth + +Possible contents of thermostat-users.properties: + + agent-tester=tester + client-tester=tester + +Corresponding potential thermostat-roles.properties: + + agent-tester=thermostat-agent, thermostat-files-grant-write-filename-ALL + client-tester=thermostat-client, thermostat-cmdc-allPrivs, \\ + thermostat-files-grant-read-filename-ALL + thermostat-agent=thermostat-write, thermostat-prepare-statement, \\ + thermostat-save-file, thermostat-purge, \\ + thermostat-register-category, thermostat-cmdc-verify, \\ + thermostat-login, thermostat-realm + thermostat-client=thermostat-agents-grant-read-agentId-ALL, \\ + thermostat-hosts-grant-read-hostname-ALL, \\ + thermostat-vms-grant-read-vmId-ALL, \\ + thermostat-vms-grant-read-username-ALL, thermostat-realm, \\ + thermostat-login, thermostat-query, \\ + thermostat-prepare-statement, thermostat-cmdc-generate, \\ + thermostat-load-file, thermostat-get-count, \\ + thermostat-register-category + thermostat-cmdc-allPrivs = thermostat-cmdc-grant-garbage-collect, \\ + thermostat-cmdc-grant-dump-heap, \\ + thermostat-cmdc-grant-thread-harvester, \\ + thermostat-cmdc-grant-killvm, \\ + thermostat-cmdc-grant-ping, \\ + thermostat-cmdc-grant-jmx-toggle-notifications + +Please be familiar with these privileges to decide whether this +set of users and roles is appropriate for you or your testing. + +Contents of ~/.thermostat/etc/agent.auth that would be valid with the above: + + username=agent-tester + password=tester + +Here is typical use of this script: + + $ echo ${USER_FLUFF} > distribution/target/image/etc/thermostat-users.properties + $ echo ${ROLE_FLUFF} > distribution/target/image/etc/thermostat-roles.properties + $ echo ${AGENT_FLUFF} > ~/.thermostat/etc/agent.auth + $ ./distribution/target/image/bin/thermostat storage --start + $ ./distribution/target/image/bin/thermostat-webservice -t /path/to/tomcat start + $ ./distribution/target/image/bin/thermostat agent -d http://127.0.0.1:8080/thermostat/storage + $ ./distribution/target/image/bin/thermostat gui + + ... and so forth. + + ./distribution/target/image/bin/thermostat-webservice -t /path/to/tomcat stop + ./distribution/target/image/bin/thermostat storage --stop + +NOTE: Use of this script to start tomcat will blow away webapps/thermostat +in your tomcat directory and replace it with the corresponding contents +from your build image. + +NOTE2: If tomcat is already running, results of this script are undefined. + +. +END_OF_HELP +# + +TOMCAT_DIR=tomcat + +usage() { + echo "Usage: " + echo " thermostat-webservice [-t <root of tomcat directory>] <start|stop>" +} + +do_help() { + echo "$help_text" + usage +} + +check_valid_args() { + if [ "x${FUNCTION}" = "x" ] ; then + echo "You must specify either start, stop, or help." + usage + exit 1 + fi + if [ ! -d ${TOMCAT_DIR} ] ; then + echo "Tomcat directory does not exist: ${TOMCAT_DIR}" + usage + exit 1 + fi +} + +process_args() { + while [ $# -gt 0 ]; do + THIS_ARG=$1 + shift + if [ $THIS_ARG = "-h" -o $THIS_ARG = "--help" -o $THIS_ARG = "help" ] ; then + do_help + exit + break + elif [ $THIS_ARG = "-t" ] ; then + TOMCAT_DIR=$1 + shift + elif [ $THIS_ARG = "start" -o $THIS_ARG = "stop" ] ; then + FUNCTION=$THIS_ARG + else + echo "Unrecognized argument: $THIS_ARG" + exit 1 + fi + done + check_valid_args +} + +do_start() { + rm -rf webapps/thermostat + cp -r "$TH/web/war/target/thermostat-web-war-@project.version@" webapps/thermostat + JAVA_OPTS="-Djava.security.auth.login.config=${TH}/distribution/target/image/etc/thermostat_jaas.conf" ./bin/startup.sh +} + +do_stop() { + ./bin/shutdown.sh +} + +do_function() { + TH="$(pwd)" + cd ${TOMCAT_DIR} + case $1 in + start ) + do_start + ;; + stop ) + do_stop + ;; + esac + cd ${TH} +} + +process_args $* +do_function $FUNCTION + +# Please be familiar with these privileges to decide whether this +# set of users and roles is appropriate for you or your testing. +# +#TH="$(pwd)" +#cd path/to/tomcat +#rm -rf webapps/thermostat +#cp -r $TH/web/war/target/thermostat-web-war-0.16.0-SNAPSHOT webapps/thermostat +#export JAVA_OPTS="-Djava.security.auth.login.config=${TH}/distribution/target/image/etc/thermostat_jaas.conf" +#./bin/startup.sh +#cd $TH +#./distribution/target/image/bin/thermostat storage --start +#mkdir -p ~/.thermostat/etc +#echo -e "username=agent-tester\npassword=tester" > ~/.thermostat/etc/agent.auth +#./distribution/target/image/bin/thermostat agent -d http://127.0.0.1:8080/thermostat/storage
--- a/web/cmd/pom.xml Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - - Copyright 2012, 2013 Red Hat, Inc. - - This file is part of Thermostat. - - Thermostat 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 2, or (at your - option) any later version. - - Thermostat 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 Thermostat; see the file COPYING. If not see - <http://www.gnu.org/licenses />. - - Linking this code with other modules is making a combined work - based on this code. Thus, the terms and conditions of the GNU - General Public License cover the whole combination. - - As a special exception, the copyright holders of this code give - you permission to link this code with independent modules to - produce an executable, regardless of the license terms of these - independent modules, and to copy and distribute the resulting - executable under terms of your choice, provided that you also - meet, for each linked independent module, the terms and conditions - of the license of that module. An independent module is a module - which is not derived from or based on this code. If you modify - this code, you may extend this exception to your version of the - library, but you are not obligated to do so. If you do not wish - to do so, delete this exception statement from your version. - ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - - <parent> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-web</artifactId> - <version>0.18.1-SNAPSHOT</version> - </parent> - - <artifactId>thermostat-web-cmd</artifactId> - <packaging>bundle</packaging> - - <name>Thermostat Web Service Command</name> - - <dependencies> - - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-core</artifactId> - <scope>test</scope> - </dependency> - - <dependency> - <groupId>org.eclipse.jetty</groupId> - <artifactId>jetty-server</artifactId> - <version>${jetty.version}</version> - </dependency> - <dependency> - <groupId>org.eclipse.jetty</groupId> - <artifactId>jetty-webapp</artifactId> - <version>${jetty.version}</version> - </dependency> - <dependency> - <groupId>org.eclipse.jetty</groupId> - <artifactId>jetty-plus</artifactId> - <version>${jetty.version}</version> - </dependency> - - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-common-core</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-common-test</artifactId> - <version>${project.version}</version> - <scope>test</scope> - </dependency> - - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.core</artifactId> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-web-server</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat</groupId> - <artifactId>thermostat-storage-mongodb</artifactId> - <version>${project.version}</version> - </dependency> - - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <extensions>true</extensions> - <configuration> - <instructions> - <Bundle-SymbolicName>com.redhat.thermostat.web.cmd</Bundle-SymbolicName> - <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> - <Bundle-Activator>com.redhat.thermostat.web.cmd.Activator</Bundle-Activator> - <Private-Package>com.redhat.thermostat.web.cmd</Private-Package> - <!-- Do not autogenerate uses clauses in Manifests --> - <_nouses>true</_nouses> - </instructions> - </configuration> - </plugin> - </plugins> - </build> -</project> -
--- a/web/cmd/src/main/java/com/redhat/thermostat/web/cmd/Activator.java Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - - -package com.redhat.thermostat.web.cmd; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; - -import com.redhat.thermostat.common.cli.CommandRegistry; -import com.redhat.thermostat.common.cli.CommandRegistryImpl; - -public class Activator implements BundleActivator { - - private CommandRegistry reg; - - @Override - public void start(BundleContext context) throws Exception { - reg = new CommandRegistryImpl(context); - reg.registerCommand("webservice", new WebServiceCommand()); - } - - @Override - public void stop(BundleContext context) throws Exception { - reg.unregisterCommands(); - } - -} -
--- a/web/cmd/src/main/java/com/redhat/thermostat/web/cmd/LocaleResources.java Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.web.cmd; - -import com.redhat.thermostat.shared.locale.Translate; - -public enum LocaleResources { - - NEED_URL, - NEED_IP, - INVALID_PORT, - ERROR_STARTING_SERVICE, - ERROR_STOPPING_SERVICE, - ; - - static final String RESOURCE_BUNDLE = "com.redhat.thermostat.web.cmd.strings"; - - public static Translate<LocaleResources> createLocalizer() { - return new Translate<>(RESOURCE_BUNDLE, LocaleResources.class); - } - -}
--- a/web/cmd/src/main/java/com/redhat/thermostat/web/cmd/WebServiceCommand.java Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - - -package com.redhat.thermostat.web.cmd; - -import java.util.List; - -import com.redhat.thermostat.common.cli.CommandContext; -import com.redhat.thermostat.common.cli.CommandException; -import com.redhat.thermostat.common.cli.AbstractCommand; -import com.redhat.thermostat.common.utils.HostPortPair; -import com.redhat.thermostat.common.utils.HostPortsParser; -import com.redhat.thermostat.shared.locale.Translate; - -public class WebServiceCommand extends AbstractCommand { - - private WebServiceLauncher serviceLauncher; - private final Translate<LocaleResources> t = LocaleResources.createLocalizer(); - - public WebServiceCommand() { - this.serviceLauncher = new WebServiceLauncher(); - } - - // Constructor for testing - WebServiceCommand(WebServiceLauncher launcher) { - this.serviceLauncher = launcher; - } - - @Override - public void run(CommandContext ctx) throws CommandException { - String storageURL = ctx.getArguments().getArgument("dbUrl"); - String username = ctx.getArguments().getArgument("username"); - String password = ctx.getArguments().getArgument("password"); - serviceLauncher.setIpAddresses(parseIPsPorts(ctx.getArguments().getArgument("bindAddrs"))); - serviceLauncher.setStorageURL(storageURL); - serviceLauncher.setStorageUsername(username); - serviceLauncher.setStoragePassword(password); - try { - // this blocks - serviceLauncher.start(); - } catch (InterruptedException e) { - // just shut down cleanly - try { - serviceLauncher.stop(); - } catch (Exception ex) { - ex.printStackTrace(ctx.getConsole().getError()); - throw new CommandException(t.localize(LocaleResources.ERROR_STOPPING_SERVICE), ex); - } - } catch (Exception ex) { - ex.printStackTrace(ctx.getConsole().getError()); - throw new CommandException(t.localize(LocaleResources.ERROR_STARTING_SERVICE), ex); - } - } - - @Override - public boolean isStorageRequired() { - return false; - } - - private List<HostPortPair> parseIPsPorts(String rawIpsPorts) throws CommandException { - HostPortsParser parser = new HostPortsParser(rawIpsPorts); - parser.parse(); - return parser.getHostsPorts(); - } - -} -
--- a/web/cmd/src/main/java/com/redhat/thermostat/web/cmd/WebServiceLauncher.java Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,173 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - - -package com.redhat.thermostat.web.cmd; - -import java.util.List; - -import org.eclipse.jetty.plus.jaas.JAASLoginService; -import org.eclipse.jetty.security.ConstraintMapping; -import org.eclipse.jetty.security.ConstraintSecurityHandler; -import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.nio.SelectChannelConnector; -import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.util.security.Constraint; -import org.eclipse.jetty.webapp.WebAppContext; - -import com.redhat.thermostat.common.utils.HostPortPair; -import com.redhat.thermostat.shared.config.InvalidConfigurationException; -import com.redhat.thermostat.shared.locale.Translate; -import com.redhat.thermostat.storage.mongodb.MongoStorageProvider; -import com.redhat.thermostat.web.server.WebStorageEndPoint; -import com.redhat.thermostat.web.server.auth.RolePrincipal; -import com.redhat.thermostat.web.server.auth.Roles; -import com.redhat.thermostat.web.server.auth.UserPrincipal; -import com.redhat.thermostat.web.server.auth.WrappedRolePrincipal; - -class WebServiceLauncher { - - private static final Translate<LocaleResources> t = LocaleResources.createLocalizer(); - private Server server; - private String storageURL; - private String storageUsername; - private String storagePassword; - // IP/Port pairs, keyed by IP - private List<HostPortPair> ipsPorts; - - WebServiceLauncher() { - server = new Server(); - } - - // Constructor for testing - WebServiceLauncher(Server server) { - this.server = server; - } - - void start() throws Exception { - checkConfig(); - Connector[] connectors = new Connector[ipsPorts.size()]; - for (int i = 0; i < ipsPorts.size(); i++) { - HostPortPair pair = ipsPorts.get(i); - connectors[i] = new SelectChannelConnector(); - connectors[i].setPort(pair.getPort()); - connectors[i].setHost(pair.getHost()); - } - server.setConnectors( connectors ); - - WebAppContext ctx = new WebAppContext(); - ctx.setContextPath("/"); - // This prevents useless classloading, which could fail in the face of OSGi. - ctx.setConfigurations(new org.eclipse.jetty.webapp.Configuration[0]); - - ServletHolder servletHolder = new ServletHolder("rest-storage-end-point", new WebStorageEndPoint()); - servletHolder.setInitParameter(WebStorageEndPoint.STORAGE_ENDPOINT, storageURL); - servletHolder.setInitParameter(WebStorageEndPoint.STORAGE_USERNAME, storageUsername); - servletHolder.setInitParameter(WebStorageEndPoint.STORAGE_PASSWORD, storagePassword); - servletHolder.setInitParameter(WebStorageEndPoint.STORAGE_CLASS, MongoStorageProvider.class.getName()); - ctx.addServlet(servletHolder, "/"); - - configureSecurity(ctx); - - server.setHandler(ctx); - server.start(); - server.join(); - } - - private void configureSecurity(WebAppContext ctx) { - ConstraintSecurityHandler secHandler = new ConstraintSecurityHandler(); - ConstraintMapping constraintMap = new ConstraintMapping(); - Constraint constraint = new Constraint(); - constraint.setAuthenticate(true); - constraint.setRoles(new String[] { Roles.ACCESS_REALM }); - constraint.setName("Entire Application"); - constraintMap.setPathSpec("/*"); - constraintMap.setConstraint(constraint); - - secHandler.setRealmName("Thermostat Realm"); - secHandler.addConstraintMapping(constraintMap); - JAASLoginService loginS = new JAASLoginService(); - loginS.setLoginModuleName("ThermostatJAASLogin"); - loginS.setName("Thermostat Realm"); - loginS.setRoleClassNames(new String[] { - WrappedRolePrincipal.class.getName(), - RolePrincipal.class.getName(), - UserPrincipal.class.getName() - }); - secHandler.setLoginService(loginS); - ctx.setSecurityHandler(secHandler); - } - - void stop() throws Exception { - server.stop(); - server.join(); - } - - public void setStorageURL(String storageURL) { - this.storageURL = storageURL; - } - - public void setStorageUsername(String storageUsername) { - this.storageUsername = storageUsername; - } - - public void setStoragePassword(String storagePassword) { - this.storagePassword = storagePassword; - } - - public void setIpAddresses(List<HostPortPair> ipsPorts) { - this.ipsPorts = ipsPorts; - } - - /* - * StorageURL, port must have been set - */ - private void checkConfig() throws InvalidConfigurationException { - if (storageURL == null) { - throw new InvalidConfigurationException(t.localize(LocaleResources.NEED_URL)); - } - if (ipsPorts == null) { - throw new InvalidConfigurationException(t.localize(LocaleResources.NEED_IP)); - } - for (HostPortPair pair: ipsPorts) { - if (pair.getPort() <= 0) { - throw new InvalidConfigurationException(t.localize(LocaleResources.INVALID_PORT, Integer.toString(pair.getPort()))); - } - } - } -} -
--- a/web/cmd/src/main/resources/com/redhat/thermostat/web/cmd/strings.properties Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -NEED_URL = Storage URL must be set -NEED_IP = IP adresses to bind to must be set -INVALID_PORT = Invalid port number {0} -ERROR_STARTING_SERVICE = Error starting web storage service -ERROR_STOPPING_SERVICE = Error stopping web storage service
--- a/web/cmd/src/test/java/com/redhat/thermostat/web/cmd/LocaleResourcesTest.java Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.web.cmd; - -import com.redhat.thermostat.testutils.AbstractLocaleResourcesTest; - -public class LocaleResourcesTest extends AbstractLocaleResourcesTest<LocaleResources> { - - @Override - protected Class<LocaleResources> getEnumClass() { - return LocaleResources.class; - } - - @Override - protected String getResourceBundle() { - return LocaleResources.RESOURCE_BUNDLE; - } - -} \ No newline at end of file
--- a/web/cmd/src/test/java/com/redhat/thermostat/web/cmd/WebServiceCommandTest.java Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,121 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.web.cmd; - -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.Matchers.any; - -import java.util.List; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; - -import com.redhat.thermostat.common.cli.CommandException; -import com.redhat.thermostat.common.cli.SimpleArguments; -import com.redhat.thermostat.test.TestCommandContextFactory; - -public class WebServiceCommandTest { - - private TestCommandContextFactory cmdCtxFactory; - private BundleContext bundleContext; - private WebServiceLauncher launcher; - private WebServiceCommand cmd; - - @Before - public void setUp() { - Bundle sysBundle = mock(Bundle.class); - bundleContext = mock(BundleContext.class); - when(bundleContext.getBundle(0)).thenReturn(sysBundle); - cmdCtxFactory = new TestCommandContextFactory(bundleContext); - launcher = mock(WebServiceLauncher.class); - cmd = new WebServiceCommand(launcher); - } - - @After - public void tearDown() { - cmdCtxFactory = null; - cmd = null; - launcher = null; - } - - @SuppressWarnings("unchecked") - @Test - public void verifyLauncherStart() throws Exception { - SimpleArguments args = new SimpleArguments(); - String storageUrl = "mongodb://127.0.0.1:27518"; - args.addArgument("dbUrl", storageUrl); - args.addArgument("bindAddrs", "127.0.0.1:8888,127.0.0.2:9999"); - try { - cmd.run(cmdCtxFactory.createContext(args)); - } catch (CommandException e) { - fail("should not throw exception"); - } - verify(launcher).setStorageURL(storageUrl); - verify(launcher).setStorageUsername(null); - verify(launcher).setStoragePassword(null); - verify(launcher).setIpAddresses(any(List.class)); - verify(launcher).start(); - } - - @SuppressWarnings("unchecked") - @Test - public void verifyLauncherStartWithAuth() throws Exception { - SimpleArguments args = new SimpleArguments(); - String storageUrl = "mongodb://127.0.0.1:27518"; - args.addArgument("dbUrl", storageUrl); - args.addArgument("bindAddrs", "127.0.0.1:8888,127.0.0.2:9999"); - args.addArgument("username", "testuser"); - args.addArgument("password", "testpasswd"); - try { - cmd.run(cmdCtxFactory.createContext(args)); - } catch (CommandException e) { - fail("should not throw exception"); - } - verify(launcher).setStorageURL(storageUrl); - verify(launcher).setStorageUsername("testuser"); - verify(launcher).setStoragePassword("testpasswd"); - verify(launcher).setIpAddresses(any(List.class)); - verify(launcher).start(); - } -} -
--- a/web/cmd/src/test/java/com/redhat/thermostat/web/cmd/WebServiceLauncherTest.java Fri Nov 15 15:50:46 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.web.cmd; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.doNothing; - -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.jetty.server.Server; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import com.redhat.thermostat.common.utils.HostPortPair; -import com.redhat.thermostat.shared.config.InvalidConfigurationException; - -public class WebServiceLauncherTest { - - private WebServiceLauncher launcher; - private List<HostPortPair> dummyIp; - - @Before - public void setUp() { - dummyIp = new ArrayList<HostPortPair>(); - dummyIp.add(new HostPortPair("127.0.0.1", 8889)); - } - - @After - public void tearDown() { - launcher = null; - dummyIp = null; - } - - @Test( expected=InvalidConfigurationException.class ) - public void unsetStorageURLThrowsException() throws Exception { - launcher = new WebServiceLauncher(); - launcher.setIpAddresses(dummyIp); - launcher.start(); - } - - @Test( expected=InvalidConfigurationException.class ) - public void unsetIpAddressesThrowsException() throws Exception { - launcher = new WebServiceLauncher(); - launcher.setStorageURL("something not null"); - launcher.setIpAddresses(null); - launcher.start(); - } - - @Test - public void invalidPortThrowsException() throws Exception { - int excptnsThrown = 0; - int excptnsExpected = 2; - launcher = new WebServiceLauncher(); - List<HostPortPair> ips = new ArrayList<>(); - ips.add(new HostPortPair("127.0.0.1", -10)); - try { - launcher.setIpAddresses(ips); - launcher.start(); - } catch (InvalidConfigurationException e) { - excptnsThrown++; - } - ips = new ArrayList<>(); - ips.add(new HostPortPair("127.0.0.1", 0)); - try { - launcher.setIpAddresses(ips); - launcher.start(); - } catch (InvalidConfigurationException e) { - excptnsThrown++; - } - assertEquals(excptnsExpected, excptnsThrown); - } - - @Test - @Ignore("server.start() throws NPE since it's a final method declared in class AbstractLifeCycle and Mockito doesn't do final method mocking.") - public void verifyStartDoesStartServer() throws Exception { - Server server = mock(Server.class); - doNothing().when(server).start(); - WebServiceLauncher launcher = new WebServiceLauncher(server); - launcher.setIpAddresses(dummyIp); - launcher.setStorageURL("mongodb://test.example.org/db"); - launcher.start(); - verify(server).start(); - launcher.stop(); - } - -} -