http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/DoubleConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/DoubleConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/DoubleConverter.java new file mode 100644 index 0000000..eb5b717 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/DoubleConverter.java @@ -0,0 +1,94 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Double, using the Java number syntax: + * (-)?[0-9]*\.[0-9]*. In case of error the value given also is tried being parsed as integral number using + * {@link LongConverter}. Additionally the following values are supported: + * <ul> + * <li>NaN (ignoring case)</li> + * <li>POSITIVE_INFINITY (ignoring case)</li> + * <li>NEGATIVE_INFINITY (ignoring case)</li> + * </ul> + */ +@Component(service = Converter.class) +public class DoubleConverter implements Converter<Double> { + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger(DoubleConverter.class.getName()); + /** + * The converter used, when floating point parse failed. + */ + private final LongConverter integerConverter = new LongConverter(); + + @Override + public Double convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), "<double>", "MIN", "MIN_VALUE", "MAX", "MAX_VALUE", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN"); + String trimmed = Objects.requireNonNull(value).trim(); + switch (trimmed.toUpperCase(Locale.ENGLISH)) { + case "POSITIVE_INFINITY": + return Double.POSITIVE_INFINITY; + case "NEGATIVE_INFINITY": + return Double.NEGATIVE_INFINITY; + case "NAN": + return Double.NaN; + case "MIN_VALUE": + case "MIN": + return Double.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Double.MAX_VALUE; + default: + try { + return Double.valueOf(trimmed); + } catch (Exception e) { + // OK perhaps we have an integral number that must be converted to the double type... + LOG.finest("Parsing of double as floating number failed, trying parsing integral" + + " number/hex instead..."); + } + Long val = integerConverter.convert(trimmed); + if(val!=null){ + return val.doubleValue(); + } + return null; + } + + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/DurationConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/DurationConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/DurationConverter.java new file mode 100644 index 0000000..eb05097 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/DurationConverter.java @@ -0,0 +1,60 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class DurationConverter implements Converter<Duration> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Duration convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), + Duration.of(1234, ChronoUnit.SECONDS).toString()); + try { + return Duration.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse Duration: " + value); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/FileConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/FileConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/FileConverter.java new file mode 100644 index 0000000..38badd4 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/FileConverter.java @@ -0,0 +1,63 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.io.File; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URL(value). + */ +@Component(service = Converter.class) +public class FileConverter implements Converter<File> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public File convert(String value) { + ConversionContext context = ConversionContext.getContext(); + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(),"<File>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return new File(trimmed); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable File Name: " + trimmed, e); + } + return null; + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/FloatConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/FloatConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/FloatConverter.java new file mode 100644 index 0000000..b66a8e1 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/FloatConverter.java @@ -0,0 +1,94 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Float, using the Java number syntax: + * (-)?[0-9]*\.[0-9]*. In case of error the value given also is tried being parsed as integral number using + * {@link LongConverter}. Additionally the following values are supported: + * <ul> + * <li>NaN (ignoring case)</li> + * <li>POSITIVE_INFINITY (ignoring case)</li> + * <li>NEGATIVE_INFINITY (ignoring case)</li> + * </ul> + */ +@Component(service = Converter.class) +public class FloatConverter implements Converter<Float> { + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger(FloatConverter.class.getName()); + /** + * The converter used, when floating point parse failed. + */ + private final IntegerConverter integerConverter = new IntegerConverter(); + + @Override + public Float convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), "<float>", "MIN", "MIN_VALUE", "MAX", "MAX_VALUE", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN"); + String trimmed = Objects.requireNonNull(value).trim(); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "POSITIVE_INFINITY": + return Float.POSITIVE_INFINITY; + case "NEGATIVE_INFINITY": + return Float.NEGATIVE_INFINITY; + case "NAN": + return Float.NaN; + case "MIN_VALUE": + case "MIN": + return Float.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Float.MAX_VALUE; + default: + try { + return Float.valueOf(trimmed); + } catch(Exception e){ + // OK perhaps we have an integral number that must be converted to the double type... + LOG.finest("Parsing of float as floating number failed, trying parsing integral" + + " number/hex instead..."); + } + Integer val = integerConverter.convert(trimmed); + if(val!=null) { + return val.floatValue(); + } + LOG.finest("Unparseable float value: " + trimmed); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/InstantConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/InstantConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/InstantConverter.java new file mode 100644 index 0000000..b6d485f --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/InstantConverter.java @@ -0,0 +1,58 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.time.Instant; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class InstantConverter implements Converter<Instant> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Instant convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), Instant.now().toString()); + try{ + return Instant.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse Instant: " + value); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/IntegerConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/IntegerConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/IntegerConverter.java new file mode 100644 index 0000000..5a62a23 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/IntegerConverter.java @@ -0,0 +1,87 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Integer, the supported format is one of the following: + * <ul> + * <li>123 (byte value)</li> + * <li>0xFF (byte value)</li> + * <li>0XDF (byte value)</li> + * <li>0D1 (byte value)</li> + * <li>-123 (byte value)</li> + * <li>-0xFF (byte value)</li> + * <li>-0XDF (byte value)</li> + * <li>-0D1 (byte value)</li> + * <li>MIN_VALUE (ignoring case)</li> + * <li>MIN (ignoring case)</li> + * <li>MAX_VALUE (ignoring case)</li> + * <li>MAX (ignoring case)</li> + * </ul> + */ +@Component(service = Converter.class) +public class IntegerConverter implements Converter<Integer> { + + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger(IntegerConverter.class.getName()); + + @Override + public Integer convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), "<int>", "MIN_VALUE", "MIN", "MAX_VALUE", "MAX"); + String trimmed = Objects.requireNonNull(value).trim(); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Integer.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Integer.MAX_VALUE; + default: + try{ + return Integer.decode(trimmed); + } + catch(Exception e){ + LOG.finest("Unparseable Integer value: " + trimmed); + return null; + } + } + + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateConverter.java new file mode 100644 index 0000000..edfaa3a --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateConverter.java @@ -0,0 +1,58 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.time.LocalDate; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class LocalDateConverter implements Converter<LocalDate> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public LocalDate convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), LocalDate.now().toString()); + try{ + return LocalDate.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse LocalDate: " + value); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateTimeConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateTimeConverter.java new file mode 100644 index 0000000..9450e30 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/LocalDateTimeConverter.java @@ -0,0 +1,58 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.time.LocalDateTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class LocalDateTimeConverter implements Converter<LocalDateTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public LocalDateTime convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), LocalDateTime.now().toString()); + try{ + return LocalDateTime.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse LocalDateTime: " + value); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/LocalTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/LocalTimeConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/LocalTimeConverter.java new file mode 100644 index 0000000..dde2214 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/LocalTimeConverter.java @@ -0,0 +1,58 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.time.LocalTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class LocalTimeConverter implements Converter<LocalTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public LocalTime convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), LocalTime.now().toString()); + try{ + return LocalTime.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse LocalTime: " + value); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/LongConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/LongConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/LongConverter.java new file mode 100644 index 0000000..8861c09 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/LongConverter.java @@ -0,0 +1,84 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Long, the supported format is one of the following: + * <ul> + * <li>123 (byte value)</li> + * <li>0xFF (byte value)</li> + * <li>0XDF (byte value)</li> + * <li>0D1 (byte value)</li> + * <li>-123 (byte value)</li> + * <li>-0xFF (byte value)</li> + * <li>-0XDF (byte value)</li> + * <li>-0D1 (byte value)</li> + * <li>MIN_VALUE (ignoring case)</li> + * <li>MIN (ignoring case)</li> + * <li>MAX_VALUE (ignoring case)</li> + * <li>MAX (ignoring case)</li> + * </ul> + */ +@Component(service = Converter.class) +public class LongConverter implements Converter<Long> { + + private static final Logger LOGGER = Logger.getLogger(LongConverter.class.getName()); + + @Override + public Long convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), "<long>", "MIN", "MIN_VALUE", "MAX", "MAX_VALUE"); + + String trimmed = Objects.requireNonNull(value).trim(); + switch (trimmed.toUpperCase(Locale.ENGLISH)) { + case "MIN_VALUE": + case "MIN": + return Long.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Long.MAX_VALUE; + default: + try { + return Long.decode(trimmed); + } + catch(Exception e){ + LOGGER.finest("Unable to parse Long value: " + value); + return null; + } + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/NumberConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/NumberConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/NumberConverter.java new file mode 100644 index 0000000..2f2893d --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/NumberConverter.java @@ -0,0 +1,85 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.math.BigDecimal; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Number. Valid inputs are: + * <pre> + * POSITIVE_INFINITY -> Double.POSITIVE_INFINITY + * NEGATIVE_INFINITY -> Double.NEGATIVE_INFINITY + * NaN > Double.NaN + * 0xFFDCD3D2 -> Long + * 1234566789.23642327352735273752 -> new BigDecimal(input) + * </pre> + */ +@Component(service = Converter.class) +public class NumberConverter implements Converter<Number> { + /** the logger. */ + private static final Logger LOGGER = Logger.getLogger(NumberConverter.class.getName()); + /** Converter used for trying to parse as an integral value. */ + private final LongConverter longConverter = new LongConverter(); + + @Override + public Number convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), "<double>, <long>", "0x (hex)", "0X... (hex)", "POSITIVE_INFINITY", + "NEGATIVE_INFINITY", "NAN"); + + String trimmed = Objects.requireNonNull(value).trim(); + switch(trimmed.toUpperCase(Locale.ENGLISH)) { + case "POSITIVE_INFINITY": + return Double.POSITIVE_INFINITY; + case "NEGATIVE_INFINITY": + return Double.NEGATIVE_INFINITY; + case "NAN": + return Double.NaN; + default: + Long lVal = longConverter.convert(trimmed); + if (lVal != null) { + return lVal; + } + try{ + return new BigDecimal(trimmed); + } + catch(Exception e){ + LOGGER.finest("Unparseable Number: " + trimmed); + return null; + } + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetDateTimeConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetDateTimeConverter.java new file mode 100644 index 0000000..8b78814 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetDateTimeConverter.java @@ -0,0 +1,58 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.time.OffsetDateTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class OffsetDateTimeConverter implements Converter<OffsetDateTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public OffsetDateTime convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), OffsetDateTime.now().toString()); + try{ + return OffsetDateTime.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse OffsetDateTime: " + value); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetTimeConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetTimeConverter.java new file mode 100644 index 0000000..8186ac0 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/OffsetTimeConverter.java @@ -0,0 +1,58 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.time.OffsetTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class OffsetTimeConverter implements Converter<OffsetTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public OffsetTime convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), OffsetTime.now().toString()); + try{ + return OffsetTime.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse OffsetTime: " + value); + return null; + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/OptionalConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/OptionalConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/OptionalConverter.java new file mode 100644 index 0000000..ef307cc --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/OptionalConverter.java @@ -0,0 +1,78 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Optional; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class OptionalConverter implements Converter<Optional> { + + @Override + public Optional convert(String value) { + ConversionContext context = ConversionContext.getContext(); + if(value==null){ + return Optional.empty(); + } + try{ + if(context==null){ + throw new IllegalStateException("Failed to evaluate target type, context == null."); + } + Type targetType = context.getTargetType(); + if(Optional.class.equals(targetType)){ + ParameterizedType pt = (ParameterizedType) targetType; + if(String.class.equals(pt.getActualTypeArguments()[0])){ + return Optional.of(value); + } + else{ + targetType = pt.getActualTypeArguments()[0]; + } + } + if(context.getConfiguration()==null){ + throw new IllegalStateException("Parametrized converters require a configuration for accessing the converters of their" + + "child elements."); + } + ConvertQuery converter = new ConvertQuery(value, targetType); + return Optional.ofNullable(converter.apply(context.getConfiguration())); + }catch (IllegalStateException ise){ + throw ise; + }catch(Exception e){ + throw new IllegalArgumentException("Error evaluating config value.", e); + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/PathConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/PathConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/PathConverter.java new file mode 100644 index 0000000..6bc9fc3 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/PathConverter.java @@ -0,0 +1,64 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URL(value). + */ +@Component(service = Converter.class) +public class PathConverter implements Converter<Path> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Path convert(String value) { + ConversionContext context = ConversionContext.getContext(); + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(),"<File>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return FileSystems.getDefault().getPath(value); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable Path: " + trimmed, e); + } + return null; + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/ShortConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/ShortConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/ShortConverter.java new file mode 100644 index 0000000..19c5f77 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/ShortConverter.java @@ -0,0 +1,85 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Short, the supported format is one of the following: + * <ul> + * <li>123 (byte value)</li> + * <li>0xFF (byte value)</li> + * <li>0XDF (byte value)</li> + * <li>0D1 (byte value)</li> + * <li>-123 (byte value)</li> + * <li>-0xFF (byte value)</li> + * <li>-0XDF (byte value)</li> + * <li>-0D1 (byte value)</li> + * <li>MIN_VALUE (ignoring case)</li> + * <li>MIN (ignoring case)</li> + * <li>MAX_VALUE (ignoring case)</li> + * <li>MAX (ignoring case)</li> + * </ul> + */ +@Component(service = Converter.class) +public class ShortConverter implements Converter<Short> { + + /** the logger. */ + private static final Logger LOG = Logger.getLogger(ShortConverter.class.getName()); + + @Override + public Short convert(String value) { + ConversionContext context = ConversionContext.getContext(); + context.addSupportedFormats(getClass(), "short", "MIN", "MIN_VALUE", "MAX", "MAX_VALUE"); + String trimmed = Objects.requireNonNull(value).trim(); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Short.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Short.MAX_VALUE; + default: + try{ + return Short.decode(trimmed); + } + catch(Exception e){ + LOG.finest("Unparseable Short: " + trimmed); + return null; + } + } + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/SupplierConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/SupplierConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/SupplierConverter.java new file mode 100644 index 0000000..e39f93a --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/SupplierConverter.java @@ -0,0 +1,70 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.function.Supplier; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = Converter.class) +public class SupplierConverter implements Converter<Supplier> { + + private static final Logger LOG = Logger.getLogger(SupplierConverter.class.getName()); + + @Override + public Supplier convert(String value) { + ConversionContext context = ConversionContext.getContext(); + return () -> { + try{ + Type targetType = context.getTargetType(); + ParameterizedType pt = (ParameterizedType) targetType; + if(String.class.equals(pt.getActualTypeArguments()[0])){ + return value; + } + ConvertQuery converter = new ConvertQuery(value, pt.getActualTypeArguments()[0]); + Object o = converter.apply(context.getConfiguration()); + if(o==null){ + throw new IllegalArgumentException("No such value: " + context.getKey()); + } + return o; + }catch(Exception e){ + throw new IllegalArgumentException("Error evaluating config value.", e); + } + }; + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/URIConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/URIConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/URIConverter.java new file mode 100644 index 0000000..d63103a --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/URIConverter.java @@ -0,0 +1,63 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.net.URI; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URI(value). + */ +@Component(service = Converter.class) +public class URIConverter implements Converter<URI> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public URI convert(String value) { + ConversionContext context = ConversionContext.getContext(); + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(), "<uri> -> new URI(uri)"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return new URI(trimmed); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable URI: " + trimmed, e); + } + return null; + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/URLConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/URLConverter.java b/code/core/src/main/java/org/apache/tamaya/core/converters/URLConverter.java new file mode 100644 index 0000000..27c69b1 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/URLConverter.java @@ -0,0 +1,63 @@ +/* + * 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.tamaya.core.converters; + +import org.apache.tamaya.base.convert.ConversionContext; +import org.osgi.service.component.annotations.Component; + +import javax.config.spi.Converter; +import java.net.URL; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URL(value). + */ +@Component(service = Converter.class) +public class URLConverter implements Converter<URL> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public URL convert(String value) { + ConversionContext context = ConversionContext.getContext(); + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(),"<URL>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return new URL(trimmed); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable URL: " + trimmed, e); + } + return null; + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/java/org/apache/tamaya/core/converters/package-info.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/converters/package-info.java b/code/core/src/main/java/org/apache/tamaya/core/converters/package-info.java new file mode 100644 index 0000000..2eb5858 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/converters/package-info.java @@ -0,0 +1,23 @@ +/* + * 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. + */ + +/** + * Contains implementations of the converters provided by default. + */ +package org.apache.tamaya.core.converters; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigProviderResolver ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigProviderResolver b/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigProviderResolver new file mode 100644 index 0000000..7177d12 --- /dev/null +++ b/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigProviderResolver @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.core.TamayaConfigProviderResolver http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigSource ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigSource b/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigSource new file mode 100644 index 0000000..ffa770a --- /dev/null +++ b/code/core/src/main/resources/META-INF/services/javax.config.spi.ConfigSource @@ -0,0 +1,22 @@ +# +# 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 current 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. +# +org.apache.tamaya.base.configsource.CLIConfigSource +org.apache.tamaya.base.configsource.EnvironmentConfigSource +org.apache.tamaya.base.configsource.JavaConfigurationConfigSource +org.apache.tamaya.base.configsource.SystemConfigSource \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/resources/META-INF/services/javax.config.spi.Converter ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/javax.config.spi.Converter b/code/core/src/main/resources/META-INF/services/javax.config.spi.Converter new file mode 100644 index 0000000..f7b9134 --- /dev/null +++ b/code/core/src/main/resources/META-INF/services/javax.config.spi.Converter @@ -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 current 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. +# +org.apache.tamaya.core.converters.BooleanConverter +org.apache.tamaya.core.converters.ByteConverter +org.apache.tamaya.core.converters.CharConverter +org.apache.tamaya.core.converters.ClassConverter +org.apache.tamaya.core.converters.DoubleConverter +org.apache.tamaya.core.converters.FloatConverter +org.apache.tamaya.core.converters.IntegerConverter +org.apache.tamaya.core.converters.LongConverter +org.apache.tamaya.core.converters.ShortConverter +org.apache.tamaya.core.converters.BigDecimalConverter +org.apache.tamaya.core.converters.BigIntegerConverter +org.apache.tamaya.core.converters.CurrencyConverter +org.apache.tamaya.core.converters.NumberConverter +org.apache.tamaya.core.converters.URIConverter +org.apache.tamaya.core.converters.URLConverter +org.apache.tamaya.core.converters.FileConverter +org.apache.tamaya.core.converters.PathConverter +org.apache.tamaya.core.converters.DurationConverter +org.apache.tamaya.core.converters.LocalDateConverter +org.apache.tamaya.core.converters.LocalDateTimeConverter +org.apache.tamaya.core.converters.LocalTimeConverter +org.apache.tamaya.core.converters.OffsetDateTimeConverter +org.apache.tamaya.core.converters.OffsetTimeConverter +org.apache.tamaya.core.converters.InstantConverter +org.apache.tamaya.core.converters.OptionalConverter +org.apache.tamaya.core.converters.SupplierConverter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigValueEvaluator ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigValueEvaluator b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigValueEvaluator new file mode 100644 index 0000000..26d41ab --- /dev/null +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigValueEvaluator @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.base.DefaultConfigValueEvaluator \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/main/resources/tamaya-banner.txt ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/tamaya-banner.txt b/code/core/src/main/resources/tamaya-banner.txt new file mode 100644 index 0000000..b4ceca4 --- /dev/null +++ b/code/core/src/main/resources/tamaya-banner.txt @@ -0,0 +1,11 @@ + + ââââââ âââââââ ââââââ ââââââââââ âââââââââââ âââââââââ ââââââ ââââ ââââ ââââââ âââ âââ ââââââ +âââââââââââââââââââââââââââââââââââ âââââââââââ ââââââââââââââââââââââ âââââââââââââââââ ââââââââââââ +âââââââââââââââââââââââââââ ââââââââââââââ âââ âââââââââââââââââââââââââââ âââââââ ââââââââ +âââââââââââââââ âââââââââââ ââââââââââââââ âââ âââââââââââââââââââââââââââ âââââ ââââââââ +âââ ââââââ âââ ââââââââââââââ âââââââââââ âââ âââ ââââââ âââ ââââââ âââ âââ âââ âââ +âââ ââââââ âââ âââ ââââââââââ âââââââââââ âââ âââ ââââââ ââââââ âââ âââ âââ âââ + +Apache Tamaya Configuration API: http://tamaya.incubator.apache.org + + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/test/java/org/apache/tamaya/core/BannerManagerTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/BannerManagerTest.java b/code/core/src/test/java/org/apache/tamaya/core/BannerManagerTest.java new file mode 100644 index 0000000..bb750b1 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/BannerManagerTest.java @@ -0,0 +1,89 @@ +/* + * 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.tamaya.core; + +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.PrintStream; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.Permission; + +/* + * Note: + * The tests of this class will fail PIT, our coverage tool. + * Therefore we excluded this class in the parent POM + * from the test execution. + * Oliver B. Fischer, 2017-09-16 + */ +public class BannerManagerTest { + + @Test + public void valueConsoleSendsBannerToSystemOut() { + + SecurityManager sm = new SecurityManager(); + AccessControlContext con = AccessController.getContext(); + + Permission p = new RuntimePermission("setIO"); + + /* + * Here we check the precondition for this unit test + * and the correct setup of the test environment + * The JVM must have been started with + * -Djava.security.policy=<path_to_core_module</src/test/resources/java-security.policy + */ + sm.checkPermission(p, con); + + PrintStream standard = System.out; + PrintStream printStream = Mockito.mock(PrintStream.class); + + System.setOut(printStream); + standard.println("Changed stream for STDOUT successfully"); + + try { + BannerManager bm = new BannerManager("console"); + bm.outputBanner(); + + } finally { + System.setOut(standard); + } + Mockito.verify(printStream, Mockito.atLeastOnce()).println(Mockito.anyString()); + } + + @Test + public void invalidValueAvoidsLoggingToConsonle() { + + PrintStream standard = System.out; + PrintStream printStream = Mockito.mock(PrintStream.class); + + System.setOut(printStream); + + try { + BannerManager bm = new BannerManager("snafu"); + bm.outputBanner(); + + } finally { + System.setOut(standard); + } + + Mockito.verify(printStream, Mockito.never()).println(Mockito.anyString()); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d0e14ed7/code/core/src/test/java/org/apache/tamaya/core/ConfigBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/ConfigBuilderTest.java b/code/core/src/test/java/org/apache/tamaya/core/ConfigBuilderTest.java new file mode 100644 index 0000000..8767621 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/ConfigBuilderTest.java @@ -0,0 +1,94 @@ +///* +// * 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.tamaya.core; +// +//import org.apache.tamaya.TypeLiteral; +//import org.apache.tamaya.spi.ConfigContext; +//import org.apache.tamaya.spisupport.DefaultConfigBuilder; +//import org.junit.Test; +// +//import javax.config.Config; +//import javax.config.spi.ConfigBuilder; +//import javax.config.spi.ConfigProviderResolver; +//import javax.config.spi.ConfigSource; +//import javax.config.spi.Converter; +// +//import static org.junit.Assert.*; +// +///** +// * Tests for {@link ConfigBuilder} by atsticks on 06.09.16. +// */ +//public class ConfigBuilderTest { +// +// private TestConfigSource testConfigSource = new TestConfigSource(){}; +// +// +// @Test +// public void addConfigSources_Array() throws Exception { +// ConfigSource testPS2 = new TestConfigSource("addConfigSources_Array", 1); +// ConfigBuilder b = new DefaultConfigBuilder() +// .withSources(testConfigSource, testPS2); +// Config cfg = b.build(); +// assertEquals(2, ConfigContext.of(cfg).getSources().size()); +// assertTrue(ConfigContext.of(cfg).getSources().contains(testConfigSource)); +// assertTrue(ConfigContext.of(cfg).getSources().contains(testPS2)); +// // Ensure no sorting happens during add, so switch ordinals! +// testPS2 = new TestConfigSource("addConfigSources_Array", 1); +// b = ConfigProviderResolver.instance().getBuilder() +// .withSources(testPS2, testConfigSource); +// cfg = b.build(); +// assertEquals(2, ConfigContext.of(cfg).getSources().size()); +// assertTrue(ConfigContext.of(cfg).getSources().contains(testConfigSource)); +// assertTrue(ConfigContext.of(cfg).getSources().contains(testPS2)); +// assertEquals(ConfigContext.of(cfg).getSources().get(1).getName(), "TestConfigSource"); +// assertEquals(ConfigContext.of(cfg).getSources().get(0).getName(), "addConfigSources_Array"); +// } +// +// @Test +// @SuppressWarnings({ "rawtypes", "unchecked" }) +// public void addPropertyConverters_Array() throws Exception { +// Converter<String> converter = new Converter<String>(){ +// @Override +// public String convert(String value) { +// return value; +// } +// }; //(value) -> value.toLowerCase(); +// ConfigBuilder b = ConfigProviderResolver.instance().getBuilder() +// .withConverters(converter); +// Config cfg = b.build(); +// ConfigContext ctx = ConfigContext.of(cfg); +// assertTrue(ctx.getConverters(TypeLiteral.of(String.class)).contains(converter)); +// assertEquals(1, ctx.getConverters().size()); +// b = ConfigProviderResolver.instance().getBuilder() +// .withConverters(converter); +// b.withConverters(converter); +// assertEquals(1, ctx.getConverters().size()); +// } +// +// @Test +// public void build() throws Exception { +// ConfigBuilder b = ConfigProviderResolver.instance().getBuilder(); +// Config cfg = b.build(); +// ConfigContext ctx = ConfigContext.of(cfg); +// assertNotNull(ctx); +// assertTrue(ctx.getSources().isEmpty()); +// assertTrue(ctx.getPropertyFilters().isEmpty()); +// } +// +//} \ No newline at end of file
