Repository: incubator-tamaya Updated Branches: refs/heads/master 9e7420917 -> 3ca2f3626
TAMAYA-63: Added converters for URL, URI and Class. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/3ca2f362 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/3ca2f362 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/3ca2f362 Branch: refs/heads/master Commit: 3ca2f3626a437c5147628ffdf0712228a2b60fae Parents: 9e74209 Author: anatole <anat...@apache.org> Authored: Thu Feb 12 23:00:48 2015 +0100 Committer: anatole <anat...@apache.org> Committed: Thu Feb 12 23:00:48 2015 +0100 ---------------------------------------------------------------------- .../internal/converters/ClassConverter.java | 62 ++++++++++++++++++++ .../core/internal/converters/URLConverter.java | 46 +++++++++++++++ .../core/internal/converters/URiConverter.java | 47 +++++++++++++++ 3 files changed, 155 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ca2f362/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.java new file mode 100644 index 0000000..6288bd8 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ClassConverter.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.PropertyConverter; + +import java.util.Locale; +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> + */ +public class ClassConverter implements PropertyConverter<Class<?>>{ + + private Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Class<?> convert(String value) { + 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; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ca2f362/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java new file mode 100644 index 0000000..ad78ba8 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java @@ -0,0 +1,46 @@ +/* + * 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.PropertyConverter; + +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). + */ +public class URLConverter implements PropertyConverter<URL>{ + + private Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public URL convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + try{ + return new URL(trimmed); + } + catch(Exception e){ + LOG.log(Level.FINE, e, () -> "Unparseable URL: " + trimmed); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ca2f362/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URiConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URiConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URiConverter.java new file mode 100644 index 0000000..a788975 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/URiConverter.java @@ -0,0 +1,47 @@ +/* + * 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.PropertyConverter; + +import java.net.URI; +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 URI(value). + */ +public class URiConverter implements PropertyConverter<URI>{ + + private Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public URI convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + try{ + return new URI(trimmed); + } + catch(Exception e){ + LOG.log(Level.FINE, e, () -> "Unparseable URI: " + trimmed); + } + return null; + } +}