|
Lines 10-18
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.wst.common.componentcore.internal.impl; |
11 |
package org.eclipse.wst.common.componentcore.internal.impl; |
| 12 |
|
12 |
|
|
|
13 |
import org.eclipse.core.runtime.Assert; |
| 14 |
import org.eclipse.core.runtime.CoreException; |
| 15 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 16 |
import org.eclipse.core.runtime.ISafeRunnable; |
| 17 |
import org.eclipse.core.runtime.IStatus; |
| 18 |
import org.eclipse.core.runtime.Platform; |
| 19 |
import org.eclipse.core.runtime.SafeRunner; |
| 13 |
import org.eclipse.emf.common.util.URI; |
20 |
import org.eclipse.emf.common.util.URI; |
| 14 |
import org.eclipse.emf.ecore.resource.Resource; |
21 |
import org.eclipse.emf.ecore.resource.Resource; |
|
|
22 |
import org.eclipse.jem.util.RegistryReader; |
| 23 |
import org.eclipse.wst.common.componentcore.internal.ModulecorePlugin; |
| 15 |
import org.eclipse.wst.common.internal.emf.resource.FileNameResourceFactoryRegistry; |
24 |
import org.eclipse.wst.common.internal.emf.resource.FileNameResourceFactoryRegistry; |
|
|
25 |
import org.eclipse.wst.common.internal.emf.resource.ResourceFactoryDescriptor; |
| 26 |
import org.eclipse.wst.common.internal.emf.utilities.DefaultOverridableResourceFactoryRegistry; |
| 16 |
|
27 |
|
| 17 |
/** |
28 |
/** |
| 18 |
* <p> |
29 |
* <p> |
|
Lines 22-31
Link Here
|
| 22 |
public class WTPResourceFactoryRegistry extends FileNameResourceFactoryRegistry { |
33 |
public class WTPResourceFactoryRegistry extends FileNameResourceFactoryRegistry { |
| 23 |
|
34 |
|
| 24 |
public static final WTPResourceFactoryRegistry INSTANCE = new WTPResourceFactoryRegistry(); |
35 |
public static final WTPResourceFactoryRegistry INSTANCE = new WTPResourceFactoryRegistry(); |
|
|
36 |
|
| 37 |
private final static boolean LOG_WARNINGS = false; |
| 38 |
|
| 39 |
|
| 40 |
private WTPResourceFactoryRegistry() { |
| 41 |
new ResourceFactoryRegistryReader().readRegistry(); |
| 42 |
} |
| 25 |
|
43 |
|
| 26 |
public Resource.Factory delegatedGetFactory(URI uri) { |
44 |
public Resource.Factory delegatedGetFactory(URI uri) { |
| 27 |
if (WTPResourceFactoryRegistry.INSTANCE == this) |
45 |
if (WTPResourceFactoryRegistry.INSTANCE == this) |
| 28 |
return super.delegatedGetFactory(uri); |
46 |
return super.delegatedGetFactory(uri); |
| 29 |
return WTPResourceFactoryRegistry.INSTANCE.getFactory(uri); |
47 |
return WTPResourceFactoryRegistry.INSTANCE.getFactory(uri); |
|
|
48 |
} |
| 49 |
|
| 50 |
public synchronized Resource.Factory getFactory(URI uri) { |
| 51 |
|
| 52 |
Resource.Factory resourceFactory = null; |
| 53 |
if(uri != null && uri.lastSegment() != null) { |
| 54 |
ResourceFactoryDescriptor descriptor = getDescriptor(uri); |
| 55 |
|
| 56 |
if(descriptor != null) { |
| 57 |
resourceFactory = getFactory(descriptor); |
| 58 |
} |
| 59 |
} |
| 60 |
if(resourceFactory == null) |
| 61 |
resourceFactory = super.getFactory(uri); |
| 62 |
return resourceFactory; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Register a file name representing the last segment of a URI with the corresponding |
| 68 |
* Resource.Factory. |
| 69 |
*/ |
| 70 |
public synchronized void registerLastFileSegment(String aSimpleFileName, Resource.Factory aFactory) { |
| 71 |
|
| 72 |
if(LOG_WARNINGS) { |
| 73 |
/* the third entry in the array is this stack frame, we walk back from there. */ |
| 74 |
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); |
| 75 |
if(stackTrace.length > 4) { |
| 76 |
StringBuffer warningMessage = new StringBuffer("WTPResourceFactoryRegistry.registerLastFileSegment() was called explicitly from " + stackTrace[3]); |
| 77 |
warningMessage.append("\nThis happened around: \n"); |
| 78 |
for (int i = 4; (i < stackTrace.length) && i < 8; i++) { |
| 79 |
warningMessage.append("\tnear ").append(stackTrace[i]).append('\n'); |
| 80 |
} |
| 81 |
warningMessage.append(".\nClients should use the org.eclipse.wst.common.modulecore.resourceFactories extension point instead."); |
| 82 |
ModulecorePlugin.log(IStatus.INFO, 0, warningMessage.toString(), null); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
super.registerLastFileSegment(aSimpleFileName, aFactory); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Declares a subclass to create Resource.Factory(ies) from an extension. |
| 92 |
*/ |
| 93 |
private class ConfigurationResourceFactoryDescriptor extends ResourceFactoryDescriptor implements IResourceFactoryExtPtConstants { |
| 94 |
|
| 95 |
private String shortSegment; |
| 96 |
private final IConfigurationElement element; |
| 97 |
|
| 98 |
public ConfigurationResourceFactoryDescriptor(IConfigurationElement ext) throws CoreException { |
| 99 |
Assert.isNotNull(ext); |
| 100 |
element = ext; |
| 101 |
init(); |
| 102 |
} |
| 103 |
|
| 104 |
private void init() throws CoreException { |
| 105 |
shortSegment = element.getAttribute(ATT_SHORT_SEGMENT); |
| 106 |
if(shortSegment == null || shortSegment.trim().length() == 0) |
| 107 |
throw new CoreException( |
| 108 |
ModulecorePlugin.createErrorStatus(0, |
| 109 |
"The shortSegment attribute of " + TAG_RESOURCE_FACTORY + //$NON-NLS-1$ |
| 110 |
" must specify a valid, non-null, non-empty value in " + //$NON-NLS-1$ |
| 111 |
element.getNamespaceIdentifier(), null)); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
public boolean isEnabledFor(URI fileURI) { |
| 116 |
/* shortSegment must be non-null for the descriptor to be created, |
| 117 |
* a validation check in init() verifies this requirement */ |
| 118 |
if(fileURI != null && fileURI.lastSegment() != null) |
| 119 |
return shortSegment.equals(fileURI.lastSegment()); |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
public Resource.Factory createFactory() { |
| 124 |
|
| 125 |
final Resource.Factory[] factory = new Resource.Factory[1]; |
| 126 |
|
| 127 |
SafeRunner.run(new ISafeRunnable() { |
| 128 |
|
| 129 |
public void run() throws Exception { |
| 130 |
factory[0] = (Resource.Factory) element.createExecutableExtension(ATT_CLASS); |
| 131 |
} |
| 132 |
|
| 133 |
public void handleException(Throwable exception) { |
| 134 |
ModulecorePlugin.log(ModulecorePlugin.createErrorStatus(0, exception.getMessage(), exception)); |
| 135 |
} |
| 136 |
}); |
| 137 |
|
| 138 |
return factory[0] != null ? factory[0] : DefaultOverridableResourceFactoryRegistry.GLOBAL_FACTORY; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
public String getShortSegment() { |
| 143 |
return shortSegment; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
private class ResourceFactoryRegistryReader extends RegistryReader implements IResourceFactoryExtPtConstants { |
| 149 |
|
| 150 |
public ResourceFactoryRegistryReader() { |
| 151 |
super(Platform.getPluginRegistry(), ModulecorePlugin.PLUGIN_ID, EXTPT_RESOURCE_FACTORIES); |
| 152 |
} |
| 153 |
|
| 154 |
public boolean readElement(final IConfigurationElement element) { |
| 155 |
|
| 156 |
if(element != null && TAG_RESOURCE_FACTORY.equals(element.getName())) { |
| 157 |
final boolean[] success = new boolean[] { true }; |
| 158 |
SafeRunner.run(new ISafeRunnable() { |
| 159 |
|
| 160 |
public void run() throws Exception { |
| 161 |
addDescriptor(new ConfigurationResourceFactoryDescriptor(element)); |
| 162 |
} |
| 163 |
|
| 164 |
public void handleException(Throwable exception) { |
| 165 |
ModulecorePlugin.log(ModulecorePlugin.createErrorStatus(0, exception.getMessage(), exception)); |
| 166 |
success[0] = false; |
| 167 |
} |
| 168 |
}); |
| 169 |
return success[0]; |
| 170 |
} else { |
| 171 |
return false; |
| 172 |
} |
| 173 |
} |
| 30 |
} |
174 |
} |
| 31 |
} |
175 |
} |