Issue 191: NetworkInterfaces not allowed on gae

This commit is contained in:
Adrian Cole 2010-07-25 18:54:24 -07:00
parent 06ecdb8d7a
commit b40b6a45bc
3 changed files with 3 additions and 32 deletions

View File

@ -20,8 +20,6 @@ package org.jclouds.ohai;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -35,7 +33,6 @@ import org.jclouds.domain.JsonBall;
import org.jclouds.json.Json; import org.jclouds.json.Json;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.inject.internal.Maps; import com.google.inject.internal.Maps;
@ -53,22 +50,16 @@ public class WhiteListCompliantJVM implements Supplier<Map<String, JsonBall>> {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private final Function<byte[], String> byteArrayToMacAddress;
private final NetworkInterface defaultNetworkInterface;
private final Json json; private final Json json;
private final Provider<Long> nanoTimeProvider; private final Provider<Long> nanoTimeProvider;
private final Provider<Properties> systemPropertiesProvider; private final Provider<Properties> systemPropertiesProvider;
@Inject @Inject
public WhiteListCompliantJVM(Json json, Function<byte[], String> byteArrayToMacAddress, public WhiteListCompliantJVM(Json json, @Named("nanoTime") Provider<Long> nanoTimeProvider,
@Named("nanoTime") Provider<Long> nanoTimeProvider, @Named("systemProperties") Provider<Properties> systemPropertiesProvider) {
@Named("systemProperties") Provider<Properties> systemPropertiesProvider,
NetworkInterface defaultNetworkInterface) {
this.json = checkNotNull(json, "json"); this.json = checkNotNull(json, "json");
this.byteArrayToMacAddress = checkNotNull(byteArrayToMacAddress, "byteArrayToMacAddress");
this.nanoTimeProvider = checkNotNull(nanoTimeProvider, "nanoTimeProvider"); this.nanoTimeProvider = checkNotNull(nanoTimeProvider, "nanoTimeProvider");
this.systemPropertiesProvider = checkNotNull(systemPropertiesProvider, "systemPropertiesProvider"); this.systemPropertiesProvider = checkNotNull(systemPropertiesProvider, "systemPropertiesProvider");
this.defaultNetworkInterface = checkNotNull(defaultNetworkInterface, "defaultNetworkInterface");
} }
public Map<String, JsonBall> get() { public Map<String, JsonBall> get() {
@ -83,13 +74,6 @@ public class WhiteListCompliantJVM implements Supplier<Map<String, JsonBall>> {
returnVal.put("ohai_time", new JsonBall(now)); returnVal.put("ohai_time", new JsonBall(now));
returnVal.put("java", new JsonBall(json.toJson(systemProperties))); returnVal.put("java", new JsonBall(json.toJson(systemProperties)));
try {
String mac = byteArrayToMacAddress.apply(defaultNetworkInterface.getHardwareAddress());
returnVal.put("macaddress", new JsonBall(mac));
} catch (SocketException e) {
logger.warn(e, "could not read address from %s", defaultNetworkInterface.getDisplayName());
}
String platform = systemProperties.getProperty("os.name"); String platform = systemProperties.getProperty("os.name");
platform = platform.replaceAll("[ -]", "").toLowerCase(); platform = platform.replaceAll("[ -]", "").toLowerCase();

View File

@ -18,8 +18,6 @@
*/ */
package org.jclouds.ohai.config; package org.jclouds.ohai.config;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -80,10 +78,4 @@ public class BaseOhaiModule extends AbstractModule {
return ImmutableList.<Supplier<Map<String, JsonBall>>> of(injector.getInstance(WhiteListCompliantJVM.class)); return ImmutableList.<Supplier<Map<String, JsonBall>>> of(injector.getInstance(WhiteListCompliantJVM.class));
} }
@Provides
@Singleton
protected NetworkInterface provideDefaultNetworkInterface() throws SocketException {
return NetworkInterface.getNetworkInterfaces().nextElement();
}
} }

View File

@ -25,7 +25,6 @@ package org.jclouds.ohai;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.net.NetworkInterface;
import java.net.SocketException; import java.net.SocketException;
import java.util.Properties; import java.util.Properties;
@ -33,7 +32,6 @@ import org.jclouds.chef.config.ChefParserModule;
import org.jclouds.json.Json; import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule; import org.jclouds.json.config.GsonModule;
import org.jclouds.ohai.config.BaseOhaiModule; import org.jclouds.ohai.config.BaseOhaiModule;
import org.jclouds.ohai.functions.ByteArrayToMacAddress;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.inject.Guice; import com.google.inject.Guice;
@ -50,7 +48,6 @@ public class WhiteListCompliantJVMTest {
@Test @Test
public void test() throws SocketException { public void test() throws SocketException {
byte[] mac = NetworkInterface.getNetworkInterfaces().nextElement().getHardwareAddress();
final Properties sysProperties = new Properties(); final Properties sysProperties = new Properties();
sysProperties.setProperty("os.name", "Mac OS X"); sysProperties.setProperty("os.name", "Mac OS X");
@ -69,14 +66,12 @@ public class WhiteListCompliantJVMTest {
} }
}); });
String macString = injector.getInstance(ByteArrayToMacAddress.class).apply(mac);
Json json = injector.getInstance(Json.class); Json json = injector.getInstance(Json.class);
WhiteListCompliantJVM WhiteListCompliantJVM = injector.getInstance(WhiteListCompliantJVM.class); WhiteListCompliantJVM WhiteListCompliantJVM = injector.getInstance(WhiteListCompliantJVM.class);
assertEquals( assertEquals(
json.toJson(WhiteListCompliantJVM.get()), json.toJson(WhiteListCompliantJVM.get()),
"{\"ohai_time\":1279992919.32529,\"java\":{\"user.name\":\"user\",\"os.version\":\"10.3.0\",\"os.name\":\"Mac OS X\"},\"macaddress\":\"" "{\"ohai_time\":1279992919.32529,\"java\":{\"user.name\":\"user\",\"os.version\":\"10.3.0\",\"os.name\":\"Mac OS X\"},\"platform\":\"macosx\",\"platform_version\":\"10.3.0\",\"current_user\":\"user\"}");
+ macString + "\",\"platform\":\"macosx\",\"platform_version\":\"10.3.0\",\"current_user\":\"user\"}");
} }
} }