http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java b/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java new file mode 100644 index 0000000..165c642 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java @@ -0,0 +1,41 @@ +/* + * 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.testdata; + +import org.apache.tamaya.base.filter.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertyValue; + +import javax.annotation.Priority; + +/** + * Simple PropertyFilter that filters exact one value, registered using ServiceLoader. + */ +@Priority(100) +public class TestPropertyFilter implements PropertyFilter{ + @Override + public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { + if("name4".equals(context.getProperty().getKey())){ + return valueToBeFiltered.toBuilder() + .setValue(valueToBeFiltered.getValue() + "(filtered)") + .build(); + } + return valueToBeFiltered; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java b/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java new file mode 100644 index 0000000..5a427e0 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertySourceProvider.java @@ -0,0 +1,79 @@ +/* + * 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.testdata; + +import org.apache.tamaya.spisupport.propertysource.BasePropertySource; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; +import org.apache.tamaya.spi.PropertyValue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Test provider reading properties from classpath:cfg/final/**.properties. + */ +public class TestPropertySourceProvider implements PropertySourceProvider { + + private List<PropertySource> list = new ArrayList<>(); + + public TestPropertySourceProvider(){ + list.add(new MyPropertySource()); + list = Collections.unmodifiableList(list); + } + + @Override + public Collection<PropertySource> getPropertySources() { + return list; + } + + private static class MyPropertySource extends BasePropertySource { + + private Map<String, PropertyValue> properties = new HashMap<>(); + + public MyPropertySource() { + super(200); + properties.put("name", PropertyValue.of("name", "Robin", "test")); + properties.put("name3", PropertyValue.of("name3", "Lukas", "test")); + properties.put("name4", PropertyValue.of("name4", "Sereina", "test")); + properties.put("name5", PropertyValue.of("name5", "Benjamin", "test")); + properties = Collections.unmodifiableMap(properties); + } + + @Override + public String getName() { + return "final-testdata-properties"; + } + + @Override + public Map<String, PropertyValue> getProperties() { + return properties; + } + + @Override + public boolean isScannable() { + return true; + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java b/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java new file mode 100644 index 0000000..4abd917 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java @@ -0,0 +1,45 @@ +/* + * 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.testdata; + +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.base.filter.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertyValue; + +import javax.annotation.Priority; + +/** + * Simple PropertyFilter that filters exact one value, registered using ServiceLoader. + */ +@Priority(200) +public class TestRemovingPropertyFilter implements PropertyFilter{ + @Override + public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { + if("name5".equals(context.getProperty().getKey())){ + return null; + } + else if("name3".equals(context.getProperty().getKey())){ + return valueToBeFiltered.toBuilder().setValue( + "Mapped to name: " + ConfigurationProvider.getConfiguration().get("name")) + .build(); + } + return valueToBeFiltered; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/META-INF/javaconfiguration.properties ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/META-INF/javaconfiguration.properties b/code/old/core/src/test/resources/META-INF/javaconfiguration.properties new file mode 100644 index 0000000..33beabb --- /dev/null +++ b/code/old/core/src/test/resources/META-INF/javaconfiguration.properties @@ -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 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. + +confkey1=javaconf-value1 +confkey2=javaconf-value2 +confkey3=javaconf-value3 +confkey4=javaconf-value4 +confkey5=javaconf-value5 http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/META-INF/javaconfiguration.xml ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/META-INF/javaconfiguration.xml b/code/old/core/src/test/resources/META-INF/javaconfiguration.xml new file mode 100644 index 0000000..f6cdc97 --- /dev/null +++ b/code/old/core/src/test/resources/META-INF/javaconfiguration.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> + +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> +<properties> + <entry key="aaeehh">ä</entry> + <entry key="ö">o</entry> +</properties> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi new file mode 100644 index 0000000..968be8f --- /dev/null +++ b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -0,0 +1,18 @@ +# 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. + +org.apache.tamaya.core.internal.CoreConfigurationProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter new file mode 100644 index 0000000..d039696 --- /dev/null +++ b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -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.internal.CTestConverter \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter new file mode 100644 index 0000000..18e61cb --- /dev/null +++ b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter @@ -0,0 +1,20 @@ +# +# 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.testdata.TestPropertyFilter +org.apache.tamaya.core.testdata.TestRemovingPropertyFilter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource new file mode 100644 index 0000000..14e0c24 --- /dev/null +++ b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -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.core.testdata.TestPropertyDefaultSource +org.apache.tamaya.spisupport.propertysource.SystemPropertySource +org.apache.tamaya.spisupport.propertysource.EnvironmentPropertySource +org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider new file mode 100644 index 0000000..c9f255a --- /dev/null +++ b/code/old/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider @@ -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.testdata.TestPropertySourceProvider http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/invalid-properties.xml ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/invalid-properties.xml b/code/old/core/src/test/resources/invalid-properties.xml new file mode 100644 index 0000000..d8b10b7 --- /dev/null +++ b/code/old/core/src/test/resources/invalid-properties.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> + +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> +<properties> + <entry key="a"> + <entry key="b">1</entry> +</properties> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/java-security.policy ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/java-security.policy b/code/old/core/src/test/resources/java-security.policy new file mode 100644 index 0000000..95b6ab3 --- /dev/null +++ b/code/old/core/src/test/resources/java-security.policy @@ -0,0 +1,5 @@ + +grant { + permission java.lang.RuntimePermission "setIO"; +}; + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/non-xml-properties.xml ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/non-xml-properties.xml b/code/old/core/src/test/resources/non-xml-properties.xml new file mode 100644 index 0000000..8de819a --- /dev/null +++ b/code/old/core/src/test/resources/non-xml-properties.xml @@ -0,0 +1,18 @@ +<!-- +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. +--> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/overrideOrdinal.properties ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/overrideOrdinal.properties b/code/old/core/src/test/resources/overrideOrdinal.properties new file mode 100644 index 0000000..96935a8 --- /dev/null +++ b/code/old/core/src/test/resources/overrideOrdinal.properties @@ -0,0 +1,25 @@ +# 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. + +#override ordinal +tamaya.ordinal=16784 + +mykey1=myval1 +mykey2=myval2 +mykey3=myval3 +mykey4=myval4 +mykey5=myval5 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/testfile.properties ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/testfile.properties b/code/old/core/src/test/resources/testfile.properties new file mode 100644 index 0000000..abd7ee8 --- /dev/null +++ b/code/old/core/src/test/resources/testfile.properties @@ -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 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. + +key1=val1 +key2=val2 +key3=val3 +key4=val4 +key5=val5 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/valid-properties.xml ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/valid-properties.xml b/code/old/core/src/test/resources/valid-properties.xml new file mode 100644 index 0000000..7eb51d9 --- /dev/null +++ b/code/old/core/src/test/resources/valid-properties.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> + +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> +<properties> + <entry key="a">b</entry> + <entry key="b">1</entry> +</properties> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/resources/x34.properties ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/resources/x34.properties b/code/old/core/src/test/resources/x34.properties new file mode 100644 index 0000000..f2c4a0a --- /dev/null +++ b/code/old/core/src/test/resources/x34.properties @@ -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 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. + +x34=x34 +x34.a.b.c=C http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/bnd.bnd ---------------------------------------------------------------------- diff --git a/code/old/spi-support/bnd.bnd b/code/old/spi-support/bnd.bnd new file mode 100644 index 0000000..876ca2e --- /dev/null +++ b/code/old/spi-support/bnd.bnd @@ -0,0 +1,26 @@ +-buildpath: \ + osgi.annotation; version=6.0.0,\ + osgi.core; version=6.0,\ + osgi.cmpn; version=6.0 + +-testpath: \ + ${junit} + +javac.source: 1.8 +javac.target: 1.8 + +Bundle-Version: ${version}.${tstamp} +Bundle-Name: Apache Tamaya - SPI Support +Bundle-SymbolicName: org.apache.tamaya.spisupport +Bundle-Description: Apacha Tamaya Config - SPI Support +Bundle-Category: Implementation +Bundle-Copyright: (C) Apache Foundation +Bundle-License: Apache Licence version 2 +Bundle-Vendor: Apache Software Foundation +Bundle-ContactAddress: [email protected] +Bundle-DocURL: http://tamaya.apache.org +Export-Package: \ + org.apache.tamaya.spisupport,org.apache.tamaya.spisupport.propertysource +Import-Package: \ + org.apache.tamaya,\ + org.apache.tamaya.spi http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/pom.xml ---------------------------------------------------------------------- diff --git a/code/old/spi-support/pom.xml b/code/old/spi-support/pom.xml new file mode 100644 index 0000000..4135834 --- /dev/null +++ b/code/old/spi-support/pom.xml @@ -0,0 +1,81 @@ +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-code</artifactId> + <version>0.4-incubating-SNAPSHOT</version> + </parent> + + <artifactId>tamaya-spisupport</artifactId> + <name>Apache Tamaya Core SPI Support</name> + <description>Apache Tamaya Support Classes useful when implementing the Tamaya SPI or code independent of the core RI + implementation.</description> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <!-- + ! See https://issues.apache.org/jira/browse/TAMAYA-318 + !--> + <groupId>org.pitest</groupId> + <artifactId>pitest-maven</artifactId> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java new file mode 100644 index 0000000..92fd614 --- /dev/null +++ b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigValueEvaluator.java @@ -0,0 +1,48 @@ +/* + * 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.spisupport; + +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.PropertyValue; + +import java.util.Map; + + +/** + * Component SPI which encapsulates the evaluation of a single or full <b>raw</b>value + * for a {@link ConfigurationContext}. + */ +public interface ConfigValueEvaluator { + + /** + * Evaluates single value using a {@link ConfigurationContext}. + * @param key the config key, not null. + * @param context the context, not null. + * @return the value, or null. + */ + PropertyValue evaluteRawValue(String key, ConfigurationContext context); + + /** + * Evaluates all property values from a {@link ConfigurationContext}. + * @param context the context, not null. + * @return the value, or null. + */ + Map<String, PropertyValue> evaluateRawValues(ConfigurationContext context); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java new file mode 100644 index 0000000..b764ed6 --- /dev/null +++ b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/ConfigurationBuilder.java @@ -0,0 +1,334 @@ +/* + * 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.spisupport; + + + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.*; + +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + +/** + * A builder for creating new or adapting instances of {@link Configuration}. + * Builders can be obtained in exactly two ways: + * <ol> + * <li>By accessing a preinitialized builder from an existing {@link Configuration}, + * by calling {@link org.apache.tamaya.Configuration#toBuilder()}.</li> + * <li>By accessing an empty builder instance from + * {@link org.apache.tamaya.ConfigurationProvider#getConfigurationBuilder()}.</li> + * </ol> + */ +public interface ConfigurationBuilder { + + /** + * Init this builder instance with the given {@link ConfigurationContext} instance. This + * method will use any existing property sources, filters, converters and the combination + * policy of the given {@link ConfigurationContext} and initialize the current builder + * with them. + * + * @param context the {@link ConfigurationContext} instance to be used, not {@code null}. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder setContext(ConfigurationContext context); + + /** + * This method can be used for adding {@link PropertySource}s. + * Hereby the property source is added to the tail of property sources with + * lowest priority regardless of its current ordinal value. To sort the property + * sources based on their ordinals call {@link #sortPropertySources}. + * + * @param propertySources the PropertySources to add + * @return this builder, for chaining, never null. + * @throws IllegalArgumentException If a property source with a given name already + * exists. + */ + ConfigurationBuilder addPropertySources(PropertySource... propertySources); + + /** + * This method can be used for programmatically adding {@link PropertySource}s. + * Hereby the property source is added to the tail of property sources with + * lowest priority regardless of its current ordinal value. To sort the property + * sources based on their ordinals call {@link #sortPropertySources}. + * + * @param propertySources the PropertySources to add + * @return this builder, for chaining, never null. + * @throws IllegalArgumentException If a property source with a given name already + * exists. + */ + ConfigurationBuilder addPropertySources(Collection<PropertySource> propertySources); + + /** + * Add all registered (default) property sources to the context built. The sources are ordered + * based on their ordinal values and added to the chain of property sources with + * higher priority. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addDefaultPropertySources(); + + /** + * Removes the given property sources, if existing. The existing order of property + * sources is preserved. + * + * @param propertySources the property sources to remove, not {@code null}. + * @return the builder for chaining. + */ + ConfigurationBuilder removePropertySources(PropertySource... propertySources); + + /** + * Removes the given property sources, if existing. The existing order of property + * sources is preserved. + * + * @param propertySources the property sources to remove, not {@code null}. + * @return the builder for chaining. + */ + ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources); + + /** + * Access the current chain of property sources. Items at the end of the list have + * precedence/more significance. + * + * @return the property source chain, never {@code null}. + */ + List<PropertySource> getPropertySources(); + + /** + * Access the current chain of property filters. Items at the end of the list have + * precedence/more significance. + * + * @return the property source chain, never {@code null}. + */ + List<PropertyFilter> getPropertyFilters(); + + /** + * Access the current registered property converters. + * + * @return the current registered property converters. + */ + Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter(); + + /** + * Increases the priority of the given property source, by moving it towards the end + * of the chain of property sources. If the property source given is already at the end + * this method has no effect. This operation does not change any ordinal values. + * + * @param propertySource the property source to be incresed regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder increasePriority(PropertySource propertySource); + + /** + * Decreases the priority of the given property source, by moving it towards the start + * of the chain of property sources. If the property source given is already the first + * this method has no effect. This operation does not change any ordinal values. + * + * @param propertySource the property source to be decresed regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder decreasePriority(PropertySource propertySource); + + /** + * Increases the priority of the given property source to be maximal, by moving it to + * the tail of the of property source chain. If the property source given is + * already the last item this method has no effect. This operation does not change + * any ordinal values. + * + * @param propertySource the property source to be maximized regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder highestPriority(PropertySource propertySource); + + /** + * Decreases the priority of the given property source to be minimal, by moving it to + * the start of the chain of property source chain. If the property source given is + * already the first item this method has no effect. This operation does not change + * any ordinal values. + * + * @param propertySource the property source to be minimized regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder lowestPriority(PropertySource propertySource); + + /** + * Adds the given PropertyFilter instances, hereby the instances are added + * to the end of the list with highest priority. The ordering of existing + * property filters remains unchanged. To sort the property + * filters call {@link #sortPropertyFilter}. + * + * @param filters the filters to add + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addPropertyFilters(PropertyFilter... filters); + + /** + * Adds the given PropertyFilter instances, hereby the instances are added + * to the end of the list with highest priority. The ordering of existing + * property filters remains unchanged. To sort the property + * filters call {@link #sortPropertyFilter}. + * + * @param filters the filters to add + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters); + + /** + * Add all registered (default) property filters to the context built. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addDefaultPropertyFilters(); + + + /** + * Removes the given PropertyFilter instances, if existing. The order of the remaining + * filters is preserved. + * + * @param filters the filter to remove + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder removePropertyFilters(PropertyFilter... filters); + + /** + * Removes the given PropertyFilter instances, if existing. The order of the remaining + * filters is preserved. + * + * @param filters the filter to remove + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder removePropertyFilters(Collection<PropertyFilter> filters); + + /** + * This method can be used for adding {@link PropertyConverter}s. + * Converters are added at the end after any existing converters. + * For converters already registered for the current target type the + * method has no effect. + * + * @param typeToConvert the type for which the converters is for + * @param propertyConverters the PropertyConverters to add for this type + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, + PropertyConverter<T>... propertyConverters); + + /** + * This method can be used for adding {@link PropertyConverter}s. + * Converters are added at the end after any existing converters. + * For converters already registered for the current target type the + * method has no effect. + * + * @param typeToConvert the type for which the converters is for + * @param propertyConverters the PropertyConverters to add for this type + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, + Collection<PropertyConverter<T>> propertyConverters); + + /** + * Add all registered (default) property converters to the context built. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addDefaultPropertyConverters(); + + /** + * Removes the given PropertyConverter instances for the given type, + * if existing. + * + * @param typeToConvert the type which the converters is for + * @param propertyConverters the converters to remove + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, + PropertyConverter<T>... propertyConverters); + + /** + * Removes the given PropertyConverter instances for the given type, + * if existing. + * + * @param typeToConvert the type which the converters is for + * @param propertyConverters the converters to remove + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, + Collection<PropertyConverter<T>> propertyConverters); + + /** + * Removes all converters for the given type, which actually renders a given type + * unsupported for type conversion. + * + * @param typeToConvert the type which the converters is for + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert); + + /** + * Sorts the current registered property sources using the given comparator. + * + * NOTE: property sources at the beginning have minimal significance. + * + * @param comparator the comparator to be used, not {@code null}. + * @return this instance for chaining. + */ + ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator); + + /** + * Sorts the current registered property filters using the given comparator. + * + * NOTE: property filters at the beginning have minimal significance. + * + * @param comparator the comparator to be used, not {@code null}. + * @return this instance for chaining. + */ + ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator); + + /** + * Sets the {@link PropertyValueCombinationPolicy} used to evaluate the final + * property values. + * + * @param policy the {@link PropertyValueCombinationPolicy} used, not {@code null}. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy); + + /** + * Builds a new {@link Configuration} based on the data in this builder. The ordering of property + * sources and property filters is not changed, regardless of their ordinals. For ensure a certain + * ordering/significance call {@link #sortPropertyFilter(Comparator)} and/or {@link #sortPropertySources(Comparator)} + * before building the context. + * + * @return the final configuration. + */ + Configuration build(); + +} + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.java ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.java b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.java new file mode 100644 index 0000000..d50ed7d --- /dev/null +++ b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigValueEvaluator.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.spisupport; + +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.util.HashMap; +import java.util.Map; + + +/** + * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the + * chain of {@link PropertySource} and {@link PropertyFilter} + * instance to evaluate the current Configuration. + */ +public class DefaultConfigValueEvaluator implements ConfigValueEvaluator{ + + @Override + public PropertyValue evaluteRawValue(String key, ConfigurationContext context) { + PropertyValue unfilteredValue = null; + for (PropertySource propertySource : context.getPropertySources()) { + unfilteredValue = context.getPropertyValueCombinationPolicy(). + collect(unfilteredValue, key, propertySource); + } + if(unfilteredValue==null || unfilteredValue.getValue()==null){ + return null; + } + return unfilteredValue; + } + + @Override + public Map<String, PropertyValue> evaluateRawValues(ConfigurationContext context) { + Map<String, PropertyValue> result = new HashMap<>(); + for (PropertySource propertySource : context.getPropertySources()) { + for (Map.Entry<String,PropertyValue> propEntry: propertySource.getProperties().entrySet()) { + PropertyValue unfilteredValue = result.get(propEntry.getKey()); + unfilteredValue = context.getPropertyValueCombinationPolicy(). + collect(unfilteredValue, propEntry.getKey(), propertySource); + if(unfilteredValue!=null){ + result.put(unfilteredValue.getKey(), unfilteredValue); + } + } + } + return result; + } + + @Override + public String toString() { + return "DefaultConfigEvaluator{}"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java new file mode 100644 index 0000000..fbcf35f --- /dev/null +++ b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java @@ -0,0 +1,283 @@ +/* + * 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.spisupport; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.ConfigOperator; +import org.apache.tamaya.ConfigQuery; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; +import org.apache.tamaya.spi.PropertyValueCombinationPolicy; +import org.apache.tamaya.spi.ServiceContextManager; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the + * chain of {@link PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} + * instance to evaluate the current Configuration. + */ +public class DefaultConfiguration implements Configuration { + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger(DefaultConfiguration.class.getName()); + + /** + * The current {@link ConfigurationContext} of the current instance. + */ + private final ConfigurationContext configurationContext; + + /** + * EvaluationStrategy + */ + private ConfigValueEvaluator configEvaluator = loadConfigValueEvaluator(); + + private ConfigValueEvaluator loadConfigValueEvaluator() { + ConfigValueEvaluator eval = null; + try{ + eval = ServiceContextManager.getServiceContext() + .getService(ConfigValueEvaluator.class); + }catch(Exception e){ + LOG.log(Level.WARNING, "Failed to load ConfigValueEvaluator from ServiceContext, using default.", e); + } + if(eval==null){ + eval = new DefaultConfigValueEvaluator(); + } + return eval; + } + + + /** + * Constructor. + * @param configurationContext The configuration Context to be used. + */ + public DefaultConfiguration(ConfigurationContext configurationContext){ + this.configurationContext = Objects.requireNonNull(configurationContext); + } + + /** + * Get a given value, filtered with the context's filters as needed. + * @param key the property's key, not null. + * @return the filtered value, or null. + */ + @Override + public String get(String key) { + Objects.requireNonNull(key, "Key must not be null."); + + PropertyValue value = configEvaluator.evaluteRawValue(key, configurationContext); + if(value==null || value.getValue()==null){ + return null; + } + value = PropertyFiltering.applyFilter(value, configurationContext); + if(value!=null){ + return value.getValue(); + } + return null; + } + + /** + * Evaluates the raw value using the context's PropertyValueCombinationPolicy. + * @param key the key, not null. + * @return the value, before filtering is applied. + */ + protected PropertyValue evaluteRawValue(String key) { + List<PropertySource> propertySources = configurationContext.getPropertySources(); + PropertyValue filteredValue = null; + PropertyValueCombinationPolicy combinationPolicy = this.configurationContext + .getPropertyValueCombinationPolicy(); + for (PropertySource propertySource : propertySources) { + filteredValue = combinationPolicy.collect(filteredValue, key, propertySource); + } + return filteredValue; + } + + + @Override + public String getOrDefault(String key, String defaultValue) { + Objects.requireNonNull(key, "Key must not be null."); + + String val = get(key); + if(val==null){ + return defaultValue; + } + return val; + } + + @Override + public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { + Objects.requireNonNull(key, "Key must not be null."); + Objects.requireNonNull(type, "Target type must not be null"); + + T val = get(key, type); + if(val==null){ + return defaultValue; + } + return val; + } + + /** + * Get the current properties, composed by the loaded {@link PropertySource} and filtered + * by registered {@link org.apache.tamaya.spi.PropertyFilter}. + * + * @return the final properties. + */ + @Override + public Map<String, String> getProperties() { + Map<String, PropertyValue> filtered = PropertyFiltering.applyFilters( + configEvaluator.evaluateRawValues(configurationContext), + configurationContext); + Map<String,String> result = new HashMap<>(); + for(PropertyValue val:filtered.values()){ + if(val.getValue()!=null) { + result.put(val.getKey(), val.getValue()); + // TODO: Discuss metadata handling... + result.putAll(val.getMetaEntries()); + } + } + return result; + } + + + /** + * Accesses the current String value for the given key and tries to convert it + * using the {@link PropertyConverter} instances provided by the current + * {@link ConfigurationContext}. + * + * @param key the property's absolute, or relative path, e.g. @code + * a/b/c/d.myProperty}, never {@code null}. + * @param type The target type required, not {@code null}. + * @param <T> the value type + * @return the converted value, never {@code null}. + */ + @Override + public <T> T get(String key, Class<T> type) { + return get(key, (TypeLiteral<T>)TypeLiteral.of(type)); + } + + /** + * Accesses the current String value for the given key and tries to convert it + * using the {@link PropertyConverter} instances provided by the current + * {@link ConfigurationContext}. + * + * @param key the property's absolute, or relative path, e.g. @code + * a/b/c/d.myProperty}. + * @param type The target type required, not null. + * @param <T> the value type + * @return the converted value, never null. + */ + @Override + public <T> T get(String key, TypeLiteral<T> type) { + Objects.requireNonNull(key, "Key must not be null."); + Objects.requireNonNull(type, "Target type must not be null"); + + return convertValue(key, get(key), type); + } + + @SuppressWarnings("unchecked") + protected <T> T convertValue(String key, String value, TypeLiteral<T> type) { + if (value != null) { + List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); + ConversionContext context = new ConversionContext.Builder(this, this.configurationContext, key, type) + .build(); + for (PropertyConverter<T> converter : converters) { + try { + T t = converter.convert(value, context); + if (t != null) { + return t; + } + } catch (Exception e) { + LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert value: " + value, e); + } + } + // if the target type is a String, we can return the value, no conversion required. + if(type.equals(TypeLiteral.of(String.class))){ + return (T)value; + } + // unsupported type, throw an exception + throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + + ", supported formats: " + context.getSupportedFormats()); + } + return null; + } + + @Override + public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { + Objects.requireNonNull(key); + Objects.requireNonNull(type); + + T val = get(key, type); + if(val==null){ + return defaultValue; + } + return val; + } + + @Override + public Configuration with(ConfigOperator operator) { + return operator.operate(this); + } + + @Override + public <T> T query(ConfigQuery<T> query) { + return query.query(this); + } + + @Override + public ConfigurationContext getContext() { + return this.configurationContext; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DefaultConfiguration that = (DefaultConfiguration) o; + + if (!configurationContext.equals(that.configurationContext)) return false; + return configEvaluator.getClass().equals(that.configEvaluator.getClass()); + } + + @Override + public int hashCode() { + int result = configurationContext.hashCode(); + result = 31 * result + configEvaluator.getClass().hashCode(); + return result; + } + + @Override + public String toString() { + return "Configuration{\n " + + configurationContext + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java new file mode 100644 index 0000000..a770230 --- /dev/null +++ b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationBuilder.java @@ -0,0 +1,239 @@ +/* + * 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.spisupport; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.*; +import org.apache.tamaya.spi.ConfigurationBuilder; + +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + +/** + * Default implementation of {@link ConfigurationBuilder}. + */ +public class DefaultConfigurationBuilder implements ConfigurationBuilder { + + protected final DefaultConfigurationContextBuilder contextBuilder; + + /** + * Creates a new builder instance. + */ + public DefaultConfigurationBuilder() { + this.contextBuilder = new DefaultConfigurationContextBuilder(); + } + + /** + * Creates a new builder instance. + */ + public DefaultConfigurationBuilder(ConfigurationContext context) { + this.contextBuilder = new DefaultConfigurationContextBuilder(context); + } + + /** + * Creates a new builder instance initializing it with the given context. + * @param configuration the configuration to be used, not null. + */ + public DefaultConfigurationBuilder(Configuration configuration) { + this.contextBuilder = new DefaultConfigurationContextBuilder(configuration.getContext()); + } + + /** + * Allows to set configuration context during unit tests. + */ + public ConfigurationBuilder setConfiguration(Configuration configuration) { + this.contextBuilder.setContext(configuration.getContext()); + return this; + } + + + @Override + public ConfigurationBuilder setContext(ConfigurationContext context) { + this.contextBuilder.setContext(context); + return this; + } + + @Override + public ConfigurationBuilder addPropertySources(PropertySource... sources){ + this.contextBuilder.addPropertySources(sources); + return this; + } + + @Override + public ConfigurationBuilder addPropertySources(Collection<PropertySource> sources){ + this.contextBuilder.addPropertySources(sources); + return this; + } + + public ConfigurationBuilder addDefaultPropertyFilters() { + this.contextBuilder.addDefaultPropertyFilters(); + return this; + } + + public ConfigurationBuilder addDefaultPropertySources() { + this.contextBuilder.addDefaultPropertySources(); + return this; + } + + public ConfigurationBuilder addDefaultPropertyConverters() { + this.contextBuilder.addDefaultPropertyConverters(); + return this; + } + + @Override + public ConfigurationBuilder removePropertySources(PropertySource... propertySources) { + this.contextBuilder.removePropertySources(propertySources); + return this; + } + + @Override + public ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources) { + this.contextBuilder.removePropertySources(propertySources); + return this; + } + + @Override + public List<PropertySource> getPropertySources() { + return this.contextBuilder.getPropertySources(); + } + + @Override + public ConfigurationBuilder increasePriority(PropertySource propertySource) { + this.contextBuilder.increasePriority(propertySource); + return this; + } + + @Override + public ConfigurationBuilder decreasePriority(PropertySource propertySource) { + this.contextBuilder.decreasePriority(propertySource); + return this; + } + + @Override + public ConfigurationBuilder highestPriority(PropertySource propertySource) { + this.contextBuilder.highestPriority(propertySource); + return this; + } + + @Override + public ConfigurationBuilder lowestPriority(PropertySource propertySource) { + this.contextBuilder.lowestPriority(propertySource); + return this; + } + + @Override + public ConfigurationBuilder addPropertyFilters(PropertyFilter... filters){ + this.contextBuilder.addPropertyFilters(filters); + return this; + } + + @Override + public ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters){ + this.contextBuilder.addPropertyFilters(filters); + return this; + } + + @Override + public ConfigurationBuilder removePropertyFilters(PropertyFilter... filters) { + this.contextBuilder.removePropertyFilters(filters); + return this; + } + + @Override + public ConfigurationBuilder removePropertyFilters(Collection<PropertyFilter> filters) { + this.contextBuilder.removePropertyFilters(filters); + return this; + } + + + @Override + public <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, + PropertyConverter<T>... converters) { + this.contextBuilder.removePropertyConverters(typeToConvert, converters); + return this; + } + + @Override + public <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, + Collection<PropertyConverter<T>> converters) { + this.contextBuilder.removePropertyConverters(typeToConvert, converters); + return this; + } + + @Override + public ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert) { + this.contextBuilder.removePropertyConverters(typeToConvert); + return this; + } + + + @Override + public ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy combinationPolicy){ + this.contextBuilder.setPropertyValueCombinationPolicy(combinationPolicy); + return this; + } + + @Override + public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, PropertyConverter<T>... propertyConverters){ + this.contextBuilder.addPropertyConverters(type, propertyConverters); + return this; + } + + @Override + public <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> type, Collection<PropertyConverter<T>> propertyConverters){ + this.contextBuilder.addPropertyConverters(type, propertyConverters); + return this; + } + + /** + * Builds a new configuration based on the configuration of this builder instance. + * + * @return a new {@link org.apache.tamaya.Configuration configuration instance}, + * never {@code null}. + */ + @Override + public Configuration build() { + return new DefaultConfiguration(this.contextBuilder.build()); + } + + @Override + public ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator) { + this.contextBuilder.sortPropertyFilter(comparator); + return this; + } + + @Override + public ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator) { + this.contextBuilder.sortPropertySources(comparator); + return this; + } + + @Override + public List<PropertyFilter> getPropertyFilters() { + return this.contextBuilder.getPropertyFilters(); + } + + @Override + public Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter() { + return this.contextBuilder.getPropertyConverter(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java ---------------------------------------------------------------------- diff --git a/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java new file mode 100644 index 0000000..d880e8d --- /dev/null +++ b/code/old/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfigurationContext.java @@ -0,0 +1,277 @@ +/* + * 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.spisupport; + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.*; + +import java.util.*; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.logging.Logger; + +/** + * Default implementation of a simple ConfigurationContext. + */ +public class DefaultConfigurationContext implements ConfigurationContext { + + /** The logger used. */ + private final static Logger LOG = Logger.getLogger(DefaultConfigurationContext.class.getName()); + + /** + * Subcomponent handling {@link PropertyConverter} instances. + */ + private final PropertyConverterManager propertyConverterManager = new PropertyConverterManager(); + + /** + * The current unmodifiable list of loaded {@link PropertySource} instances. + */ + private List<PropertySource> immutablePropertySources; + + /** + * The current unmodifiable list of loaded {@link PropertyFilter} instances. + */ + private List<PropertyFilter> immutablePropertyFilters; + + /** + * The overriding policy used when combining PropertySources registered to evalute the final configuration + * values. + */ + private PropertyValueCombinationPolicy propertyValueCombinationPolicy; + + /** + * Lock for internal synchronization. + */ + private final ReentrantReadWriteLock propertySourceLock = new ReentrantReadWriteLock(); + + @SuppressWarnings("unchecked") + protected DefaultConfigurationContext(DefaultConfigurationContextBuilder builder) { + List<PropertySource> propertySources = new ArrayList<>(); + // first we load all PropertySources which got registered via java.util.ServiceLoader + propertySources.addAll(builder.propertySources); + // now sort them according to their ordinal values + immutablePropertySources = Collections.unmodifiableList(propertySources); + + // as next step we pick up the PropertyFilters pretty much the same way + List<PropertyFilter> propertyFilters = new ArrayList<>(builder.getPropertyFilters()); + immutablePropertyFilters = Collections.unmodifiableList(propertyFilters); + + // Finally add the converters + for(Map.Entry<TypeLiteral<?>, Collection<PropertyConverter<?>>> en:builder.getPropertyConverter().entrySet()) { + for (@SuppressWarnings("rawtypes") PropertyConverter converter : en.getValue()) { + this.propertyConverterManager.register(en.getKey(), converter); + } + } + LOG.info("Registered " + propertyConverterManager.getPropertyConverters().size() + " property converters: " + + propertyConverterManager.getPropertyConverters()); + + propertyValueCombinationPolicy = builder.combinationPolicy; + if(propertyValueCombinationPolicy==null){ + propertyValueCombinationPolicy = ServiceContextManager.getServiceContext().getService(PropertyValueCombinationPolicy.class); + } + if(propertyValueCombinationPolicy==null){ + propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; + } + LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy); + } + + + @Deprecated + @Override + public void addPropertySources(PropertySource... propertySourcesToAdd) { + Lock writeLock = propertySourceLock.writeLock(); + try { + writeLock.lock(); + List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources); + newPropertySources.addAll(Arrays.asList(propertySourcesToAdd)); + Collections.sort(newPropertySources, PropertySourceComparator.getInstance()); + + this.immutablePropertySources = Collections.unmodifiableList(newPropertySources); + } finally { + writeLock.unlock(); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof DefaultConfigurationContext)){ + return false; + } + + DefaultConfigurationContext that = (DefaultConfigurationContext) o; + + if (!propertyConverterManager.equals(that.propertyConverterManager)) { + return false; + } + if (!immutablePropertySources.equals(that.immutablePropertySources)) { + return false; + } + if (!immutablePropertyFilters.equals(that.immutablePropertyFilters)) { + return false; + } + return getPropertyValueCombinationPolicy().equals(that.getPropertyValueCombinationPolicy()); + + } + + @Override + public int hashCode() { + int result = propertyConverterManager.hashCode(); + result = 31 * result + immutablePropertySources.hashCode(); + result = 31 * result + immutablePropertyFilters.hashCode(); + result = 31 * result + getPropertyValueCombinationPolicy().hashCode(); + return result; + } + + @Override + public String toString() { + StringBuilder b = new StringBuilder("ConfigurationContext{\n"); + b.append(" Property Sources\n"); + b.append(" ----------------\n"); + if(immutablePropertySources.isEmpty()){ + b.append(" No property sources loaded.\n\n"); + }else { + b.append(" CLASS NAME ORDINAL SCANNABLE SIZE STATE ERROR\n\n"); + for (PropertySource ps : immutablePropertySources) { + b.append(" "); + appendFormatted(b, ps.getClass().getSimpleName(), 30); + appendFormatted(b, ps.getName(), 70); + appendFormatted(b, String.valueOf(PropertySourceComparator.getOrdinal(ps)), 8); + appendFormatted(b, String.valueOf(ps.isScannable()), 10); + if (ps.isScannable()) { + appendFormatted(b, String.valueOf(ps.getProperties().size()), 8); + } else { + appendFormatted(b, "-", 8); + } + PropertyValue state = ps.get("_state"); + if(state==null){ + appendFormatted(b, "OK", 10); + }else { + appendFormatted(b, state.getValue(), 10); + if("ERROR".equals(state.getValue())){ + PropertyValue val = ps.get("_exception"); + if(val!=null) { + appendFormatted(b, val.getValue(), 30); + } + } + } + b.append('\n'); + } + b.append("\n"); + } + b.append(" Property Filters\n"); + b.append(" ----------------\n"); + if(immutablePropertyFilters.isEmpty()){ + b.append(" No property filters loaded.\n\n"); + }else { + b.append(" CLASS INFO\n\n"); + for (PropertyFilter filter : getPropertyFilters()) { + b.append(" "); + appendFormatted(b, filter.getClass().getSimpleName(), 30); + b.append(removeNewLines(filter.toString())); + b.append('\n'); + } + b.append("\n\n"); + } + b.append(" Property Converters\n"); + b.append(" -------------------\n"); + b.append(" CLASS TYPE INFO\n\n"); + for(Map.Entry<TypeLiteral<?>, List<PropertyConverter<?>>> converterEntry:getPropertyConverters().entrySet()){ + for(PropertyConverter converter: converterEntry.getValue()){ + b.append(" "); + appendFormatted(b, converter.getClass().getSimpleName(), 30); + appendFormatted(b, converterEntry.getKey().getRawType().getSimpleName(), 30); + b.append(removeNewLines(converter.toString())); + b.append('\n'); + } + } + b.append("\n\n"); + b.append(" PropertyValueCombinationPolicy: " + getPropertyValueCombinationPolicy().getClass().getName()).append('\n'); + b.append('}'); + return b.toString(); + } + + private void appendFormatted(StringBuilder b, String text, int length) { + int padding; + if(text.length() <= (length)){ + b.append(text); + padding = length - text.length(); + }else{ + b.append(text.substring(0, length-1)); + padding = 1; + } + for(int i=0;i<padding;i++){ + b.append(' '); + } + } + + private String removeNewLines(String s) { + return s.replace('\n', ' ').replace('\r', ' '); + } + + + @Override + public List<PropertySource> getPropertySources() { + return immutablePropertySources; + } + + @Override + public PropertySource getPropertySource(String name) { + for(PropertySource ps:getPropertySources()){ + if(name.equals(ps.getName())){ + return ps; + } + } + return null; + } + + @Override + public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) { + propertyConverterManager.register(typeToConvert, propertyConverter); + LOG.info("Added PropertyConverter: " + propertyConverter.getClass().getName()); + } + + @Override + public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { + return propertyConverterManager.getPropertyConverters(); + } + + @Override + public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) { + return propertyConverterManager.getPropertyConverters(targetType); + } + + @Override + public List<PropertyFilter> getPropertyFilters() { + return immutablePropertyFilters; + } + + @Override + public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy(){ + return propertyValueCombinationPolicy; + } + + @Override + public ConfigurationContextBuilder toBuilder() { + return new DefaultConfigurationContextBuilder(this); + } + +}
