This is an automated email from the ASF dual-hosted git repository. amichair pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/aries-rsa.git
commit 37191ba920ab0bed2216c77fe21422ae95efcd0a Author: Amichai Rothman <[email protected]> AuthorDate: Mon Mar 23 11:43:59 2026 +0200 ARIES-2212 Refactor ImportRegistrationImpl initialization to use one-time init instead of setters --- .../aries/rsa/core/ImportRegistrationImpl.java | 80 +++++++++++----------- .../aries/rsa/core/RemoteServiceAdminCore.java | 10 ++- .../aries/rsa/core/ImportRegistrationImplTest.java | 8 +-- 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/rsa/src/main/java/org/apache/aries/rsa/core/ImportRegistrationImpl.java b/rsa/src/main/java/org/apache/aries/rsa/core/ImportRegistrationImpl.java index f7151cce..13cbe900 100644 --- a/rsa/src/main/java/org/apache/aries/rsa/core/ImportRegistrationImpl.java +++ b/rsa/src/main/java/org/apache/aries/rsa/core/ImportRegistrationImpl.java @@ -51,23 +51,28 @@ public class ImportRegistrationImpl implements ImportRegistration, ImportReferen private EventProducer eventProducer; - public ImportRegistrationImpl(Throwable ex) { - exception = ex; - initParent(); - } - + /** + * Constructs a new import registration. + * + * @param endpoint the imported endpoint info + * @param closeHandler a callback function that will be invoked when the registration is closed + * @param eventProducer an event producer that will be invoked when the endpoint is updated + */ public ImportRegistrationImpl(EndpointDescription endpoint, CloseHandler closeHandler, EventProducer eventProducer) { this.endpoint = endpoint; this.closeHandler = closeHandler; this.eventProducer = eventProducer; - initParent(); + parent = this; + children = new ArrayList<>(1); } /** - * Creates a clone of the given parent instance. + * Constructs an import registration that is linked + * (shares state) with the given import registration. + * + * @param ir the import registration that this instance is linked to */ public ImportRegistrationImpl(ImportRegistration ir) { - // we always want a link to the parent... parent = ((ImportRegistrationImpl)ir).getParent(); exception = parent.getException(); endpoint = parent.getImportedEndpointDescription(); @@ -77,9 +82,34 @@ public class ImportRegistrationImpl implements ImportRegistration, ImportReferen parent.instanceAdded(this); } - private void initParent() { - parent = this; - children = new ArrayList<>(1); + /** + * Initializes this import registration in an error state. + * + * @param exception the exception that occurred during initialization + */ + public void init(Throwable exception) { + if (parent.exception != null || parent.clientServiceFactory != null || parent.importedService != null) { + throw new IllegalStateException("already initialized"); + } + this.exception = exception; + } + + /** + * Initializes this import registration in a successful state. + * + * @param csf the {@link ClientServiceFactory} which is the implementation + * of the locally registered service which provides proxies to the + * remote imported service. + * @param sreg the {@link ServiceRegistration} representing the locally + * registered {@link ClientServiceFactory} service which provides + * proxies to the remote imported service. + */ + public void init(ClientServiceFactory csf, ServiceRegistration sreg) { + if (parent.exception != null || parent.clientServiceFactory != null || parent.importedService != null) { + throw new IllegalStateException("already initialized"); + } + this.clientServiceFactory = csf; + this.importedService = sreg; } private void ensureParent() { @@ -189,38 +219,10 @@ public class ImportRegistrationImpl implements ImportRegistration, ImportReferen return exception; } - public void setException(Throwable ex) { - exception = ex; - } - private synchronized boolean isInvalid() { return exception != null || closed.get(); } - /** - * Sets the {@link ServiceRegistration} representing the locally - * registered {@link ClientServiceFactory} service which provides - * proxies to the remote imported service. It is set only on the parent. - * - * @param sreg the ServiceRegistration - */ - public void setImportedServiceRegistration(ServiceRegistration sreg) { - ensureParent(); - importedService = sreg; - } - - /** - * Sets the {@link ClientServiceFactory} which is the implementation - * of the locally registered service which provides proxies to the - * remote imported service. It is set only on the parent. - * - * @param csf the ClientServiceFactory - */ - public void setClientServiceFactory(ClientServiceFactory csf) { - ensureParent(); - clientServiceFactory = csf; - } - public ImportRegistrationImpl getParent() { return parent; } diff --git a/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java b/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java index a347d356..b05477ee 100644 --- a/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java +++ b/rsa/src/main/java/org/apache/aries/rsa/core/RemoteServiceAdminCore.java @@ -460,28 +460,26 @@ public class RemoteServiceAdminCore implements RemoteServiceAdmin { } protected ImportRegistrationImpl exposeServiceFactory(String[] interfaceNames, - EndpointDescription epd, + EndpointDescription endpoint, DistributionProvider handler) { - ImportRegistrationImpl imReg = new ImportRegistrationImpl(epd, closeHandler, eventProducer); + ImportRegistrationImpl imReg = new ImportRegistrationImpl(endpoint, closeHandler, eventProducer); try { - EndpointDescription endpoint = imReg.getImportedEndpointDescription(); Dictionary<String, Object> serviceProps = new Hashtable<>(endpoint.getProperties()); serviceProps.put(RemoteConstants.SERVICE_IMPORTED, true); serviceProps.remove(RemoteConstants.SERVICE_EXPORTED_INTERFACES); ClientServiceFactory csf = new ClientServiceFactory(endpoint, handler, imReg); - imReg.setClientServiceFactory(csf); // Export the factory using the api context as it has very few imports. // If the bundle publishing the factory does not import the service interface // package then the factory is visible for all consumers which we want. ServiceRegistration<?> csfReg = apictx.registerService(interfaceNames, csf, serviceProps); - imReg.setImportedServiceRegistration(csfReg); + imReg.init(csf, csfReg); } catch (Exception ex) { // Only logging at debug level as this might be written to the log at the TopologyManager LOG.debug("Can not proxy service with interfaces {}: {}", Arrays.toString(interfaceNames), ex.getMessage(), ex); - imReg.setException(ex); + imReg.init(ex); } return imReg; } diff --git a/rsa/src/test/java/org/apache/aries/rsa/core/ImportRegistrationImplTest.java b/rsa/src/test/java/org/apache/aries/rsa/core/ImportRegistrationImplTest.java index dd398a12..d388a390 100644 --- a/rsa/src/test/java/org/apache/aries/rsa/core/ImportRegistrationImplTest.java +++ b/rsa/src/test/java/org/apache/aries/rsa/core/ImportRegistrationImplTest.java @@ -35,7 +35,8 @@ public class ImportRegistrationImplTest { Exception e = c.createMock(Exception.class); c.replay(); - ImportRegistrationImpl i = new ImportRegistrationImpl(e); + ImportRegistrationImpl i = new ImportRegistrationImpl(null, null, null); + i.init(e); assertEquals(e, i.getException()); assertNull(i.getImportedEndpointDescription()); @@ -72,20 +73,19 @@ public class ImportRegistrationImplTest { c.replay(); ImportRegistrationImpl i1 = new ImportRegistrationImpl(endpoint, closeHandler, null); + i1.init(null, sr); ImportRegistrationImpl i2 = new ImportRegistrationImpl(i1); ImportRegistrationImpl i3 = new ImportRegistrationImpl(i2); try { - i2.setImportedServiceRegistration(sr); + i2.init(null, sr); fail("An exception should be thrown here !"); } catch (IllegalStateException e) { // must be thrown here } - i1.setImportedServiceRegistration(sr); - assertEquals(i1, i1.getParent()); assertEquals(i1, i2.getParent()); assertEquals(i1, i3.getParent());
