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

Fix #960 Do not expose mutable collections from FHIR BuildItems #754

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.camel.quarkus.component.fhir.deployment;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import ca.uhn.fhir.context.FhirContext;
import io.quarkus.builder.item.SimpleBuildItem;

public abstract class AbstractPropertiesBuildItem extends SimpleBuildItem {

private final Map<String, String> properties;

protected AbstractPropertiesBuildItem(String path) {
this.properties = Collections.unmodifiableMap(loadProperties(path));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
static Map<String, String> loadProperties(String path) {
try (InputStream str = FhirContext.class.getClassLoader().getResourceAsStream(path)) {
Properties prop = new Properties();
prop.load(str);
return new HashMap<String, String>((Map) prop);
} catch (Exception e) {
throw new RuntimeException("Please ensure FHIR is on the classpath", e);
}
}

public Map<String, String> getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,29 @@
*/
package org.apache.camel.quarkus.component.fhir.deployment;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Map;
import java.util.Set;

import ca.uhn.fhir.context.FhirContext;

public final class FhirUtil {

private FhirUtil() {
}
private static final String RESOURCE_PREFIX = "resource.";

public static Properties loadProperties(String path) {
try (InputStream str = FhirContext.class.getResourceAsStream(path)) {
Properties prop = new Properties();
prop.load(str);
return prop;
} catch (Exception e) {
throw new RuntimeException("Please ensure FHIR is on the classpath", e);
}
private FhirUtil() {
}

public static Collection<String> getModelClasses(Properties properties) {
public static Collection<String> getModelClasses(Map<String, String> properties) {
return getInnerClasses(properties.values().toArray(new String[0]));
}

public static Collection<String> getResourceDefinitions(Properties properties) {
public static Collection<String> getResourceDefinitions(Map<String, String> properties) {
List<String> resources = new ArrayList<>();
for (String stringPropertyName : properties.stringPropertyNames()) {
if (stringPropertyName.contains("resource.")) {
resources.add(stringPropertyName.replace("resource.", ""));
for (String stringPropertyName : properties.keySet()) {
if (stringPropertyName.contains(RESOURCE_PREFIX)) {
resources.add(stringPropertyName.substring(RESOURCE_PREFIX.length()));
}
}
return resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@
*/
package org.apache.camel.quarkus.component.fhir.deployment.dstu2;

import java.util.Properties;
import org.apache.camel.quarkus.component.fhir.deployment.AbstractPropertiesBuildItem;

import io.quarkus.builder.item.SimpleBuildItem;
public final class Dstu2PropertiesBuildItem extends AbstractPropertiesBuildItem {

public final class Dstu2PropertiesBuildItem extends SimpleBuildItem {

private final Properties properties;

public Dstu2PropertiesBuildItem(Properties properties) {
this.properties = properties;
public Dstu2PropertiesBuildItem(String path) {
super(path);
}

public Properties getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.quarkus.component.fhir.deployment.dstu2;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import ca.uhn.fhir.model.dstu2.resource.*;
Expand Down Expand Up @@ -198,15 +197,15 @@

import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getModelClasses;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getResourceDefinitions;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.loadProperties;

public class FhirDstu2Processor {

private static final String FHIR_VERSION_PROPERTIES = "ca/uhn/fhir/model/dstu2/fhirversion.properties";

@BuildStep(onlyIf = FhirFlags.Dstu2Enabled.class)
Dstu2PropertiesBuildItem properties(BuildProducer<NativeImageResourceBuildItem> resource) {
Properties properties = loadProperties("/ca/uhn/fhir/model/dstu2/fhirversion.properties");
resource.produce(new NativeImageResourceBuildItem("ca/uhn/fhir/model/dstu2/fhirversion.properties"));
return new Dstu2PropertiesBuildItem(properties);
resource.produce(new NativeImageResourceBuildItem(FHIR_VERSION_PROPERTIES));
return new Dstu2PropertiesBuildItem(FHIR_VERSION_PROPERTIES);
}

@BuildStep(onlyIf = FhirFlags.Dstu2Enabled.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@
*/
package org.apache.camel.quarkus.component.fhir.deployment.dstu3;

import java.util.Properties;
import org.apache.camel.quarkus.component.fhir.deployment.AbstractPropertiesBuildItem;

import io.quarkus.builder.item.SimpleBuildItem;
public final class Dstu3PropertiesBuildItem extends AbstractPropertiesBuildItem {

public final class Dstu3PropertiesBuildItem extends SimpleBuildItem {

private final Properties properties;

public Dstu3PropertiesBuildItem(Properties properties) {
this.properties = properties;
public Dstu3PropertiesBuildItem(String path) {
super(path);
}

public Properties getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.quarkus.component.fhir.deployment.dstu3;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import io.quarkus.arc.deployment.BeanContainerBuildItem;
Expand All @@ -39,15 +38,14 @@
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getInnerClasses;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getModelClasses;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getResourceDefinitions;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.loadProperties;

public class FhirDstu3Processor {
private static final String FHIR_VERSION_PROPERTIES = "org/hl7/fhir/dstu3/model/fhirversion.properties";

@BuildStep(onlyIf = FhirFlags.Dstu3Enabled.class)
Dstu3PropertiesBuildItem properties(BuildProducer<NativeImageResourceBuildItem> resource) {
Properties properties = loadProperties("/org/hl7/fhir/dstu3/model/fhirversion.properties");
resource.produce(new NativeImageResourceBuildItem("org/hl7/fhir/dstu3/model/fhirversion.properties"));
return new Dstu3PropertiesBuildItem(properties);
resource.produce(new NativeImageResourceBuildItem(FHIR_VERSION_PROPERTIES));
return new Dstu3PropertiesBuildItem(FHIR_VERSION_PROPERTIES);
}

@BuildStep(onlyIf = FhirFlags.Dstu3Enabled.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.quarkus.component.fhir.deployment.r4;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import io.quarkus.arc.deployment.BeanContainerBuildItem;
Expand All @@ -33,15 +32,14 @@
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getInnerClasses;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getModelClasses;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getResourceDefinitions;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.loadProperties;

public class FhirR4Processor {
private static final String FHIR_VERSION_PROPERTIES = "org/hl7/fhir/r4/model/fhirversion.properties";

@BuildStep(onlyIf = FhirFlags.R4Enabled.class)
R4PropertiesBuildItem properties(BuildProducer<NativeImageResourceBuildItem> resource) {
resource.produce(new NativeImageResourceBuildItem("org/hl7/fhir/r4/model/fhirversion.properties"));
Properties properties = loadProperties("/org/hl7/fhir/r4/model/fhirversion.properties");
return new R4PropertiesBuildItem(properties);
resource.produce(new NativeImageResourceBuildItem(FHIR_VERSION_PROPERTIES));
return new R4PropertiesBuildItem(FHIR_VERSION_PROPERTIES);
}

@BuildStep(onlyIf = FhirFlags.R4Enabled.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@
*/
package org.apache.camel.quarkus.component.fhir.deployment.r4;

import java.util.Properties;
import org.apache.camel.quarkus.component.fhir.deployment.AbstractPropertiesBuildItem;

import io.quarkus.builder.item.SimpleBuildItem;
public final class R4PropertiesBuildItem extends AbstractPropertiesBuildItem {

public final class R4PropertiesBuildItem extends SimpleBuildItem {

private final Properties properties;

public R4PropertiesBuildItem(Properties properties) {
this.properties = properties;
public R4PropertiesBuildItem(String path) {
super(path);
}

public Properties getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.quarkus.component.fhir.deployment.r5;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import io.quarkus.arc.deployment.BeanContainerBuildItem;
Expand All @@ -33,15 +32,14 @@
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getInnerClasses;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getModelClasses;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.getResourceDefinitions;
import static org.apache.camel.quarkus.component.fhir.deployment.FhirUtil.loadProperties;

public class FhirR5Processor {
private static final String FHIR_VERSION_PROPERTIES = "org/hl7/fhir/r5/model/fhirversion.properties";

@BuildStep(onlyIf = FhirFlags.R5Enabled.class)
R5PropertiesBuildItem properties(BuildProducer<NativeImageResourceBuildItem> resource) {
resource.produce(new NativeImageResourceBuildItem("org/hl7/fhir/r5/model/fhirversion.properties"));
Properties properties = loadProperties("/org/hl7/fhir/r5/model/fhirversion.properties");
return new R5PropertiesBuildItem(properties);
resource.produce(new NativeImageResourceBuildItem(FHIR_VERSION_PROPERTIES));
return new R5PropertiesBuildItem(FHIR_VERSION_PROPERTIES);
}

@BuildStep(onlyIf = FhirFlags.R5Enabled.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@
*/
package org.apache.camel.quarkus.component.fhir.deployment.r5;

import java.util.Properties;
import org.apache.camel.quarkus.component.fhir.deployment.AbstractPropertiesBuildItem;

import io.quarkus.builder.item.SimpleBuildItem;
public final class R5PropertiesBuildItem extends AbstractPropertiesBuildItem {

public final class R5PropertiesBuildItem extends SimpleBuildItem {

private final Properties properties;

public R5PropertiesBuildItem(Properties properties) {
this.properties = properties;
public R5PropertiesBuildItem(String path) {
super(path);
}

public Properties getProperties() {
return properties;
}
}