Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2022-01-31 06:22:50 +0000
committerEike Stepper2022-01-31 06:22:50 +0000
commitc23959f69d7c13f97c9a2100dba5f75f992c207d (patch)
tree8a57f4d096583e9821e7f9f03fd0314a30babd4f /plugins
parentf3dfd3b33146ef61c739bfd8bcda3335fc009266 (diff)
downloadcdo-c23959f69d7c13f97c9a2100dba5f75f992c207d.tar.gz
cdo-c23959f69d7c13f97c9a2100dba5f75f992c207d.tar.xz
cdo-c23959f69d7c13f97c9a2100dba5f75f992c207d.zip
[578460] Provide a global session registry
https://bugs.eclipse.org/bugs/show_bug.cgi?id=578460
Diffstat (limited to 'plugins')
-rw-r--r--plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/session/CDOSessionRegistry.java72
-rw-r--r--plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/view/CDOViewRegistry.java7
-rw-r--r--plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionImpl.java4
-rw-r--r--plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionRegistryImpl.java109
-rw-r--r--plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/util/AbstractRegistry.java136
-rw-r--r--plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewImpl.java6
-rw-r--r--plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewRegistryImpl.java93
7 files changed, 351 insertions, 76 deletions
diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/session/CDOSessionRegistry.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/session/CDOSessionRegistry.java
new file mode 100644
index 0000000000..3d15b1e3e9
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/session/CDOSessionRegistry.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2012, 2019 Eike Stepper (Loehne, Germany) 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
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.emf.cdo.session;
+
+import org.eclipse.net4j.util.container.IContainer;
+
+/**
+ * A global registry of all open {@link CDOSession sessions}.
+ *
+ * @author Eike Stepper
+ * @since 4.17
+ * @noextend This interface is not intended to be extended by clients.
+ * @noimplement This interface is not intended to be implemented by clients.
+ */
+public interface CDOSessionRegistry extends IContainer<CDOSessionRegistry.Registration>
+{
+ /**
+ * The singleton instance of the {@link CDOSessionRegistry session registry}.
+ */
+ public static final CDOSessionRegistry INSTANCE = org.eclipse.emf.internal.cdo.session.CDOSessionRegistryImpl.INSTANCE;
+
+ /**
+ * A symbolic constant returned from {@link #getID(CDOSession)} if the session is not registered.
+ */
+ public static final int NOT_REGISTERED = 0;
+
+ /**
+ * Returns the {@link Registration#getID() IDs} of all registered {@link CDOSession sessions}.
+ */
+ public int[] getIDs();
+
+ /**
+ * Returns all registered {@link CDOSession sessions}.
+ */
+ public CDOSession[] getSessions();
+
+ /**
+ * Returns the {@link Registration#getID() ID} of the given {@link CDOSession session} if it is registered, {@value #NOT_REGISTERED} otherwise.
+ */
+ public int getID(CDOSession session);
+
+ /**
+ * Returns the {@link CDOSession session} with the given {@link Registration#getID() ID} if it is registered, <code>null</code> otherwise.
+ */
+ public CDOSession getSession(int id);
+
+ /**
+ * A bidirectional mapping between a registered {@link CDOSession session} and its assigned {@link #getID() ID}.
+ *
+ * @author Eike Stepper
+ */
+ public interface Registration
+ {
+ /**
+ * Returns the ID of this registration.
+ */
+ public int getID();
+
+ /**
+ * Returns the {@link CDOSession} of this registration.
+ */
+ public CDOSession getSession();
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/view/CDOViewRegistry.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/view/CDOViewRegistry.java
index 53a8d9c078..da13f869ea 100644
--- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/view/CDOViewRegistry.java
+++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/view/CDOViewRegistry.java
@@ -11,7 +11,6 @@
package org.eclipse.emf.cdo.view;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
-import org.eclipse.emf.cdo.view.CDOViewRegistry.Registration;
import org.eclipse.net4j.util.container.IContainer;
@@ -23,10 +22,10 @@ import org.eclipse.net4j.util.container.IContainer;
* @noextend This interface is not intended to be extended by clients.
* @noimplement This interface is not intended to be implemented by clients.
*/
-public interface CDOViewRegistry extends IContainer<Registration>
+public interface CDOViewRegistry extends IContainer<CDOViewRegistry.Registration>
{
/**
- * The {@link ClassLoader class loader}-wide singleton instance of the {@link CDOViewRegistry view registry}.
+ * The singleton instance of the {@link CDOViewRegistry view registry}.
*/
public static final CDOViewRegistry INSTANCE = org.eclipse.emf.internal.cdo.view.CDOViewRegistryImpl.INSTANCE;
@@ -56,7 +55,7 @@ public interface CDOViewRegistry extends IContainer<Registration>
public CDOView getView(int id);
/**
- * A bidirectional mapping between a registered {@link CDOView view} and its {@link ClassLoader class loader}-wide {@link #getID() ID}.
+ * A bidirectional mapping between a registered {@link CDOView view} and its {@link #getID() ID}.
*
* @author Eike Stepper
*/
diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionImpl.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionImpl.java
index 65759c4587..244b390d2d 100644
--- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionImpl.java
+++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionImpl.java
@@ -1587,6 +1587,8 @@ public abstract class CDOSessionImpl extends CDOTransactionContainerImpl impleme
checkState(sessionProtocol, "sessionProtocol"); //$NON-NLS-1$
checkState(remoteSessionManager, "remoteSessionManager"); //$NON-NLS-1$
+
+ CDOSessionRegistryImpl.INSTANCE.register(this);
}
protected void doActivateAfterBranchManager() throws Exception
@@ -1597,6 +1599,8 @@ public abstract class CDOSessionImpl extends CDOTransactionContainerImpl impleme
@Override
protected void doDeactivate() throws Exception
{
+ CDOSessionRegistryImpl.INSTANCE.deregister(this);
+
super.doDeactivate();
unhookSessionProtocol();
diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionRegistryImpl.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionRegistryImpl.java
new file mode 100644
index 0000000000..4e08235518
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/session/CDOSessionRegistryImpl.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2012, 2015, 2016, 2019 Eike Stepper (Loehne, Germany) 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
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.emf.internal.cdo.session;
+
+import org.eclipse.emf.cdo.session.CDOSession;
+import org.eclipse.emf.cdo.session.CDOSessionRegistry;
+
+import org.eclipse.emf.internal.cdo.util.AbstractRegistry;
+
+/**
+ * @author Eike Stepper
+ * @since 4.2
+ */
+public class CDOSessionRegistryImpl extends AbstractRegistry<CDOSession, CDOSessionRegistry.Registration> implements CDOSessionRegistry
+{
+ public static final CDOSessionRegistryImpl INSTANCE = new CDOSessionRegistryImpl();
+
+ public CDOSessionRegistryImpl()
+ {
+ }
+
+ @Override
+ public CDOSession[] getSessions()
+ {
+ return getRegisteredElements();
+ }
+
+ @Override
+ public CDOSession getSession(int id)
+ {
+ return getElement(id);
+ }
+
+ @Override
+ protected CDOSession[] newArray(int size)
+ {
+ return new CDOSession[size];
+ }
+
+ @Override
+ protected Registration[] newRegistrationArray(int size)
+ {
+ return new Registration[size];
+ }
+
+ @Override
+ protected Registration newRegistration(int id, CDOSession session)
+ {
+ return new RegistrationImpl(id, session);
+ }
+
+ @Override
+ protected int getRegisteredID(Registration registration)
+ {
+ return registration.getID();
+ }
+
+ @Override
+ protected CDOSession getRegisteredElement(Registration registration)
+ {
+ return registration.getSession();
+ }
+
+ void register(CDOSession session)
+ {
+ registerElement(session);
+ }
+
+ void deregister(CDOSession session)
+ {
+ deregisterElement(session);
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class RegistrationImpl implements Registration
+ {
+ private final int id;
+
+ private final CDOSession session;
+
+ public RegistrationImpl(int id, CDOSession session)
+ {
+ this.id = id;
+ this.session = session;
+ }
+
+ @Override
+ public int getID()
+ {
+ return id;
+ }
+
+ @Override
+ public CDOSession getSession()
+ {
+ return session;
+ }
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/util/AbstractRegistry.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/util/AbstractRegistry.java
new file mode 100644
index 0000000000..9ba36ef4bd
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/util/AbstractRegistry.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2022 Eike Stepper (Loehne, Germany) 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
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.emf.internal.cdo.util;
+
+import org.eclipse.net4j.util.container.Container;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Eike Stepper
+ */
+public abstract class AbstractRegistry<T, R> extends Container<R>
+{
+ public static final int NOT_REGISTERED = 0;
+
+ private final Map<Integer, R> registrationsByID = new HashMap<>();
+
+ private final Map<T, R> registrationsByElement = new HashMap<>();
+
+ private int lastID;
+
+ public AbstractRegistry()
+ {
+ }
+
+ public T[] getRegisteredElements()
+ {
+ R[] registrations = getElements();
+ T[] registeredElements = newArray(registrations.length);
+
+ for (int i = 0; i < registeredElements.length; i++)
+ {
+ registeredElements[i] = getRegisteredElement(registrations[i]);
+ }
+
+ return registeredElements;
+ }
+
+ @Override
+ public synchronized R[] getElements()
+ {
+ int size = registrationsByID.size();
+ R[] array = newRegistrationArray(size);
+ return registrationsByID.values().toArray(array);
+ }
+
+ public synchronized T getElement(int id)
+ {
+ R registration = registrationsByID.get(id);
+ if (registration != null)
+ {
+ return getRegisteredElement(registration);
+ }
+
+ return null;
+ }
+
+ public synchronized int[] getIDs()
+ {
+ int[] result = new int[registrationsByID.size()];
+
+ int i = 0;
+ for (Integer id : registrationsByID.keySet())
+ {
+ result[i++] = id;
+ }
+
+ return result;
+ }
+
+ public synchronized int getID(T element)
+ {
+ R registration = registrationsByElement.get(element);
+ if (registration != null)
+ {
+ return getRegisteredID(registration);
+ }
+
+ return NOT_REGISTERED;
+ }
+
+ protected final void registerElement(T element)
+ {
+ R registration;
+ synchronized (this)
+ {
+ if (registrationsByElement.containsKey(element))
+ {
+ return;
+ }
+
+ int id = ++lastID;
+ registration = newRegistration(id, element);
+
+ registrationsByID.put(id, registration);
+ registrationsByElement.put(element, registration);
+ }
+
+ fireElementAddedEvent(registration);
+ }
+
+ protected final void deregisterElement(T element)
+ {
+ R registration;
+ synchronized (this)
+ {
+ registration = registrationsByElement.remove(element);
+ if (registration != null)
+ {
+ int id = getRegisteredID(registration);
+ registrationsByID.remove(id);
+ }
+ }
+
+ fireElementRemovedEvent(registration);
+ }
+
+ protected abstract T[] newArray(int size);
+
+ protected abstract R[] newRegistrationArray(int size);
+
+ protected abstract R newRegistration(int id, T element);
+
+ protected abstract int getRegisteredID(R registration);
+
+ protected abstract T getRegisteredElement(R registration);
+}
diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewImpl.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewImpl.java
index 20021fb00d..336b0349f4 100644
--- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewImpl.java
+++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewImpl.java
@@ -1526,8 +1526,6 @@ public class CDOViewImpl extends AbstractCDOView
sessionProtocol.openView(viewID, isReadOnly(), this);
}
- CDOViewRegistryImpl.INSTANCE.register(this);
-
Runnable runnable = SessionUtil.getTestDelayInViewActivation();
if (runnable != null)
{
@@ -1568,12 +1566,15 @@ public class CDOViewImpl extends AbstractCDOView
throw ex;
}
}
+
+ CDOViewRegistryImpl.INSTANCE.register(this);
}
@Override
protected void doBeforeDeactivate() throws Exception
{
closing = true;
+ CDOViewRegistryImpl.INSTANCE.deregister(this);
// Detach the view set from the view.
InternalCDOViewSet viewSet = getViewSet();
@@ -1602,7 +1603,6 @@ public class CDOViewImpl extends AbstractCDOView
unitManager.deactivate();
commitInfoDistributor.deactivate();
- CDOViewRegistryImpl.INSTANCE.deregister(this);
LifecycleUtil.deactivate(invalidator, OMLogger.Level.WARN);
try
diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewRegistryImpl.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewRegistryImpl.java
index 56ebf7a56d..140eb8337f 100644
--- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewRegistryImpl.java
+++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewRegistryImpl.java
@@ -12,116 +12,71 @@ package org.eclipse.emf.internal.cdo.view;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewRegistry;
-import org.eclipse.emf.cdo.view.CDOViewRegistry.Registration;
-import org.eclipse.net4j.util.container.Container;
-
-import java.util.HashMap;
-import java.util.Map;
+import org.eclipse.emf.internal.cdo.util.AbstractRegistry;
/**
* @author Eike Stepper
* @since 4.2
- * @see CDOView
*/
-public class CDOViewRegistryImpl extends Container<Registration> implements CDOViewRegistry
+public class CDOViewRegistryImpl extends AbstractRegistry<CDOView, CDOViewRegistry.Registration> implements CDOViewRegistry
{
public static final CDOViewRegistryImpl INSTANCE = new CDOViewRegistryImpl();
- private final Map<Integer, Registration> ids = new HashMap<>();
-
- private final Map<CDOView, Registration> views = new HashMap<>();
-
- private int lastID;
-
public CDOViewRegistryImpl()
{
}
@Override
- public synchronized Registration[] getElements()
+ public CDOView[] getViews()
{
- return ids.values().toArray(new Registration[ids.size()]);
+ return getRegisteredElements();
}
@Override
- public synchronized int[] getIDs()
+ public CDOView getView(int id)
{
- int[] result = new int[ids.size()];
-
- int i = 0;
- for (Integer id : ids.keySet())
- {
- result[i++] = id;
- }
-
- return result;
+ return getElement(id);
}
@Override
- public synchronized CDOView[] getViews()
+ protected CDOView[] newArray(int size)
{
- return views.keySet().toArray(new CDOView[views.size()]);
+ return new CDOView[size];
}
@Override
- public synchronized int getID(CDOView view)
+ protected Registration[] newRegistrationArray(int size)
{
- Registration registration = views.get(view);
- if (registration != null)
- {
- return registration.getID();
- }
+ return new Registration[size];
+ }
- return NOT_REGISTERED;
+ @Override
+ protected Registration newRegistration(int id, CDOView view)
+ {
+ return new RegistrationImpl(id, view);
}
@Override
- public synchronized CDOView getView(int id)
+ protected int getRegisteredID(Registration registration)
{
- Registration registration = ids.get(id);
- if (registration != null)
- {
- return registration.getView();
- }
+ return registration.getID();
+ }
- return null;
+ @Override
+ protected CDOView getRegisteredElement(Registration registration)
+ {
+ return registration.getView();
}
void register(CDOView view)
{
- Registration registration;
- synchronized (this)
- {
- if (views.containsKey(view))
- {
- return;
- }
-
- int id = ++lastID;
- registration = new RegistrationImpl(id, view);
-
- ids.put(id, registration);
- views.put(view, registration);
- }
-
- fireElementAddedEvent(registration);
+ registerElement(view);
}
void deregister(CDOView view)
{
- Registration registration;
- synchronized (this)
- {
- registration = views.remove(view);
- if (registration != null)
- {
- int id = registration.getID();
- ids.remove(id);
- }
- }
-
- fireElementRemovedEvent(registration);
+ deregisterElement(view);
}
/**

Back to the top