summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-03-12 16:08:08 (EDT)
committerEike Stepper2007-03-12 16:08:08 (EDT)
commit3f0f713cc5ec0533cb2a788b99a6f588e0896f10 (patch)
treec92596e1aa4103960f1dd058299f56cf584ed6c6
parent4b355ffc2ef80bfb9bcc9859d94c2ef504c28365 (diff)
downloadcdo-3f0f713cc5ec0533cb2a788b99a6f588e0896f10.zip
cdo-3f0f713cc5ec0533cb2a788b99a6f588e0896f10.tar.gz
cdo-3f0f713cc5ec0533cb2a788b99a6f588e0896f10.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/util/container/ManagedContainer.java367
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IContainer.java2
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IManagedContainer.java47
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/lifecycle/LifecycleUtil.java2
4 files changed, 417 insertions, 1 deletions
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/util/container/ManagedContainer.java b/plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/util/container/ManagedContainer.java
new file mode 100644
index 0000000..68c1ea6
--- /dev/null
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/internal/net4j/util/container/ManagedContainer.java
@@ -0,0 +1,367 @@
+/***************************************************************************
+ * Copyright (c) 2004-2007 Eike Stepper, Germany.
+ * 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.internal.net4j.util.container;
+
+import org.eclipse.net4j.util.ObjectUtil;
+import org.eclipse.net4j.util.container.IContainerDelta;
+import org.eclipse.net4j.util.container.IManagedContainer;
+import org.eclipse.net4j.util.factory.IFactory;
+import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
+import org.eclipse.net4j.util.registry.IRegistry;
+
+import org.eclipse.internal.net4j.util.event.Notifier;
+import org.eclipse.internal.net4j.util.registry.HashMapRegistry;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map.Entry;
+
+/**
+ * @author Eike Stepper
+ */
+public class ManagedContainer extends Notifier implements IManagedContainer
+{
+ private IRegistry<FactoryKey, IFactory> factoryRegistry = new HashMapRegistry();
+
+ private IRegistry<ElementKey, Object> elementRegistry = new HashMapRegistry();
+
+ private long lastElementID;
+
+ public IFactory[] getFactories()
+ {
+ return factoryRegistry.values().toArray(new IFactory[factoryRegistry.size()]);
+ }
+
+ public IFactory[] getFactories(String productGroup)
+ {
+ List<IFactory> result = new ArrayList();
+ for (IFactory factory : factoryRegistry.values())
+ {
+ if (ObjectUtil.equals(factory.getProductGroup(), productGroup))
+ {
+ result.add(factory);
+ }
+ }
+
+ return result.toArray(new IFactory[result.size()]);
+ }
+
+ public IFactory getFactory(String productGroup, String factoryType)
+ {
+ FactoryKey key = new FactoryKey(productGroup, factoryType);
+ return factoryRegistry.get(key);
+ }
+
+ public void registerFactory(IFactory factory)
+ {
+ FactoryKey key = new FactoryKey(factory.getProductGroup(), factory.getType());
+ factoryRegistry.put(key, factory);
+ }
+
+ public void deregisterFactory(IFactory factory)
+ {
+ FactoryKey key = new FactoryKey(factory.getProductGroup(), factory.getType());
+ factoryRegistry.remove(key);
+ }
+
+ public boolean isEmpty()
+ {
+ return elementRegistry.isEmpty();
+ }
+
+ public Object[] getElements()
+ {
+ return elementRegistry.values().toArray();
+ }
+
+ public Object[] getElements(String productGroup)
+ {
+ List result = new ArrayList();
+ for (Entry<ElementKey, Object> entry : elementRegistry.entrySet())
+ {
+ ElementKey key = entry.getKey();
+ if (ObjectUtil.equals(key.getProductGroup(), productGroup))
+ {
+ result.add(entry.getValue());
+ }
+ }
+
+ return result.toArray();
+ }
+
+ public Object[] getElements(String productGroup, String factoryType)
+ {
+ List result = new ArrayList();
+ for (Entry<ElementKey, Object> entry : elementRegistry.entrySet())
+ {
+ ElementKey key = entry.getKey();
+ if (ObjectUtil.equals(key.getProductGroup(), productGroup)
+ && ObjectUtil.equals(key.getFactoryType(), factoryType))
+ {
+ result.add(entry.getValue());
+ }
+ }
+
+ return result.toArray();
+ }
+
+ public Object getElement(String productGroup, String factoryType, String description)
+ {
+ synchronized (elementRegistry)
+ {
+ ElementKey key = new ElementKey(productGroup, factoryType, description);
+ Object element = elementRegistry.get(key);
+ if (element == null)
+ {
+ IFactory factory = getFactory(productGroup, factoryType);
+ if (factory != null)
+ {
+ element = factory.create(description);
+ if (element != null)
+ {
+ key.setID(++lastElementID);
+ elementRegistry.put(key, element);
+ fireEvent(new SingleDeltaContainerEvent(this, element, IContainerDelta.Kind.ADDED));
+ }
+ }
+ }
+
+ return element;
+ }
+ }
+
+ public Object removeElement(String productGroup, String factoryType, String description)
+ {
+ ElementKey key = new ElementKey(productGroup, factoryType, description);
+ Object element = elementRegistry.remove(key);
+ if (element != null)
+ {
+ fireEvent(new SingleDeltaContainerEvent(this, element, IContainerDelta.Kind.REMOVED));
+ }
+
+ return element;
+ }
+
+ public void clearElements()
+ {
+ synchronized (elementRegistry)
+ {
+ if (!elementRegistry.isEmpty())
+ {
+ ContainerEvent event = new ContainerEvent(this);
+ for (Object element : elementRegistry.values())
+ {
+ event.addDelta(element, IContainerDelta.Kind.REMOVED);
+ }
+
+ elementRegistry.clear();
+ fireEvent(event);
+ }
+ }
+ }
+
+ public void loadElements(InputStream stream) throws IOException
+ {
+ synchronized (elementRegistry)
+ {
+ clearElements();
+ ObjectInputStream ois = new ObjectInputStream(stream);
+ int size = ois.readInt();
+ for (int i = 0; i < size; i++)
+ {
+ try
+ {
+ ElementKey key = (ElementKey)ois.readObject();
+ Object element = getElement(key.getProductGroup(), key.getFactoryType(), key.getDescription());
+
+ boolean active = ois.readBoolean();
+ if (active)
+ {
+ LifecycleUtil.activate(element);
+ }
+ }
+ catch (ClassNotFoundException cannotHappen)
+ {
+ }
+ }
+ }
+ }
+
+ public void saveElements(OutputStream stream) throws IOException
+ {
+ synchronized (elementRegistry)
+ {
+ ObjectOutputStream oos = new ObjectOutputStream(stream);
+ List<Entry<ElementKey, Object>> entries = new ArrayList(elementRegistry.entrySet());
+ Collections.sort(entries, new EntryComparator());
+
+ oos.writeInt(entries.size());
+ for (Entry<ElementKey, Object> entry : entries)
+ {
+ oos.writeObject(entry.getKey());
+ oos.writeBoolean(LifecycleUtil.isActive(entry.getValue()));
+ }
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class FactoryKey
+ {
+ private String productGroup;
+
+ private String factoryType;
+
+ public FactoryKey(String productGroup, String factoryType)
+ {
+ this.productGroup = productGroup;
+ this.factoryType = factoryType;
+ }
+
+ public String getProductGroup()
+ {
+ return productGroup;
+ }
+
+ public String getFactoryType()
+ {
+ return factoryType;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj instanceof FactoryKey)
+ {
+ FactoryKey key = (FactoryKey)obj;
+ return ObjectUtil.equals(productGroup, key.productGroup) && ObjectUtil.equals(factoryType, key.factoryType);
+ }
+
+ return false;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return ObjectUtil.hashCode(productGroup) ^ ObjectUtil.hashCode(factoryType);
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class ElementKey implements Serializable, Comparable
+ {
+ private static final long serialVersionUID = 1L;
+
+ private long id;
+
+ private String productGroup;
+
+ private String factoryType;
+
+ private String description;
+
+ public ElementKey(String productGroup, String factoryType, String description)
+ {
+ this.productGroup = productGroup;
+ this.factoryType = factoryType;
+ this.description = description;
+ }
+
+ public long getID()
+ {
+ return id;
+ }
+
+ public void setID(long id)
+ {
+ this.id = id;
+ }
+
+ public String getProductGroup()
+ {
+ return productGroup;
+ }
+
+ public String getFactoryType()
+ {
+ return factoryType;
+ }
+
+ public String getDescription()
+ {
+ return description;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj instanceof ElementKey)
+ {
+ ElementKey key = (ElementKey)obj;
+ return ObjectUtil.equals(productGroup, key.productGroup) && ObjectUtil.equals(factoryType, key.factoryType)
+ && ObjectUtil.equals(description, key.description);
+ }
+
+ return false;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return ObjectUtil.hashCode(productGroup) ^ ObjectUtil.hashCode(factoryType) ^ ObjectUtil.hashCode(description);
+ }
+
+ public int compareTo(Object o)
+ {
+ if (o instanceof ElementKey)
+ {
+ ElementKey key = (ElementKey)o;
+ if (id < key.id)
+ {
+ return -1;
+ }
+
+ if (id > key.id)
+ {
+ return 1;
+ }
+
+ return 0;
+ }
+
+ return 0;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class EntryComparator implements Comparator
+ {
+ public int compare(Object o1, Object o2)
+ {
+ Entry<ElementKey, Object> entry1 = (Entry<ElementKey, Object>)o1;
+ Entry<ElementKey, Object> entry2 = (Entry<ElementKey, Object>)o2;
+ return entry1.getKey().compareTo(entry2.getKey());
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IContainer.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IContainer.java
index 3a04e68..758b7d5 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IContainer.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IContainer.java
@@ -17,5 +17,7 @@ import org.eclipse.net4j.util.event.INotifier;
*/
public interface IContainer<E> extends INotifier
{
+ public boolean isEmpty();
+
public E[] getElements();
}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IManagedContainer.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IManagedContainer.java
new file mode 100644
index 0000000..90ccb55
--- /dev/null
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/container/IManagedContainer.java
@@ -0,0 +1,47 @@
+/***************************************************************************
+ * Copyright (c) 2004-2007 Eike Stepper, Germany.
+ * 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.net4j.util.container;
+
+import org.eclipse.net4j.util.factory.IFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * @author Eike Stepper
+ */
+public interface IManagedContainer extends IContainer
+{
+ public IFactory[] getFactories();
+
+ public IFactory[] getFactories(String productGroup);
+
+ public IFactory getFactory(String productGroup, String factoryType);
+
+ public void registerFactory(IFactory factory);
+
+ public void deregisterFactory(IFactory factory);
+
+ public Object[] getElements(String productGroup);
+
+ public Object[] getElements(String productGroup, String factoryType);
+
+ public Object getElement(String productGroup, String factoryType, String description);
+
+ public Object removeElement(String productGroup, String factoryType, String description);
+
+ public void clearElements();
+
+ public void loadElements(InputStream stream) throws IOException;
+
+ public void saveElements(OutputStream stream) throws IOException;
+}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/lifecycle/LifecycleUtil.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/lifecycle/LifecycleUtil.java
index 43d724e..172d0d0 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/lifecycle/LifecycleUtil.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/lifecycle/LifecycleUtil.java
@@ -70,7 +70,7 @@ public final class LifecycleUtil
throw new LifecycleException(ex);
}
}
- else if (useAnnotation)
+ else if (object != null && useAnnotation)
{
invokeAnnotation(object, Activator.class);
}