http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.java new file mode 100644 index 0000000..61f9f4e --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.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.internal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spi.TypeLiteral; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.core.internal.converters.*; +import org.apache.tamaya.spisupport.DefaultConfigurationBuilder; + +import java.io.File; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.URI; +import java.net.URL; +import java.nio.file.Path; +import java.util.*; + +/** + * Default implementation of {@link ConfigurationBuilder}. + */ +public final class CoreConfigurationBuilder extends DefaultConfigurationBuilder { + + /** + * Creates a new builder instance. + */ + public CoreConfigurationBuilder() { + super(); + } + + /** + * Creates a new builder instance. + */ + public CoreConfigurationBuilder(Configuration config) { + super(config); + } + + /** + * Creates a new builder instance initializing it with the given context. + * @param context the context to be used, not null. + */ + public CoreConfigurationBuilder(ConfigurationContext context) { + super(context); + } + + @SuppressWarnings("unchecked") + protected void addCorePropertyConverters() { + addPropertyConverters(TypeLiteral.<BigDecimal>of(BigDecimal.class), new BigDecimalConverter()); + addPropertyConverters(TypeLiteral.<BigInteger>of(BigInteger.class), new BigIntegerConverter()); + addPropertyConverters(TypeLiteral.<Boolean>of(Boolean.class), new BooleanConverter()); + addPropertyConverters(TypeLiteral.<Byte>of(Byte.class), new ByteConverter()); + addPropertyConverters(TypeLiteral.<Character>of(Character.class), new CharConverter()); + addPropertyConverters(TypeLiteral.<Class<?>>of(Class.class), new ClassConverter()); + addPropertyConverters(TypeLiteral.<Currency>of(Currency.class), new CurrencyConverter()); + addPropertyConverters(TypeLiteral.<Double>of(Double.class), new DoubleConverter()); + addPropertyConverters(TypeLiteral.<File>of(File.class), new FileConverter()); + addPropertyConverters(TypeLiteral.<Float>of(Float.class), new FloatConverter()); + addPropertyConverters(TypeLiteral.<Integer>of(Integer.class), new IntegerConverter()); + addPropertyConverters(TypeLiteral.<Long>of(Long.class), new LongConverter()); + addPropertyConverters(TypeLiteral.<Number>of(Number.class), new NumberConverter()); + addPropertyConverters(TypeLiteral.<Path>of(Path.class), new PathConverter()); + addPropertyConverters(TypeLiteral.<Short>of(Short.class), new ShortConverter()); + addPropertyConverters(TypeLiteral.<URI>of(URI.class), new URIConverter()); + addPropertyConverters(TypeLiteral.<URL>of(URL.class), new URLConverter()); + } + + @Override + public Configuration build() { + return new CoreConfiguration(contextBuilder.build()); + } + +}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java new file mode 100644 index 0000000..caf7fbb --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java @@ -0,0 +1,101 @@ +/* + * 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.internal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.ConfigurationProviderSpi; +import org.apache.tamaya.spisupport.DefaultConfiguration; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; +import org.osgi.service.component.annotations.Component; + +import java.util.Objects; + +/** + * Implementation of the Configuration API. This class uses the current {@link org.apache.tamaya.spi.ConfigurationContext} to evaluate the + * chain of {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} + * instance to evaluate the current Configuration. + */ +@Component(service = ConfigurationProviderSpi.class) +public class CoreConfigurationProvider implements ConfigurationProviderSpi { + + private Configuration config = new CoreConfigurationBuilder() + .addDefaultPropertyConverters() + .addDefaultPropertyFilters() + .addDefaultPropertySources() + .build(); + { + String bannerConfig = config.getOrDefault("tamaya.banner", "OFF"); + + BannerManager bm = new BannerManager(bannerConfig); + bm.outputBanner(); + } + + @Override + public Configuration getConfiguration() { + return config; + } + + @Override + public Configuration createConfiguration(ConfigurationContext context) { + return new CoreConfiguration(context); + } + + @Override + public ConfigurationBuilder getConfigurationBuilder() { + return new CoreConfigurationBuilder(); + } + + @Override + public ConfigurationContextBuilder getConfigurationContextBuilder() { + return new DefaultConfigurationContextBuilder(); + } + + @Override + public void setConfiguration(Configuration config) { + Objects.requireNonNull(config.getContext()); + this.config = Objects.requireNonNull(config); + } + + @Override + public boolean isConfigurationSettable() { + return true; + } + + /** + * @deprecated use {@link Configuration#getContext()} instead. + */ + @Deprecated + @Override + public ConfigurationContext getConfigurationContext() { + return this.config.getContext(); + } + + /** + * @deprecated the context should be given upon creation of the {@link Configuration} + */ + @Deprecated + @Override + public void setConfigurationContext(ConfigurationContext context){ + this.config = new CoreConfigurationBuilder(context).build(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceComparator.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceComparator.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceComparator.java new file mode 100644 index 0000000..c9815cf --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceComparator.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.internal; + +import org.osgi.framework.ServiceReference; + +import javax.annotation.Priority; +import java.util.Comparator; + +/** + * Comparator implementation for ordering services loaded based on their increasing priority values. + */ +@SuppressWarnings("rawtypes") +class OSGIServiceComparator implements Comparator<ServiceReference> { + + @Override + public int compare(ServiceReference o1, ServiceReference o2) { + int prio = getPriority(o1) - getPriority(o2); + if (prio < 0) { + return 1; + } else if (prio > 0) { + return -1; + } else { + return 0; //o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()); + } + } + + /** + * Checks the given instance for a @Priority annotation. If present the annotation's value is evaluated. If no such + * annotation is present, a default priority {@code 1} is returned. + * + * @param o the instance, not {@code null}. + * @return a priority, by default 1. + */ + public static int getPriority(Object o) { + return getPriority(o.getClass()); + } + + /** + * Checks the given type optionally annotated with a @Priority. If present the annotation's value is evaluated. + * If no such annotation is present, a default priority {@code 1} is returned. + * + * @param type the type, not {@code null}. + * @return a priority, by default 1. + */ + public static int getPriority(Class<? extends Object> type) { + int prio = 1; + Priority priority = type.getAnnotation(Priority.class); + if (priority != null) { + prio = priority.value(); + } + return prio; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java new file mode 100644 index 0000000..b7328d9 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java @@ -0,0 +1,172 @@ +/* + * 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.internal; + +import org.apache.tamaya.spi.ConfigurationProviderSpi; +import org.apache.tamaya.spi.ServiceContext; +import org.osgi.framework.Bundle; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; + +import java.io.IOException; +import java.net.URL; +import java.util.*; +import java.util.logging.Logger; + +/** + * ServiceContext implementation based on OSGI Service mechanisms. + */ +public class OSGIServiceContext implements ServiceContext{ + + private static final Logger LOG = Logger.getLogger(OSGIServiceContext.class.getName()); + private static final OSGIServiceComparator REF_COMPARATOR = new OSGIServiceComparator(); + + private final OSGIServiceLoader osgiServiceLoader; + + public OSGIServiceContext(OSGIServiceLoader osgiServiceLoader){ + this.osgiServiceLoader = Objects.requireNonNull(osgiServiceLoader); + } + + public boolean isInitialized(){ + return osgiServiceLoader != null; + } + + + @Override + public int ordinal() { + return 10; + } + + @Override + public <T> T getService(Class<T> serviceType) { + LOG.finest("TAMAYA Loading service: " + serviceType.getName()); + ServiceReference<T> ref = this.osgiServiceLoader.getBundleContext().getServiceReference(serviceType); + if(ref!=null){ + return this.osgiServiceLoader.getBundleContext().getService(ref); + } + if(ConfigurationProviderSpi.class==serviceType){ + @SuppressWarnings("unchecked") + T service = (T)new CoreConfigurationProvider(); + this.osgiServiceLoader.getBundleContext().registerService( + serviceType.getName(), + service, + new Hashtable<String, Object>()); + return service; + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public <T> T create(Class<T> serviceType) { + LOG.finest("TAMAYA Creating service: " + serviceType.getName()); + ServiceReference<T> ref = this.osgiServiceLoader.getBundleContext().getServiceReference(serviceType); + if(ref!=null){ + try { + return (T)this.osgiServiceLoader.getBundleContext().getService(ref).getClass().newInstance(); + } catch (Exception e) { + return null; + } + } + return null; + } + + @Override + public <T> List<T> getServices(Class<T> serviceType) { + LOG.finest("TAMAYA Loading services: " + serviceType.getName()); + List<ServiceReference<T>> refs = new ArrayList<>(); + List<T> services = new ArrayList<>(refs.size()); + try { + refs.addAll(this.osgiServiceLoader.getBundleContext().getServiceReferences(serviceType, null)); + Collections.sort(refs, REF_COMPARATOR); + for(ServiceReference<T> ref:refs){ + T service = osgiServiceLoader.getBundleContext().getService(ref); + if(service!=null) { + services.add(service); + } + } + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + } + try{ + for(T service:ServiceLoader.load(serviceType)){ + services.add(service); + } + return services; + } catch (Exception e) { + e.printStackTrace(); + } + return services; + } + + @Override + public Enumeration<URL> getResources(String resource, ClassLoader cl) throws IOException{ + LOG.finest("TAMAYA Loading resources: " + resource); + List<URL> result = new ArrayList<>(); + URL url = osgiServiceLoader.getBundleContext().getBundle() + .getEntry(resource); + if(url != null) { + LOG.finest("TAMAYA Resource: " + resource + " found in unregistered bundle " + + osgiServiceLoader.getBundleContext().getBundle().getSymbolicName()); + result.add(url); + } + for(Bundle bundle: osgiServiceLoader.getResourceBundles()) { + url = bundle.getEntry(resource); + if (url != null && !result.contains(url)) { + LOG.finest("TAMAYA Resource: " + resource + " found in registered bundle " + bundle.getSymbolicName()); + result.add(url); + } + } + for(Bundle bundle: osgiServiceLoader.getBundleContext().getBundles()) { + url = bundle.getEntry(resource); + if (url != null && !result.contains(url)) { + LOG.finest("TAMAYA Resource: " + resource + " found in unregistered bundle " + bundle.getSymbolicName()); + result.add(url); + } + } + return Collections.enumeration(result); + } + + @Override + public URL getResource(String resource, ClassLoader cl){ + LOG.finest("TAMAYA Loading resource: " + resource); + URL url = osgiServiceLoader.getBundleContext().getBundle() + .getEntry(resource); + if(url!=null){ + LOG.finest("TAMAYA Resource: " + resource + " found in bundle " + + osgiServiceLoader.getBundleContext().getBundle().getSymbolicName()); + return url; + } + for(Bundle bundle: osgiServiceLoader.getResourceBundles()) { + url = bundle.getEntry(resource); + if(url != null){ + LOG.finest("TAMAYA Resource: " + resource + " found in registered bundle " + bundle.getSymbolicName()); + return url; + } + } + for(Bundle bundle: osgiServiceLoader.getBundleContext().getBundles()) { + url = bundle.getEntry(resource); + if(url != null){ + LOG.finest("TAMAYA Resource: " + resource + " found in unregistered bundle " + bundle.getSymbolicName()); + return url; + } + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java new file mode 100644 index 0000000..1b77ebb --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceLoader.java @@ -0,0 +1,254 @@ +/* + * 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.internal; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.tamaya.base.PriorityServiceComparator; +import org.osgi.framework.*; + +/** + * A bundle listener that registers services defined in META-INF/services, when + * a bundle is starting. + * + * @author [email protected] + */ +@SuppressWarnings("rawtypes") +public class OSGIServiceLoader implements BundleListener { + // Provide logging + private static final Logger log = Logger.getLogger(OSGIServiceLoader.class.getName()); + private static final String META_INF_SERVICES = "META-INF/services/"; + + private BundleContext context; + + private Set<Bundle> resourceBundles = Collections.synchronizedSet(new HashSet<Bundle>()); + + public OSGIServiceLoader(BundleContext context) { + this.context = Objects.requireNonNull(context); + // Check for matching bundles already installed... + for (Bundle bundle : context.getBundles()) { + switch (bundle.getState()) { + case Bundle.ACTIVE: + checkAndLoadBundle(bundle); + } + } + } + + public BundleContext getBundleContext() { + return context; + } + + public Set<Bundle> getResourceBundles() { + synchronized (resourceBundles) { + return new HashSet<>(resourceBundles); + } + } + + @Override + public void bundleChanged(BundleEvent bundleEvent) { + // Parse and create metadata when installed + if (bundleEvent.getType() == BundleEvent.STARTED) { + Bundle bundle = bundleEvent.getBundle(); + checkAndLoadBundle(bundle); + } else if (bundleEvent.getType() == BundleEvent.STOPPED) { + Bundle bundle = bundleEvent.getBundle(); + checkAndUnloadBundle(bundle); + } + } + + private void checkAndUnloadBundle(Bundle bundle) { + if (bundle.getEntry(META_INF_SERVICES) == null) { + return; + } + synchronized (resourceBundles) { + resourceBundles.remove(bundle); + log.fine("Unregistered ServiceLoader bundle: " + bundle.getSymbolicName()); + } + Enumeration<String> entryPaths = bundle.getEntryPaths(META_INF_SERVICES); + while (entryPaths.hasMoreElements()) { + String entryPath = entryPaths.nextElement(); + if (!entryPath.endsWith("/")) { + removeEntryPath(bundle, entryPath); + } + } + } + + private void checkAndLoadBundle(Bundle bundle) { + if (bundle.getEntry(META_INF_SERVICES) == null) { + return; + } + synchronized (resourceBundles) { + resourceBundles.add(bundle); + log.info("Registered ServiceLoader bundle: " + bundle.getSymbolicName()); + } + Enumeration<String> entryPaths = bundle.getEntryPaths(META_INF_SERVICES); + while (entryPaths.hasMoreElements()) { + String entryPath = entryPaths.nextElement(); + if (!entryPath.endsWith("/")) { + processEntryPath(bundle, entryPath); + } + } + } + + private void processEntryPath(Bundle bundle, String entryPath) { + try { + String serviceName = entryPath.substring(META_INF_SERVICES.length()); + if (!serviceName.startsWith("org.apache.tamaya")) { + // Ignore non Tamaya entries... + return; + } + Class<?> serviceClass = bundle.loadClass(serviceName); + URL child = bundle.getEntry(entryPath); + InputStream inStream = child.openStream(); + log.info("Loading Services " + serviceClass.getName() + " from bundle...: " + bundle.getSymbolicName()); + try (BufferedReader br = new BufferedReader(new InputStreamReader(inStream, "UTF-8"))) { + String implClassName = br.readLine(); + while (implClassName != null) { + int hashIndex = implClassName.indexOf("#"); + if (hashIndex > 0) { + implClassName = implClassName.substring(0, hashIndex - 1); + } else if (hashIndex == 0) { + implClassName = ""; + } + implClassName = implClassName.trim(); + if (implClassName.length() > 0) { + try { + // Load the service class + log.fine("Loading Class " + implClassName + " from bundle...: " + bundle.getSymbolicName()); + Class<?> implClass = bundle.loadClass(implClassName); + if (!serviceClass.isAssignableFrom(implClass)) { + log.warning("Configured service: " + implClassName + " is not assignable to " + + serviceClass.getName()); + continue; + } + log.info("Loaded Service Factory (" + serviceName + "): " + implClassName); + // Provide service properties + Hashtable<String, String> props = new Hashtable<>(); + props.put(Constants.VERSION_ATTRIBUTE, bundle.getVersion().toString()); + String vendor = bundle.getHeaders().get(Constants.BUNDLE_VENDOR); + props.put(Constants.SERVICE_VENDOR, (vendor != null ? vendor : "anonymous")); + // Translate annotated @Priority into a service ranking + props.put(Constants.SERVICE_RANKING, + String.valueOf(PriorityServiceComparator.getPriority(implClass))); + + // Register the service factory on behalf of the intercepted bundle + JDKUtilServiceFactory factory = new JDKUtilServiceFactory(implClass); + BundleContext bundleContext = bundle.getBundleContext(); + bundleContext.registerService(serviceName, factory, props); + log.info("Registered Tamaya service class: " + implClassName + "(" + serviceName + ")"); + } catch (Exception e) { + log.log(Level.SEVERE, "Failed to load service: " + implClassName, e); + } catch (NoClassDefFoundError err) { + log.log(Level.SEVERE, "Failed to load service: " + implClassName, err); + } + } + implClassName = br.readLine(); + } + } + } catch (RuntimeException rte) { + throw rte; + } catch (Exception e) { + log.log(Level.SEVERE, "Failed to read services from: " + entryPath, e); + } + } + + private void removeEntryPath(Bundle bundle, String entryPath) { + try { + String serviceName = entryPath.substring(META_INF_SERVICES.length()); + if (!serviceName.startsWith("org.apache.tamaya")) { + // Ignore non Tamaya entries... + return; + } + Class<?> serviceClass = bundle.loadClass(serviceName); + + URL child = bundle.getEntry(entryPath); + InputStream inStream = child.openStream(); + + BufferedReader br = new BufferedReader(new InputStreamReader(inStream, "UTF-8")); + String implClassName = br.readLine(); + while (implClassName != null) { + int hashIndex = implClassName.indexOf("#"); + if (hashIndex > 0) { + implClassName = implClassName.substring(0, hashIndex - 1); + } else if (hashIndex == 0) { + implClassName = ""; + } + implClassName = implClassName.trim(); + if (implClassName.length() > 0) { + log.fine("Unloading Service (" + serviceName + "): " + implClassName); + try { + // Load the service class + Class<?> implClass = bundle.loadClass(implClassName); + if (!serviceClass.isAssignableFrom(implClass)) { + log.warning("Configured service: " + implClassName + " is not assignable to " + + serviceClass.getName()); + continue; + } + ServiceReference<?> ref = bundle.getBundleContext().getServiceReference(implClass); + if (ref != null) { + bundle.getBundleContext().ungetService(ref); + } + } catch (Exception e) { + log.log(Level.SEVERE, "Failed to unload service: " + implClassName, e); + } catch (NoClassDefFoundError err) { + log.log(Level.SEVERE, "Failed to unload service: " + implClassName, err); + } + } + implClassName = br.readLine(); + } + br.close(); + } catch (RuntimeException rte) { + throw rte; + } catch (Exception e) { + log.log(Level.SEVERE, "Failed to read services from: " + entryPath, e); + } + } + + /** + * Service factory simply instantiating the configured service. + */ + static class JDKUtilServiceFactory implements ServiceFactory { + private final Class<?> serviceClass; + + public JDKUtilServiceFactory(Class<?> serviceClass) { + this.serviceClass = serviceClass; + } + + @Override + public Object getService(Bundle bundle, ServiceRegistration registration) { + try { + log.fine("Creating Service...:" + serviceClass.getName()); + return serviceClass.newInstance(); + } catch (Exception ex) { + ex.printStackTrace(); + throw new IllegalStateException("Failed to create service: " + serviceClass.getName(), ex); + } + } + + @Override + public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java new file mode 100644 index 0000000..7e71b7e --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java @@ -0,0 +1,76 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to BigDecimal, the supported format is one of the following: + * <ul> + * <li>232573527352.76352753</li> + * <li>-23257352.735276352753</li> + * <li>-0xFFFFFF (integral numbers only)</li> + * <li>-0XFFFFAC (integral numbers only)</li> + * <li>0xFFFFFF (integral numbers only)</li> + * <li>0XFFFFAC (integral numbers only)</li> + * </ul> + */ +@Component(service = PropertyConverter.class) +public class BigDecimalConverter implements PropertyConverter<BigDecimal>{ + + /** The logger. */ + private static final Logger LOG = Logger.getLogger(BigDecimalConverter.class.getName()); + /** Converter to be used if the format is not directly supported by BigDecimal, e.g. for integral hex values. */ + private final BigIntegerConverter integerConverter = new BigIntegerConverter(); + + @Override + public BigDecimal convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "<bigDecimal> -> new BigDecimal(String)"); + + String trimmed = Objects.requireNonNull(value).trim(); + try{ + return new BigDecimal(trimmed); + } catch(Exception e){ + LOG.finest("Parsing BigDecimal failed, trying BigInteger for: " + value); + BigInteger bigInt = integerConverter.convert(value, context); + if(bigInt!=null){ + return new BigDecimal(bigInt); + } + LOG.finest("Failed to parse BigDecimal from: " + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java new file mode 100644 index 0000000..edca14a --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java @@ -0,0 +1,106 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.math.BigInteger; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to BigInteger, the supported format is one of the following: + * <ul> + * <li>0xFFFFFF</li> + * <li>0XFFFFAC</li> + * <li>23257352735276352753</li> + * <li>-0xFFFFFF</li> + * <li>-0XFFFFAC</li> + * <li>-23257352735276352753</li> + * </ul> + */ +@Component(service = PropertyConverter.class) +public class BigIntegerConverter implements PropertyConverter<BigInteger>{ + + /** The logger. */ + private static final Logger LOG = Logger.getLogger(BigIntegerConverter.class.getName()); + /** Converter used to decode hex, octal values. */ + private final ByteConverter byteConverter = new ByteConverter(); + + @Override + public BigInteger convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "[-]0X.. (hex)", "[-]0x... (hex)", "<bigint> -> new BigInteger(bigint)"); + String trimmed = Objects.requireNonNull(value).trim(); + if(trimmed.startsWith("0x") || trimmed.startsWith("0X")){ + LOG.finest("Parsing Hex value to BigInteger: " + value); + trimmed = trimmed.substring(2); + StringBuilder decimal = new StringBuilder(); + for(int offset = 0;offset < trimmed.length();offset+=2){ + if(offset==trimmed.length()-1){ + LOG.finest("Invalid Hex-Byte-String: " + value); + return null; + } + byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2), context); + if(val<10){ + decimal.append('0').append(val); + } else{ + decimal.append(val); + } + } + return new BigInteger(decimal.toString()); + } else if(trimmed.startsWith("-0x") || trimmed.startsWith("-0X")){ + LOG.finest("Parsing Hex value to BigInteger: " + value); + trimmed = trimmed.substring(3); + StringBuilder decimal = new StringBuilder(); + for(int offset = 0;offset < trimmed.length();offset+=2){ + if(offset==trimmed.length()-1){ + LOG.finest("Invalid Hex-Byte-String: " + trimmed); + return null; + } + byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2), context); + if(val<10){ + decimal.append('0').append(val); + } else{ + decimal.append(val); + } + } + return new BigInteger('-' + decimal.toString()); + } + try{ + return new BigInteger(trimmed); + } catch(Exception e){ + LOG.log(Level.FINEST, "Failed to parse BigInteger from: " + value, 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java new file mode 100644 index 0000000..7e61140 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java @@ -0,0 +1,73 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class BooleanConverter implements PropertyConverter<Boolean> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Boolean convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "yes (ignore case)", "y (ignore case)", "true (ignore case)", "t (ignore case)", "1", "no (ignore case)", "n (ignore case)", "false (ignore case)", "f (ignore case)", "0"); + String ignoreCaseValue = Objects.requireNonNull(value) + .trim() + .toLowerCase(Locale.ENGLISH); + switch(ignoreCaseValue) { + case "1": + case "yes": + case "y": + case "true": + case "t": + case "on": + return Boolean.TRUE; + case "no": + case "n": + case "false": + case "f": + case "0": + case "off": + return Boolean.FALSE; + default: + LOG.finest("Unknown boolean value encountered: " + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java new file mode 100644 index 0000000..9b213cf --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java @@ -0,0 +1,83 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Byte, 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 = PropertyConverter.class) +public class ByteConverter implements PropertyConverter<Byte>{ + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Byte convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(),"<byte>", "MIN_VALUE", "MIN", "MAX_VALUE", "MAX"); + String trimmed = Objects.requireNonNull(value).trim(); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Byte.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Byte.MAX_VALUE; + default: + try{ + return Byte.decode(trimmed); + } + catch(Exception e){ + LOG.log(Level.FINEST, "Unparseable Byte: " + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java new file mode 100644 index 0000000..3895969 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java @@ -0,0 +1,80 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Character, the supported format is one of the following: + * <ul> + * <li>'a'</li> + * <li>123 (byte value)</li> + * <li>0xFF (byte value)</li> + * <li>0XDF (byte value)</li> + * <li>0D1 (byte value)</li> + * </ul> + */ +@Component(service = PropertyConverter.class) +public class CharConverter implements PropertyConverter<Character>{ + + private static final Logger LOG = Logger.getLogger(CharConverter.class.getName()); + + @Override + public Character convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(),"\\'<char>\\'", "<char>", "<charNum>"); + String trimmed = Objects.requireNonNull(value).trim(); + if(trimmed.isEmpty()){ + return null; + } + if(trimmed.startsWith("'")) { + try { + trimmed = trimmed.substring(1, trimmed.length() - 1); + if (trimmed.isEmpty()) { + return null; + } + return trimmed.charAt(0); + } catch (Exception e) { + LOG.finest("Invalid character format encountered: '" + value + "', valid formats are 'a', 101 and a."); + return null; + } + } + try { + Integer val = Integer.parseInt(trimmed); + return (char) val.shortValue(); + } catch (Exception e) { + LOG.finest("Character format is not numeric: '" + value + "', using first character."); + return trimmed.charAt(0); + } + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.java new file mode 100644 index 0000000..b29bc15 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Class, hereby using the following classloaders: + * <ul> + * <li>The current ThreadContext ClassLoader</li> + * <li>The Classloader of this class</li> + * <li>The system Classloader</li> + * </ul> + */ +@Component(service = PropertyConverter.class) +public class ClassConverter implements PropertyConverter<Class<?>>{ + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Class<?> convert(String value, ConversionContext context) { + if(value==null){ + return null; + } + context.addSupportedFormats(getClass(),"<fullyQualifiedClassName>"); + String trimmed = Objects.requireNonNull(value).trim(); + try{ + return Class.forName(trimmed, false, Thread.currentThread().getContextClassLoader()); + } + catch(Exception e){ + LOG.finest("Class not found in context CL: " + trimmed); + } + try{ + return Class.forName(trimmed, false, ClassConverter.class.getClassLoader()); + } + catch(Exception e){ + LOG.finest("Class not found in ClassConverter's CL: " + trimmed); + } + try{ + return Class.forName(trimmed, false, ClassLoader.getSystemClassLoader()); + } + catch(Exception e){ + LOG.finest("Class not found in System CL (giving up): " + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java new file mode 100644 index 0000000..acdc0c1 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java @@ -0,0 +1,68 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.ConfigQuery; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spi.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.List; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Query to convert a String value. + * @param <T> the target type. + */ +final class ConvertQuery<T> implements ConfigQuery<T> { + + private static final Logger LOG = Logger.getLogger(ConvertQuery.class.getName()); + + private String rawValue; + private TypeLiteral<T> type; + + public ConvertQuery(String rawValue, TypeLiteral<T> type) { + this.rawValue = Objects.requireNonNull(rawValue); + this.type = Objects.requireNonNull(type); + } + + @Override + public T query(Configuration config) { + List<PropertyConverter<T>> converters = config.getContext().getPropertyConverters(type); + ConversionContext context = new ConversionContext.Builder(type).setConfigurationContext(config.getContext()) + .setConfiguration(config).setKey(ConvertQuery.class.getName()).build(); + for(PropertyConverter<?> conv: converters) { + try{ + if(conv instanceof OptionalConverter){ + continue; + } + T result = (T)conv.convert(rawValue, context); + if(result!=null){ + return result; + } + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Converter "+ conv +" failed to convert to " + type); + } + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java new file mode 100644 index 0000000..b769d06 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java @@ -0,0 +1,102 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.util.Currency; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Currency, the supported format is one of the following: + * <ul> + * <li>CHF (currency code)</li> + * <li>123 (numeric currency value > + * = 0)</li> + * <li>DE (ISO 2-digit country)</li> + * <li>de_DE, de_DE_123 (Locale)</li> + * </ul> + */ +@Component(service = PropertyConverter.class) +public class CurrencyConverter implements PropertyConverter<Currency> { + + private static final Logger LOG = Logger.getLogger(CurrencyConverter.class.getName()); + + @Override + public Currency convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "<currencyCode>, using Locale.ENGLISH", "<numericValue>", "<locale>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH)); + } catch (Exception e) { + LOG.log(Level.FINEST, "Not a valid textual currency code: " + trimmed + ", checking for numeric...", e); + } + try { + // Check for numeric code + Integer numCode = Integer.parseInt(trimmed); + for (Currency currency : Currency.getAvailableCurrencies()) { + if (currency.getNumericCode() == numCode) { + return currency; + } + } + } catch (Exception e) { + LOG.log(Level.FINEST, "Not a valid numeric currency code: " + trimmed + ", checking for locale...", e); + } + try { + // Check for numeric code + String[] parts = trimmed.split("\\_"); + Locale locale; + switch (parts.length) { + case 1: + locale = new Locale("", parts[0]); + break; + case 2: + locale = new Locale(parts[0], parts[1]); + break; + case 3: + locale = new Locale(parts[0], parts[1], parts[2]); + break; + default: + locale = null; + } + if (locale != null) { + return Currency.getInstance(locale); + } + LOG.finest("Not a valid currency: " + trimmed + ", giving up..."); + } catch (Exception e) { + LOG.log(Level.FINEST, "Not a valid country locale for currency: " + trimmed + ", giving up...", 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java new file mode 100644 index 0000000..e527756 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java @@ -0,0 +1,93 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +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 = PropertyConverter.class) +public class DoubleConverter implements PropertyConverter<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) { + 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, context); + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DurationConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DurationConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DurationConverter.java new file mode 100644 index 0000000..f258186 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/DurationConverter.java @@ -0,0 +1,59 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +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 = PropertyConverter.class) +public class DurationConverter implements PropertyConverter<Duration> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Duration convert(String value, ConversionContext context) { + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java new file mode 100644 index 0000000..e9891be --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java @@ -0,0 +1,62 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +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 = PropertyConverter.class) +public class FileConverter implements PropertyConverter<File> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public File convert(String value, ConversionContext context) { + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java new file mode 100644 index 0000000..84daa10 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java @@ -0,0 +1,93 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +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 = PropertyConverter.class) +public class FloatConverter implements PropertyConverter<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) { + 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, context); + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/InstantConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/InstantConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/InstantConverter.java new file mode 100644 index 0000000..4198b72 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/InstantConverter.java @@ -0,0 +1,57 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.time.Instant; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class InstantConverter implements PropertyConverter<Instant> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Instant convert(String value, ConversionContext context) { + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java new file mode 100644 index 0000000..d09df9b --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java @@ -0,0 +1,86 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +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 = PropertyConverter.class) +public class IntegerConverter implements PropertyConverter<Integer>{ + + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger(IntegerConverter.class.getName()); + + @Override + public Integer convert(String value, ConversionContext context) { + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java new file mode 100644 index 0000000..3bf9b67 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java @@ -0,0 +1,57 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.time.LocalDate; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class LocalDateConverter implements PropertyConverter<LocalDate> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public LocalDate convert(String value, ConversionContext context) { + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java new file mode 100644 index 0000000..eb14000 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java @@ -0,0 +1,57 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.time.LocalDateTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class LocalDateTimeConverter implements PropertyConverter<LocalDateTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public LocalDateTime convert(String value, ConversionContext context) { + 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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java new file mode 100644 index 0000000..bf7d8ab --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java @@ -0,0 +1,57 @@ +/* + * 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.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.time.LocalTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class LocalTimeConverter implements PropertyConverter<LocalTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public LocalTime convert(String value, ConversionContext context) { + 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(); + } +}
