diff options
author | Alexander Kurtakov | 2016-01-26 13:39:48 +0000 |
---|---|---|
committer | Alexander Kurtakov | 2016-01-26 14:15:21 +0000 |
commit | 85cc2a741eb2939641b9cd094f22f73b52749e72 (patch) | |
tree | def2deb83f67a19acc96c20915fe1bcaa4f427f1 /rpm | |
parent | 4d13c66f2ae2c1b3fbad83a0cd7d0565122d8079 (diff) | |
download | org.eclipse.linuxtools-85cc2a741eb2939641b9cd094f22f73b52749e72.tar.gz org.eclipse.linuxtools-85cc2a741eb2939641b9cd094f22f73b52749e72.tar.xz org.eclipse.linuxtools-85cc2a741eb2939641b9cd094f22f73b52749e72.zip |
rpm: Make use of Java 8 Files.
Remove Util.copyFile and fileExists in favor of the Java 8 methods.
Change-Id: Id604d6e2cf83c7711ce5aeea15ef954c52bf1233
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
Reviewed-on: https://git.eclipse.org/r/65180
Tested-by: Hudson CI
Diffstat (limited to 'rpm')
11 files changed, 613 insertions, 647 deletions
diff --git a/rpm/org.eclipse.linuxtools.rpm.core/src/org/eclipse/linuxtools/rpm/core/utils/Utils.java b/rpm/org.eclipse.linuxtools.rpm.core/src/org/eclipse/linuxtools/rpm/core/utils/Utils.java index 124333bde0..4975c3f487 100644 --- a/rpm/org.eclipse.linuxtools.rpm.core/src/org/eclipse/linuxtools/rpm/core/utils/Utils.java +++ b/rpm/org.eclipse.linuxtools.rpm.core/src/org/eclipse/linuxtools/rpm/core/utils/Utils.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009-2010 Red Hat, Inc. + * Copyright (c) 2009, 2016 Red Hat, Inc. * 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 @@ -11,14 +11,10 @@ package org.eclipse.linuxtools.rpm.core.utils; import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.SequenceInputStream; -import java.nio.channels.FileChannel; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IStatus; @@ -28,7 +24,6 @@ import org.eclipse.osgi.util.NLS; import org.osgi.framework.FrameworkUtil; /** * Utilities for calling system executables. - * */ public class Utils { @@ -187,29 +182,4 @@ public class Utils { stream.close(); return retStr.toString(); } - - /** - * Checks whether a file exists. - * - * @param cmdPath The file path to be checked. - * @return <code>true</code> if the file exists, <code>false</code> otherwise. - */ - public static boolean fileExist(String cmdPath) { - return new File(cmdPath).exists(); - } - - /** - * Copy file from one destination to another. - * @param in The source file. - * @param out The destination. - * @throws IOException If an I/O exception occurs. - */ - public static void copyFile(File in, File out) throws IOException { - try (FileInputStream fin = new FileInputStream(in); - FileChannel inChannel = fin.getChannel(); - FileOutputStream fos = new FileOutputStream(out); - FileChannel outChannel = fos.getChannel()) { - inChannel.transferTo(0, inChannel.size(), outChannel); - } - } } diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/RunRpmlintAction.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/RunRpmlintAction.java index 356a0b47c3..506680071b 100644 --- a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/RunRpmlintAction.java +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/RunRpmlintAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2013 Red Hat, Inc. + * Copyright (c) 2009, 2016 Red Hat, Inc. * 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 @@ -11,6 +11,8 @@ package org.eclipse.linuxtools.internal.rpm.rpmlint.actions; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -82,7 +84,7 @@ public class RunRpmlintAction extends AbstractHandler{ String rpmlintPath = new ScopedPreferenceStore(InstanceScope.INSTANCE,Activator.PLUGIN_ID).getString( PreferenceConstants.P_RPMLINT_PATH); try { - if (Utils.fileExist(rpmlintPath)) { + if (Files.exists(Paths.get(rpmlintPath))) { String output = Utils.runCommandToString(rpmlintPath, "-i", location); //$NON-NLS-1$ MessageConsole myConsole = findConsole(Messages.RunRpmlintAction_0); diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/ToggleRpmlintNatureAction.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/ToggleRpmlintNatureAction.java index 80d23e162f..039026df33 100644 --- a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/ToggleRpmlintNatureAction.java +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/ToggleRpmlintNatureAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2013 Red Hat, Inc. + * Copyright (c) 2009, 2016 Red Hat, Inc. * 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 @@ -10,6 +10,9 @@ *******************************************************************************/ package org.eclipse.linuxtools.internal.rpm.rpmlint.actions; +import java.nio.file.Files; +import java.nio.file.Paths; + import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.resources.IProject; @@ -25,7 +28,6 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.linuxtools.internal.rpm.rpmlint.Activator; import org.eclipse.linuxtools.internal.rpm.rpmlint.builder.RpmlintNature; import org.eclipse.linuxtools.internal.rpm.rpmlint.preferences.PreferenceConstants; -import org.eclipse.linuxtools.rpm.core.utils.Utils; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.handlers.HandlerUtil; import org.eclipse.ui.preferences.ScopedPreferenceStore; @@ -66,7 +68,7 @@ public class ToggleRpmlintNatureAction extends AbstractHandler { private static void toggleNature(IProject project) { String rpmlintPath = new ScopedPreferenceStore(InstanceScope.INSTANCE,Activator.PLUGIN_ID).getString( PreferenceConstants.P_RPMLINT_PATH); - if(!Utils.fileExist(rpmlintPath)) { + if(!Files.exists(Paths.get(rpmlintPath))) { IStatus warning = new Status( IStatus.WARNING, Activator.PLUGIN_ID, diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/parser/RpmlintParser.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/parser/RpmlintParser.java index a8af2d9613..1e44343f98 100644 --- a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/parser/RpmlintParser.java +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/parser/RpmlintParser.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Alphonse Van Assche. + * Copyright (c) 2007, 2016 Alphonse Van Assche. * 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 @@ -15,6 +15,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.StringReader; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -59,7 +61,7 @@ public class RpmlintParser { * a guard if we have configuration changing or someone playing with the * project files. */ - if (visitedResources.isEmpty() || !Utils.fileExist(rpmlintPath)) { + if (visitedResources.isEmpty() || !Files.exists(Paths.get(rpmlintPath))) { return new ArrayList<>(); } return parseRpmlintOutput(runRpmlintCommand(visitedResources)); diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmMacroProposalsListTest.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmMacroProposalsListTest.java index fc5e920351..e74d217b63 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmMacroProposalsListTest.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmMacroProposalsListTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Alphonse Van Assche. + * Copyright (c) 2007, 2016 Alphonse Van Assche. * 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 @@ -13,10 +13,11 @@ package org.eclipse.linuxtools.rpm.ui.editor.tests; import static org.junit.Assert.fail; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Map; import org.eclipse.linuxtools.internal.rpm.ui.editor.RpmMacroProposalsList; -import org.eclipse.linuxtools.rpm.core.utils.Utils; import org.junit.Before; import org.junit.Test; @@ -81,7 +82,7 @@ public class RpmMacroProposalsListTest { @Test public final void testGetMacroEval() { - if (Utils.fileExist("/bin/rpm")) { + if (Files.exists(Paths.get("/bin/rpm"))) { if (RpmMacroProposalsList.getMacroEval("%_libdir").indexOf("lib") == -1) { fail("getMacroEval faild, eval don't end with 'lib'"); } diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmPackageProposalsListTest.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmPackageProposalsListTest.java index 5489cec2da..2d5625c16b 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmPackageProposalsListTest.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/RpmPackageProposalsListTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Alphonse Van Assche. + * Copyright (c) 2007, 2016 Alphonse Van Assche. * 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 @@ -13,15 +13,14 @@ package org.eclipse.linuxtools.rpm.ui.editor.tests; import static org.junit.Assert.fail; -import java.io.BufferedWriter; -import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator; import org.eclipse.linuxtools.internal.rpm.ui.editor.RpmPackageProposalsList; import org.eclipse.linuxtools.internal.rpm.ui.editor.preferences.PreferenceConstants; -import org.eclipse.linuxtools.rpm.core.utils.Utils; import org.junit.Before; import org.junit.Test; @@ -30,15 +29,10 @@ public class RpmPackageProposalsListTest { private RpmPackageProposalsList packageProposalsList; @Before - public void setUp() { + public void setUp() throws IOException { Activator.getDefault().getPreferenceStore().setValue( PreferenceConstants.P_RPM_LIST_FILEPATH, "/tmp/pkglist"); - try (BufferedWriter out = new BufferedWriter(new FileWriter( - "/tmp/pkglist"))) { - out.write("setup\ntest\nrpm\n"); - } catch (IOException e) { - e.printStackTrace(); - } + Files.write(Paths.get("/tmp/pkglist"), "setup\ntest\nrpm\n".getBytes()); packageProposalsList = new RpmPackageProposalsList(); } @@ -52,7 +46,7 @@ public class RpmPackageProposalsListTest { @Test public final void testGetValue() { - if (Utils.fileExist("/bin/rpm")) { + if (Files.exists(Paths.get("/bin/rpm"))) { if (!packageProposalsList.getValue("rpm").startsWith( "<b>Name: </b>rpm")) { fail("getValue failed, rpm package info doesn't start with '<b>Name:<b> rpm'"); @@ -69,7 +63,7 @@ public class RpmPackageProposalsListTest { @Test public final void testGetRpmInfo() { - if (Utils.fileExist("/bin/rpm")) { + if (Files.exists(Paths.get("/bin/rpm"))) { if (!packageProposalsList.getRpmInfo("rpm").startsWith( "<b>Name: </b>rpm")) { fail("getRpmInfo failed, rpm package info doesn't start with '<b>Name:<b> rpm'"); diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageBuildProposalsJob.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageBuildProposalsJob.java index c03f946832..822804d5fb 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageBuildProposalsJob.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageBuildProposalsJob.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Alphonse Van Assche. + * Copyright (c) 2007, 2016 Alphonse Van Assche. * 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 @@ -12,11 +12,12 @@ package org.eclipse.linuxtools.internal.rpm.ui.editor; import java.io.BufferedReader; import java.io.BufferedWriter; -import java.io.File; import java.io.FileInputStream; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Date; import java.util.HashSet; import java.util.Set; @@ -37,195 +38,189 @@ import org.eclipse.osgi.util.NLS; public final class RpmPackageBuildProposalsJob extends Job { - private RpmPackageBuildProposalsJob(String name) { - super(name); - this.addJobChangeListener(updateFinishedListener); - } + private RpmPackageBuildProposalsJob(String name) { + super(name); + this.addJobChangeListener(updateFinishedListener); + } - private static RpmPackageBuildProposalsJob job = null; + private static RpmPackageBuildProposalsJob job = null; - private IJobChangeListener updateFinishedListener = new JobChangeAdapter(); + private IJobChangeListener updateFinishedListener = new JobChangeAdapter(); - protected static final IPropertyChangeListener PROPERTY_LISTENER = event -> { - if (event.getProperty().equals(PreferenceConstants.P_CURRENT_RPMTOOLS)) { - updateAsync(); - } + protected static final IPropertyChangeListener PROPERTY_LISTENER = event -> { + if (event.getProperty().equals(PreferenceConstants.P_CURRENT_RPMTOOLS)) { + updateAsync(); + } }; - protected static final IPreferenceStore STORE = Activator.getDefault().getPreferenceStore(); - - @Override - protected IStatus run(IProgressMonitor monitor) { - return retrievePackageList(monitor); - } - - @Override - public boolean shouldSchedule() { - return equals(job); - } - - public static void updateSync() { - update(false); - } - - public static void updateAsync() { - update(true); - } - - /** - * Run the Job if it's needed according with the configuration set in the - * preference page. - */ - private static void update(boolean async) { - boolean runJob = false; - // Today's date - Date today = new Date(); - if (STORE.getBoolean(PreferenceConstants.P_RPM_LIST_BACKGROUND_BUILD)) { - int period = STORE.getInt(PreferenceConstants.P_RPM_LIST_BUILD_PERIOD); - // each time that the plugin is loaded. - if (period == 1) { - runJob = true; - } else { - long lastBuildTime = STORE.getLong(PreferenceConstants.P_RPM_LIST_LAST_BUILD); - if (lastBuildTime == 0) { - runJob = true; - } else { - long interval = (today.getTime() - lastBuildTime) - / (1000 * 60 * 60 * 24); - // run the job once a week - if (period == 2 && interval >= 7) { - runJob = true; - // run the job once a month - } else if (period == 3 && interval >= 30) { - runJob = true; - } - } - } - if (runJob) { - if (job == null) { - job = new RpmPackageBuildProposalsJob(Messages.RpmPackageBuildProposalsJob_0); - } else { - job.cancel(); - } - if (async) { - job.schedule(); - } else { - job.run(new NullProgressMonitor()); - } - STORE.setValue(PreferenceConstants.P_RPM_LIST_LAST_BUILD, today.getTime()); - } - } else { - if (job != null) { - job.cancel(); - job = null; - } - } - } - - /** - * Retrieve the package list - * - * @param monitor - * to update - * @return a <code>IStatus</code> - */ - private IStatus retrievePackageList(IProgressMonitor monitor) { - String rpmListCmd = STORE.getString(PreferenceConstants.P_CURRENT_RPMTOOLS); - String rpmListFilepath = STORE.getString(PreferenceConstants.P_RPM_LIST_FILEPATH); - File bkupFile = new File(rpmListFilepath + ".bkup"); //$NON-NLS-1$ - try { - monitor.beginTask(Messages.RpmPackageBuildProposalsJob_1, - IProgressMonitor.UNKNOWN); - if (Utils.fileExist("/bin/sh")) { //$NON-NLS-1$ - BufferedProcessInputStream in = Utils.runCommandToInputStream( - "/bin/sh", "-c", rpmListCmd); //$NON-NLS-1$ //$NON-NLS-2$ - // backup pkg list file - File rpmListFile = new File(rpmListFilepath); - if (rpmListFile.exists()) { - Utils.copyFile(new File(rpmListFilepath), bkupFile); - } - - try (BufferedWriter out = new BufferedWriter(new FileWriter(rpmListFile, false)); - BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { - monitor.subTask(Messages.RpmPackageBuildProposalsJob_2 - + rpmListCmd + Messages.RpmPackageBuildProposalsJob_3); - String line; - while ((line = reader.readLine()) != null) { - monitor.subTask(line); - out.write(line + "\n"); //$NON-NLS-1$ - if (monitor.isCanceled()) { - in.destroyProcess(); - in.close(); - out.close(); - // restore backup - if (rpmListFile.exists() && bkupFile.exists()) { - Utils.copyFile(bkupFile, rpmListFile); - bkupFile.delete(); - } - Activator.packagesList = new RpmPackageProposalsList(); - return Status.CANCEL_STATUS; - } - } - in.close(); - out.close(); - bkupFile.delete(); - int processExitValue = 0; - try { - processExitValue = in.getExitValue(); - } catch (InterruptedException e) { - return Status.CANCEL_STATUS; - } - if (processExitValue != 0) { - SpecfileLog - .log(IStatus.WARNING, - processExitValue, - NLS.bind( - Messages.RpmPackageBuildProposalsJob_NonZeroReturn, - processExitValue), null); - } - } - } - } catch (IOException e) { - SpecfileLog.logError(e); - return Status.CANCEL_STATUS; - } finally { - monitor.done(); - } - // Update package list - Activator.packagesList = new RpmPackageProposalsList(); - return Status.OK_STATUS; - } - - public static Set<String> getPackages() throws InterruptedException, IOException { - if (job.getThread() != Thread.currentThread()) { - job.join(); - } - final Set<String> list = new HashSet<>(); - String rpmpkgsFile = Activator.getDefault().getPreferenceStore() - .getString(PreferenceConstants.P_RPM_LIST_FILEPATH); - - try (BufferedReader reader = new BufferedReader( - new InputStreamReader(new FileInputStream(rpmpkgsFile)))) { - String line = reader.readLine(); - while (line != null) { - list.add(line.trim()); - line = reader.readLine(); - } - } - return list; - } - - /** - * Enable and disable the property change listener. - * - * @param activated Flag indicating whether the listener to be enabled or disabled. - */ - public static void setPropertyChangeListener(boolean activated) { - if (activated) { - STORE.addPropertyChangeListener(PROPERTY_LISTENER); - } else { - STORE.removePropertyChangeListener(PROPERTY_LISTENER); - } - } + protected static final IPreferenceStore STORE = Activator.getDefault().getPreferenceStore(); + + @Override + protected IStatus run(IProgressMonitor monitor) { + return retrievePackageList(monitor); + } + + @Override + public boolean shouldSchedule() { + return equals(job); + } + + public static void updateSync() { + update(false); + } + + public static void updateAsync() { + update(true); + } + + /** + * Run the Job if it's needed according with the configuration set in the + * preference page. + */ + private static void update(boolean async) { + boolean runJob = false; + // Today's date + Date today = new Date(); + if (STORE.getBoolean(PreferenceConstants.P_RPM_LIST_BACKGROUND_BUILD)) { + int period = STORE.getInt(PreferenceConstants.P_RPM_LIST_BUILD_PERIOD); + // each time that the plugin is loaded. + if (period == 1) { + runJob = true; + } else { + long lastBuildTime = STORE.getLong(PreferenceConstants.P_RPM_LIST_LAST_BUILD); + if (lastBuildTime == 0) { + runJob = true; + } else { + long interval = (today.getTime() - lastBuildTime) / (1000 * 60 * 60 * 24); + // run the job once a week + if (period == 2 && interval >= 7) { + runJob = true; + // run the job once a month + } else if (period == 3 && interval >= 30) { + runJob = true; + } + } + } + if (runJob) { + if (job == null) { + job = new RpmPackageBuildProposalsJob(Messages.RpmPackageBuildProposalsJob_0); + } else { + job.cancel(); + } + if (async) { + job.schedule(); + } else { + job.run(new NullProgressMonitor()); + } + STORE.setValue(PreferenceConstants.P_RPM_LIST_LAST_BUILD, today.getTime()); + } + } else { + if (job != null) { + job.cancel(); + job = null; + } + } + } + + /** + * Retrieve the package list + * + * @param monitor + * to update + * @return a <code>IStatus</code> + */ + private IStatus retrievePackageList(IProgressMonitor monitor) { + String rpmListCmd = STORE.getString(PreferenceConstants.P_CURRENT_RPMTOOLS); + String rpmListFilepath = STORE.getString(PreferenceConstants.P_RPM_LIST_FILEPATH); + Path bkupFile = Paths.get(rpmListFilepath + ".bkup"); //$NON-NLS-1$ + try { + monitor.beginTask(Messages.RpmPackageBuildProposalsJob_1, IProgressMonitor.UNKNOWN); + if (Files.exists(Paths.get("/bin/sh"))) { //$NON-NLS-1$ + BufferedProcessInputStream in = Utils.runCommandToInputStream("/bin/sh", "-c", rpmListCmd); //$NON-NLS-1$ //$NON-NLS-2$ + // backup pkg list file + Path rpmListFile = Paths.get(rpmListFilepath); + if (Files.exists(rpmListFile)) { + Files.copy(Paths.get(rpmListFilepath), bkupFile); + } + + try (BufferedWriter out = Files.newBufferedWriter(rpmListFile); + BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { + monitor.subTask(Messages.RpmPackageBuildProposalsJob_2 + rpmListCmd + + Messages.RpmPackageBuildProposalsJob_3); + String line; + while ((line = reader.readLine()) != null) { + monitor.subTask(line); + out.write(line + "\n"); //$NON-NLS-1$ + if (monitor.isCanceled()) { + in.destroyProcess(); + in.close(); + out.close(); + // restore backup + if (Files.exists(rpmListFile) && Files.exists(bkupFile)) { + Files.copy(bkupFile, Paths.get(rpmListFilepath)); + Files.delete(bkupFile); + } + Activator.packagesList = new RpmPackageProposalsList(); + return Status.CANCEL_STATUS; + } + } + in.close(); + out.close(); + Files.deleteIfExists(bkupFile); + int processExitValue = 0; + try { + processExitValue = in.getExitValue(); + } catch (InterruptedException e) { + return Status.CANCEL_STATUS; + } + if (processExitValue != 0) { + SpecfileLog.log(IStatus.WARNING, processExitValue, + NLS.bind(Messages.RpmPackageBuildProposalsJob_NonZeroReturn, processExitValue), null); + } + } + } + } catch (IOException e) { + SpecfileLog.logError(e); + return Status.CANCEL_STATUS; + } finally { + monitor.done(); + } + // Update package list + Activator.packagesList = new RpmPackageProposalsList(); + return Status.OK_STATUS; + } + + public static Set<String> getPackages() throws InterruptedException, IOException { + if (job.getThread() != Thread.currentThread()) { + job.join(); + } + final Set<String> list = new HashSet<>(); + String rpmpkgsFile = Activator.getDefault().getPreferenceStore() + .getString(PreferenceConstants.P_RPM_LIST_FILEPATH); + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(rpmpkgsFile)))) { + String line = reader.readLine(); + while (line != null) { + list.add(line.trim()); + line = reader.readLine(); + } + } + return list; + } + + /** + * Enable and disable the property change listener. + * + * @param activated + * Flag indicating whether the listener to be enabled or + * disabled. + */ + public static void setPropertyChangeListener(boolean activated) { + if (activated) { + STORE.addPropertyChangeListener(PROPERTY_LISTENER); + } else { + STORE.removePropertyChangeListener(PROPERTY_LISTENER); + } + } } diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageProposalsList.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageProposalsList.java index d76605c26a..800025ea0a 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageProposalsList.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/RpmPackageProposalsList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Alphonse Van Assche. + * Copyright (c) 2007, 2016 Alphonse Van Assche. * 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 @@ -18,6 +18,8 @@ import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -25,153 +27,150 @@ import java.util.Set; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.linuxtools.internal.rpm.ui.editor.preferences.PreferenceConstants; -import org.eclipse.linuxtools.rpm.core.utils.Utils; /** * This class is used to retrieve and manage the RPM package proposals. * */ public class RpmPackageProposalsList { - private final Set<String> list = new HashSet<>(); + private final Set<String> list = new HashSet<>(); - public RpmPackageProposalsList() { - setPackagesList(); - } + public RpmPackageProposalsList() { + setPackagesList(); + } - private void setPackagesList() { - String rpmpkgsFile = Activator.getDefault().getPreferenceStore() - .getString(PreferenceConstants.P_RPM_LIST_FILEPATH); - if (Utils.fileExist(rpmpkgsFile)) { - try { - Set<String> newList = RpmPackageBuildProposalsJob.getPackages(); - list.clear(); - list.addAll(newList); - } catch (IOException e) { - RpmPackageBuildProposalsJob.updateAsync(); - SpecfileLog.logError(e); - } catch (InterruptedException e) { - // ignore - } - } else { - RpmPackageBuildProposalsJob.updateAsync(); - } - } + private void setPackagesList() { + String rpmpkgsFile = Activator.getDefault().getPreferenceStore() + .getString(PreferenceConstants.P_RPM_LIST_FILEPATH); + if (Files.exists(Paths.get(rpmpkgsFile))) { + try { + Set<String> newList = RpmPackageBuildProposalsJob.getPackages(); + list.clear(); + list.addAll(newList); + } catch (IOException e) { + RpmPackageBuildProposalsJob.updateAsync(); + SpecfileLog.logError(e); + } catch (InterruptedException e) { + // ignore + } + } else { + RpmPackageBuildProposalsJob.updateAsync(); + } + } - public List<String[]> getProposals(String prefix) { - int rpmpkgsMaxProposals = Activator.getDefault().getPreferenceStore() - .getInt(PreferenceConstants.P_RPM_LIST_MAX_PROPOSALS); - List<String[]> proposalsList = new ArrayList<>(list.size()); - for (String listValue : list) { - String item[] = new String[2]; - item[0] = listValue; - String message = Messages.RpmPackageProposalsList_0 - + rpmpkgsMaxProposals - + Messages.RpmPackageProposalsList_1; - item[1] = message; - if (item[0].startsWith(prefix)) { - proposalsList.add(item); - } - } - /* - * Show RPM informations only if the proposal list is less than the - * limit set in the RPM proposals preference page. - */ - if (proposalsList.size() < rpmpkgsMaxProposals) { - List<String[]> proposalsListWithInfo = new ArrayList<>(proposalsList.size()); - for (String[] proposals : proposalsList) { - proposals[1] = getRpmInfo(proposals[0]); - proposalsListWithInfo.add(proposals); - } - return proposalsListWithInfo; - } else { - return proposalsList; - } - } + public List<String[]> getProposals(String prefix) { + int rpmpkgsMaxProposals = Activator.getDefault().getPreferenceStore() + .getInt(PreferenceConstants.P_RPM_LIST_MAX_PROPOSALS); + List<String[]> proposalsList = new ArrayList<>(list.size()); + for (String listValue : list) { + String item[] = new String[2]; + item[0] = listValue; + String message = Messages.RpmPackageProposalsList_0 + rpmpkgsMaxProposals + + Messages.RpmPackageProposalsList_1; + item[1] = message; + if (item[0].startsWith(prefix)) { + proposalsList.add(item); + } + } + /* + * Show RPM informations only if the proposal list is less than the + * limit set in the RPM proposals preference page. + */ + if (proposalsList.size() < rpmpkgsMaxProposals) { + List<String[]> proposalsListWithInfo = new ArrayList<>(proposalsList.size()); + for (String[] proposals : proposalsList) { + proposals[1] = getRpmInfo(proposals[0]); + proposalsListWithInfo.add(proposals); + } + return proposalsListWithInfo; + } else { + return proposalsList; + } + } - public String getValue(String key) { - for (String item : list) { - if (item.equals(key.trim())) { - return getRpmInfo(item); - } - } - return null; - } + public String getValue(String key) { + for (String item : list) { + if (item.equals(key.trim())) { + return getRpmInfo(item); + } + } + return null; + } - public String getRpmInfo(String pkgName) { - String ret = ""; //$NON-NLS-1$ - try { - ret = org.eclipse.linuxtools.rpm.core.utils.Utils.runCommandToString("rpm", "-q", pkgName, "--qf", //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ - getformattedRpmInformations()); - } catch (IOException e) { - SpecfileLog.logError(e); - return Messages.RpmPackageProposalsList_2 - + Messages.RpmPackageProposalsList_3; - } - // Create encoder and decoder - CharsetDecoder decoder = Charset.forName(System.getProperty("file.encoding")).newDecoder(); //$NON-NLS-1$ - /* - * TODO: Jcharset may be used to detect the inputstream encoding if it's required? - * http://jchardet.sourceforge.net - */ - CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder(); //$NON-NLS-1$ - try { - ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(ret)); - CharBuffer cbuf = decoder.decode(bbuf); - ret = cbuf.toString(); - } catch (CharacterCodingException e) { - // If an error occurs when re-encoding the output, the original - // output is returned. - } - return ret; - } + public String getRpmInfo(String pkgName) { + String ret = ""; //$NON-NLS-1$ + try { + ret = org.eclipse.linuxtools.rpm.core.utils.Utils.runCommandToString("rpm", "-q", pkgName, "--qf", //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ + getformattedRpmInformations()); + } catch (IOException e) { + SpecfileLog.logError(e); + return Messages.RpmPackageProposalsList_2 + Messages.RpmPackageProposalsList_3; + } + // Create encoder and decoder + CharsetDecoder decoder = Charset.forName(System.getProperty("file.encoding")).newDecoder(); //$NON-NLS-1$ + /* + * TODO: Jcharset may be used to detect the inputstream encoding if it's + * required? http://jchardet.sourceforge.net + */ + CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder(); //$NON-NLS-1$ + try { + ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(ret)); + CharBuffer cbuf = decoder.decode(bbuf); + ret = cbuf.toString(); + } catch (CharacterCodingException e) { + // If an error occurs when re-encoding the output, the original + // output is returned. + } + return ret; + } - private String getformattedRpmInformations() { - StringBuilder formatedInfoString = new StringBuilder(); - IPreferenceStore store = Activator.getDefault().getPreferenceStore(); - if (store.getBoolean(PreferenceConstants.P_RPMINFO_NAME)) { - formatedInfoString.append("<b>Name: </b>%{NAME}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_VERSION)) { - formatedInfoString.append("<b>Version: </b>%{VERSION}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_RELEASE)) { - formatedInfoString.append("<b>Release: </b>%{Release}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_SUMMARY)) { - formatedInfoString.append("<b>Summary: </b>%{SUMMARY}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_LICENSE)) { - formatedInfoString.append("<b>License: </b>%{LICENSE}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_GROUP)) { - formatedInfoString.append("<b>Group: </b>%{GROUP}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_URL)) { - formatedInfoString.append("<b>URL: </b>%{URL}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_INSTALLTIME)) { - formatedInfoString.append("<b>Installation Date: </b>%{INSTALLTIME:date}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_DESCRIPTION)) { - formatedInfoString.append("<b>Description: </b>%{DESCRIPTION}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_PACKAGER)) { - formatedInfoString.append("<b>Packager: </b>%{PACKAGER}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_VENDOR)) { - formatedInfoString.append("<b>Vendor: </b>%{VENDOR}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_SIZE)) { - formatedInfoString.append("<b>Size: </b>%{SIZE} bytes<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_BUILDTIME)) { - formatedInfoString.append("<b>Build Date: </b>%{BUILDTIME:date}<br>"); //$NON-NLS-1$ - } - if (store.getBoolean(PreferenceConstants.P_RPMINFO_SOURCERPM)) { - formatedInfoString.append("<b>SRPM: </b>%{SOURCERPM}<br>"); //$NON-NLS-1$ - } - return formatedInfoString.toString(); + private String getformattedRpmInformations() { + StringBuilder formatedInfoString = new StringBuilder(); + IPreferenceStore store = Activator.getDefault().getPreferenceStore(); + if (store.getBoolean(PreferenceConstants.P_RPMINFO_NAME)) { + formatedInfoString.append("<b>Name: </b>%{NAME}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_VERSION)) { + formatedInfoString.append("<b>Version: </b>%{VERSION}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_RELEASE)) { + formatedInfoString.append("<b>Release: </b>%{Release}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_SUMMARY)) { + formatedInfoString.append("<b>Summary: </b>%{SUMMARY}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_LICENSE)) { + formatedInfoString.append("<b>License: </b>%{LICENSE}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_GROUP)) { + formatedInfoString.append("<b>Group: </b>%{GROUP}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_URL)) { + formatedInfoString.append("<b>URL: </b>%{URL}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_INSTALLTIME)) { + formatedInfoString.append("<b>Installation Date: </b>%{INSTALLTIME:date}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_DESCRIPTION)) { + formatedInfoString.append("<b>Description: </b>%{DESCRIPTION}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_PACKAGER)) { + formatedInfoString.append("<b>Packager: </b>%{PACKAGER}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_VENDOR)) { + formatedInfoString.append("<b>Vendor: </b>%{VENDOR}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_SIZE)) { + formatedInfoString.append("<b>Size: </b>%{SIZE} bytes<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_BUILDTIME)) { + formatedInfoString.append("<b>Build Date: </b>%{BUILDTIME:date}<br>"); //$NON-NLS-1$ + } + if (store.getBoolean(PreferenceConstants.P_RPMINFO_SOURCERPM)) { + formatedInfoString.append("<b>SRPM: </b>%{SOURCERPM}<br>"); //$NON-NLS-1$ + } + return formatedInfoString.toString(); - } + } } diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java index 4ca9842db1..ce73b94d77 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2013 Alphonse Van Assche. + * Copyright (c) 2007, 2016 Alphonse Van Assche. * 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 @@ -12,13 +12,14 @@ package org.eclipse.linuxtools.internal.rpm.ui.editor; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.linuxtools.internal.rpm.ui.editor.preferences.PreferenceConstants; -import org.eclipse.linuxtools.rpm.core.utils.Utils; import org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile; import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine; import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfilePackage; @@ -31,63 +32,69 @@ import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfilePackage; public class UiUtils { - public static void pluginSanityCheck() throws IOException { - boolean exists = (new File(PreferenceConstants.RPMMACRO_FILE)).exists(); - // Check if ~/.rpmmacros exist, if the file don't exist we create - // it with the appropriate command. - if (!exists && Utils.fileExist("/usr/bin/rpmdev-setuptree")) { //$NON-NLS-1$ - org.eclipse.linuxtools.rpm.core.utils.Utils.runCommandToInputStream("rpmdev-setuptree"); //$NON-NLS-1$ - } + public static void pluginSanityCheck() throws IOException { + boolean exists = (new File(PreferenceConstants.RPMMACRO_FILE)).exists(); + // Check if ~/.rpmmacros exist, if the file don't exist we create + // it with the appropriate command. + if (!exists && Files.exists(Paths.get("/usr/bin/rpmdev-setuptree"))) { //$NON-NLS-1$ + org.eclipse.linuxtools.rpm.core.utils.Utils.runCommandToInputStream("rpmdev-setuptree"); //$NON-NLS-1$ + } - // Check RPM tool preference. - String currentRpmTool = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.P_CURRENT_RPMTOOLS); - if (!Utils.fileExist("/usr/bin/yum")) { //$NON-NLS-1$ - if (currentRpmTool.equals(PreferenceConstants.DP_RPMTOOLS_YUM)) { - Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_CURRENT_RPMTOOLS, PreferenceConstants.DP_RPMTOOLS_RPM); - } - } else if (!Utils.fileExist("/usr/bin/urpmq")) { //$NON-NLS-1$ - if (currentRpmTool.equals(PreferenceConstants.DP_RPMTOOLS_URPM)) { - Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_CURRENT_RPMTOOLS, PreferenceConstants.DP_RPMTOOLS_RPM); - } - } - } + // Check RPM tool preference. + String currentRpmTool = Activator.getDefault().getPreferenceStore() + .getString(PreferenceConstants.P_CURRENT_RPMTOOLS); + if (!Files.exists(Paths.get("/usr/bin/yum"))) { //$NON-NLS-1$ + if (currentRpmTool.equals(PreferenceConstants.DP_RPMTOOLS_YUM)) { + Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_CURRENT_RPMTOOLS, + PreferenceConstants.DP_RPMTOOLS_RPM); + } + } else if (!Files.exists(Paths.get("/usr/bin/urpmq"))) { //$NON-NLS-1$ + if (currentRpmTool.equals(PreferenceConstants.DP_RPMTOOLS_URPM)) { + Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_CURRENT_RPMTOOLS, + PreferenceConstants.DP_RPMTOOLS_RPM); + } + } + } - /** - * Resolve defines for a given string. Defines in the string that are not found - * or have some other error will remain unchanged in the returned string. - * - * @param specfile The specfile containing the string to resolve. - * @param stringToResolve The string to resolve. - * @return The resolved string. - */ - public static String resolveDefines(Specfile specfile, String stringToResolve) { - String workingString = stringToResolve; - SpecfileDefine define; - try { - Pattern variablePattern = Pattern.compile("%\\{(\\S+?)\\}"); //$NON-NLS-1$ - Matcher variableMatcher = variablePattern.matcher(stringToResolve); - Set<String> variablesFound = new HashSet<>(); - while (variableMatcher.find()) { - String variable = variableMatcher.group(1); - if (variablesFound.contains(variable)) { - continue; - } - define = specfile.getDefine(variable); - if (define != null && !stringToResolve.equals(define.getUnresolvedStringValue())) { - workingString = workingString.replaceAll("\\%\\{"+variable+"\\}", define.getStringValue()); //$NON-NLS-1$ //$NON-NLS-2$ - } - } - return workingString; - } catch (Exception e) { - return stringToResolve; - } - } + /** + * Resolve defines for a given string. Defines in the string that are not + * found or have some other error will remain unchanged in the returned + * string. + * + * @param specfile + * The specfile containing the string to resolve. + * @param stringToResolve + * The string to resolve. + * @return The resolved string. + */ + public static String resolveDefines(Specfile specfile, String stringToResolve) { + String workingString = stringToResolve; + SpecfileDefine define; + try { + Pattern variablePattern = Pattern.compile("%\\{(\\S+?)\\}"); //$NON-NLS-1$ + Matcher variableMatcher = variablePattern.matcher(stringToResolve); + Set<String> variablesFound = new HashSet<>(); + while (variableMatcher.find()) { + String variable = variableMatcher.group(1); + if (variablesFound.contains(variable)) { + continue; + } + define = specfile.getDefine(variable); + if (define != null && !stringToResolve.equals(define.getUnresolvedStringValue())) { + workingString = workingString.replaceAll("\\%\\{" + variable + "\\}", define.getStringValue()); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + return workingString; + } catch (Exception e) { + return stringToResolve; + } + } - public static String getPackageDefineId(SpecfileDefine define, SpecfilePackage rpmPackage){ - return getPackageDefineId(define.getName(),rpmPackage); - } + public static String getPackageDefineId(SpecfileDefine define, SpecfilePackage rpmPackage) { + return getPackageDefineId(define.getName(), rpmPackage); + } - public static String getPackageDefineId(String defineName, SpecfilePackage rpmPackage){ - return defineName.toLowerCase() + ':' + rpmPackage.getPackageName(); - } + public static String getPackageDefineId(String defineName, SpecfilePackage rpmPackage) { + return defineName.toLowerCase() + ':' + rpmPackage.getPackageName(); + } } diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java index 619068e0a9..1bbc2e75bb 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Alphonse Van Assche. + * Copyright (c) 2007, 2016 Alphonse Van Assche. * 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 @@ -11,7 +11,9 @@ package org.eclipse.linuxtools.internal.rpm.ui.editor.preferences; -import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import org.eclipse.jface.preference.BooleanFieldEditor; @@ -21,7 +23,6 @@ import org.eclipse.jface.preference.RadioGroupFieldEditor; import org.eclipse.jface.preference.StringFieldEditor; import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator; -import org.eclipse.linuxtools.rpm.core.utils.Utils; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; @@ -36,109 +37,106 @@ import org.eclipse.ui.dialogs.PreferencesUtil; * RPM package proposals preference page class. * */ -public class RpmProposalsPreferencePage extends FieldEditorPreferencePage - implements IWorkbenchPreferencePage { - - private FieldEditor rpmtools; - - /* - * default constructor - */ - public RpmProposalsPreferencePage() { - super(FLAT); - setPreferenceStore(Activator.getDefault().getPreferenceStore()); - } - - @Override - public void createFieldEditors() { - rpmtools = rpmtoolsRadioGroupFieldEditor(); - addField(rpmtools); - // FIXME: there is validations problem when a FileFieldEditor is used, so - // as a quick fix, StringFieldEditor is used. - StringFieldEditor rpmListFieldEditor = new StringFieldEditor(PreferenceConstants.P_RPM_LIST_FILEPATH, - Messages.RpmProposalsPreferencePage_0, getFieldEditorParent()); - addField(rpmListFieldEditor); - addField(new BooleanFieldEditor(PreferenceConstants.P_RPM_LIST_BACKGROUND_BUILD,Messages.RpmProposalsPreferencePage_1, getFieldEditorParent())); - addField(buildTimeListRateFieldEditor()); - } - - @Override - protected Control createContents(final Composite parent) { - Link link= new Link(parent, SWT.NONE); - link.setText(Messages.RpmProposalsPreferencePage_2); - link.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - PreferencesUtil.createPreferenceDialogOn(parent.getShell() , e.text, null, null); - } - }); - Composite fieldEditorComposite = (Composite) super - .createContents(parent); - return fieldEditorComposite; - } - - private FieldEditor rpmtoolsRadioGroupFieldEditor() { - ArrayList<String[]> list = new ArrayList<>(); - list.add(new String[] { Messages.RpmProposalsPreferencePage_3, - PreferenceConstants.DP_RPMTOOLS_RPM }); - /* - * Show only installed tools. - * Don't forgot to add sanity check in UiUtils.pluginSanityCheck(). - */ - if (Utils.fileExist("/usr/bin/yum")) { //$NON-NLS-1$ - list.add(new String[] { Messages.RpmProposalsPreferencePage_4, - PreferenceConstants.DP_RPMTOOLS_YUM }); - } - if (Utils.fileExist("/usr/bin/urpmq")) { //$NON-NLS-1$ - list.add(new String[] { Messages.RpmProposalsPreferencePage_5, - PreferenceConstants.DP_RPMTOOLS_URPM }); - } - - String[][] radioItems = new String[list.size()][2]; - int pos = 0; - for (String[] item: list) { - radioItems[pos][0] = item[0]; - radioItems[pos][1] = item[1]; - pos++; - - } - - RadioGroupFieldEditor rpmToolsRadioGroupEditor = new RadioGroupFieldEditor( - PreferenceConstants.P_CURRENT_RPMTOOLS, - Messages.RpmProposalsPreferencePage_6, 1, radioItems , - getFieldEditorParent(), true); - return rpmToolsRadioGroupEditor; - } - - private FieldEditor buildTimeListRateFieldEditor() { - RadioGroupFieldEditor buildListTimeRateRadioGroupEditor = new RadioGroupFieldEditor( - PreferenceConstants.P_RPM_LIST_BUILD_PERIOD, - Messages.RpmProposalsPreferencePage_7, 1, new String[][] { - { Messages.RpmProposalsPreferencePage_8, "1" }, //$NON-NLS-1$ - { Messages.RpmProposalsPreferencePage_10, "2" }, //$NON-NLS-1$ - { Messages.RpmProposalsPreferencePage_12, "3" }}, getFieldEditorParent(), true); //$NON-NLS-1$ - return buildListTimeRateRadioGroupEditor; - } - - @Override - public void init(IWorkbench workbench) { - - } - - @Override - public void propertyChange(PropertyChangeEvent event) { - super.propertyChange(event); - if (event.getSource().equals(rpmtools)) { - if (!event.getOldValue().equals(event.getNewValue())) { - String rpmpkgsFile = Activator.getDefault() - .getPreferenceStore().getString( - PreferenceConstants.P_RPM_LIST_FILEPATH); - if (Utils.fileExist(rpmpkgsFile)) { - new File(rpmpkgsFile).delete(); - } - Activator.packagesList = null; - } - } - } +public class RpmProposalsPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage { + + private FieldEditor rpmtools; + + /* + * default constructor + */ + public RpmProposalsPreferencePage() { + super(FLAT); + setPreferenceStore(Activator.getDefault().getPreferenceStore()); + } + + @Override + public void createFieldEditors() { + rpmtools = rpmtoolsRadioGroupFieldEditor(); + addField(rpmtools); + // FIXME: there is validations problem when a FileFieldEditor is used, + // so + // as a quick fix, StringFieldEditor is used. + StringFieldEditor rpmListFieldEditor = new StringFieldEditor(PreferenceConstants.P_RPM_LIST_FILEPATH, + Messages.RpmProposalsPreferencePage_0, getFieldEditorParent()); + addField(rpmListFieldEditor); + addField(new BooleanFieldEditor(PreferenceConstants.P_RPM_LIST_BACKGROUND_BUILD, + Messages.RpmProposalsPreferencePage_1, getFieldEditorParent())); + addField(buildTimeListRateFieldEditor()); + } + + @Override + protected Control createContents(final Composite parent) { + Link link = new Link(parent, SWT.NONE); + link.setText(Messages.RpmProposalsPreferencePage_2); + link.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + PreferencesUtil.createPreferenceDialogOn(parent.getShell(), e.text, null, null); + } + }); + Composite fieldEditorComposite = (Composite) super.createContents(parent); + return fieldEditorComposite; + } + + private FieldEditor rpmtoolsRadioGroupFieldEditor() { + ArrayList<String[]> list = new ArrayList<>(); + list.add(new String[] { Messages.RpmProposalsPreferencePage_3, PreferenceConstants.DP_RPMTOOLS_RPM }); + /* + * Show only installed tools. Don't forgot to add sanity check in + * UiUtils.pluginSanityCheck(). + */ + if (Files.exists(Paths.get("/usr/bin/yum"))) { //$NON-NLS-1$ + list.add(new String[] { Messages.RpmProposalsPreferencePage_4, PreferenceConstants.DP_RPMTOOLS_YUM }); + } + if (Files.exists(Paths.get("/usr/bin/urpmq"))) { //$NON-NLS-1$ + list.add(new String[] { Messages.RpmProposalsPreferencePage_5, PreferenceConstants.DP_RPMTOOLS_URPM }); + } + + String[][] radioItems = new String[list.size()][2]; + int pos = 0; + for (String[] item : list) { + radioItems[pos][0] = item[0]; + radioItems[pos][1] = item[1]; + pos++; + + } + + RadioGroupFieldEditor rpmToolsRadioGroupEditor = new RadioGroupFieldEditor( + PreferenceConstants.P_CURRENT_RPMTOOLS, Messages.RpmProposalsPreferencePage_6, 1, radioItems, + getFieldEditorParent(), true); + return rpmToolsRadioGroupEditor; + } + + private FieldEditor buildTimeListRateFieldEditor() { + RadioGroupFieldEditor buildListTimeRateRadioGroupEditor = new RadioGroupFieldEditor( + PreferenceConstants.P_RPM_LIST_BUILD_PERIOD, Messages.RpmProposalsPreferencePage_7, 1, + new String[][] { { Messages.RpmProposalsPreferencePage_8, "1" }, //$NON-NLS-1$ + { Messages.RpmProposalsPreferencePage_10, "2" }, //$NON-NLS-1$ + { Messages.RpmProposalsPreferencePage_12, "3" } }, //$NON-NLS-1$ + getFieldEditorParent(), true); + return buildListTimeRateRadioGroupEditor; + } + + @Override + public void init(IWorkbench workbench) { + + } + + @Override + public void propertyChange(PropertyChangeEvent event) { + super.propertyChange(event); + if (event.getSource().equals(rpmtools)) { + if (!event.getOldValue().equals(event.getNewValue())) { + String rpmpkgsFile = Activator.getDefault().getPreferenceStore() + .getString(PreferenceConstants.P_RPM_LIST_FILEPATH); + try { + Files.deleteIfExists(Paths.get(rpmpkgsFile)); + } catch (IOException e) { + // ignore + } + Activator.packagesList = null; + } + } + } } diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java index efc6d6cd4c..c5e872f14f 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007-2013 Alphonse Van Assche and others. + * Copyright (c) 2007, 2016 Alphonse Van Assche 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 @@ -16,6 +16,8 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; +import java.nio.file.Files; +import java.nio.file.Paths; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; @@ -34,7 +36,6 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator; import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog; -import org.eclipse.linuxtools.rpm.core.utils.Utils; import org.eclipse.linuxtools.rpm.ui.editor.wizards.Messages; import org.eclipse.linuxtools.rpm.ui.editor.wizards.SpecfileNewWizardPage; import org.eclipse.ui.INewWizard; @@ -45,39 +46,39 @@ import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE; public class SpecfileNewWizard extends Wizard implements INewWizard { - private SpecfileNewWizardPage page; - private ISelection selection; + private SpecfileNewWizardPage page; + private ISelection selection; - /** - * Constructor for SpecfileNewWizard. - */ - public SpecfileNewWizard() { - super(); - setNeedsProgressMonitor(true); - } + /** + * Constructor for SpecfileNewWizard. + */ + public SpecfileNewWizard() { + super(); + setNeedsProgressMonitor(true); + } - /** - * Adding the page to the wizard. - */ - @Override - public void addPages() { - if (!Utils.fileExist("/usr/bin/rpmdev-newspec")) { //$NON-NLS-1$ - addPage(new NoExecutableWizardPage()); - } else { - page = new SpecfileNewWizardPage(selection); - addPage(page); - } - } + /** + * Adding the page to the wizard. + */ + @Override + public void addPages() { + if (!Files.exists(Paths.get("/usr/bin/rpmdev-newspec"))) { //$NON-NLS-1$ + addPage(new NoExecutableWizardPage()); + } else { + page = new SpecfileNewWizardPage(selection); + addPage(page); + } + } - /** - * This method is called when 'Finish' button is pressed in the wizard. We - * will create an operation and run it using wizard as execution context. - */ - @Override - public boolean performFinish() { - final String containerName = page.getProjectName(); - final String fileName = page.getFileName(); - final InputStream contentInputStream = openContentStream(); + /** + * This method is called when 'Finish' button is pressed in the wizard. We + * will create an operation and run it using wizard as execution context. + */ + @Override + public boolean performFinish() { + final String containerName = page.getProjectName(); + final String fileName = page.getFileName(); + final InputStream contentInputStream = openContentStream(); IRunnableWithProgress op = monitor -> { try { doFinish(containerName, fileName, contentInputStream, monitor); @@ -87,82 +88,77 @@ public class SpecfileNewWizard extends Wizard implements INewWizard { monitor.done(); } }; - try { - getContainer().run(true, false, op); - } catch (InterruptedException e) { - return false; - } catch (InvocationTargetException e) { - SpecfileLog.logError(e); - Throwable realException = e.getTargetException(); - MessageDialog.openError(getShell(), Messages.SpecfileNewWizard_0, - realException.getMessage()); - return false; - } - return true; - } + try { + getContainer().run(true, false, op); + } catch (InterruptedException e) { + return false; + } catch (InvocationTargetException e) { + SpecfileLog.logError(e); + Throwable realException = e.getTargetException(); + MessageDialog.openError(getShell(), Messages.SpecfileNewWizard_0, realException.getMessage()); + return false; + } + return true; + } - /** - * The worker method. It will find the container, create the file if missing - * or just replace its contents, and open the editor on the newly created - * file. - */ - private void doFinish(String projectName, String fileName, - InputStream contentInputStream, IProgressMonitor monitor) - throws CoreException { - monitor.beginTask(Messages.SpecfileNewWizard_1 + fileName, 2); - IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); - IResource resource = root.findMember(new Path(projectName)); - if (!resource.exists() || !(resource instanceof IContainer)) { - throwCoreException(Messages.SpecfileNewWizard_2 + projectName - + Messages.SpecfileNewWizard_3); - } - IContainer container = (IContainer) resource; - final IFile file = container.getFile(new Path(fileName)); - try { - InputStream stream = contentInputStream; - if (file.exists()) { - file.setContents(stream, true, true, monitor); - } else { - file.create(stream, true, monitor); - } - stream.close(); - } catch (IOException e) { - } - monitor.worked(1); - monitor.setTaskName(Messages.SpecfileNewWizard_4); - getShell().getDisplay().asyncExec(() -> { - IWorkbenchPage page = PlatformUI.getWorkbench() - .getActiveWorkbenchWindow().getActivePage(); - try { - IDE.openEditor(page, file, true); - } catch (PartInitException e) { - } + /** + * The worker method. It will find the container, create the file if missing + * or just replace its contents, and open the editor on the newly created + * file. + */ + private void doFinish(String projectName, String fileName, InputStream contentInputStream, IProgressMonitor monitor) + throws CoreException { + monitor.beginTask(Messages.SpecfileNewWizard_1 + fileName, 2); + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + IResource resource = root.findMember(new Path(projectName)); + if (!resource.exists() || !(resource instanceof IContainer)) { + throwCoreException(Messages.SpecfileNewWizard_2 + projectName + Messages.SpecfileNewWizard_3); + } + IContainer container = (IContainer) resource; + final IFile file = container.getFile(new Path(fileName)); + try { + InputStream stream = contentInputStream; + if (file.exists()) { + file.setContents(stream, true, true, monitor); + } else { + file.create(stream, true, monitor); + } + stream.close(); + } catch (IOException e) { + } + monitor.worked(1); + monitor.setTaskName(Messages.SpecfileNewWizard_4); + getShell().getDisplay().asyncExec(() -> { + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + try { + IDE.openEditor(page, file, true); + } catch (PartInitException e) { + } }); - monitor.worked(1); - } + monitor.worked(1); + } - /** - * We will initialize file contents with the modified template text. - */ - private InputStream openContentStream() { - String contents = page.getContent(); - return new ByteArrayInputStream(contents.getBytes()); - } + /** + * We will initialize file contents with the modified template text. + */ + private InputStream openContentStream() { + String contents = page.getContent(); + return new ByteArrayInputStream(contents.getBytes()); + } - private void throwCoreException(String message) throws CoreException { - IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, - IStatus.OK, message, null); - throw new CoreException(status); - } + private void throwCoreException(String message) throws CoreException { + IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, message, null); + throw new CoreException(status); + } - /** - * We will accept the selection in the workbench to see if we can initialize - * from it. - * - * @see INewWizard#init(IWorkbench, IStructuredSelection) - */ - @Override - public void init(IWorkbench workbench, IStructuredSelection selection) { - this.selection = selection; - } + /** + * We will accept the selection in the workbench to see if we can initialize + * from it. + * + * @see INewWizard#init(IWorkbench, IStructuredSelection) + */ + @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + this.selection = selection; + } }
\ No newline at end of file |