Some cleanup of RetrieveActiveBridgedInterfaces and added copyright.

This commit is contained in:
Mattias Holmqvist 2011-11-27 02:11:23 +01:00
parent 9272136548
commit 4effcc39ad
2 changed files with 40 additions and 33 deletions

View File

@ -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; package org.jclouds.virtualbox.functions;
import static com.google.common.base.Preconditions.checkNotNull; 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 static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import java.net.NetworkInterface; import java.net.NetworkInterface;
@ -25,8 +44,10 @@ import com.google.common.collect.Lists;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named; import com.google.inject.name.Named;
public class RetrieveActiveBridgedInterfaces implements /**
Function<String, List<String>> { * @author Andrea Turli
*/
public class RetrieveActiveBridgedInterfaces implements Function<String, List<String>> {
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
@ -36,7 +57,6 @@ public class RetrieveActiveBridgedInterfaces implements
@Inject @Inject
public RetrieveActiveBridgedInterfaces(ComputeServiceContext context) { public RetrieveActiveBridgedInterfaces(ComputeServiceContext context) {
super();
this.context = context; this.context = context;
} }
@ -54,16 +74,11 @@ public class RetrieveActiveBridgedInterfaces implements
// union of bridgedNetwork with inet up and !loopback // union of bridgedNetwork with inet up and !loopback
List<String> activeNetworkInterfaceNames = Lists.newArrayList(); List<String> activeNetworkInterfaceNames = Lists.newArrayList();
Enumeration<NetworkInterface> nets;
try { try {
Iterable<String> filterdBridgedInterface = null; Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface inet : Collections.list(nets)) { for (NetworkInterface inet : Collections.list(nets)) {
filterdBridgedInterface = Iterables.filter(bridgedInterfaces, Iterable<String> filteredBridgedInterface = filter(bridgedInterfaces, new IsActiveBridgedInterface(inet));
new IsActiveBridgedInterface(inet)); Iterables.addAll(activeNetworkInterfaceNames, filteredBridgedInterface);
for (String filterInetName : filterdBridgedInterface) {
activeNetworkInterfaceNames.add(filterInetName);
}
} }
} catch (SocketException e) { } catch (SocketException e) {
logger.error(e, "Problem in listing network interfaces."); logger.error(e, "Problem in listing network interfaces.");
@ -72,23 +87,19 @@ public class RetrieveActiveBridgedInterfaces implements
return activeNetworkInterfaceNames; return activeNetworkInterfaceNames;
} }
protected static List<String> retrieveBridgedInterfaceNames( protected static List<String> retrieveBridgedInterfaceNames(String bridgedIfBlocks) {
String bridgedIfBlocks) {
List<String> bridgedInterfaceNames = Lists.newArrayList(); List<String> bridgedInterfaceNames = Lists.newArrayList();
// separate the different bridge block // separate the different bridge block
for (String bridgedIfBlock : Splitter.on( for (String bridgedIfBlock : Splitter.on(Pattern.compile("(?m)^[ \t]*\r?\n")).split(bridgedIfBlocks)) {
Pattern.compile("(?m)^[ \t]*\r?\n")).split(bridgedIfBlocks)) {
Iterable<String> bridgedIfName = Iterables.filter(Splitter.on("\n") Iterable<String> bridgedIfName = filter(Splitter.on("\n").split(bridgedIfBlock), new Predicate<String>() {
.split(bridgedIfBlock), new Predicate<String>() {
@Override @Override
public boolean apply(String arg0) { public boolean apply(String arg0) {
return arg0.startsWith("Name:"); return arg0.startsWith("Name:");
} }
}); });
for (String bridgedInterfaceName : bridgedIfName) { for (String bridgedInterfaceName : bridgedIfName) {
for (String string : Splitter.on("Name:").split( for (String string : Splitter.on("Name:").split(bridgedInterfaceName)) {
bridgedInterfaceName)) {
if (!string.isEmpty()) if (!string.isEmpty())
bridgedInterfaceNames.add(string.trim()); bridgedInterfaceNames.add(string.trim());
} }
@ -108,8 +119,6 @@ public class RetrieveActiveBridgedInterfaces implements
private NetworkInterface networkInterface; private NetworkInterface networkInterface;
public IsActiveBridgedInterface(NetworkInterface networkInterface) { public IsActiveBridgedInterface(NetworkInterface networkInterface) {
super();
this.networkInterface = networkInterface; this.networkInterface = networkInterface;
} }
@ -125,5 +134,5 @@ public class RetrieveActiveBridgedInterfaces implements
} }
return false; return false;
} }
}; }
} }

View File

@ -1,4 +1,4 @@
/** /*
* Licensed to jclouds, Inc. (jclouds) under one or more * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
@ -37,8 +37,7 @@ import com.google.common.collect.ImmutableList;
* @author Andrea Turli * @author Andrea Turli
*/ */
@Test(groups = "live", singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesLiveTest") @Test(groups = "live", singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesLiveTest")
public class RetrieveActiveBridgedInterfacesLiveTest extends public class RetrieveActiveBridgedInterfacesLiveTest extends BaseVirtualBoxClientLiveTest {
BaseVirtualBoxClientLiveTest {
public static final String TEST1 = "Name: eth0\n" public static final String TEST1 = "Name: eth0\n"
+ "GUID: 30687465-0000-4000-8000-00261834d0cb\n" + "GUID: 30687465-0000-4000-8000-00261834d0cb\n"
@ -80,8 +79,7 @@ public class RetrieveActiveBridgedInterfacesLiveTest extends
ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest( ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest(
hostId, "localhost", guestId, "localhost", new Credentials("toor", hostId, "localhost", guestId, "localhost", new Credentials("toor",
"password")); "password"));
List<String> bridgedInterface = new RetrieveActiveBridgedInterfaces( List<String> bridgedInterface = new RetrieveActiveBridgedInterfaces(localHostContext).apply(hostId);
localHostContext).apply(hostId);
assertFalse(bridgedInterface.isEmpty()); assertFalse(bridgedInterface.isEmpty());
} }