Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add SystemProperty and PomProperties helpers

  • Loading branch information...
commit e4d3556af9fb75961a44d605ab83f69377efce01 1 parent 87a0a69
@jdillon jdillon authored
View
93 common/src/main/java/org/sonatype/sisu/goodies/common/PomProperties.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved.
+ *
+ * This program is licensed to you under the Apache License Version 2.0,
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the Apache License Version 2.0 is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
+ */
+package org.sonatype.sisu.goodies.common;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Properties;
+
+import com.google.common.base.Throwables;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Helper to read Maven pom.properties metadata.
+ */
+public class PomProperties
+ extends Properties
+{
+ public static final String UNKNOWN = "unknown";
+
+ public static final String GROUP_ID = "groupId";
+
+ public static final String ARTIFACT_ID = "artifactId";
+
+ public static final String VERSION = "version";
+
+ private static final Logger log = LoggerFactory.getLogger(PomProperties.class);
+
+ public PomProperties(final Class owner, final String groupId, final String artifactId) {
+ checkNotNull(owner);
+ checkNotNull(groupId);
+ checkNotNull(artifactId);
+ try {
+ loadMetadata(owner, groupId, artifactId);
+ }
+ catch (IOException e) {
+ throw Throwables.propagate(e);
+ }
+ }
+
+ private void loadMetadata(final Class owner, final String groupId, final String artifactId) throws IOException {
+ String path = String.format("/META-INF/maven/%s/%s/pom.properties", groupId, artifactId);
+ URL url = owner.getResource(path);
+ if (url == null) {
+ log.warn("Missing Maven pom.properties metadata: {}", path);
+ return;
+ }
+
+ log.debug("Loading properties from: {}", url);
+ InputStream input = url.openStream();
+ try {
+ this.load(input);
+ }
+ finally {
+ input.close();
+ }
+
+ // Complain if there is a mismatch between what we expect the gav to be and what it really is
+ String foundGroupId = getGroupId();
+ if (!groupId.equals(foundGroupId)) {
+ log.warn("Artifact groupId mismatch; expected: {}, found: {}", groupId, foundGroupId);
+ }
+ String foundArtifactId = getArtifactId();
+ if (!artifactId.equals(foundArtifactId)) {
+ log.warn("Artifact artifactId mismatch; expected: {}, found: {}", artifactId, foundArtifactId);
+ }
+ }
+
+ public String getGroupId() {
+ return getProperty(GROUP_ID, UNKNOWN);
+ }
+
+ public String getArtifactId() {
+ return getProperty(ARTIFACT_ID, UNKNOWN);
+ }
+
+ public String getVersion() {
+ return getProperty(VERSION, UNKNOWN);
+ }
+}
View
164 common/src/main/java/org/sonatype/sisu/goodies/common/SystemProperty.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved.
+ *
+ * This program is licensed to you under the Apache License Version 2.0,
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the Apache License Version 2.0 is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
+ */
+package org.sonatype.sisu.goodies.common;
+
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorManager;
+import java.beans.PropertyEditorSupport;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Helper to access System properties.
+ */
+public class SystemProperty
+{
+ private static final Logger log = LoggerFactory.getLogger(SystemProperty.class);
+
+ private final String name;
+
+ public SystemProperty(final String name) {
+ this.name = checkNotNull(name);
+ }
+
+ public SystemProperty(final Class type, final String suffix) {
+ this(checkNotNull(type, "type").getName() + "." + checkNotNull(suffix, "suffix"));
+ }
+
+ @VisibleForTesting
+ Properties properties() {
+ return System.getProperties();
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public void set(final @Nullable Object value) {
+ String str = String.valueOf(value);
+ properties().setProperty(name, str);
+ log.trace("Set {}={}", name, str);
+ }
+
+ public boolean isSet() {
+ return get() != null;
+ }
+
+ @Nullable
+ public String get() {
+ return properties().getProperty(name);
+ }
+
+ public String get(final String defaultValue) {
+ String value = get();
+ if (value == null) {
+ return defaultValue;
+ }
+ return value;
+ }
+
+ @Nullable
+ @SuppressWarnings("unchecked")
+ public <T> T get(final Class<T> type) {
+ checkNotNull(type);
+ PropertyEditor editor = PropertyEditorManager.findEditor(type);
+ if (editor == null) {
+ // HACK: Ensure we can always convert Boolean/Integer, seems like some JVM configurations can not do this?!
+ if (type == Boolean.class) {
+ editor = new BooleanPropertyEditor();
+ }
+ else if (type == Integer.class) {
+ editor = new IntegerPropertyEditor();
+ }
+ else {
+ throw new RuntimeException("No property-editor for type: " + type.getName());
+ }
+ }
+ String value = get();
+ if (value == null) {
+ return null;
+ }
+ editor.setAsText(value);
+ return (T) editor.getValue();
+ }
+
+ public <T> T get(final Class<T> type, final T defaultValue) {
+ T value = get(type);
+ if (value == null) {
+ return defaultValue;
+ }
+ return value;
+ }
+
+ public void remove() {
+ properties().remove(name);
+ log.trace("Remove: {}", name);
+ }
+
+ // TODO: This could probably be converted into a PropertyEditor, for now leave as custom code
+
+ public List<String> asList() {
+ String value = get();
+ if (value == null) {
+ return Collections.emptyList();
+ }
+ String[] items = value.split(",");
+ List<String> result = Lists.newArrayListWithCapacity(items.length);
+ for (String item : items) {
+ result.add(item.trim());
+ }
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return name + "=" + get();
+ }
+
+ private static class TextPropertyEditorSupport
+ extends PropertyEditorSupport
+ {
+ @Override
+ public void setAsText(final String text) {
+ super.setValue(text);
+ }
+ }
+
+ private static class BooleanPropertyEditor
+ extends TextPropertyEditorSupport
+ {
+ @Override
+ public Object getValue() {
+ return Boolean.valueOf(getAsText());
+ }
+ }
+
+ private static class IntegerPropertyEditor
+ extends TextPropertyEditorSupport
+ {
+ @Override
+ public Object getValue() {
+ return Integer.valueOf(getAsText());
+ }
+ }
+}
View
134 common/src/test/java/org/sonatype/sisu/goodies/common/SystemPropertyTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved.
+ *
+ * This program is licensed to you under the Apache License Version 2.0,
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the Apache License Version 2.0 is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
+ */
+package org.sonatype.sisu.goodies.common;
+
+import java.util.List;
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@link SystemProperty}.
+ */
+public class SystemPropertyTest
+{
+ private SystemProperty underTest;
+
+ @Before
+ public void setUp() throws Exception {
+ underTest = new SystemProperty("test")
+ {
+ // use private properties instead of system to avoid side-effects
+ final Properties props = new Properties();
+ @Override
+ Properties properties() {
+ return props;
+ }
+ };
+ }
+
+ @Test
+ public void verifySystemPropertySet() {
+ // sanity check since we are using private properties for all other tests
+ String name = "test" + System.currentTimeMillis();
+ try {
+ assertThat(System.getProperty(name), nullValue());
+ new SystemProperty(name).set("foo");
+ assertThat(System.getProperty(name), is("foo"));
+ }
+ finally {
+ System.getProperties().remove(name);
+ }
+ }
+
+ @Test
+ public void typeSuffixName() {
+ SystemProperty prop = new SystemProperty(Object.class, "foo");
+ assertThat(prop.name(), is("java.lang.Object.foo"));
+ }
+
+ @Test
+ public void setGetRemove() {
+ assertThat(underTest.isSet(), is(false));
+ underTest.set("foo");
+ assertThat(underTest.isSet(), is(true));
+ assertThat(underTest.get(), is("foo"));
+ underTest.remove();
+ assertThat(underTest.get(), nullValue());
+ assertThat(underTest.isSet(), is(false));
+ }
+
+ @Test
+ public void get_withDefault() {
+ assertThat(underTest.get("default"), is("default"));
+ underTest.set("foo");
+ assertThat(underTest.get("default"), is("foo"));
+ }
+
+ @Test
+ public void get_asInteger() {
+ assertThat(underTest.get(Integer.class), nullValue());
+ underTest.set("1");
+ assertThat(underTest.get(Integer.class), is(1));
+ }
+
+ @Test
+ public void get_asInteger_withDefault() {
+ assertThat(underTest.get(Integer.class, 1), is(1));
+ underTest.set("2");
+ assertThat(underTest.get(Integer.class, 1), is(2));
+ }
+
+ @Test
+ public void get_asBoolean_withDefault() {
+ assertThat(underTest.get(Boolean.class, false), is(false));
+ underTest.set("true");
+ assertThat(underTest.get(Boolean.class, false), is(true));
+ }
+
+ @Test
+ public void asList_missingProperty() {
+ List<String> result = underTest.asList();
+ assertThat(result, notNullValue());
+ assertThat(result, empty());
+ }
+
+ @Test
+ public void asList_singleValue() {
+ underTest.set("foo");
+ List<String> result = underTest.asList();
+ assertThat(result, notNullValue());
+ assertThat(result, hasSize(1));
+ assertThat(result, hasItem("foo"));
+ }
+
+ @Test
+ public void asList_multiValue() {
+ underTest.set(" foo, bar ,\nbaz\n ");
+ List<String> result = underTest.asList();
+ assertThat(result, notNullValue());
+ assertThat(result, hasSize(3));
+ assertThat(result, hasItem("foo"));
+ assertThat(result, hasItem("bar"));
+ assertThat(result, hasItem("baz"));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.