<commit_before>/**
 * 
 */
package org.csstudio.utility.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

import org.csstudio.ui.util.NoResourceEditorInput;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

/**
 * @author shroffk
 * 
 */
public class IFileUtil {

	private static final String TAG_PLUGIN = "IFileUtil"; //$NON-NLS-1$
	private static final String TAG_PART = "part"; //$NON-NLS-1$
	private static final String TAG_PATH = "path"; //$NON-NLS-1$
	private static final String TAG_COUNT = "count"; //$NON-NLS-1$
    private final static IFileUtil instance = new IFileUtil();
    final ConcurrentMap<IFile, AtomicLong> map = new ConcurrentHashMap<IFile, AtomicLong>();
    private final SyncShutdown syncShutdown = new SyncShutdown();

    private class SyncShutdown {
    	private boolean shutdown = false;
    	
    	public synchronized void shutdown() {
    		shutdown = true;
    	}
    	
    	public synchronized boolean status() {
    		return shutdown;
    	}
    }
    
	private IFileUtil() {

		PlatformUI.getWorkbench().addWindowListener(new IWindowListener(){

			@Override
			public void windowActivated(IWorkbenchWindow window) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(IWorkbenchWindow window) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosed(IWorkbenchWindow window) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(IWorkbenchWindow window) {
				for(IWorkbenchPage page: window.getPages()){
	        		page.addPartListener(new IPartListener2() {

	        		    @Override
	        		    public void partClosed(IWorkbenchPartReference partRef) {
	        		    	IFile iFile = null;
	        		    	if(!syncShutdown.status()){
	        		    		IWorkbenchPart part = partRef.getPart(false);
								if (partRef instanceof IEditorReference) {
									IEditorInput input = ((IEditorPart)part) == null ? null : ((IEditorPart)part).getEditorInput();
									if (input instanceof NoResourceEditorInput)
										input = ((NoResourceEditorInput)input).getOriginEditorInput();
									iFile = input instanceof FileEditorInput ? ((FileEditorInput)input).getFile(): null;
									
								} else {
									try {
										Field f = part.getClass().getDeclaredField("input");
										f.setAccessible(true);
										IEditorInput element = (IEditorInput) f.get(part);
										Field f2 = element.getClass().getDeclaredField("path");
										f2.setAccessible(true);
										Path fileName = (Path) f2.get(element);
										iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fileName);

									} catch (NoSuchFieldException
											| SecurityException
											| IllegalArgumentException
											| IllegalAccessException e1) {
										// I don't care if these fail, best
										// attempt to read boy view files								
										iFile = null;
										// e1.printStackTrace();
									}
								}
								if (iFile !=null && !map.isEmpty()){
									if(map.get(iFile).decrementAndGet() <= 0){
		        		    			map.remove(iFile);
		        		    			try {
											iFile.delete(true, null);
										} catch (CoreException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
		        		    		}
								}
	        		    	}
	        		    }

	        		    @Override
	        		    public void partActivated(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partBroughtToTop(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partDeactivated(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partOpened(IWorkbenchPartReference partRef) {    
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partHidden(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partVisible(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partInputChanged(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }
	        		});

	        	}
				
			}} );
		
		attachShutdownListener();
	}

    

    public static IFileUtil getInstance() {
	return instance;
    }

    private void attachShutdownListener(){
    	PlatformUI.getWorkbench().addWorkbenchListener(
				new IWorkbenchListener() {

					@Override
					public boolean preShutdown(IWorkbench workbench,
							boolean forced) {
						syncShutdown.shutdown();
						return true;
					}
<commit_msg>o.c.utility.file NPE when closing non-IFileUtil file after restart #592<commit_after>/**
 * 
 */
package org.csstudio.utility.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

import org.csstudio.ui.util.NoResourceEditorInput;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

/**
 * @author shroffk
 * 
 */
public class IFileUtil {

	private static final String TAG_PLUGIN = "IFileUtil"; //$NON-NLS-1$
	private static final String TAG_PART = "part"; //$NON-NLS-1$
	private static final String TAG_PATH = "path"; //$NON-NLS-1$
	private static final String TAG_COUNT = "count"; //$NON-NLS-1$
    final ConcurrentMap<IFile, AtomicLong> map = new ConcurrentHashMap<IFile, AtomicLong>();
    private final SyncShutdown syncShutdown = new SyncShutdown();

    private class SyncShutdown {
    	private boolean shutdown = false;
    	
    	public synchronized void shutdown() {
    		shutdown = true;
    	}
    	
    	public synchronized boolean status() {
    		return shutdown;
    	}
    }
    
    private final static class IFileUtilHolder {
        private static final IFileUtil instance = new IFileUtil();
    }
    
    public static IFileUtil getInstance() {
    	return IFileUtilHolder.instance;
    }
    
	private IFileUtil() {

		PlatformUI.getWorkbench().addWindowListener(new IWindowListener(){

			@Override
			public void windowActivated(IWorkbenchWindow window) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(IWorkbenchWindow window) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosed(IWorkbenchWindow window) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(IWorkbenchWindow window) {
				for(IWorkbenchPage page: window.getPages()){
	        		page.addPartListener(new IPartListener2() {

	        		    @Override
	        		    public void partClosed(IWorkbenchPartReference partRef) {
	        		    	IFile iFile = null;
	        		    	if(!syncShutdown.status()){
	        		    		IWorkbenchPart part = partRef.getPart(false);
								if (partRef instanceof IEditorReference) {
									IEditorInput input = ((IEditorPart)part) == null ? null : ((IEditorPart)part).getEditorInput();
									if (input instanceof NoResourceEditorInput)
										input = ((NoResourceEditorInput)input).getOriginEditorInput();
									iFile = input instanceof FileEditorInput ? ((FileEditorInput)input).getFile(): null;
									
								} else {
									try {
										Field f = part.getClass().getDeclaredField("input");
										f.setAccessible(true);
										IEditorInput element = (IEditorInput) f.get(part);
										Field f2 = element.getClass().getDeclaredField("path");
										f2.setAccessible(true);
										Path fileName = (Path) f2.get(element);
										iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fileName);

									} catch (NoSuchFieldException
											| SecurityException
											| IllegalArgumentException
											| IllegalAccessException e1) {
										// I don't care if these fail, best
										// attempt to read boy view files								
										iFile = null;
										// e1.printStackTrace();
									}
								}
								if (iFile != null && !map.isEmpty()) {
									if (map.get(iFile) != null) {
										if (map.get(iFile).decrementAndGet() <= 0) {
											map.remove(iFile);
											try {
												iFile.delete(true, null);
											} catch (CoreException e) {
												// TODO Auto-generated catch
												// block
												e.printStackTrace();
											}
										}
									}
								}
							}
	        		    }

	        		    @Override
	        		    public void partActivated(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partBroughtToTop(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partDeactivated(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partOpened(IWorkbenchPartReference partRef) {    
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partHidden(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partVisible(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }

	        		    @Override
	        		    public void partInputChanged(IWorkbenchPartReference partRef) {
	        			// TODO Auto-generated method stub

	        		    }
	        		});

	        	}
				
			}} );
		
		attachShutdownListener();
	}

    private void attachShutdownListener(){
    	PlatformUI.getWorkbench().addWorkbenchListener(
				new IWorkbenchListener() {

					@Override
					public boolean preShutdown(IWorkbench workbench,
							boolean forced) {
						syncShutdown.shutdown();
						return true;
					}

{'fraction_non_alphanumeric': 0.09235312899106003, 'fraction_numerical': 0.0021551724137931034, 'mean_word_length': 2.3242239320774742, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5016816', 'n_tokens_mistral': 4578, 'n_tokens_neox': 3537, 'n_words': 749}