From 4effcc39ad6c485d5319b54b2b0ebb1366787d54 Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Sun, 27 Nov 2011 02:11:23 +0100 Subject: [PATCH] Some cleanup of RetrieveActiveBridgedInterfaces and added copyright. --- .../RetrieveActiveBridgedInterfaces.java | 65 +++++++++++-------- ...trieveActiveBridgedInterfacesLiveTest.java | 8 +-- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfaces.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfaces.java index cd5f673f26..5e1e4a26e9 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfaces.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfaces.java @@ -1,6 +1,25 @@ +/** + * 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.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.Iterables.filter; import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot; import java.net.NetworkInterface; @@ -25,8 +44,10 @@ import com.google.common.collect.Lists; import com.google.inject.Inject; import com.google.inject.name.Named; -public class RetrieveActiveBridgedInterfaces implements - Function> { +/** + * @author Andrea Turli + */ +public class RetrieveActiveBridgedInterfaces implements Function> { @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) @@ -36,7 +57,6 @@ public class RetrieveActiveBridgedInterfaces implements @Inject public RetrieveActiveBridgedInterfaces(ComputeServiceContext context) { - super(); this.context = context; } @@ -45,25 +65,20 @@ public class RetrieveActiveBridgedInterfaces implements // Bridged Network String command = "vboxmanage list bridgedifs"; String bridgedIfBlocks = context - .getComputeService() - .runScriptOnNode(hostId, command, - runAsRoot(false).wrapInInitScript(false)).getOutput(); + .getComputeService() + .runScriptOnNode(hostId, command, + runAsRoot(false).wrapInInitScript(false)).getOutput(); List bridgedInterfaces = retrieveBridgedInterfaceNames(bridgedIfBlocks); checkNotNull(bridgedInterfaces); // union of bridgedNetwork with inet up and !loopback List activeNetworkInterfaceNames = Lists.newArrayList(); - Enumeration nets; try { - Iterable filterdBridgedInterface = null; - nets = NetworkInterface.getNetworkInterfaces(); + Enumeration nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface inet : Collections.list(nets)) { - filterdBridgedInterface = Iterables.filter(bridgedInterfaces, - new IsActiveBridgedInterface(inet)); - for (String filterInetName : filterdBridgedInterface) { - activeNetworkInterfaceNames.add(filterInetName); - } + Iterable filteredBridgedInterface = filter(bridgedInterfaces, new IsActiveBridgedInterface(inet)); + Iterables.addAll(activeNetworkInterfaceNames, filteredBridgedInterface); } } catch (SocketException e) { logger.error(e, "Problem in listing network interfaces."); @@ -72,23 +87,19 @@ public class RetrieveActiveBridgedInterfaces implements return activeNetworkInterfaceNames; } - protected static List retrieveBridgedInterfaceNames( - String bridgedIfBlocks) { + protected static List retrieveBridgedInterfaceNames(String bridgedIfBlocks) { List bridgedInterfaceNames = Lists.newArrayList(); // separate the different bridge block - for (String bridgedIfBlock : Splitter.on( - Pattern.compile("(?m)^[ \t]*\r?\n")).split(bridgedIfBlocks)) { + for (String bridgedIfBlock : Splitter.on(Pattern.compile("(?m)^[ \t]*\r?\n")).split(bridgedIfBlocks)) { - Iterable bridgedIfName = Iterables.filter(Splitter.on("\n") - .split(bridgedIfBlock), new Predicate() { + Iterable bridgedIfName = filter(Splitter.on("\n").split(bridgedIfBlock), new Predicate() { @Override public boolean apply(String arg0) { return arg0.startsWith("Name:"); } }); for (String bridgedInterfaceName : bridgedIfName) { - for (String string : Splitter.on("Name:").split( - bridgedInterfaceName)) { + for (String string : Splitter.on("Name:").split(bridgedInterfaceName)) { if (!string.isEmpty()) bridgedInterfaceNames.add(string.trim()); } @@ -102,14 +113,12 @@ public class RetrieveActiveBridgedInterfaces implements assert false; return null; } - + private class IsActiveBridgedInterface implements Predicate { private NetworkInterface networkInterface; public IsActiveBridgedInterface(NetworkInterface networkInterface) { - - super(); this.networkInterface = networkInterface; } @@ -117,13 +126,13 @@ public class RetrieveActiveBridgedInterfaces implements public boolean apply(String bridgedInterfaceName) { try { return (bridgedInterfaceName.startsWith(networkInterface - .getDisplayName()) && networkInterface.isUp() && !networkInterface - .isLoopback()); + .getDisplayName()) && networkInterface.isUp() && !networkInterface + .isLoopback()); } catch (SocketException e) { logger.error(e, "Problem in listing network interfaces."); propagate(e); } return false; } - }; + } } \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesLiveTest.java index c99c4f51d2..30c9196ceb 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesLiveTest.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to jclouds, Inc. (jclouds) under one or more * contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -37,8 +37,7 @@ import com.google.common.collect.ImmutableList; * @author Andrea Turli */ @Test(groups = "live", singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesLiveTest") -public class RetrieveActiveBridgedInterfacesLiveTest extends - BaseVirtualBoxClientLiveTest { +public class RetrieveActiveBridgedInterfacesLiveTest extends BaseVirtualBoxClientLiveTest { public static final String TEST1 = "Name: eth0\n" + "GUID: 30687465-0000-4000-8000-00261834d0cb\n" @@ -80,8 +79,7 @@ public class RetrieveActiveBridgedInterfacesLiveTest extends ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest( hostId, "localhost", guestId, "localhost", new Credentials("toor", "password")); - List bridgedInterface = new RetrieveActiveBridgedInterfaces( - localHostContext).apply(hostId); + List bridgedInterface = new RetrieveActiveBridgedInterfaces(localHostContext).apply(hostId); assertFalse(bridgedInterface.isEmpty()); }