Skip to content

Commit

Permalink
Configure ManagedAddressSpaceFragmentWithLifecycle's composite in con…
Browse files Browse the repository at this point in the history
…structor
  • Loading branch information
kevinherron committed Sep 8, 2020
1 parent f6e16e0 commit 166209c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,93 @@
import org.eclipse.milo.opcua.sdk.server.UaNodeManager;

/**
* A {@link ManagedAddressSpace} that has a {@link Lifecycle} associated with it.
* A {@link ManagedAddressSpace} fragment that has a {@link Lifecycle} associated with it.
* <p>
* This AddressSpace and its {@link UaNodeManager} will be registered with the server on startup and unregistered on
* shutdown.
* This fragment will be registered with the provided composite and its {@link UaNodeManager} will
* be registered with the server on startup and unregistered on shutdown.
* <p>
* Subclasses can register additional startup/shutdown tasks with the {@link LifecycleManager} obtained from
* {@link #getLifecycleManager()}.
* Subclasses can register additional startup/shutdown tasks with the {@link LifecycleManager}
* obtained from {@link #getLifecycleManager()}.
*/
public abstract class ManagedAddressSpaceFragmentWithLifecycle
extends ManagedAddressSpaceFragment implements Lifecycle {

private final LifecycleManager lifecycleManager = new LifecycleManager();

private final AddressSpaceComposite composite;

/**
* Create a managed fragment using the server's {@link AddressSpaceManager} as the composite.
*
* @param server the {@link OpcUaServer} instance.
*/
public ManagedAddressSpaceFragmentWithLifecycle(OpcUaServer server) {
this(server, server.getAddressSpaceManager());
}

/**
* Create a managed fragment using {@code composite}.
*
* @param server the {@link OpcUaServer} instance.
* @param composite the {@link AddressSpaceComposite} this fragment is part of.
*/
public ManagedAddressSpaceFragmentWithLifecycle(OpcUaServer server, AddressSpaceComposite composite) {
super(server);

this.composite = composite;

getLifecycleManager().addLifecycle(new Lifecycle() {
@Override
public void startup() {
registerAddressSpace(ManagedAddressSpaceFragmentWithLifecycle.this);
composite.register(ManagedAddressSpaceFragmentWithLifecycle.this);
registerNodeManager(getNodeManager());
}

@Override
public void shutdown() {
unregisterAddressSpace(ManagedAddressSpaceFragmentWithLifecycle.this);
composite.unregister(ManagedAddressSpaceFragmentWithLifecycle.this);
unregisterNodeManager(getNodeManager());
}
});
}

/**
* Create a managed fragment using the server's {@link AddressSpaceManager} as the composite.
*
* @param server the {@link OpcUaServer} instance.
* @param nodeManager the {@link UaNodeManager} to manage nodes with.
*/
public ManagedAddressSpaceFragmentWithLifecycle(OpcUaServer server, UaNodeManager nodeManager) {
this(server, nodeManager, server.getAddressSpaceManager());
}

/**
* Create a managed fragment using {@code composite}.
*
* @param server the {@link OpcUaServer} instance.
* @param nodeManager the {@link UaNodeManager} to manage nodes with.
* @param composite the {@link AddressSpaceComposite} this fragment is part of.
*/
public ManagedAddressSpaceFragmentWithLifecycle(
OpcUaServer server,
UaNodeManager nodeManager,
AddressSpaceComposite composite
) {

super(server, nodeManager);

this.composite = composite;

getLifecycleManager().addLifecycle(new Lifecycle() {
@Override
public void startup() {
registerAddressSpace(ManagedAddressSpaceFragmentWithLifecycle.this);
composite.register(ManagedAddressSpaceFragmentWithLifecycle.this);
registerNodeManager(getNodeManager());
}

@Override
public void shutdown() {
unregisterAddressSpace(ManagedAddressSpaceFragmentWithLifecycle.this);
composite.unregister(ManagedAddressSpaceFragmentWithLifecycle.this);
unregisterNodeManager(getNodeManager());
}
});
Expand All @@ -76,40 +119,21 @@ public final void shutdown() {
}

/**
* Get the {@link LifecycleManager} for this {@link ManagedAddressSpaceFragmentWithLifecycle}.
*
* @return the {@link LifecycleManager} for this {@link ManagedAddressSpaceFragmentWithLifecycle}.
*/
protected LifecycleManager getLifecycleManager() {
return lifecycleManager;
}

/**
* Register this {@link ManagedAddressSpace} with its managing entity.
* <p>
* The default implementation registers it with the Server's {@link AddressSpaceManager}.
* <p>
* ManagedAddressSpaces that belong to a {@link AddressSpaceComposite} other than Server's AddressSpaceManager
* should override this to register with that composite.
* Get the {@link AddressSpaceComposite} this fragment registers with.
*
* @param addressSpace the {@link AddressSpace} to register.
* @return the {@link AddressSpaceComposite} this fragment registers with.
*/
protected void registerAddressSpace(AddressSpaceFragment addressSpace) {
getServer().getAddressSpaceManager().register(addressSpace);
public AddressSpaceComposite getComposite() {
return composite;
}

/**
* Unregister this {@link ManagedAddressSpace} with its managing entity.
* <p>
* The default implementation unregisters it with the Server's {@link AddressSpaceManager}.
* <p>
* ManagedAddressSpaces that belong to a {@link AddressSpaceComposite} other than Server's AddressSpaceManager
* should override this to unregister with that composite.
* Get the {@link LifecycleManager} for this {@link ManagedAddressSpaceFragmentWithLifecycle}.
*
* @param addressSpace the {@link AddressSpace} to unregister.
* @return the {@link LifecycleManager} for this {@link ManagedAddressSpaceFragmentWithLifecycle}.
*/
protected void unregisterAddressSpace(AddressSpaceFragment addressSpace) {
getServer().getAddressSpaceManager().unregister(addressSpace);
protected LifecycleManager getLifecycleManager() {
return lifecycleManager;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.milo.opcua.sdk.server.OpcUaServer;
import org.eclipse.milo.opcua.sdk.server.api.AddressSpaceComposite;
import org.eclipse.milo.opcua.sdk.server.api.AddressSpaceFilter;
import org.eclipse.milo.opcua.sdk.server.api.AddressSpaceFragment;
import org.eclipse.milo.opcua.sdk.server.api.DataItem;
import org.eclipse.milo.opcua.sdk.server.api.ManagedAddressSpaceFragmentWithLifecycle;
import org.eclipse.milo.opcua.sdk.server.api.MonitoredItem;
Expand Down Expand Up @@ -91,7 +90,7 @@ private class DiagnosticsFragment extends ManagedAddressSpaceFragmentWithLifecyc
private final SubscriptionModel subscriptionModel;

public DiagnosticsFragment(OpcUaServer server) {
super(server);
super(server, ServerNamespace.this);

subscriptionModel = new SubscriptionModel(server, this);

Expand All @@ -118,16 +117,6 @@ public AddressSpaceFilter getFilter() {
return filter;
}

@Override
protected void registerAddressSpace(AddressSpaceFragment addressSpace) {
ServerNamespace.this.registerFirst(addressSpace);
}

@Override
protected void unregisterAddressSpace(AddressSpaceFragment addressSpace) {
ServerNamespace.this.unregister(addressSpace);
}

@Override
public void onDataItemsCreated(List<DataItem> dataItems) {
subscriptionModel.onDataItemsCreated(dataItems);
Expand Down Expand Up @@ -158,7 +147,7 @@ private class VendorServerInfoFragment extends ManagedAddressSpaceFragmentWithLi
private final SubscriptionModel subscriptionModel;

public VendorServerInfoFragment(OpcUaServer server) {
super(server);
super(server, ServerNamespace.this);

subscriptionModel = new SubscriptionModel(server, this);

Expand All @@ -172,16 +161,6 @@ public AddressSpaceFilter getFilter() {
return filter;
}

@Override
protected void registerAddressSpace(AddressSpaceFragment addressSpace) {
ServerNamespace.this.registerFirst(addressSpace);
}

@Override
protected void unregisterAddressSpace(AddressSpaceFragment addressSpace) {
ServerNamespace.this.unregister(addressSpace);
}

@Override
public void onDataItemsCreated(List<DataItem> dataItems) {
subscriptionModel.onDataItemsCreated(dataItems);
Expand Down

0 comments on commit 166209c

Please sign in to comment.