From: Robert Varga Date: Wed, 19 Feb 2014 02:23:06 +0000 (+0100) Subject: BUG-432: Start fixing the Registration contract X-Git-Tag: release/helium~692 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=6f8f523f377b6fc4ae4748c193749573cc546899;p=yangtools.git BUG-432: Start fixing the Registration contract Registration should be a generic construct, not tied to a particular object. Unfortunately that requires API-level breakage which needs to be orchestrated with the users. As a first step introduce ObjectRegistration concept which is equivalent with the current Registration concept and gently nudge the users towards it. Change-Id: I05723cffcc41bfa2355b5e75d3e675c4809a772a Signed-off-by: Robert Varga --- diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/ModuleInfoBackedContext.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/ModuleInfoBackedContext.java index fb70c8f06c..687b3a3336 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/ModuleInfoBackedContext.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/ModuleInfoBackedContext.java @@ -15,7 +15,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.opendaylight.yangtools.concepts.AbstractObjectRegistration; -import org.opendaylight.yangtools.concepts.Registration; +import org.opendaylight.yangtools.concepts.ObjectRegistration; import org.opendaylight.yangtools.sal.binding.generator.util.ClassLoaderUtils; import org.opendaylight.yangtools.yang.binding.YangModuleInfo; import org.opendaylight.yangtools.yang.binding.util.BindingReflections; @@ -144,7 +144,7 @@ public class ModuleInfoBackedContext extends GeneratedClassLoadingStrategy // } } - public Registration registerModuleInfo(YangModuleInfo yangModuleInfo) { + public ObjectRegistration registerModuleInfo(YangModuleInfo yangModuleInfo) { YangModuleInfoRegistration registration = new YangModuleInfoRegistration(yangModuleInfo, this); resolveModuleInfo(yangModuleInfo); diff --git a/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractObjectRegistration.java b/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractObjectRegistration.java index fa7318973b..7c322ac3e2 100644 --- a/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractObjectRegistration.java +++ b/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractObjectRegistration.java @@ -13,7 +13,7 @@ package org.opendaylight.yangtools.concepts; * Invoking the close() method triggers unregistration of the state the method * installed. */ -public abstract class AbstractObjectRegistration extends AbstractRegistration implements Registration { +public abstract class AbstractObjectRegistration extends AbstractRegistration implements ObjectRegistration { private final T instance; protected AbstractObjectRegistration(final T instance) { diff --git a/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java b/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java index 646b462199..8fe2d8ff8b 100644 --- a/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java +++ b/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java @@ -14,7 +14,7 @@ import java.util.EventListener; * is interface provides the additional guarantee that the process of * unregistration cannot fail for predictable reasons. */ -public interface ListenerRegistration extends Registration { +public interface ListenerRegistration extends ObjectRegistration { /** * Unregister the listener. No events should be delivered to the listener * once this method returns successfully. While the interface contract diff --git a/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java b/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java new file mode 100644 index 0000000000..7725829fc2 --- /dev/null +++ b/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.concepts; + +/** + * Class representing a registration of an object. Such a registration is + * a proper resource and should be cleaned up when no longer required, so + * references to the object can be removed. This mechanism lies above the + * usual Java reference mechanism, as the entity where the object is + * registered may reside outside of the Java Virtual Machine. + */ +public interface ObjectRegistration extends Registration { + /** + * Return the object instance. + * + * @return Registered object. + */ + @Override + T getInstance(); + + /** + * Unregisters the object. This operation is required not to invoke + * blocking operations. Implementations which require interaction + * with outside world must provide guarantees that any work is done + * behind the scenes and the unregistration process looks as if it + * has already succeeded once this method returns. + */ + @Override + void close() throws Exception; +} diff --git a/concepts/src/main/java/org/opendaylight/yangtools/concepts/Registration.java b/concepts/src/main/java/org/opendaylight/yangtools/concepts/Registration.java index 051200c18d..8b2631f753 100644 --- a/concepts/src/main/java/org/opendaylight/yangtools/concepts/Registration.java +++ b/concepts/src/main/java/org/opendaylight/yangtools/concepts/Registration.java @@ -19,7 +19,15 @@ public interface Registration extends AutoCloseable { * Return the object instance. * * @return Registered object. + * + * @deprecated This class is currently deprecated pending its rework for + * general-purpose registration. This rework will remove the + * getInstance() method, such that the registration is no + * longer tied to a particular object. Please use + * {@link ObjectRegistration} to ensure your code does not + * break when that happens. */ + @Deprecated T getInstance(); /** diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java index 3d704931af..fe32493b12 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/URLSchemaContextResolver.java @@ -7,6 +7,8 @@ */ package org.opendaylight.yangtools.yang.parser.impl.util; +import static com.google.common.base.Preconditions.checkArgument; + import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -18,7 +20,7 @@ import java.util.concurrent.ConcurrentMap; import org.opendaylight.yangtools.concepts.AbstractObjectRegistration; import org.opendaylight.yangtools.concepts.Identifiable; -import org.opendaylight.yangtools.concepts.Registration; +import org.opendaylight.yangtools.concepts.ObjectRegistration; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider; @@ -31,8 +33,6 @@ import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; -import static com.google.common.base.Preconditions.checkArgument; - public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider { private static final Logger LOG = LoggerFactory.getLogger(URLSchemaContextResolver.class); @@ -41,7 +41,7 @@ public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider currentSchemaContext = Optional.absent(); - public Registration registerSource(URL source) { + public ObjectRegistration registerSource(URL source) { checkArgument(source != null, "Supplied source must not be null"); InputStream yangStream = getInputStream(source); YangModelDependencyInfo modelInfo = YangModelDependencyInfo.fromInputStream(yangStream); @@ -93,6 +93,7 @@ public class URLSchemaContextResolver implements AdvancedSchemaSourceProvider