You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/04/27 09:43:26 UTC
[plc4x] branch develop updated: PLC4X-192 Support for connection
string parameter conversion.
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new cf534c7 PLC4X-192 Support for connection string parameter conversion.
new b38b9c1 Merge pull request #140 from ConnectorIO/PLC4X-192
cf534c7 is described below
commit cf534c73594b6733341a2e80fa141889d8a52d04
Author: Ćukasz Dywicki <lu...@code-house.org>
AuthorDate: Fri Apr 10 13:20:11 2020 +0200
PLC4X-192 Support for connection string parameter conversion.
---
.../spi/configuration/ConfigurationFactory.java | 19 +++++++++-
.../ConfigurationParameterConverter.java | 44 ++++++++++++++++++++++
.../annotations/ParameterConverter.java | 42 +++++++++++++++++++++
.../amsads/configuration/AdsConfiguration.java | 26 +++++++++++++
4 files changed, 129 insertions(+), 2 deletions(-)
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/ConfigurationFactory.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/ConfigurationFactory.java
index 832b9db..088cd1f 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/ConfigurationFactory.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/ConfigurationFactory.java
@@ -170,10 +170,25 @@ public class ConfigurationFactory {
* @return parsed value of the field in the type the field requires
*/
private static Object toFieldValue(Field field, String valueString) {
- if(field == null) {
+ if (field == null) {
throw new IllegalArgumentException("Field not defined");
}
- if(field.getType() == String.class) {
+
+ if (field.getAnnotation(ParameterConverter.class) != null) {
+ Class<? extends ConfigurationParameterConverter<?>> converterClass = field.getAnnotation(ParameterConverter.class).value();
+
+ try {
+ ConfigurationParameterConverter<?> converter = converterClass.getDeclaredConstructor().newInstance();
+ if (converter.getType().isAssignableFrom(field.getType())) {
+ return converter.convert(valueString);
+ }
+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
+ throw new IllegalArgumentException("Could not initialize parameter converter", e);
+ }
+ throw new IllegalArgumentException("Unsupported field type " + field.getType() + " for converter " + converterClass);
+ }
+
+ if (field.getType() == String.class) {
return valueString;
}
if ((field.getType() == boolean.class) || (field.getType() == Boolean.class)) {
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/ConfigurationParameterConverter.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/ConfigurationParameterConverter.java
new file mode 100644
index 0000000..5080c4d
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/ConfigurationParameterConverter.java
@@ -0,0 +1,44 @@
+/*
+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.plc4x.java.spi.configuration;
+
+/**
+ * Interface which allows to convert parameter from URI into its complex form.
+ */
+public interface ConfigurationParameterConverter<T> {
+
+ /**
+ * Type of supported configuration parameter.
+ *
+ * Returned value determines Java type to which this converter is able to turn string representation. Only if field
+ * type is assignable to returned type conversion attempt will be made.
+ *
+ * @return Java type constructed by converter.
+ */
+ Class<T> getType();
+
+ /**
+ * Executes conversion of parameter textual representation into java object.
+ *
+ * @param value Parameter value.
+ * @return Object representing passed string value.
+ */
+ T convert(String value);
+
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/annotations/ParameterConverter.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/annotations/ParameterConverter.java
new file mode 100644
index 0000000..733eaeb
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/configuration/annotations/ParameterConverter.java
@@ -0,0 +1,42 @@
+/*
+ * 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.plc4x.java.spi.configuration.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import org.apache.plc4x.java.spi.configuration.ConfigurationParameterConverter;
+
+/**
+ * Helper annotation to customize handling of configuration parameter conversion.
+ */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ParameterConverter {
+
+ /**
+ * Type which should be used for conversion of text value to object representation.
+ *
+ * @return Class implementing necessary conversion logic.
+ */
+ Class<? extends ConfigurationParameterConverter<?>> value();
+
+}
diff --git a/sandbox/test-java-amsads-driver/src/main/java/org/apache/plc4x/java/amsads/configuration/AdsConfiguration.java b/sandbox/test-java-amsads-driver/src/main/java/org/apache/plc4x/java/amsads/configuration/AdsConfiguration.java
index e34ff6b..606bc24 100644
--- a/sandbox/test-java-amsads-driver/src/main/java/org/apache/plc4x/java/amsads/configuration/AdsConfiguration.java
+++ b/sandbox/test-java-amsads-driver/src/main/java/org/apache/plc4x/java/amsads/configuration/AdsConfiguration.java
@@ -21,17 +21,31 @@ package org.apache.plc4x.java.amsads.configuration;
import org.apache.plc4x.java.amsads.AMSADSPlcDriver;
import org.apache.plc4x.java.amsads.readwrite.AmsNetId;
import org.apache.plc4x.java.spi.configuration.Configuration;
+import org.apache.plc4x.java.spi.configuration.ConfigurationParameterConverter;
+import org.apache.plc4x.java.spi.configuration.annotations.ConfigurationParameter;
+import org.apache.plc4x.java.spi.configuration.annotations.ParameterConverter;
+import org.apache.plc4x.java.spi.configuration.annotations.Required;
import org.apache.plc4x.java.transport.serial.SerialTransportConfiguration;
import org.apache.plc4x.java.transport.tcp.TcpTransportConfiguration;
public class AdsConfiguration implements Configuration, TcpTransportConfiguration, SerialTransportConfiguration {
+ @Required
+ @ConfigurationParameter
+ @ParameterConverter(AmsNetIdConverter.class)
protected AmsNetId targetAmsNetId;
+ @Required
+ @ConfigurationParameter
protected int targetAmsPort;
+ @Required
+ @ConfigurationParameter
+ @ParameterConverter(AmsNetIdConverter.class)
protected AmsNetId sourceAmsNetId;
+ @Required
+ @ConfigurationParameter
protected int sourceAmsPort;
public AmsNetId getTargetAmsNetId() {
@@ -71,4 +85,16 @@ public class AdsConfiguration implements Configuration, TcpTransportConfiguratio
return AMSADSPlcDriver.TCP_PORT;
}
+ public static class AmsNetIdConverter implements ConfigurationParameterConverter<AmsNetId> {
+
+ @Override
+ public Class<AmsNetId> getType() {
+ return AmsNetId.class;
+ }
+
+ @Override
+ public AmsNetId convert(String value) {
+ return AMSADSPlcDriver.AmsNetIdOf(value);
+ }
+ }
}