Mercurial > hg > release > thermostat-1.6
changeset 1743:ca91f13ef261
Clean up the API for tracking command's dependencies
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/015729.html
PR2605
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Wed, 02 Sep 2015 18:49:42 +0200 |
parents | d7a88a478235 |
children | a23998f093d2 |
files | vm-profiler/client-cli/src/main/java/com/redhat/thermostat/vm/profiler/client/cli/internal/AbstractCommand.java vm-profiler/client-cli/src/main/java/com/redhat/thermostat/vm/profiler/client/cli/internal/DependencyServices.java vm-profiler/client-cli/src/main/java/com/redhat/thermostat/vm/profiler/client/cli/internal/ProfileVmCommand.java vm-profiler/client-cli/src/test/java/com/redhat/thermostat/vm/profiler/client/cli/internal/AbstractCommandTest.java vm-profiler/client-cli/src/test/java/com/redhat/thermostat/vm/profiler/client/cli/internal/DependencyServicesTest.java |
diffstat | 5 files changed, 188 insertions(+), 181 deletions(-) [+] |
line wrap: on
line diff
--- a/vm-profiler/client-cli/src/main/java/com/redhat/thermostat/vm/profiler/client/cli/internal/AbstractCommand.java Thu Jun 25 15:46:42 2015 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -/* - * Copyright 2012-2015 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.vm.profiler.client.cli.internal; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; - -public abstract class AbstractCommand extends com.redhat.thermostat.common.cli.AbstractCommand { - - // TODO these changes should probably be promoted to the AbstractCommand public API - - private Map<Class<?>, BlockingQueue<?>> serviceHolder = new HashMap<>(); - - private <T> BlockingQueue<T> getHolder(Class<T> serviceClass) { - if (!serviceHolder.containsKey(serviceClass)) { - serviceHolder.put(serviceClass, new LinkedBlockingQueue<T>(1)); - } - return (BlockingQueue<T>) serviceHolder.get(serviceClass); - } - - protected <T> void addService(Class<T> serviceClass, T item) { - Objects.requireNonNull(item); - BlockingQueue<T> holder = getHolder(serviceClass); - try { - holder.put(item); - } catch (InterruptedException e) { - throw new AssertionError("Should not happen"); - } - } - - protected <T> void removeService(Class<T> serviceClass) { - BlockingQueue<T> holder = getHolder(serviceClass); - if (holder.peek() != null) { - holder.remove(); - } - } - - /** @return the service, or <code>null</code> */ - protected <T> T getService(Class<T> serviceClass) { - BlockingQueue<T> holder = getHolder(serviceClass); - try { - // a crappy version of peek()-with-timeout - T retValue = holder.poll(500, TimeUnit.MILLISECONDS); - if (retValue != null) { - holder.add(retValue); - } - return retValue; - } catch (InterruptedException e) { - return null; - } - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-profiler/client-cli/src/main/java/com/redhat/thermostat/vm/profiler/client/cli/internal/DependencyServices.java Wed Sep 02 18:49:42 2015 +0200 @@ -0,0 +1,96 @@ +/* + * Copyright 2012-2015 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.vm.profiler.client.cli.internal; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +/** + * Contains dependencies for a command. + * + * Provides ways to add ({@link #addService(Class, Object)}), remove ( + * {@link #removeService(Class)}) and fetch ({@link #getService(Class)}) + * services. Fetching has timeout support. + */ +public class DependencyServices { + + private Map<Class<?>, BlockingQueue<?>> serviceHolder = new HashMap<>(); + + private <T> BlockingQueue<T> getHolder(Class<T> serviceClass) { + if (!serviceHolder.containsKey(serviceClass)) { + serviceHolder.put(serviceClass, new LinkedBlockingQueue<T>(1)); + } + return (BlockingQueue<T>) serviceHolder.get(serviceClass); + } + + /** @throws IllegalArgumentException if service is already added */ + public <T> void addService(Class<T> serviceClass, T item) { + Objects.requireNonNull(item); + BlockingQueue<T> holder = getHolder(serviceClass); + boolean added = holder.offer(item); + if (!added) { + throw new IllegalArgumentException("service is already being tracked"); + } + } + + public <T> void removeService(Class<T> serviceClass) { + BlockingQueue<T> holder = getHolder(serviceClass); + if (holder.peek() != null) { + holder.remove(); + } + } + + /** @return the service, or {@code null} */ + public <T> T getService(Class<T> serviceClass) { + BlockingQueue<T> holder = getHolder(serviceClass); + try { + // a crappy version of peek()-with-timeout + T retValue = holder.poll(500, TimeUnit.MILLISECONDS); + if (retValue != null) { + holder.add(retValue); + } + return retValue; + } catch (InterruptedException e) { + return null; + } + } + +}
--- a/vm-profiler/client-cli/src/main/java/com/redhat/thermostat/vm/profiler/client/cli/internal/ProfileVmCommand.java Thu Jun 25 15:46:42 2015 -0400 +++ b/vm-profiler/client-cli/src/main/java/com/redhat/thermostat/vm/profiler/client/cli/internal/ProfileVmCommand.java Wed Sep 02 18:49:42 2015 +0200 @@ -46,6 +46,7 @@ import com.redhat.thermostat.client.cli.HostVMArguments; import com.redhat.thermostat.client.command.RequestQueue; +import com.redhat.thermostat.common.cli.AbstractCommand; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.Console; @@ -73,19 +74,21 @@ private static final String STOP_ARGUMENT = "stop"; private static final String STATUS_ARGUMENT = "status"; private static final String SHOW_ARGUMENT = "show"; + + private final DependencyServices myServices = new DependencyServices(); @Override public void run(CommandContext ctx) throws CommandException { HostVMArguments args = new HostVMArguments(ctx.getArguments(), true, true); - AgentInfoDAO agentInfoDAO = getService(AgentInfoDAO.class); - VmInfoDAO vmInfoDAO = getService(VmInfoDAO.class); + AgentInfoDAO agentInfoDAO = myServices.getService(AgentInfoDAO.class); + VmInfoDAO vmInfoDAO = myServices.getService(VmInfoDAO.class); requireNonNull(agentInfoDAO, translator.localize(LocaleResources.AGENT_SERVICE_UNAVAILABLE)); requireNonNull(vmInfoDAO, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); - RequestQueue requestQueue = getService(RequestQueue.class); + RequestQueue requestQueue = myServices.getService(RequestQueue.class); requireNonNull(requestQueue, translator.localize(LocaleResources.QUEUE_SERVICE_UNAVAILABLE)); AgentInformation agentInfo = agentInfoDAO.getAgentInformation(args.getHost()); @@ -176,7 +179,7 @@ } private void showProfilingStatus(Console console, VmRef vm) { - ProfileDAO dao = getService(ProfileDAO.class); + ProfileDAO dao = myServices.getService(ProfileDAO.class); ProfileStatusChange latest = dao.getLatestStatus(vm); boolean profiling = false; if (latest != null) { @@ -192,7 +195,7 @@ } private void showProfilingResults(Console console, VmRef vm) { - ProfileDAO dao = getService(ProfileDAO.class); + ProfileDAO dao = myServices.getService(ProfileDAO.class); InputStream data = dao.loadLatestProfileData(vm); if (data == null) { console.getError().println(translator.localize(LocaleResources.PROFILING_DATA_NOT_AVAILABLE).getContents()); @@ -226,34 +229,34 @@ } void setAgentInfoDAO(AgentInfoDAO dao) { - addService(AgentInfoDAO.class, dao); + myServices.addService(AgentInfoDAO.class, dao); } void unsetAgentInfoDAO() { - removeService(AgentInfoDAO.class); + myServices.removeService(AgentInfoDAO.class); } void setVmInfoDAO(VmInfoDAO dao) { - addService(VmInfoDAO.class, dao); + myServices.addService(VmInfoDAO.class, dao); } void unsetVmInfoDAO() { - removeService(VmInfoDAO.class); + myServices.removeService(VmInfoDAO.class); } void setRequestQueue(RequestQueue queue) { - addService(RequestQueue.class, queue); + myServices.addService(RequestQueue.class, queue); } void unsetRequestQueue() { - removeService(RequestQueue.class); + myServices.removeService(RequestQueue.class); } void setProfileDAO(ProfileDAO dao) { - addService(ProfileDAO.class, dao); + myServices.addService(ProfileDAO.class, dao); } void unsetProfileDAO() { - removeService(ProfileDAO.class); + myServices.removeService(ProfileDAO.class); } }
--- a/vm-profiler/client-cli/src/test/java/com/redhat/thermostat/vm/profiler/client/cli/internal/AbstractCommandTest.java Thu Jun 25 15:46:42 2015 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,77 +0,0 @@ -/* - * Copyright 2012-2015 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.vm.profiler.client.cli.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -import org.junit.Test; - -import com.redhat.thermostat.common.cli.CommandContext; -import com.redhat.thermostat.common.cli.CommandException; - -public class AbstractCommandTest { - - static class TestCommand extends AbstractCommand { - @Override - public void run(CommandContext ctx) throws CommandException { /* do nothing */ } - } - - @Test - public void uninjectedServiceIsNotAvailable() throws Exception { - AbstractCommand command = new TestCommand(); - - assertNull(command.getService(Object.class)); - } - - @Test - public void injectedServiceIsAvailable() throws Exception { - AbstractCommand command = new TestCommand(); - command.addService(Object.class, command); - - assertEquals(command, command.getService(Object.class)); - } - - @Test - public void injectedAndRemovedServiceIsNotAvailable() { - AbstractCommand command = new TestCommand(); - command.addService(Object.class, command); - - command.removeService(Object.class); - assertNull(command.getService(Object.class)); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-profiler/client-cli/src/test/java/com/redhat/thermostat/vm/profiler/client/cli/internal/DependencyServicesTest.java Wed Sep 02 18:49:42 2015 +0200 @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2015 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.vm.profiler.client.cli.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class DependencyServicesTest { + + @Test + public void uninjectedServiceIsNotAvailable() throws Exception { + DependencyServices services = new DependencyServices(); + + assertNull(services.getService(Object.class)); + } + + @Test + public void injectedServiceIsAvailable() throws Exception { + DependencyServices services = new DependencyServices(); + services.addService(Object.class, services); + + assertEquals(services, services.getService(Object.class)); + } + + @Test(expected=IllegalArgumentException.class) + public void injectingSameServiceTwiceIsAnError() { + DependencyServices services = new DependencyServices(); + services.addService(Object.class, services); + services.addService(Object.class, services); + } + + @Test + public void injectedAndRemovedServiceIsNotAvailable() { + DependencyServices services = new DependencyServices(); + services.addService(Object.class, services); + + services.removeService(Object.class); + assertNull(services.getService(Object.class)); + } +}