blob: 5034c7ae89a0fb7d30f5dd785137f04c390a7d6b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*******************************************************************************
* Copyright (c) 2005, 2007 IBM Corporation 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*
* Created on Oct 26, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.eclipse.wst.common.internal.emf;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.ILock;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.wst.common.internal.emf.utilities.ResourceIsLoadingAdapter;
/**
* The ResourceSynchronizedIsLoadingAdapter is used to synchronize the loading
* of EMF resources. This is the Eclipse version of ResourceIsLoadingAdapter,
* and uses the Eclipse ILock technology to acquire a semaphore until the
* Resource is loaded. the waitForResourceToLoad() method will pause until
* either (a) the Resource has loaded, the Adapter is notified, and the
* semaphore is released or (b) the DELAY timeout is exceeded, which prevents
* full deadlock.
*
* @author mdelder
*/
public class ResourceSynchronizedIsLoadingAdapter extends ResourceIsLoadingAdapter {
private final ILock loadingLock;
/**
* The delay is default to 5 minutes. This is the upward threshhold. The
* lock will wait up to DELAY milliseconds to acquire the lock, and bail
* if not. It does not mean that it will wait DELAY milliseconds always.
* In general, the wait should be almost instanteous -- just as long as
* document loading remains speedy.
*/
private static final long DELAY = 300000;
public ResourceSynchronizedIsLoadingAdapter() {
loadingLock = Platform.getJobManager().newLock();
if (loadingLock != null)
loadingLock.acquire();
}
/*
* (non-Javadoc)
*
* @see com.ibm.wtp.internal.emf.utilities.ResourceIsLoadingAdapter#waitForResourceToLoad()
*/
public void waitForResourceToLoad() {
if (loadingLock == null)
return;
boolean lockAcquired = false;
try {
if (loadingLock != null)
if (!(lockAcquired = loadingLock.acquire(DELAY)))
logWarning();
}
catch (InterruptedException e) {
// ignore, just continue
}
finally {
if (lockAcquired)
loadingLock.release();
}
}
/**
*
*/
private void logWarning() {
Notifier target = getTarget();
if (target == null || !(target instanceof Resource)) {
Resource resource = (Resource) target;
System.err.println("[WARNING] Could not acquire Semaphore Lock for Resource: \"" + resource.getURI() + "\" in " + getClass());
}
}
/*
* (non-Javadoc)
*
* @see com.ibm.wtp.internal.emf.utilities.ResourceIsLoadingAdapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
*/
public void notifyChanged(Notification notification) {
if (notification.getNotifier() != null) {
// listen for the remove of the loading adapter
if (isSetLoadedResourceNotification(notification)) {
if (loadingLock != null)
loadingLock.release();
removeIsLoadingSupport();
}
}
}
/*
* (non-Javadoc)
*
* @see com.ibm.wtp.internal.emf.utilities.ResourceIsLoadingAdapter#forceRelease()
*/
public void forceRelease() {
if (loadingLock != null && loadingLock.getDepth() > 0)
loadingLock.release();
}
}
|