Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the possibility to have a property file for all classes by not ch… #117

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ hs_err_pid*
/.project
/doc/
/property-inject.launch

/.idea
17 changes: 0 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,6 @@
</build>

<profiles>
<profile>
<id>coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<configuration>
<jacocoReports>
<jacocoReport>${project.build.directory}/site/jacoco/jacoco.xml</jacocoReport>
<jacocoReport>${project.build.directory}/site/jacoco-it/jacoco.xml</jacocoReport>
</jacocoReports>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>release</id>
<build>
Expand Down
43 changes: 28 additions & 15 deletions src/main/java/io/xlate/inject/PropertyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import java.io.InputStream;
import java.lang.reflect.Executable;
import java.lang.reflect.Member;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLStreamHandler;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -52,32 +49,46 @@ URLStreamHandler classPathHandler(Class<?> beanType) {
return new ClasspathURLStreamHandler(beanType.getClassLoader());
}

URL getResourceUrl(String filename) throws MalformedURLException {
return getResourceUrlByLocation(filename, this.getClass());
}

URL getResourceUrl(PropertyResource annotation, Class<?> beanType) throws MalformedURLException {
final String location = annotation.value();
final URL resourceUrl;
String location = annotation.value();


if (location.isEmpty()) {
StringBuilder resourceName = new StringBuilder(CLASSPATH);
resourceName.append(':');
resourceName.append(beanType.getName().replace('.', '/'));
resourceName.append(".properties");
resourceUrl = new URL(null, resourceName.toString(), classPathHandler(beanType));
StringBuilder resourceName = new StringBuilder(CLASSPATH);
resourceName.append(':');
resourceName.append(beanType.getName().replace('.', '/'));
resourceName.append(".properties");
return new URL(null, resourceName.toString(), classPathHandler(beanType));
mueller-jens marked this conversation as resolved.
Show resolved Hide resolved
} else {
final String resolvedLocation;

String resolvedLocation;
if (annotation.resolveEnvironment()) {
resolvedLocation = replaceEnvironmentReferences(location);
} else {
resolvedLocation = location;
}
return getResourceUrlByLocation(resolvedLocation, beanType);
}
}

private URL getResourceUrlByLocation(String location,Class<?> beanType) throws MalformedURLException {
URL resourceUrl;
try {
final URI resourceId = URI.create(resolvedLocation);
final URI resourceId;
if (location.indexOf(':')>0 && !location.startsWith(CLASSPATH)) { // we assume it is an url
resourceId = new URL(location).toURI();
} else {
resourceId = URI.create(location);
}
final String scheme = resourceId.getScheme();

if (scheme != null) {
if (CLASSPATH.equals(scheme)) {
resourceUrl = new URL(null, resolvedLocation, classPathHandler(beanType));
resourceUrl = new URL(null, location, classPathHandler(beanType));
} else {
resourceUrl = resourceId.toURL();
}
Expand All @@ -86,8 +97,10 @@ URL getResourceUrl(PropertyResource annotation, Class<?> beanType) throws Malfor
}
} catch (IllegalArgumentException | MalformedURLException e) {
throw new InjectionException(e);
} catch (URISyntaxException e) {
throw new InjectionException(e);
}
mueller-jens marked this conversation as resolved.
Show resolved Hide resolved
}


return resourceUrl;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/xlate/inject/PropertyFileProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.xlate.inject;

public interface PropertyFileProvider {

String getLocation();

}
18 changes: 15 additions & 3 deletions src/main/java/io/xlate/inject/PropertyProducerBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.InjectionException;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
Expand All @@ -48,6 +50,9 @@ public class PropertyProducerBean {

private final PropertyFactory factory = new PropertyFactory();

@Inject
private Instance<PropertyFileProvider> propertyFilenameProvider;

@Produces
@Dependent
@Property
Expand Down Expand Up @@ -219,15 +224,22 @@ String getProperty(InjectionPoint point) throws IOException {
return systemProperty;
}

final boolean hasGlobalFile = propertyFilenameProvider != null && propertyFilenameProvider.isResolvable();

final PropertyResource resource = annotation.resource();
final String value;
final URL resourceUrl = factory.getResourceUrl(resource, beanType);
value = factory.getProperty(resourceUrl, resource.format(), resource.allowMissingResource(), propertyName, defaultValue);
String value;
URL resourceUrl = factory.getResourceUrl(resource, beanType);
value = factory.getProperty(resourceUrl, resource.format(), hasGlobalFile ? true : resource.allowMissingResource(), propertyName, defaultValue);
mueller-jens marked this conversation as resolved.
Show resolved Hide resolved

if (value != null && annotation.resolveEnvironment()) {
return factory.replaceEnvironmentReferences(value);
}

if (value == null && hasGlobalFile){
resourceUrl = factory.getResourceUrl(propertyFilenameProvider.get().getLocation());
value = factory.getProperty(resourceUrl, resource.format(), resource.allowMissingResource(), propertyName, defaultValue);
}

return value;
}
}
18 changes: 16 additions & 2 deletions src/main/java/io/xlate/inject/PropertyResourceProducerBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.InjectionException;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;

@ApplicationScoped
public class PropertyResourceProducerBean {
Expand All @@ -35,6 +37,9 @@ public class PropertyResourceProducerBean {

private final PropertyFactory factory = new PropertyFactory();

@Inject
private Instance<PropertyFileProvider> propertyFilenameProvider;

@Produces
@Dependent
@PropertyResource
Expand All @@ -48,13 +53,22 @@ public Properties produceProperties(InjectionPoint point) {
final Class<?> beanType = point.getMember().getDeclaringClass();
final PropertyResource annotation = annotated.getAnnotation(PropertyResource.class);
final PropertyResourceFormat format = annotation.format();
URL resourceUrl = null;

Properties p = new Properties();
try {
URL resourceUrl = null;
boolean hasGlobalPropertyFile =propertyFilenameProvider != null && propertyFilenameProvider.isResolvable();
if (hasGlobalPropertyFile) {
String globalFile = propertyFilenameProvider.get().getLocation();
resourceUrl = factory.getResourceUrl(globalFile);
p.putAll(factory.getProperties(resourceUrl, format, annotation.allowMissingResource()));
}
resourceUrl = factory.getResourceUrl(annotation, beanType);
return factory.getProperties(resourceUrl, format, annotation.allowMissingResource());
p.putAll(factory.getProperties(resourceUrl, format, hasGlobalPropertyFile ? true: annotation.allowMissingResource()));
mueller-jens marked this conversation as resolved.
Show resolved Hide resolved
return p;
} catch (Exception e) {
throw new InjectionException(e);
}
}

}
7 changes: 6 additions & 1 deletion src/test/java/io/xlate/inject/PropertyProducerBeanIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldJunit5Extension;
import org.jboss.weld.junit5.WeldSetup;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -32,7 +33,7 @@ class PropertyProducerBeanIT {

@WeldSetup
public WeldInitiator weld = WeldInitiator
.from(PropertyProducerBean.class)
.from(PropertyProducerBean.class, TestFileProvider.class)
.build();

@Inject @Property
Expand Down Expand Up @@ -75,6 +76,10 @@ class PropertyProducerBeanIT {
@Property
int int3;

@BeforeAll
public static void setUp() {
System.setProperty("string6.property.name", "string6value.system");
}
@Test
void testString1_DefaultLookup() {
assertEquals("string1value", string1);
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/io/xlate/inject/PropertyProducerBeanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
Expand All @@ -41,6 +42,7 @@
import javax.enterprise.inject.spi.InjectionPoint;
import javax.json.Json;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
Expand All @@ -55,11 +57,14 @@ class PropertyProducerBeanTest {
@Mock
PropertyResource defaultPropertyResource;

private Locale locale;
@BeforeEach
void setup() {
bean = new PropertyProducerBean();
when(defaultPropertyResource.value()).thenReturn("");
when(defaultPropertyResource.format()).thenReturn(PropertyResourceFormat.PROPERTIES);
locale = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
}

private Property mockProperty(String name,
Expand Down Expand Up @@ -953,4 +958,8 @@ void testProducePropertyJsonObjectInvalid() {
});
}

@AfterEach
public void teardown(){
Locale.setDefault(locale);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class PropertyResourceProducerBeanIT {
.from(PropertyResourceProducerBean.class)
.build();


@Inject
@PropertyResource
Properties defaultProps;
Expand All @@ -59,4 +60,5 @@ void testProps2() {
assertEquals(1, props2.size());
assertEquals("true", props2.getProperty("value.is.found"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

class PropertyResourceProducerBeanTest extends AbstractInjectionPointTest {

Expand Down Expand Up @@ -137,6 +139,7 @@ void testEnvironmentVarForResourceLocation() {
}

@Test
@DisabledOnOs(OS.WINDOWS)
void testProducePropertiesUnreadable() {
File resource = new File("target/test-classes/io/xlate/inject/Unreadable.properties");
resource.setReadable(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (C) 2018 xlate.io LLC, http://www.xlate.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package io.xlate.inject;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Properties;

import javax.inject.Inject;

import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldJunit5Extension;
import org.jboss.weld.junit5.WeldSetup;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(WeldJunit5Extension.class)
class PropertyResourceProducerBeanWithConfigurationFileIT {

@WeldSetup
WeldInitiator weld = WeldInitiator
.from(PropertyResourceProducerBean.class, TestFileProvider.class)
.build();

@Inject
@PropertyResource
Properties defaultProps;


@Inject
@PropertyResource("io/xlate/inject/PropertyResourceProducerBeanIT2.properties")
Properties props2;

@Test
void testGlobalFile() {
assertNotNull(defaultProps);
System.out.println(defaultProps);
assertEquals(3, defaultProps.size());
assertEquals("x", defaultProps.getProperty("key1"));
assertEquals("y", defaultProps.getProperty("key2"));
assertEquals("false", defaultProps.getProperty("value.is.found"));
}

@Test
void testGlobalFileOverriddenByLocalLocation() {
assertNotNull(props2);
System.out.println(props2);
assertEquals(3, props2.size());
assertEquals("true", props2.getProperty("value.is.found"));
assertEquals("x", props2.getProperty("key1"));
assertEquals("y", props2.getProperty("key2"));
}


}
Loading