Author: adietish
Date: 2010-11-25 14:00:25 -0500 (Thu, 25 Nov 2010)
New Revision: 26950
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/AbstractDeltaCloudObjectRepository.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImagesRepository.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstancesRepository.java
Log:
[JBIDE-7694] revamped synchronization in DeltaCloud
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/AbstractDeltaCloudObjectRepository.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/AbstractDeltaCloudObjectRepository.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/AbstractDeltaCloudObjectRepository.java 2010-11-25
19:00:25 UTC (rev 26950)
@@ -0,0 +1,106 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+public abstract class AbstractDeltaCloudObjectRepository<E, C> {
+
+ private List<E> objects = new ArrayList<E>();
+ // TODO switch to readwrite lock to gain performance
+ private Lock lock = new ReentrantLock();
+ private Class<E> typeClass;
+
+ protected AbstractDeltaCloudObjectRepository(Class<E> typeClass) {
+ this.typeClass = typeClass;
+ }
+
+ public void add(E object) {
+ try {
+ lock();
+ objects.add(object);
+ } finally {
+ unlock();
+ }
+ }
+
+ public void add(Collection<E> objects) {
+ try {
+ lock();
+ for (E object : objects) {
+ objects.add(object);
+ }
+ } finally {
+ unlock();
+ }
+ }
+
+ public void clear() {
+ try {
+ lock();
+ objects.clear();
+ } finally {
+ unlock();
+ }
+ }
+
+ public E[] get() {
+ try {
+ lock();
+ @SuppressWarnings("unchecked")
+ E[] objectArray = (E[]) objects.toArray((E[]) Array.newInstance(typeClass,
objects.size()));
+ return objectArray;
+ } finally {
+ unlock();
+ }
+ }
+
+ protected E getById(C criteria) {
+ try {
+ lock();
+ E matchingObject = null;
+ for (E object : objects) {
+ // TODO: use comparator
+ if (matches(criteria, object)) {
+ matchingObject = object;
+ break;
+ }
+ }
+ return matchingObject;
+ } finally {
+ unlock();
+ }
+ }
+
+ protected abstract boolean matches(C criteria, E object);
+
+ public void remove(DeltaCloudInstance instance) {
+ try {
+ lock();
+ objects.remove(instance);
+ } finally {
+ unlock();
+ }
+ }
+
+ protected void lock() {
+ lock.lock();
+ }
+
+ protected void unlock() {
+ lock.unlock();
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/AbstractDeltaCloudObjectRepository.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImagesRepository.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImagesRepository.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImagesRepository.java 2010-11-25
19:00:25 UTC (rev 26950)
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.jboss.tools.deltacloud.core.client.Image;
+
+/**
+ * @author André Dietisheim
+ */
+public class DeltaCloudImagesRepository extends
AbstractDeltaCloudObjectRepository<DeltaCloudImage, String> {
+
+ public DeltaCloudImagesRepository() {
+ super(DeltaCloudImage.class);
+ }
+
+ public DeltaCloudImage add(Image image, DeltaCloud cloud) {
+ DeltaCloudImage deltaCloudImage = new DeltaCloudImage(image, cloud);
+ add(deltaCloudImage);
+ return deltaCloudImage;
+ }
+
+ public Collection<DeltaCloudImage> add(Collection<Image> imagesToAdd,
DeltaCloud cloud) {
+ List<DeltaCloudImage> deltaCloudImages = new ArrayList<DeltaCloudImage>();
+ for (Image image : imagesToAdd) {
+ DeltaCloudImage deltaCloudImage = add(image, cloud);
+ deltaCloudImages.add(deltaCloudImage);
+ }
+ return deltaCloudImages;
+ }
+
+ @Override
+ protected boolean matches(String id, DeltaCloudImage image) {
+ return image != null
+ && id.equals(image.getId());
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImagesRepository.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstancesRepository.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstancesRepository.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstancesRepository.java 2010-11-25
19:00:25 UTC (rev 26950)
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.jboss.tools.deltacloud.core.client.Instance;
+
+/**
+ * @author André Dietisheim
+ */
+public class DeltaCloudInstancesRepository extends
AbstractDeltaCloudObjectRepository<DeltaCloudInstance, String> {
+
+ /**
+ * Instantiates a new delta cloud instances.
+ */
+ public DeltaCloudInstancesRepository() {
+ super(DeltaCloudInstance.class);
+ }
+
+ /**
+ * Adds the.
+ *
+ * @param instance the instance
+ * @param cloud the cloud
+ * @return the delta cloud instance
+ */
+ public DeltaCloudInstance add(Instance instance, DeltaCloud cloud) {
+ DeltaCloudInstance deltaCloudInstance = new DeltaCloudInstance(cloud, instance);
+ add(deltaCloudInstance);
+ return deltaCloudInstance;
+ }
+
+ /**
+ * Adds the.
+ *
+ * @param instancesToAdd the instances to add
+ * @param cloud the cloud
+ * @return the collection
+ */
+ public Collection<DeltaCloudInstance> add(Collection<Instance>
instancesToAdd, DeltaCloud cloud) {
+ List<DeltaCloudInstance> deltaCloudInstances = new
ArrayList<DeltaCloudInstance>();
+ for (Instance instance : instancesToAdd) {
+ DeltaCloudInstance deltaCloudInstance = add(instance, cloud);
+ deltaCloudInstances.add(deltaCloudInstance);
+ }
+ return deltaCloudInstances;
+ }
+
+ /* (non-Javadoc)
+ * @see
org.jboss.tools.deltacloud.core.AbstractDeltaCloudObjectRepository#matches(java.lang.Object,
java.lang.Object)
+ */
+ @Override
+ protected boolean matches(String id, DeltaCloudInstance instance) {
+ return instance != null
+ && id.equals(instance.getId());
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstancesRepository.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain