|
Lines 10-19
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jst.jsf.common.metadata.internal; |
11 |
package org.eclipse.jst.jsf.common.metadata.internal; |
| 12 |
|
12 |
|
|
|
13 |
import java.util.ArrayList; |
| 13 |
import java.util.HashMap; |
14 |
import java.util.HashMap; |
| 14 |
import java.util.Iterator; |
15 |
import java.util.Iterator; |
|
|
16 |
import java.util.List; |
| 15 |
import java.util.Map; |
17 |
import java.util.Map; |
|
|
18 |
import java.util.StringTokenizer; |
| 16 |
|
19 |
|
|
|
20 |
import org.eclipse.core.resources.IProject; |
| 21 |
import org.eclipse.core.resources.IResourceChangeEvent; |
| 22 |
import org.eclipse.core.resources.IResourceChangeListener; |
| 23 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 17 |
import org.eclipse.core.runtime.IStatus; |
24 |
import org.eclipse.core.runtime.IStatus; |
| 18 |
import org.eclipse.jst.jsf.common.JSFCommonPlugin; |
25 |
import org.eclipse.jst.jsf.common.JSFCommonPlugin; |
| 19 |
|
26 |
|
|
Lines 21-76
Link Here
|
| 21 |
* Creates instances of IMetaDataLocators and caches them so that there is only one instance of a particular locator |
28 |
* Creates instances of IMetaDataLocators and caches them so that there is only one instance of a particular locator |
| 22 |
* when client requests one. |
29 |
* when client requests one. |
| 23 |
*/ |
30 |
*/ |
| 24 |
public class MetaDataLocatorFactory { |
31 |
public class MetaDataLocatorFactory |
|
|
32 |
implements IResourceChangeListener { |
| 33 |
|
| 25 |
private static MetaDataLocatorFactory INSTANCE = null; |
34 |
private static MetaDataLocatorFactory INSTANCE = null; |
|
|
35 |
private Map<String, IMetaDataLocator> _locators; |
| 26 |
|
36 |
|
|
|
37 |
private static final boolean DEBUG = false; |
| 27 |
/** |
38 |
/** |
| 28 |
* @return singleton instance of the MetaDataLocatorFactory |
39 |
* @return singleton instance of the MetaDataLocatorFactory |
| 29 |
*/ |
40 |
*/ |
| 30 |
public synchronized static MetaDataLocatorFactory getInstance(){ |
41 |
public synchronized static MetaDataLocatorFactory getInstance(){ |
| 31 |
if (INSTANCE == null){ |
42 |
if (INSTANCE == null){ |
| 32 |
INSTANCE = new MetaDataLocatorFactory(); |
43 |
INSTANCE = new MetaDataLocatorFactory(); |
|
|
44 |
ResourcesPlugin.getWorkspace().addResourceChangeListener(INSTANCE, IResourceChangeEvent.PRE_CLOSE); |
| 33 |
} |
45 |
} |
| 34 |
return INSTANCE; |
46 |
return INSTANCE; |
| 35 |
} |
47 |
} |
| 36 |
|
48 |
|
| 37 |
private HashMap _locators; |
|
|
| 38 |
|
49 |
|
| 39 |
private Map getLocators() { |
50 |
|
|
|
51 |
private Map<String, IMetaDataLocator> getLocators() { |
| 40 |
if (_locators == null){ |
52 |
if (_locators == null){ |
| 41 |
_locators = new HashMap(); |
53 |
_locators = new HashMap<String, IMetaDataLocator>(); |
| 42 |
} |
54 |
} |
| 43 |
return _locators; |
55 |
return _locators; |
| 44 |
} |
56 |
} |
| 45 |
|
57 |
|
| 46 |
/** |
58 |
/** |
| 47 |
* @param locatorClassName |
59 |
* @param locatorClassName - may NOT be null |
| 48 |
* @param bundleId |
60 |
* @param bundleId - may NOT be null |
| 49 |
* @return IMetaDataLocator |
61 |
* @param project - may be null |
|
|
62 |
* @return shared instance of IMetaDataLocator |
| 63 |
* may return null if is IPathSensitiveMetaDataLocator and there is no project context |
| 50 |
*/ |
64 |
*/ |
| 51 |
public IMetaDataLocator getLocator(String locatorClassName, String bundleId){ |
65 |
public IMetaDataLocator getLocator(final String locatorClassName, final String bundleId, final IProject project){ |
|
|
66 |
final Class klass = JSFCommonPlugin.loadClass(locatorClassName, bundleId); |
| 52 |
String key = getKey(locatorClassName, bundleId); |
67 |
String key = getKey(locatorClassName, bundleId); |
| 53 |
IMetaDataLocator locator = (IMetaDataLocator)getLocators().get(key); |
68 |
IMetaDataLocator locator = null; |
| 54 |
if (locator == null){ |
69 |
try { |
| 55 |
Class klass = JSFCommonPlugin.loadClass(locatorClassName, bundleId); |
70 |
IMetaDataLocator tempLocator = (IMetaDataLocator)klass.newInstance(); |
| 56 |
try { |
71 |
if (tempLocator != null) { |
| 57 |
locator = (IMetaDataLocator)klass.newInstance(); |
72 |
if (tempLocator instanceof IPathSensitiveMetaDataLocator) { |
| 58 |
if (locator != null) { |
73 |
if (project == null) |
|
|
74 |
return null; |
| 75 |
|
| 76 |
key = getKey(locatorClassName, project.getName()); |
| 77 |
} |
| 78 |
|
| 79 |
locator = getLocators().get(key); |
| 80 |
if (locator == null) { |
| 81 |
locator = tempLocator; |
| 82 |
if (locator instanceof IPathSensitiveMetaDataLocator) |
| 83 |
((IPathSensitiveMetaDataLocator)locator).setProjectContext(project); |
| 84 |
|
| 85 |
if (DEBUG) |
| 86 |
System.out.println("Created locator: "+locator.toString()); //$NON-NLS-1$ |
| 87 |
|
| 59 |
getLocators().put(key, locator); |
88 |
getLocators().put(key, locator); |
| 60 |
locator.startLocating(); |
89 |
locator.startLocating(); |
| 61 |
} |
90 |
} |
| 62 |
} catch (InstantiationException e) { |
|
|
| 63 |
JSFCommonPlugin.log(IStatus.ERROR, "Could not instantiate IMetaDataLocator: "+key, e); |
| 64 |
} catch (IllegalAccessException e) { |
| 65 |
JSFCommonPlugin.log(IStatus.ERROR, "IllegalAccessException while creating IMetaDataLocator: "+key, e); |
| 66 |
} |
91 |
} |
|
|
92 |
} catch (InstantiationException e) { |
| 93 |
JSFCommonPlugin.log(IStatus.ERROR, "Could not instantiate IMetaDataLocator: "+key, e); //$NON-NLS-1$ |
| 94 |
} catch (IllegalAccessException e) { |
| 95 |
JSFCommonPlugin.log(IStatus.ERROR, "IllegalAccessException while creating IMetaDataLocator: "+key, e); //$NON-NLS-1$ |
| 67 |
} |
96 |
} |
|
|
97 |
|
| 68 |
return locator; |
98 |
return locator; |
| 69 |
} |
99 |
} |
| 70 |
|
100 |
|
| 71 |
private String getKey(String locatorClassName, String bundleId) { |
101 |
/** |
| 72 |
StringBuffer buf = new StringBuffer(bundleId); |
102 |
* @param locatorClassName |
| 73 |
buf.append(":"); |
103 |
* @param contextId - this may be the bundleID or the projectName if it is a path sensitive locator |
|
|
104 |
* @return key |
| 105 |
*/ |
| 106 |
private String getKey(final String locatorClassName, final String contextId) { |
| 107 |
StringBuffer buf = new StringBuffer(contextId); |
| 108 |
buf.append(":"); //$NON-NLS-1$ |
| 74 |
buf.append(locatorClassName); |
109 |
buf.append(locatorClassName); |
| 75 |
return buf.toString(); |
110 |
return buf.toString(); |
| 76 |
} |
111 |
} |
|
Lines 79-88
Link Here
|
| 79 |
* Stops and disposes all locators |
114 |
* Stops and disposes all locators |
| 80 |
*/ |
115 |
*/ |
| 81 |
public void dispose(){ |
116 |
public void dispose(){ |
|
|
117 |
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); |
| 82 |
for (Iterator it=getLocators().values().iterator();it.hasNext();){ |
118 |
for (Iterator it=getLocators().values().iterator();it.hasNext();){ |
| 83 |
IMetaDataLocator locator = (IMetaDataLocator)it.next(); |
119 |
IMetaDataLocator locator = (IMetaDataLocator)it.next(); |
| 84 |
locator.stopLocating(); |
120 |
locator.stopLocating(); |
| 85 |
} |
121 |
} |
| 86 |
getLocators().clear(); |
122 |
getLocators().clear(); |
| 87 |
} |
123 |
} |
|
|
124 |
|
| 125 |
/* |
| 126 |
* (non-Javadoc) |
| 127 |
* |
| 128 |
* @see |
| 129 |
* org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org |
| 130 |
* .eclipse.core.resources.IResourceChangeEvent) |
| 131 |
*/ |
| 132 |
public void resourceChanged(final IResourceChangeEvent event) { |
| 133 |
if (event.getType() == IResourceChangeEvent.PRE_CLOSE |
| 134 |
|| event.getType() == IResourceChangeEvent.PRE_DELETE) { |
| 135 |
// a project is closing - release and cleanup |
| 136 |
final IProject aProject = (IProject) event.getResource(); |
| 137 |
|
| 138 |
if (aProject != null) { |
| 139 |
List<String> locatorsToRemove = new ArrayList<String>(); |
| 140 |
for (Iterator it=getLocators().keySet().iterator();it.hasNext();){ |
| 141 |
String key = (String)it.next(); |
| 142 |
if (locatorIsForProject(key, aProject.getName())) { |
| 143 |
locatorsToRemove.add(key); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (! locatorsToRemove.isEmpty()) { |
| 148 |
for (String key : locatorsToRemove) { |
| 149 |
IMetaDataLocator locator = getLocators().get(key); |
| 150 |
|
| 151 |
if (DEBUG) |
| 152 |
System.out.println("Removed locator: "+locator.toString()); //$NON-NLS-1$ |
| 153 |
|
| 154 |
locator.stopLocating(); |
| 155 |
getLocators().remove(key); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private boolean locatorIsForProject(final String key, final String projectName) { |
| 163 |
StringTokenizer t = new StringTokenizer(key, ":"); //$NON-NLS-1$ |
| 164 |
String contextId = t.nextToken(); |
| 165 |
if (contextId.equals(projectName)) |
| 166 |
return true; |
| 167 |
return false; |
| 168 |
} |
| 88 |
} |
169 |
} |