Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 188516 Details for
Bug 307587
NatureManager is NOT threadsafe causing incorrect responses to isNatureEnabled() (and others)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
Importer code
ZipImportUtil.java (text/plain), 10.56 KB, created by
Min Idzelis
on 2011-02-08 08:58:58 EST
(
hide
)
Description:
Importer code
Filename:
MIME Type:
Creator:
Min Idzelis
Created:
2011-02-08 08:58:58 EST
Size:
10.56 KB
patch
obsolete
> >import java.io.IOException; >import java.net.URL; >import java.util.ArrayList; >import java.util.Enumeration; >import java.util.HashSet; >import java.util.List; >import java.util.Set; >import java.util.zip.ZipEntry; >import java.util.zip.ZipFile; > >import junit.framework.TestCase; > >import org.eclipse.core.resources.IProject; >import org.eclipse.core.resources.IResource; >import org.eclipse.core.resources.IResourceChangeEvent; >import org.eclipse.core.resources.IResourceChangeListener; >import org.eclipse.core.resources.IResourceDelta; >import org.eclipse.core.resources.IResourceDeltaVisitor; >import org.eclipse.core.resources.IWorkspace; >import org.eclipse.core.resources.IWorkspaceRunnable; >import org.eclipse.core.resources.ResourcesPlugin; >import org.eclipse.core.runtime.Assert; >import org.eclipse.core.runtime.CoreException; >import org.eclipse.core.runtime.FileLocator; >import org.eclipse.core.runtime.IPath; >import org.eclipse.core.runtime.IProgressMonitor; >import org.eclipse.core.runtime.IStatus; >import org.eclipse.core.runtime.NullProgressMonitor; >import org.eclipse.core.runtime.Path; >import org.eclipse.core.runtime.Platform; >import org.eclipse.core.runtime.Status; >import org.eclipse.core.runtime.SubMonitor; >import org.eclipse.jface.operation.IRunnableWithProgress; >import org.eclipse.ui.dialogs.IOverwriteQuery; >import org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider; >import org.eclipse.ui.wizards.datatransfer.ImportOperation; >import org.osgi.framework.Bundle; > >public class ZipImportUtil { > public static void importZipFile(final String fullyResolvedLocation, IProgressMonitor top) > throws Exception { > > IWorkspaceRunnable runnable = new IWorkspaceRunnable() { > > @Override > public void run(IProgressMonitor monitor) throws CoreException { > ZipFile zip = null; > try { > SubMonitor mon = SubMonitor.convert(monitor); > zip = new ZipFile(fullyResolvedLocation); > ZipLeveledStructureProvider zipStructure = new ZipLeveledStructureProvider(zip); > zipStructure.setStrip(1); > List<ZipEntry> list = zipStructure.getChildren(zipStructure.getRoot()); > mon.beginTask("", list.size()); //$NON-NLS-1$ > for (ZipEntry zipEntry : list) { > SubMonitor parts = mon.newChild(1); > parts.beginTask("", 3); //$NON-NLS-1$ > String name = zipEntry.getName(); > IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name); > if (!project.isOpen() && project.exists()) { > project.open(parts.newChild(1)); > } > else { > parts.worked(1); > } > if (!project.exists()) { > project.create(parts.newChild(1)); > } > else { > parts.worked(1); > } > if (!project.isOpen()) { > project.open(parts.newChild(1)); > } > else { > parts.worked(1); > } > > ImportOperation op = new ImportOperation(new Path(name), zipEntry, zipStructure, > new IOverwriteQuery() { > > @Override > public String queryOverwrite(String pathString) { > return IOverwriteQuery.ALL; > } > }); > > op.run(parts.newChild(1)); > } > } > catch (Exception e) { > IStatus status = new Status(IStatus.ERROR, Constants.PLUGIN_ID, "Error during import", e); //$NON-NLS-1$ > throw new CoreException(status); > } > finally { > if (zip != null) { > try { > zip.close(); > } > catch (IOException e) {} > } > } > } > }; > > ResourcesPlugin.getWorkspace().run(runnable, ResourcesPlugin.getWorkspace().getRoot(), > IWorkspace.AVOID_UPDATE, top); > } > > /** > * use this to import a PI file that is located within your plugin > * directory, relative to a given plugin bundle eg: > * ZipImportUtil.importZipFile( WebPageTestPlugin.getDefault().getBundle(), > * "projects/LinksBuilder-junit.zip" ); > * > * @throws IOException > */ > public static void importZipFile(Bundle bundle, String pluginRelativePath, IProgressMonitor monitor) > throws Exception { > Assert.isNotNull(bundle, "Bundle parameter was null"); //$NON-NLS-1$ > String installLocation = getInstallLocation(bundle, pluginRelativePath); > Assert.isNotNull(installLocation, "Could not find path to project interchange file"); //$NON-NLS-1$ > importZipFile(installLocation, monitor); > } > > /** > * Retuns the plugins install location > * > * @throws IOException > */ > public static String getInstallLocation(Bundle bundle, String fileName) throws IOException { > Assert.isNotNull(bundle, "Bundle was null"); //$NON-NLS-1$ > URL fileURL = FileLocator.find(bundle, new Path(fileName), null); > Assert.isNotNull(fileURL, "FileLocator#find(Bundle,IPath,Map) returned null for Bundle: " //$NON-NLS-1$ > + bundle.getSymbolicName() + " path: " + fileName); //$NON-NLS-1$ > String location = FileLocator.toFileURL(fileURL).getPath(); > return location; > } > > /** > * <p> > * Create a runnable that will import the zip for a given project name > * </p> > * > * @param projectName > * name of the project who's ZIP should be imported by the > * created runnable > * @param the > * bundle that contains the project name (in a folder called > * /resources/) > * @return {@link IRunnableWithProgress} that when run will import the ZIP > * for the project of the given name > * @throws CoreException > * @throws IOException > */ > public static IRunnableWithProgress createImportRunnable(String projectName, String bundleId) > throws CoreException, IOException { > final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); > project.refreshLocal(IResource.DEPTH_INFINITE, null); > > final List<String> filesToBeImported = new ArrayList<String>(); > final String fileName = "/resources/" + projectName + ".zip"; //$NON-NLS-1$ //$NON-NLS-2$ > > final Bundle bundle = Platform.getBundle(bundleId); > TestCase.assertNotNull("Could not find bundle: " + bundleId, bundle); //$NON-NLS-1$ > > URL bundleURL = FileLocator.find(bundle, new Path(fileName), null); > TestCase.assertNotNull("Could not create bundle URL for: " + fileName + " in bundle " //$NON-NLS-1$ //$NON-NLS-2$ > + bundle.getSymbolicName(), bundleURL); > > URL fileURL = FileLocator.toFileURL(bundleURL); > TestCase.assertNotNull("Could not create file URL for: " + bundleURL, fileURL); //$NON-NLS-1$ > > String location = fileURL.getPath(); > ZipFile file = new ZipFile(location); > Enumeration<? extends ZipEntry> entries = file.entries(); > while (entries.hasMoreElements()) { > ZipEntry zipEntry = entries.nextElement(); > if (!zipEntry.isDirectory()) { > String name = zipEntry.getName(); > IPath path = new Path(name); > path = path.makeAbsolute(); > filesToBeImported.add(path.toString()); > } > } > file.close(); > > final IResourceChangeListener listener = new IResourceChangeListener() { > > @Override > public void resourceChanged(IResourceChangeEvent event) { > > Visitor visitor = new Visitor(); > try { > if (event.getDelta() != null) > event.getDelta().accept(visitor); > } > catch (CoreException e) { > throw new RuntimeException(e); > } > synchronized (filesToBeImported) { > filesToBeImported.removeAll(visitor.getResources()); > filesToBeImported.notifyAll(); > } > } > > }; > > IRunnableWithProgress wait = new IRunnableWithProgress() { > > @Override > public void run(IProgressMonitor monitor) { > SubMonitor sub = SubMonitor.convert(monitor, 100); > sub.subTask("Importing..."); //$NON-NLS-1$ > try { > try { > if (!project.exists()) { > ResourcesPlugin.getWorkspace().addResourceChangeListener(listener); > > ZipImportUtil.importZipFile(bundle, fileName, sub.newChild(50)); > } > else { > boolean deleteSuccess = false; > //try multiple times to delete project, something might be busy reading it making it fail to delete > for(int i = 0; i < 20 && !deleteSuccess; ++i) { > try { > System.out.println("Attempt to Delete " + i + ": " + project); //$NON-NLS-1$ //$NON-NLS-2$ > project.delete(true, null); > deleteSuccess = true; > } catch(CoreException e) { > Thread.sleep(1000); > project.refreshLocal(IProject.DEPTH_INFINITE, new NullProgressMonitor()); > } > } > > Assert.isLegal(deleteSuccess, > "Was unable to delete existing project before importing again.\n " + //$NON-NLS-1$ > project); > > ResourcesPlugin.getWorkspace().addResourceChangeListener(listener); > if (deleteSuccess) > System.out.println("Deleted: " + project); //$NON-NLS-1$ > > ZipImportUtil.importZipFile(bundle, fileName, sub.newChild(50)); > } > } > catch (Exception e) { > throw new RuntimeException("Exception during import of: " + fileName, e); //$NON-NLS-1$ > } > > synchronized (filesToBeImported) { > while (!filesToBeImported.isEmpty()) { > monitor.subTask("Waiting for listeners to be notified... " //$NON-NLS-1$ > + filesToBeImported.size() + " files remaining"); //$NON-NLS-1$ > try { > filesToBeImported.wait(1000); > } > catch (InterruptedException e) { > Thread.interrupted(); > } > if (monitor.isCanceled()) { > throw new RuntimeException("Waiting canceled..."); //$NON-NLS-1$ > } > } > } > } > finally { > ResourcesPlugin.getWorkspace().removeResourceChangeListener(listener); > monitor.done(); > } > } > }; > > return wait; > } > > private static class Visitor implements IResourceDeltaVisitor { > private final Set<String> files = new HashSet<String>(); > > @Override > public boolean visit(IResourceDelta delta) throws CoreException { > > IResource r = delta.getResource(); > if (r.getType() == IResource.FILE) { > if (delta.getKind() == IResourceDelta.CHANGED) { > if ((delta.getFlags() & IResourceDelta.CONTENT) == IResourceDelta.CONTENT) { > files.add(r.getFullPath().toString()); > } > else { > // no op > } > } > else if (delta.getKind() == IResourceDelta.ADDED) { > files.add(r.getFullPath().toString()); > } > else if (delta.getKind() == IResourceDelta.REMOVED) { > files.add(r.getFullPath().toString()); > } > } > else { > if (r.getType() == IResource.FOLDER || r.getType() == IResource.PROJECT > || r.getType() == IResource.ROOT) { > if ((delta.getKind() == IResourceDelta.ADDED) > || (delta.getKind() == IResourceDelta.REMOVED)) { > > } > } > } > return true; > } > > public Set<String> getResources() { > return files; > } > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 307587
:
177513
|
177601
|
177612
|
177643
|
185251
|
185303
|
188503
| 188516 |
189502
|
189520