TAMAYA-45: Moved event annotations out of package (functionality must be discussed with question of update handling in general).
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/2acf67b7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/2acf67b7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/2acf67b7 Branch: refs/heads/master Commit: 2acf67b770c4e3c61293aad87599346ccb827799 Parents: 4f5269e Author: anatole <[email protected]> Authored: Sun Jan 18 01:04:56 2015 +0100 Committer: anatole <[email protected]> Committed: Sun Jan 18 01:09:26 2015 +0100 ---------------------------------------------------------------------- .../tamaya/event/ObservesConfigChange.java | 38 +++++ .../apache/tamaya/event/PropertyChangeSet.java | 125 +++++++++++++++ .../tamaya/event/PropertyChangeSetBuilder.java | 154 +++++++++++++++++++ .../org/apache/tamaya/inject/ConfigRoot.java | 43 ++++++ .../tamaya/inject/ConfigurationInjector.java | 25 +-- .../org/apache/tamaya/inject/DefaultAreas.java | 43 ------ .../tamaya/inject/ObservesConfigChange.java | 38 ----- .../apache/tamaya/inject/PropertyChangeSet.java | 125 --------------- .../tamaya/inject/PropertyChangeSetBuilder.java | 154 ------------------- .../internal/ConfigChangeCallbackMethod.java | 2 +- .../tamaya/inject/internal/ConfiguredField.java | 4 +- .../inject/internal/ConfiguredSetterMethod.java | 6 +- .../tamaya/inject/internal/ConfiguredType.java | 8 +- .../internal/DefaultConfigurationInjector.java | 11 +- .../tamaya/inject/internal/InjectionUtils.java | 12 +- .../internal/WeakConfigListenerManager.java | 2 +- 16 files changed, 401 insertions(+), 389 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/event/ObservesConfigChange.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/event/ObservesConfigChange.java b/modules/injection/src/main/java/org/apache/tamaya/event/ObservesConfigChange.java new file mode 100644 index 0000000..9fc6d9b --- /dev/null +++ b/modules/injection/src/main/java/org/apache/tamaya/event/ObservesConfigChange.java @@ -0,0 +1,38 @@ +/* + * 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.event; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to annotate a method on a class to be informed on config changes. + * The exact behaviour, when configuration change events are sent can be configured + * on each configured property/method by adding the {@link org.apache.tamaya.inject.WithLoadPolicy} + * annotation. By default listeners are informed on all changes of configurations that were used as + * input configurations for configuring a class/instance. Additionally {@link org.apache.tamaya.inject.ConfiguredProperty} + * annotations can be added that allows to constrain changes to some limited properties. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { ElementType.METHOD }) +public @interface ObservesConfigChange { + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSet.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSet.java b/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSet.java new file mode 100644 index 0000000..079f333 --- /dev/null +++ b/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSet.java @@ -0,0 +1,125 @@ +/* + * 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.event; + +import java.io.Serializable; +import java.util.*; + +/** + * Event that contains a set of changed keys of a {@link org.apache.tamaya.spi.PropertySource}. Any values must + * be obtained by calling the {@link org.apache.tamaya.Configuration} due to security reasons. This event can be used + * by the {@link org.apache.tamaya.spi.PropertySource} implementation to cleanup caches as needed. + * This class is immutable and thread-safe. To create instances use + * {@link PropertyChangeSetBuilder}. + */ +public final class PropertyChangeSet implements Serializable{ + /** The serialVersionUID. */ + private static final long serialVersionUID = 1L; + /** The base property source name. */ + private String propertySourceName; + /** The timestamp of the change. */ + private long timestamp = System.currentTimeMillis(); + /** + * The recorded keys added. + */ + final SortedSet<String> addedKeys = new TreeSet<>(); + + /** + * The recorded keys updated. + */ + final SortedSet<String> updatedKeys = new TreeSet<>(); + + /** + * The recorded keys removed. + */ + final SortedSet<String> removedKeys = new TreeSet<>(); + + + /** + * Constructor used by {@link PropertyChangeSetBuilder}. + * @param builder The builder instance, not null. + */ + PropertyChangeSet(PropertyChangeSetBuilder builder) { + this.propertySourceName = Objects.requireNonNull(builder.propertySourceName); + this.addedKeys.addAll(builder.addedKeys); + this.removedKeys.addAll(builder.removedKeys); + this.updatedKeys.addAll(builder.updatedKeys); + } + + /** + * Get the underlying property provider/configuration. + * @return the underlying property provider/configuration, never null. + */ + public String getPropertySourceName(){ + return this.propertySourceName; + } + + /** + * Get the timestamp of this changeset. This allows to track, if a ChangeSet was already applied. + * @return the timestamp + */ + public long getTimestamp(){ + return timestamp; + } + + /** + * Get the keys added. + * @return the added keys, never null. + */ + public Collection<String> getKeysAdded(){ + return Collections.unmodifiableCollection(this.addedKeys); + } + + /** + * Get the keys removed. + * @return the removed keys, never null. + */ + public Collection<String> getKeysRemoved(){ + return Collections.unmodifiableCollection(this.removedKeys); + } + + /** + * Get the updated keys. + * @return the updated keys, never null. + */ + public Collection<String> getKeysUpdated(){ + return Collections.unmodifiableCollection(this.addedKeys); + } + + + /** + * CHecks if the current change set does not contain any changes. + * @return tru, if the change set is empty. + */ + public boolean isEmpty(){ + return this.addedKeys.isEmpty() && this.updatedKeys.isEmpty() && this.removedKeys.isEmpty(); + } + + + @Override + public String toString() { + return "ConfigChangeSet{" + + "propertySourceName=" + propertySourceName + + ", timestamp=" + timestamp + + ", addedKeys=" + addedKeys + + ", updatedKeys=" + updatedKeys + + ", removedKeys=" + removedKeys + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSetBuilder.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSetBuilder.java b/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSetBuilder.java new file mode 100644 index 0000000..57a6264 --- /dev/null +++ b/modules/injection/src/main/java/org/apache/tamaya/event/PropertyChangeSetBuilder.java @@ -0,0 +1,154 @@ +/* + * 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.event; + +import org.apache.tamaya.spi.PropertySource; + +import java.beans.PropertyChangeEvent; +import java.util.*; + +/** + * Models a set current changes to be applied to a configuration/property provider. Such a set can be applied + * to any {@link PropertySource} instance. If the provider is mutable it may check the + * version given and applyChanges the changes to the provider/configuration, including triggering current regarding + * change events. + * <p> + * For appropriate conversion a {@code Function<String, Codec>} can be applied, which performs correct conversion, + * when changed values are set. This function enables connecting e.g. setters on a configuration template with + * the corresponding conversion logic, so the template calls are correctly converted back. + */ +public final class PropertyChangeSetBuilder { + /** + * The recorded changes. + */ + final SortedSet<String> addedKeys = new TreeSet<>(); + + /** + * The recorded changes. + */ + final SortedSet<String> updatedKeys = new TreeSet<>(); + + /** + * The recorded changes. + */ + final SortedSet<String> removedKeys = new TreeSet<>(); + + /** + * The underlying configuration/provider. + */ + String propertySourceName; + + /** + * Constructor. + * + * @param source the underlying configuration/provider, not null. + */ + private PropertyChangeSetBuilder(PropertySource source) { + this.propertySourceName = Objects.requireNonNull(source).getName(); + } + + /** + * Creates a new instance current this builder. + * + * @param source the underlying property provider/configuration, not null. + * @return the builder for chaining. + */ + public static PropertyChangeSetBuilder of(PropertySource source) { + return new PropertyChangeSetBuilder(source); + } + + + /** + * Marks the given key(s) as removed. + * + * @param keys the keys removed + * @return the builder for chaining. + */ + public PropertyChangeSetBuilder remove(String... keys) { + for (String removeKey : keys) { + this.removedKeys.add(removeKey); + } + return this; + } + + /** + * Marks the given key(s) as added. + * + * @param keys the keys added + * @return the builder for chaining. + */ + public PropertyChangeSetBuilder add(String... keys) { + for (String addKey : keys) { + this.addedKeys.add(addKey); + } + return this; + } + + /** + * Marks the given key(s) as updaed. + * + * @param keys the keys updated + * @return the builder for chaining. + */ + public PropertyChangeSetBuilder update(String... keys) { + for (String uptKey : keys) { + this.updatedKeys.add(uptKey); + } + return this; + } + + + /** + * Compares the two property maps and adds the corresponding updated/aded/removed keys to the builder. + * + * @param map1 the source map, not null. + * @param map2 the target map, not null. + * @return the builder for chaining. + */ + public PropertyChangeSetBuilder addChanges(Map<String,String> map1, Map<String,String> map2) { + List<PropertyChangeEvent> changes = new ArrayList<>(); + for (Map.Entry<String, String> en : map1.entrySet()) { + String val = map2.get(en.getKey()); + if (val==null) { + remove(en.getKey()); + } else if (!val.equals(en.getValue())) { + update(en.getKey()); + } + } + for (Map.Entry<String, String> en : map2.entrySet()) { + String val = map1.get(en.getKey()); + if (val==null) { + add(en.getKey()); + } + // update case already handled before! + } + return this; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "PropertyChangeEventBuilder [propertySourceName=" + propertySourceName + ", " + + ", added=" + addedKeys + ", updated=" + updatedKeys + ", removed=" + removedKeys +"]"; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigRoot.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigRoot.java b/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigRoot.java new file mode 100644 index 0000000..86de2fb --- /dev/null +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigRoot.java @@ -0,0 +1,43 @@ +/* + * 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.inject; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to control injection and resolution current a configured bean. The configuration keys + * to be resolved are basically determined by the {@link ConfiguredProperty} + * annotation(s). Nevertheless these annotations can also have relative key names. This annotation allows + * to define a configuration area that is prefixed to all relative configuration keys within the + * corresponding class/template interface. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { ElementType.TYPE }) +public @interface ConfigRoot { + + /** + * Allows to declare an operator that should be applied before injecting values into the bean. + * @return the operator class to be used. + */ + String[] value(); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java b/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java index f7f7ee7..de1c29e 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/ConfigurationInjector.java @@ -20,6 +20,8 @@ package org.apache.tamaya.inject; import org.apache.tamaya.spi.ServiceContext; +import java.util.function.Supplier; + /** * Accessor interface for injection of configuration and configuration templates. @@ -27,24 +29,29 @@ import org.apache.tamaya.spi.ServiceContext; public interface ConfigurationInjector { /** - * Extract the configuration annotation config and registers it per class, for later reuse. - * @param type the type to be configured. - * @return the configured type registered. - */ - void registerType(Class<?> type); - - /** * Configured the current instance and reigsterd necessary listener to forward config change events as * defined by the current annotations in place. + * * @param instance the instance to be configured + * @return the configured instance (allows chaining of operations). + */ + <T> T configure(T instance); + + + /** + * Creates a supplier for configured instances of the given type {@code T}. + * @param supplier the supplier to create new instances. + * @param <T> the target type. + * @return a supplier creating configured instances of {@code T}. */ - void configure(Object instance); + <T> Supplier<T> getConfiguredSupplier(Supplier<T> supplier); /** * Get the current injector instance. + * * @return the current injector, not null. */ - public static ConfigurationInjector getInstance(){ + public static ConfigurationInjector getInstance() { return ServiceContext.getInstance().getService(ConfigurationInjector.class).get(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/DefaultAreas.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/DefaultAreas.java b/modules/injection/src/main/java/org/apache/tamaya/inject/DefaultAreas.java deleted file mode 100644 index a327f4e..0000000 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/DefaultAreas.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.inject; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation to control injection and resolution current a configured bean. The configuration keys - * to be resolved are basically determined by the {@link ConfiguredProperty} - * annotation(s). Nevertheless these annotations can also have relative key names. This annotation allows - * to define a configuration area that is prefixed to all relative configuration keys within the - * corresponding class/template interface. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(value = { ElementType.TYPE }) -public @interface DefaultAreas { - - /** - * Allows to declare an operator that should be applied before injecting values into the bean. - * @return the operator class to be used. - */ - String[] value(); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/ObservesConfigChange.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/ObservesConfigChange.java b/modules/injection/src/main/java/org/apache/tamaya/inject/ObservesConfigChange.java deleted file mode 100644 index 95ea972..0000000 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/ObservesConfigChange.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.inject; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation to annotate a method on a class to be informed on config changes. - * The exact behaviour, when configuration change events are sent can be configured - * on each configured property/method by adding the {@link WithLoadPolicy} - * annotation. By default listeners are informed on all changes of configurations that were used as - * input configurations for configuring a class/instance. Additionally {@link ConfiguredProperty} - * annotations can be added that allows to constrain changes to some limited properties. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(value = { ElementType.METHOD }) -public @interface ObservesConfigChange { - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSet.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSet.java b/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSet.java deleted file mode 100644 index bccb161..0000000 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSet.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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.inject; - -import java.io.Serializable; -import java.util.*; - -/** - * Event that contains a set of changed keys of a {@link org.apache.tamaya.spi.PropertySource}. Any values must - * be obtained by calling the {@link org.apache.tamaya.Configuration} due to security reasons. This event can be used - * by the {@link org.apache.tamaya.spi.PropertySource} implementation to cleanup caches as needed. - * This class is immutable and thread-safe. To create instances use - * {@link PropertyChangeSetBuilder}. - */ -public final class PropertyChangeSet implements Serializable{ - /** The serialVersionUID. */ - private static final long serialVersionUID = 1L; - /** The base property source name. */ - private String propertySourceName; - /** The timestamp of the change. */ - private long timestamp = System.currentTimeMillis(); - /** - * The recorded keys added. - */ - final SortedSet<String> addedKeys = new TreeSet<>(); - - /** - * The recorded keys updated. - */ - final SortedSet<String> updatedKeys = new TreeSet<>(); - - /** - * The recorded keys removed. - */ - final SortedSet<String> removedKeys = new TreeSet<>(); - - - /** - * Constructor used by {@link PropertyChangeSetBuilder}. - * @param builder The builder instance, not null. - */ - PropertyChangeSet(PropertyChangeSetBuilder builder) { - this.propertySourceName = Objects.requireNonNull(builder.propertySourceName); - this.addedKeys.addAll(builder.addedKeys); - this.removedKeys.addAll(builder.removedKeys); - this.updatedKeys.addAll(builder.updatedKeys); - } - - /** - * Get the underlying property provider/configuration. - * @return the underlying property provider/configuration, never null. - */ - public String getPropertySourceName(){ - return this.propertySourceName; - } - - /** - * Get the timestamp of this changeset. This allows to track, if a ChangeSet was already applied. - * @return the timestamp - */ - public long getTimestamp(){ - return timestamp; - } - - /** - * Get the keys added. - * @return the added keys, never null. - */ - public Collection<String> getKeysAdded(){ - return Collections.unmodifiableCollection(this.addedKeys); - } - - /** - * Get the keys removed. - * @return the removed keys, never null. - */ - public Collection<String> getKeysRemoved(){ - return Collections.unmodifiableCollection(this.removedKeys); - } - - /** - * Get the updated keys. - * @return the updated keys, never null. - */ - public Collection<String> getKeysUpdated(){ - return Collections.unmodifiableCollection(this.addedKeys); - } - - - /** - * CHecks if the current change set does not contain any changes. - * @return tru, if the change set is empty. - */ - public boolean isEmpty(){ - return this.addedKeys.isEmpty() && this.updatedKeys.isEmpty() && this.removedKeys.isEmpty(); - } - - - @Override - public String toString() { - return "ConfigChangeSet{" + - "propertySourceName=" + propertySourceName + - ", timestamp=" + timestamp + - ", addedKeys=" + addedKeys + - ", updatedKeys=" + updatedKeys + - ", removedKeys=" + removedKeys + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSetBuilder.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSetBuilder.java b/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSetBuilder.java deleted file mode 100644 index be37f7d..0000000 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/PropertyChangeSetBuilder.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * 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.inject; - -import org.apache.tamaya.spi.PropertySource; - -import java.beans.PropertyChangeEvent; -import java.util.*; - -/** - * Models a set current changes to be applied to a configuration/property provider. Such a set can be applied - * to any {@link PropertySource} instance. If the provider is mutable it may check the - * version given and applyChanges the changes to the provider/configuration, including triggering current regarding - * change events. - * <p> - * For appropriate conversion a {@code Function<String, Codec>} can be applied, which performs correct conversion, - * when changed values are set. This function enables connecting e.g. setters on a configuration template with - * the corresponding conversion logic, so the template calls are correctly converted back. - */ -public final class PropertyChangeSetBuilder { - /** - * The recorded changes. - */ - final SortedSet<String> addedKeys = new TreeSet<>(); - - /** - * The recorded changes. - */ - final SortedSet<String> updatedKeys = new TreeSet<>(); - - /** - * The recorded changes. - */ - final SortedSet<String> removedKeys = new TreeSet<>(); - - /** - * The underlying configuration/provider. - */ - String propertySourceName; - - /** - * Constructor. - * - * @param source the underlying configuration/provider, not null. - */ - private PropertyChangeSetBuilder(PropertySource source) { - this.propertySourceName = Objects.requireNonNull(source).getName(); - } - - /** - * Creates a new instance current this builder. - * - * @param source the underlying property provider/configuration, not null. - * @return the builder for chaining. - */ - public static PropertyChangeSetBuilder of(PropertySource source) { - return new PropertyChangeSetBuilder(source); - } - - - /** - * Marks the given key(s) as removed. - * - * @param keys the keys removed - * @return the builder for chaining. - */ - public PropertyChangeSetBuilder remove(String... keys) { - for (String removeKey : keys) { - this.removedKeys.add(removeKey); - } - return this; - } - - /** - * Marks the given key(s) as added. - * - * @param keys the keys added - * @return the builder for chaining. - */ - public PropertyChangeSetBuilder add(String... keys) { - for (String addKey : keys) { - this.addedKeys.add(addKey); - } - return this; - } - - /** - * Marks the given key(s) as updaed. - * - * @param keys the keys updated - * @return the builder for chaining. - */ - public PropertyChangeSetBuilder update(String... keys) { - for (String uptKey : keys) { - this.updatedKeys.add(uptKey); - } - return this; - } - - - /** - * Compares the two property maps and adds the corresponding updated/aded/removed keys to the builder. - * - * @param map1 the source map, not null. - * @param map2 the target map, not null. - * @return the builder for chaining. - */ - public PropertyChangeSetBuilder addChanges(Map<String,String> map1, Map<String,String> map2) { - List<PropertyChangeEvent> changes = new ArrayList<>(); - for (Map.Entry<String, String> en : map1.entrySet()) { - String val = map2.get(en.getKey()); - if (val==null) { - remove(en.getKey()); - } else if (!val.equals(en.getValue())) { - update(en.getKey()); - } - } - for (Map.Entry<String, String> en : map2.entrySet()) { - String val = map1.get(en.getKey()); - if (val==null) { - add(en.getKey()); - } - // update case already handled before! - } - return this; - } - - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "PropertyChangeEventBuilder [propertySourceName=" + propertySourceName + ", " + - ", added=" + addedKeys + ", updated=" + updatedKeys + ", removed=" + removedKeys +"]"; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigChangeCallbackMethod.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigChangeCallbackMethod.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigChangeCallbackMethod.java index 74ae68f..5f51741 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigChangeCallbackMethod.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigChangeCallbackMethod.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.inject.internal; -import org.apache.tamaya.inject.PropertyChangeSet; +import org.apache.tamaya.event.PropertyChangeSet; import java.lang.reflect.Method; import java.util.Optional; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java index 96b5f38..5e4db2a 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredField.java @@ -23,8 +23,8 @@ import java.util.List; import java.util.Objects; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.inject.ConfigRoot; import org.apache.tamaya.inject.ConfiguredProperty; -import org.apache.tamaya.inject.DefaultAreas; /** * Small class that contains and manages all information anc access to a configured field and a concrete instance current @@ -97,7 +97,7 @@ public class ConfiguredField { */ public boolean matchesKey(String key) { ConfiguredProperty prop = this.annotatedField.getAnnotation(ConfiguredProperty.class); - DefaultAreas areasAnnot = this.annotatedField.getDeclaringClass().getAnnotation(DefaultAreas.class); + ConfigRoot areasAnnot = this.annotatedField.getDeclaringClass().getAnnotation(ConfigRoot.class); List<String> keys = InjectionUtils.evaluateKeys(this.annotatedField, areasAnnot, prop); return keys.contains(key); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java index cc3ed05..7a41ac9 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredSetterMethod.java @@ -24,9 +24,9 @@ import java.util.Optional; import java.util.function.Consumer; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.inject.ConfigRoot; import org.apache.tamaya.inject.ConfiguredProperty; -import org.apache.tamaya.inject.DefaultAreas; -import org.apache.tamaya.inject.PropertyChangeSet; +import org.apache.tamaya.event.PropertyChangeSet; /** * Small class that contains and manages all information and access to a configured field and a concrete instance current @@ -107,7 +107,7 @@ public class ConfiguredSetterMethod { * @return true, if the key is referenced. */ public boolean matchesKey(String key) { - DefaultAreas areasAnnot = this.setterMethod.getDeclaringClass().getAnnotation(DefaultAreas.class); + ConfigRoot areasAnnot = this.setterMethod.getDeclaringClass().getAnnotation(ConfigRoot.class); ConfiguredProperty prop = this.setterMethod.getAnnotation(ConfiguredProperty.class); if (InjectionUtils.evaluateKeys(this.setterMethod, areasAnnot, prop).contains(key)) { return true; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java index ff155c4..08ee8c9 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java @@ -24,11 +24,11 @@ import java.util.*; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; +import org.apache.tamaya.inject.ConfigRoot; import org.apache.tamaya.inject.ConfiguredProperty; -import org.apache.tamaya.inject.DefaultAreas; import org.apache.tamaya.inject.NoConfig; -import org.apache.tamaya.inject.ObservesConfigChange; -import org.apache.tamaya.inject.PropertyChangeSet; +import org.apache.tamaya.event.ObservesConfigChange; +import org.apache.tamaya.event.PropertyChangeSet; import org.apache.tamaya.spi.PropertySource; /** @@ -187,7 +187,7 @@ public class ConfiguredType { } public static boolean isConfigured(Class type) { - if (type.getAnnotation(DefaultAreas.class) != null) { + if (type.getAnnotation(ConfigRoot.class) != null) { return true; } // if no class level annotation is there we might have field level annotations only http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java index f62838c..6f6794b 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java @@ -24,6 +24,7 @@ import javax.annotation.Priority; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; /** * Simple injector singleton that also registers instances configured using weak references. @@ -43,8 +44,7 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector return configuredTypes.computeIfAbsent(type, ConfiguredType::new); } - @Override - public void registerType(Class<?> type) { + void registerType(Class<?> type) { registerTypeInternal(type); } @@ -54,11 +54,16 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector * * @param instance the instance to be configured */ - public void configure(Object instance) { + public <T> T configure(T instance) { Class type = Objects.requireNonNull(instance).getClass(); ConfiguredType configuredType = registerTypeInternal(type); Objects.requireNonNull(configuredType).configure(instance); + return instance; } + @Override + public <T> Supplier<T> getConfiguredSupplier(Supplier<T> supplier) { + return () -> configure(supplier.get()); + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java index 3944cd6..bf4f77d 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java @@ -31,8 +31,8 @@ import java.util.logging.Logger; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; +import org.apache.tamaya.inject.ConfigRoot; import org.apache.tamaya.inject.ConfiguredProperty; -import org.apache.tamaya.inject.DefaultAreas; import org.apache.tamaya.inject.DefaultValue; import org.apache.tamaya.inject.WithLoadPolicy; import org.apache.tamaya.inject.WithPropertyConverter; @@ -72,7 +72,7 @@ final class InjectionUtils { * several keys to be looked up (in absolute or relative form). * @return the list current keys in order how they should be processed/looked up. */ - public static List<String> evaluateKeys(Member member, DefaultAreas areasAnnot, ConfiguredProperty propertyAnnotation) { + public static List<String> evaluateKeys(Member member, ConfigRoot areasAnnot, ConfiguredProperty propertyAnnotation) { List<String> keys = new ArrayList<>(Arrays.asList(propertyAnnotation.keys())); if (keys.isEmpty()) { keys.add(member.getName()); @@ -103,7 +103,7 @@ final class InjectionUtils { * @param areasAnnot the (optional) annotation definining areas to be looked up. * @return the list current keys in order how they should be processed/looked up. */ - public static List<String> evaluateKeys(Member member, DefaultAreas areasAnnot) { + public static List<String> evaluateKeys(Member member, ConfigRoot areasAnnot) { List<String> keys = new ArrayList<>(); String name = member.getName(); String mainKey; @@ -132,7 +132,7 @@ final class InjectionUtils { * @return the keys to be returned, or null. */ public static String getConfigValue(Method method) { - DefaultAreas areasAnnot = method.getDeclaringClass().getAnnotation(DefaultAreas.class); + ConfigRoot areasAnnot = method.getDeclaringClass().getAnnotation(ConfigRoot.class); WithLoadPolicy loadPolicy = Utils.getAnnotation(WithLoadPolicy.class, method, method.getDeclaringClass()); return getConfigValueInternal(method, areasAnnot, loadPolicy); } @@ -144,7 +144,7 @@ final class InjectionUtils { * @return the keys to be returned, or null. */ public static String getConfigValue(Field field) { - DefaultAreas areasAnnot = field.getDeclaringClass().getAnnotation(DefaultAreas.class); + ConfigRoot areasAnnot = field.getDeclaringClass().getAnnotation(ConfigRoot.class); WithLoadPolicy loadPolicy = Utils.getAnnotation(WithLoadPolicy.class, field, field.getDeclaringClass()); return getConfigValueInternal(field, areasAnnot, loadPolicy); } @@ -154,7 +154,7 @@ final class InjectionUtils { * * @return the keys to be returned, or null. */ - private static String getConfigValueInternal(AnnotatedElement element, DefaultAreas areasAnnot, WithLoadPolicy loadPolicy) { + private static String getConfigValueInternal(AnnotatedElement element, ConfigRoot areasAnnot, WithLoadPolicy loadPolicy) { ConfiguredProperty prop = element.getAnnotation(ConfiguredProperty.class); DefaultValue defaultAnnot = element.getAnnotation(DefaultValue.class); String configValue = null; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2acf67b7/modules/injection/src/main/java/org/apache/tamaya/inject/internal/WeakConfigListenerManager.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/WeakConfigListenerManager.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/WeakConfigListenerManager.java index dd8bf31..3ea8ed7 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/WeakConfigListenerManager.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/WeakConfigListenerManager.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.inject.internal; -import org.apache.tamaya.inject.PropertyChangeSet; +import org.apache.tamaya.event.PropertyChangeSet; import java.util.Map; import java.util.WeakHashMap;
