From c9c4a1aef05aea1a53257d20a0afcc4ba89ba2b5 Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Thu, 15 Mar 2012 04:42:33 +0000 Subject: [PATCH] made jetty serve preseed.cfg from inside the jar (was serving from src/test/resources) --- .../VirtualBoxPropertiesBuilder.java | 2 +- .../VirtualBoxComputeServiceAdapter.java | 2 +- .../admin/StartJettyIfNotAlreadyRunning.java | 46 ++++++++++----- .../statements/DeleteGShadowLock.java | 2 +- .../src/{test => main}/resources/preseed.cfg | 0 ...StartJettyIfNotAlreadyRunningLiveTest.java | 59 +++++++++++++++++++ 6 files changed, 92 insertions(+), 19 deletions(-) rename labs/virtualbox/src/{test => main}/resources/preseed.cfg (100%) create mode 100644 labs/virtualbox/src/test/java/org/jclouds/virtualbox/StartJettyIfNotAlreadyRunningLiveTest.java diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java index 2ac68ed108..4f6c728155 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java @@ -78,7 +78,7 @@ public class VirtualBoxPropertiesBuilder extends PropertiesBuilder { properties.put(VIRTUALBOX_IMAGES_DESCRIPTOR, yamlDescriptor); - properties.put(VIRTUALBOX_PRECONFIGURATION_URL, "http://10.0.2.2:23232/src/test/resources/preseed.cfg"); + properties.put(VIRTUALBOX_PRECONFIGURATION_URL, "http://10.0.2.2:23232/preseed.cfg"); return properties; } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java index 1f15abbb0b..5ae08a2338 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java @@ -96,7 +96,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter { - @Resource + @javax.annotation.Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) protected Logger logger = Logger.NULL; private Server jetty; @@ -60,7 +67,7 @@ public class StartJettyIfNotAlreadyRunning extends CacheLoader { } @Override - public URI load(IsoSpec isoSpec) throws Exception { + public URI load(IsoSpec isoSpec) throws Exception { try { start(); } catch (Exception e) { @@ -74,24 +81,31 @@ public class StartJettyIfNotAlreadyRunning extends CacheLoader { if (jetty.getState().equals(Server.STARTED)) { logger.debug("not starting jetty, as existing host is serving %s", preconfigurationUrl); } else { - logger.debug(">> starting jetty to serve %s", preconfigurationUrl); - ResourceHandler resourceHandler = new ResourceHandler(); - resourceHandler.setDirectoriesListed(true); - resourceHandler.setWelcomeFiles(new String[]{"index.html"}); - - resourceHandler.setResourceBase(""); - HandlerList handlers = new HandlerList(); - handlers.setHandlers(new Handler[]{resourceHandler, new DefaultHandler()}); - jetty.setHandler(handlers); - try { + logger.debug(">> starting jetty to serve %s", preconfigurationUrl); + + Resource resource = Resource.newClassPathResource("preseed.cfg"); + checkState(resource.getInputStream() != null, "cannot find preseed.cfg"); + + ContextHandler capHandler = new ContextHandler(); + capHandler.setContextPath("/preseed.cfg"); + + ResourceHandler resourceHandler = new ResourceHandler(); + resourceHandler.setBaseResource(resource); + capHandler.setHandler(resourceHandler); + + HandlerList handlers = new HandlerList(); + handlers.setHandlers(new Handler[] { capHandler, new DefaultHandler() }); + jetty.setHandler(handlers); + jetty.start(); + + logger.debug("<< serving %s", resourceHandler.getBaseResource()); } catch (Exception e) { logger.error(e, "Server jetty could not be started for %s", preconfigurationUrl); + throw Throwables.propagate(e); } - logger.debug("<< serving %s", resourceHandler.getBaseResource()); } - } @PreDestroy() diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java index e267e14cdc..d2b6e26acc 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java @@ -26,7 +26,7 @@ import org.jclouds.scriptbuilder.domain.Statement; import com.google.common.collect.ImmutableList; /** - * Deletes /etc/gshadow.lock https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/732864. + * Deletes /etc/gshadow.lock. see https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/732864. * * @author dralves * diff --git a/labs/virtualbox/src/test/resources/preseed.cfg b/labs/virtualbox/src/main/resources/preseed.cfg similarity index 100% rename from labs/virtualbox/src/test/resources/preseed.cfg rename to labs/virtualbox/src/main/resources/preseed.cfg diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/StartJettyIfNotAlreadyRunningLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/StartJettyIfNotAlreadyRunningLiveTest.java new file mode 100644 index 0000000000..49f3c7cacc --- /dev/null +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/StartJettyIfNotAlreadyRunningLiveTest.java @@ -0,0 +1,59 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.virtualbox; + +import java.net.URI; +import java.net.URL; +import java.util.Properties; + +import org.apache.commons.io.IOUtils; +import org.eclipse.jetty.server.Server; +import org.jclouds.virtualbox.config.VirtualBoxConstants; +import org.jclouds.virtualbox.functions.admin.StartJettyIfNotAlreadyRunning; +import org.testng.annotations.Test; + +/** + * Tests that jetty is able to serve the preseed.cfg file. This test is here to have access to the + * defaultProperties() method in {@link VirtualBoxPropertiesBuilder}. + * + * @author dralves + * + */ +@Test(groups = "live", singleThreaded = true, testName = "StartJettyIfNotAlreadyRunningLiveTest") +public class StartJettyIfNotAlreadyRunningLiveTest { + + @Test + public void testJettyServerServesPreseedFile() throws Exception { + Properties props = new VirtualBoxPropertiesBuilder().defaultProperties(); + + String preconfigurationUrl = props.getProperty(VirtualBoxConstants.VIRTUALBOX_PRECONFIGURATION_URL); + + int port = URI.create(preconfigurationUrl).getPort(); + + Server server = new Server(port); + + StartJettyIfNotAlreadyRunning starter = new StartJettyIfNotAlreadyRunning(preconfigurationUrl, server); + + starter.load(null); + + // if this opens up a file we're golden + IOUtils.toString(new URL("http://127.0.0.1:" + port + "/preseed.cfg").openStream()); + } + +}