I am creating a dynamic IProject in eclipse. This IProject acts like the "New Plug-in with JAR archives" project in eclipse. Then I point to the reference of this outside jar file. Which btw I extract then link to the temporary IProject.
<linkedResources>
<link>
<name>SampleTestSuite</name>
<type>2</type>
<location>D:/eclipse-rcp-indigo/tests/SampleTestSuite</location>
</link>
</linkedResources>
Now, I'm planning to make this IProject Run-As JUnit. I want to delete the extracted and linked jar file after the JUnit. This is my code below in running JUnit but after file deletion it will throw an error because the linkedresource will not be there. Any ideas how to monitor if JUnit is already finished so that the linkedresource will be deleted?
Activator.getDisplay().syncExec(new Runnable() {
public void run() {
Job job = new Job("Test Runner JUnit") {
#Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("Launching JUnit", 100);
try {
ILaunch launch = new Launch(launchConfiguration,
"run", null);
launch.setAttribute(
"org.eclipse.debug.ui.ATTR_CONSOLE_ENCODING",
DebugPlugin.getDefault().getLaunchManager()
.getEncoding(launchConfiguration));
DebugPlugin.getDefault().getLaunchManager()
.addLaunch(launch);
launchDelegate.launch(launchConfiguration, "run",
launch, monitor);
} catch (CoreException e) {
return e.getStatus();
} finally {
monitor.done();
}
return Status.OK_STATUS;
}
};
job.addJobChangeListener(new JobChangeAdapter() {
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
System.out.println("Job completed successfully");
// delete the file here
} else
System.out
.println("Job did not complete successfully");
}
});
job.setPriority(Job.INTERACTIVE);
job.schedule();
}
});
I just added this one
IProcess[] processes = launch.getProcesses();
for (IProcess process : processes) {
while (!process.isTerminated()) {
}
//delete here
}
Related
currently I am working on a plugin for Eclipse CDT.
What I want to achieve:
Prepend a tool (let us call it cov) before the commandline.
gcc -omain.o main.c
should become
cov gcc -omain.o main.c
I tried to write a custom BuildRunner, which extend ExternalBuildrunner, modifies the tools before invoking the external runner. I do this by editing the COmmandLinePattern
So far it works, if I do not restore the old CommandLinePattern.
Is there a way to update configuration to use the modified tools, and later after invocation write the old configurations back.
Before modifiyng, I backup the tool-commandlinepatterns, so that would not be the problem. I am missing the update step here I guess.
public class CovBuildRunner extends ExternalBuildRunner {
private final Set<String> VALUES;
public CovBuildRunner() {
VALUES = new HashSet<String>();
VALUES.add("gcc");
VALUES.add("g++");
}
#Override
public boolean invokeBuild(int kind, IProject project,
IConfiguration configuration, IBuilder builder, IConsole console,
IMarkerGenerator markerGenerator,
IncrementalProjectBuilder projectBuilder, IProgressMonitor monitor)
throws CoreException {
Map<ITool, String> cmdPatternBackup = new HashMap<>();
for (ITool tool : configuration.getTools()) {
if (cmdPatternBackup.containsKey(tool)) {
System.out.println("ERROR! TOOL ALREADY MODIFIED!");
} else if (this.supports(tool)) {
cmdPatternBackup.put(tool, tool.getCommandLinePattern());
tool.setCommandLinePattern("cov " + tool.getCommandLinePattern());
System.out.println(
configuration.getToolCommand(tool));
}
}
configuration.getToolChain().set;
boolean success = invokeExternalBuild(kind, project, configuration, builder, console, markerGenerator, projectBuilder, monitor);
for (ITool tool : cmdPatternBackup.keySet()) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Setting back " + tool.getName() + "\n" + tool.getCommandLinePattern());
tool.setCommandLinePattern(cmdPatternBackup.get(tool));
}
return success;
}
private boolean supports(ITool tool) {
String command = tool.getToolCommand();
return VALUES.contains(command);
}
}
I try make implementation for comparing the files before they are uploaded.
If file whith name is exist in system ask about create new version or just override it.
Here is the problem, how to get file name?
I can't use receiveUpload(), because after this method file is remove from upload component ?
The problem is that once you start an upload using the Upload component, it can only be interrupted by calling the interruptUpload() method, and you cannot resume anytime later.
The interruption is permanent.
This means you cannot pause in the middle of the upload to see if you already have the file in your system. You have to upload the file all the way.
Considering this drawback, you can sill check in your system if you have the file, after the upload finishes. If you have the file, you can show a confirmation dialog in which you decide wether to keep the file or overwrite.
The following is an example in which I check in the "system" (I just keep a String list with the filenames) if the file has already been uploaded:
public class RestrictingUpload extends Upload implements Upload.SucceededListener, Upload.Receiver {
private List<String> uploadedFilenames;
private ByteArrayOutputStream latestUploadedOutputStream;
public RestrictingUpload() {
setCaption("Upload");
setButtonCaption("Upload file");
addSucceededListener(this);
setReceiver(this);
uploadedFilenames = new ArrayList<String>();
}
#Override
public OutputStream receiveUpload(String filename, String mimeType) {
latestUploadedOutputStream = new ByteArrayOutputStream();
return latestUploadedOutputStream;
}
#Override
public void uploadSucceeded(SucceededEvent event) {
if (fileExistsInSystem(event.getFilename())) {
confirmOverwrite(event.getFilename());
} else {
uploadedFilenames.add(event.getFilename());
}
}
private void confirmOverwrite(final String filename) {
ConfirmDialog confirmDialog = new ConfirmDialog();
String message = String.format("The file %s already exists in the system. Overwrite?", filename);
confirmDialog.show(getUI(), "Overwrite?", message, "Overwrite", "Cancel", new ConfirmDialog.Listener() {
#Override
public void onClose(ConfirmDialog dialog) {
if (dialog.isConfirmed()) {
copyFileToSystem(filename);
}
}
});
}
private void copyFileToSystem(String filename) {
try {
IOUtils.write(latestUploadedOutputStream.toByteArray(), new FileOutputStream(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
private boolean fileExistsInSystem(String filename) {
return uploadedFilenames.contains(filename);
}
}
Note that I have used 2 external libraries:
Apache Commons IO 2.4 (http://mvnrepository.com/artifact/commons-io/commons-io/2.4) for writing to streams
ConfirmDialog from Vaadin Directory (https://vaadin.com/directory#addon/confirmdialog)
You can get the code snippet for this class from Gist: https://gist.github.com/gabrielruiu/9960772 which you can paste into your UI and test it out.
I want to run an action (with a rule) when a file enters the folder in my alfresco repository. The file needs to be moved to a new folder. The new folder will be named after the metadata property "subject" from the file I uploaded.
I am not able to figure out how to do this. Who got any tips?
(A repository webscript is also an option).
This is how I see it:
import java.util.List;
public class MoveExecuter extends ActionExecuterAbstractBase {
public static final String DESTINATION_FOLDER = "destination-folder";
private FileFolderService fileFolderService;
private NodeService nodeService;
#Override
protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
paramList.add(
new ParameterDefinitionImpl(DESTINATION_FOLDER,
DataTypeDefinition.NODE_REF,
true,
getParamDisplayLabel(METADATA VALUE FROM FIELD SUBJECT FROM INCOMING FILE)));}
public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
NodeRef destinationParent = (NodeRef)ruleAction.getParameterValue(DESTINATION_FOLDER);
// if the node exists
if (this.nodeService.exists(destinationParent) == true) {
try {
fileFolderService.move(incomingfile, destinationParent, null);
} catch (FileNotFoundException e) {
// Do nothing
}
if (this.nodeService.exists(destinationParent) == false) {
try {
nodeService.createNode(parentRef, assocTypeQName, assocQName, "metadata field subject");
fileFolderService.move(incomingfile, destinationParent, null);
} catch (FileNotFoundException e) {
// Do nothing
}
}
}
}
For such a simple action I'd just use a JavaScript instead of a java Action.
Install the JavaScript addon from googlecode or github (newer version)
And just write your Javascript code according the api and run it in runtime in the console to test your code.
My bat file is:
#echo off
java -cp * MyTimerTasker
I try to run main function in MyTimerTaskerClass.
All jars and bat file are in same folder.
When I try to run bat file with double click, it runs.
When I try to run with right click and run as administrator, command window shows and disappears but my main function does not start.
When I try to run with task scheduler, it never starts.
Edit: My main class.
public class MyTimerTasker {
public static void main(String[] args) throws IOException {
FTPDownloadFiles ftpDownloadFiles = new FTPDownloadFiles();
System.out.println("Running ...");
DatabaseTask databaseTask = new DatabaseTask();
databaseTask.connectToDatabase();
ftpDownloadFiles.downloadFiles();
try {
databaseTask.parseFiles(JdbcConnection.filesPath);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
databaseTask.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
You are adding the classpath of all the files in the working directory. You should add the name of the jar to the classpath.
If you start as administrator, it starts in another directory (same if you start as a scheduled task).
You will have to set your working directory in the batchfile:
cd /d "%~dp0"
This will change the working directory to the folder, where your batchfile resides.
I need to know which method is called inside eclipse when I press "CTRL+ SHIFT + O" (Organise Imports), in order to invoke it after a code generation. What the name of this method and where can I find it (Package.Interface)
Thanks
"Organize Imports" action is contributed by org.eclipse.jdt.ui.actions.OrganizeImportsAction, which, in turn, calls org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.
Finaly Managed to do it with this code (targetSite is a IWorkbench site initialized at the ame time as shell):
#Override
public void postLaunchAction(final IProject project, final IProgressMonitor monitor) throws CoreException {
super.postLaunchAction(project, monitor);
Runnable job = new Runnable() {
#Override
public void run() {
OrganizeImportsAction org = new OrganizeImportsAction(SpringServicesAction.this.targetSite);
try {
IJavaProject prj = null;
if (project.hasNature("org.eclipse.jdt.core.javanature")) {
prj = JavaCore.create(project);
}
IStructuredSelection selection = new StructuredSelection(prj);
org.run(selection);
} catch (CoreException ce) {
ce.printStackTrace();
}
}
};
this.shell.getDisplay().syncExec(job);
}
For reference, this is how I did it:
I made a large automated refactor in the codebase in our project. Due to a (i think so) bug in eclipse with refactoring static methods which are staticly imported in another file, i had to call organize imports after each refactor (also because I commit every move to git automaticly):
private void organizeImports(ICompilationUnit cu)
throws OperationCanceledException, CoreException {
cu.becomeWorkingCopy(null);
CompilationUnit unit = cu.reconcile(AST.JLS4, false, null, pm);
NullProgressMonitor pm = new NullProgressMonitor();
OrganizeImportsOperation op = new OrganizeImportsOperation(cu, unit,
true, true, true, null);
TextEdit edit = op.createTextEdit(pm);
if (edit == null) {
return;
}
JavaModelUtil.applyEdit(cu, edit, true, pm);
cu.commitWorkingCopy(true, pm);
cu.save(pm, true);
}
Disadvantagde: Discouraged access. If somebody has an idea to call this action properly without creating a new runnable and without using a shell etc., please comment.