Eclipse RCP p2 update not working - eclipse

I have a personal Eclipse RCP product (com.example.product) based on one personal feature (com.example.feature) which is composed of one personal plugin (com.example.plugin) and a bunch of others from Eclipse Helios (3.6). I want the app to check for updates and update itself if necessary from a p2 site. I want it to be headless, ie the user does not interact in the update process, but may see progress in a dialog.
I based my implementation for the updates on the RCP Mail application. I changed the P2Util.checkForUpdates method a bit to include some logging so I can see what, if anything, is going wrong there:
static IStatus checkForUpdates(IProvisioningAgent agent,
IProgressMonitor monitor) throws OperationCanceledException,
InvocationTargetException {
ProvisioningSession session = new ProvisioningSession(agent);
UpdateOperation operation = new UpdateOperation(session);
SubMonitor sub = SubMonitor.convert(monitor,
"Checking for application updates...", 200);
IStatus status = operation.resolveModal(sub.newChild(100));
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
return status;
}
if (status.getSeverity() == IStatus.CANCEL)
throw new OperationCanceledException();
if (status.getSeverity() != IStatus.ERROR) {
try {
logger.info( "Status is " + status );
Update[] updates = operation.getPossibleUpdates();
for( Update u : updates){
logger.info( "Update is " + u );
}
ProvisioningJob job = operation.getProvisioningJob(null);
if( job == null ){
logger.error( "Provisioning Job is null" );
}
status = job.runModal(sub.newChild(100));
if (status.getSeverity() == IStatus.CANCEL) {
throw new OperationCanceledException();
}
} catch ( Exception e ){
logger.error( "Exception while trying to get updates", e);
}
}
return status;
}
I have a p2.inf file in my feature at the same level as my example.product file. It contains:
org.eclipse.equinox.p2.touchpoint.eclipse.addRepository":
instructions.configure=\
org.eclipse.equinox.p2.touchpoint.eclipse.addRepository(type:0,location:file${#58}/C${#58}/workspace/updatesite/);\
org.eclipse.equinox.p2.touchpoint.eclipse.addRepository(type:1,location:file${#58}/C${#58}/workspace/updatesite/);
I build the product with plugin, feature and product IDs set to 1.0.0.
I can export and run my product from eclipse using the product export wizard. I tick generate metadata repository when I do this.
I create my update site using the Create an Update Site Project option in the Feature Manfiest Editor. I add my `com.example.feature' and build it. Just to see if it works I try browsing it via eclipse IDE Install New Software option and I can see the feature there.
I build the update site with all 3 IDs changed to 1.0.1. When I start the app it says there are no updates to install, there are no errors in the logs.
I don't know why it doesn't update from the update site, but things that have crossed my mind are:
1) I may need more info in the p2.inf file, but I'm not sure what, maybe something like namespace, name and range, but I can't find a good practical example.
2) In the checkForUpdates method I may need to do something with profiles to change what installable units are being updated. Again, I only found comments hinting at this and not any example code that shows how.
Any hints or ideas are much appreciated here, this is eating a lot of time.

Look at this code. Rebuild your product with a new product version and try to setup a http server. It didnt work with file repo for me. Just publishing the feature will not work.
final IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
sub = SubMonitor.convert(monitor, Messages.getString("UpdateManager.searchforupdates"), 200); //$NON-NLS-1$
final Update update = getUpdate(profile, provisioningContext, engine, context);
status = operation.resolveModal(sub.newChild(100));
LogHelper.log(status);
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
status = null;
return;
}
if (status.getSeverity() == IStatus.CANCEL)
throw new OperationCanceledException();
if (status.getSeverity() != IStatus.ERROR) {
log(IStatus.INFO, "Checking for available update matches", null); //$NON-NLS-1$
Update[] selected = new Update[1];
operation.setSelectedUpdates(new Update[0]);
for (Update available : operation.getPossibleUpdates()) {
if (available.equals(update)) {
log(IStatus.INFO, "Update matches available: " + update, null); //$NON-NLS-1$
selected[0] = available;
operation.setSelectedUpdates(selected);
}
}
if (selected[0] == null) {
status = null;
monitor.setCanceled(true);
log(IStatus.WARNING, "No Update matches selected", null); //$NON-NLS-1$
return;
}
ProvisioningJob job = operation.getProvisioningJob(monitor);
if (job != null) {
status = job.runModal(sub.newChild(100));
if (status.getSeverity() != IStatus.ERROR) {
prefStore.setValue(JUSTUPDATED, true);
Display.getDefault().syncExec(new Runnable() {
public void run() {
PlatformUI.getWorkbench().restart();
}
});
} else {
LogHelper.log(status);
}
} else {
log(IStatus.INFO, "getJob returned null", null); //$NON-NLS-1$
status = null;
}
if (status != null && status.getSeverity() == IStatus.CANCEL)
throw new OperationCanceledException();
}
}
};
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
new ProgressMonitorDialog(null).run(true, true, runnable);
} catch (InvocationTargetException x) {
log(IStatus.ERROR, "Runnable failure", x); //$NON-NLS-1$
} catch (InterruptedException e) {
}
}
});

#user473284's answer had some suggestions that I used but I don't know if they were definite requirements
1) using a local web server instead of trying to point to a file
2) Incrementing the product version and using the update repository generated by the export product wizard.
I never did find the implementation for the getUpdate method referenced from the code sample so I couldn't make use of the snippet.
After the above changes I was still left with the app detecting no updates on startup. Debugging showed that my repository was not showing up in the session. I had to explicitly add the update url in the code, despite having it in the p2.inf and in set in the feature manifest editor form field. Here's the code for this:
public static void addUpdateSite(IProvisioningAgent provisioningAgent)
throws InvocationTargetException {
// Load repository manager
IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) provisioningAgent
.getService(IMetadataRepositoryManager.SERVICE_NAME);
if (metadataManager == null) {
logger.error( "Metadata manager was null");
Throwable throwable = new
Throwable("Could not load Metadata Repository Manager");
throwable.fillInStackTrace();
throw new InvocationTargetException(throwable);
}
// Load artifact manager
IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) provisioningAgent
.getService(IArtifactRepositoryManager.SERVICE_NAME);
if (artifactManager == null) {
logger.error( "Artifact manager was null");
Throwable throwable = new Throwable(
"Could not load Artifact Repository Manager");
throwable.fillInStackTrace();
throw new InvocationTargetException(throwable);
}
// Load repo
try {
URI repoLocation = new URI("http://localhost/respository");
logger.info( "Adding repository " + repoLocation );
metadataManager.loadRepository(repoLocation, null);
artifactManager.loadRepository(repoLocation, null);
} catch (ProvisionException pe) {
logger.error( "Caught provisioning exception " + pe.getMessage(), pe);
throw new InvocationTargetException(pe);
} catch (URISyntaxException e) {
logger.error( "Caught URI syntax exception " + e.getMessage(), e);
throw new InvocationTargetException(e);
}
}
I now call this first thing in the checkForUpdates method from the original question. After this change my app at least now sees the update and attempts to install it. I'm still having problem but that deserves a separate question of its own which I've created at https://stackoverflow.com/questions/3944953/error-during-p2-eclipse-rcp-app-headless-update

Web server is not mandatory, you can get updates with file location.
It is mandatory to change product version too.
You can't update those features with Update Site Project build which are exported with product, however you can do that with some hacking in exported product.
If you add some other features with (Install New Softwares) option then you can update these features with Update Site Project build.
Hopefully this will be helpful. If you need more clarification you can ask.

Related

IArtifactRepositoryManager is null with Eclipse Oxygen 4.7.2

The following function worked perfectly in Eclipse Neon 4.6.x target runtime for a very long time:
public static boolean addRepository(IProvisioningAgent agent, String repo) {
Utils.log(String.format("adding repository at %s", repo));
IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
if (metadataManager == null || artifactManager == null) {
Utils.log("P2Utils.addRepository - missing metadata or artifact manager");
return false;
}
try {
URI uri = new URI(repo);
metadataManager.addRepository(uri);
artifactManager.addRepository(uri);
return true;
} catch (Exception e) {
Utils.log(e);
return false;
}
}
It stopped working with Eclipse Oxygen 4.7.2 target, the artifactManager returned is always null. Does anyone involved knows what was changed and how to fix this? It breaks our updates.., I am at a loss where to look.
Got the response from eclipse forum, there were some plugins missing:
org.eclipse.equinox.p2.artifact.repository
org.eclipse.equinox.p2.transport.ecf

Java WatchService code works in Eclipse but same code fails in Tomcat in Docker

In Eclipse, a unit test fires up this code and when the watched file is altered, outputs the new text.
In Tomcat, it hangs where shown.
I've grovelled around online for quite some time trying to find anyone with a similar problem, but nothing showed up.
This is vanilla watcher code, and because it works fine in Eclipse, there must be a Tomcat-specific issue, but what?
The file is accessible to the code in Tomcat. It's not a permissions problem.
public class Foo extends Thread {
File watchedFile = <the watched file>;
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = watchedFile.getParentFile().toPath();
dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
#Override
public void run() {
while (true) {
WatchKey key;
try {
key = watcher().take(); <<< HANGS HERE IN TOMCAT, DOESN'T HANG HERE IN ECLIPSE.
} catch (InterruptedException | ClosedWatchServiceException e) {
break;
}
try {
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
WatchEvent<Path> pathEvent = (WatchEvent<Path>)event;
if (pathEvent.context().toString().equals(watchedFile.getName()) {
// Do something.
}
}
}
} catch (Exception e) {
throw new RuntimeException("Trouble: " + e.getMessage(), e);
}
if (!key.reset()) {
break;
}
}
}
}
UPDATE: . The problem was that I was writing to the file from outside Docker, so the change event wasn’t seen.
It’s similar to Java WatchService not generating events while watching mapped drives.

Eclipse plugin - getting the IStackframe object from a selection in DebugView

So, this is the problem I am stuck at for a few weeks.
I am developing an Eclipse plugin which fills in a View with custom values depending on a particular StackFrame selection in the Debug View.
In particular, I want to listen to the stackframe selected and would like to get the underlying IStackFrame object.
However, I have tried more than a dozen things and all of them have failed. So I tried adding DebugContextListener to get the DebugContextEvent and finally the selection. However, the main issue is that ISelection doesn't return the underlying IStackFrame object. It instead returns an object of type AbstractDMVMNode.DMVMContext. I tried getting an adapter but that didn't work out too. I posted this question sometime back also:
Eclipse Plugin Dev- Extracting IStackFrame object from selection in Debug View
Since then, I have tried out many different approaches. I tried adding IDebugEventSetListener (but this failed as it cannot identify stack frame selection in the debug view).
I tried adding an object contribution action but this too was pointless as it ultimately returned me an ISelection which is useless as it only returns me an object of class AbstractDMVMNode.DMVMContext and not IStackframe.
Moreover, I checked out the implementation of the VariablesView source code itself in the org.eclipse.debug.ui plugin. It looks like a few versions back (VariablesView source code in version 3.2), the underlying logic was to use the ISelection and get the IStackFrame. All the other resources on the internet also advocate the same. However, now, this scheme no longer works as ISelection doesn't return you an IStackFrame. Also, the source code for the latest eclipse Debug plugin (which doesn't use this scheme) was not very intuitive for me.
Can anyone tell how I should proceed ? Is hacking the latest Eclipse source code for VariablesView my only option ? This doesn't look like a good design practice and I believe there should be a much more elegant way of doing this.
PS: I have tried all the techniques and all of them return an ISelection. So, if your approach too return the same thing, then it is most likely incorrect.
Edit (Code snippet for trying to adapt the ISelection):
// Following is the listener implemnetation
IDebugContextListener flistener = new IDebugContextListener() {
#Override
public void debugContextChanged(DebugContextEvent event) {
if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
contextActivated(event.getContext());
}
};
};
// Few things I tried in the contextActivated Method
//Attempt 1 (Getting the Adapter):
private void contextActivated(ISelection context) {
if (context instanceof StructuredSelection) {
Object data = ((StructuredSelection) context).getFirstElement();
if( data instanceof IAdaptable){
System.out.println("check1");
IStackFrame model = (IStackFrame)((IAdaptable)data).getAdapter(IStackFrame.class);
if(model != null){
System.out.println("success" + model.getName());
}
}
}
}
// Attemp2 (Directly getting it from ISelection):
private void contextActivated(ISelection context) {
if (context instanceof StructuredSelection) {
System.out.println("a");
Object data = ((StructuredSelection) context).getFirstElement();
if (data instanceof IStackFrame) {
System.out.println("yes");
} else {
System.out.println("no" + data.getClass().getName());
}
}
// This always execute the else and it prints: org.eclipse.cdt.dsf.ui.viewmodel.datamodel.AbstractDMVMNode.DMVMContext
}
// Attemp3 (Trying to obtain it from the viewer (similiar to object action binding in some ways):
private void contextActivated(ISelection context) {
VariablesView variablesView = (VariablesView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(IDebugUIConstants.ID_VARIABLE_VIEW);
if (variablesView != null) {
Object input = ((TreeViewer) variablesView.getViewer()).getInput();
if(input != null) System.out.println(input.getClass().getName());
if (input instanceof IStackFrame) {
System.out.println("success");
} else if (input instanceof IThread) {
System.out.println("success");
try {
IStackFrame[] stackFrames = ((IThread) input).getStackFrames();
for (IStackFrame iStackFrame : stackFrames) {
printVariables(iStackFrame);
}
} catch (DebugException e) {
e.printStackTrace();
}
}
}
}
While I am building this view to work with both JDT & CDT, I am testing it out on the C project. So, this might be the reason why I always get the returned object type as AbstractDMVMNode.DMVMContext. Should my implementation be different to handle both these cases ? I believe I should be building a generic view. Also, if AbstractDMVMNode.DMVMContext is CDT specific, I should I go about implementing it for the CDT case?

Problems when using robocode with JESS

I'm attempting to use JESS in order to utilise a rule-based system for making a robot. I've got both robocode and the JESS .jar imported into Eclipse. Here's my code -
public class myRobot extends Robot {
Rete r = new Rete();
public void run() {
try {
String reset = "(reset)";
r.executeCommand(reset);
String enemyInfo = "(deftemplate enemyInfo (slot present) (slot energy) (slot name))";
r.executeCommand(enemyInfo);
while (true) {
String command = "(assert (enemyInfo (present no) (energy -1) (name none)))";
r.executeCommand(command);
}
} catch (JessException ex) {
System.err.println(ex);
}
}
public void onScannedRobot(ScannedRobotEvent e) {
try {
String command = "(assert (enemyInfo (present yes) (energy " + e.getEnergy() + ") (name " + e.getName() + ")))";
r.executeCommand(command);
} catch (JessException ex) {
System.err.println(ex);
}
}
}
I haven't added in any rules yet because I just wanted to check that robocode and JESS run properly together. When I fire this up, the robocode application opens up. But when I try adding this robot in a battle and starting it, the application just freezes up completely.
I can't access the robot's console to see what's wrong since it hangs just immediately after I try and start a battle. Since all my System.out.println() debugging statements are printed to the robot's console as opposed to the main one, I can't even figure out what's wrong. Any suggestions to get this to work?
The problem is, that the robocode application does not know the JESS library. You will need to include it in the jar of your exported robot.
How to include librarys to jars

How do I avoid rebuilding my database everytime I make a change to my Windows Workflow

I am building a Windows Workflow into a .Net webapp. The problem I have is that every time I add or remove or change an activity in the state machine and then re-compile and run it, it no longer matches up to what is in the database for tracking and persistence. Instead of updating the changes, or creating a new version of the workflow in the database it throws an error. Sometimes it an "index out of bounds" error, other times its "Cannot get the member XXXXX."
To get it to work I have to delete the database and re-build it. I believe this works because when the workflow engine looks for the workflow in the database it doesn't find it and thus adds it to the database. This is very frustrating at the stage I am at, where I am building the workflow and it changes many times in a day.
Is there a way I can simply get the workflow engine to update the relevant tables in the database with the changes I have made to the workflow? Or overwrite it with a new version? I have looked at various webpages on setting up the workflow for dynamic updating at runtime but none of these suggestions worked for me. And whilst that is a feature I would like for my workflow, I am not sure if it is really the way to solve the problem I am having. It is not so much that I am creating a new version of the workflow, rather I am building a workflow and want to test the part I have just finished/added and I don't care about previous versions of the workflow and any partially complete workflows in the database.
Thanks.
I too faced similar problem , everytime Workflow design was change, all the old workflow instances in the DB use to throw error.
To overcome this , I created Tracking profile for each type of workflow. If I changed any workflow, i updated the version of my profile. This worked for me ..Here is the sample code -
For this you need to add tracking service to workflow runtime .
TrackingProfile profile = CreateProfile();
StoreProfileToDB(profile, connString, typeof(ExceptionWF.ParameterExceptionWF),"2.0.0.0");
private static TrackingProfile CreateProfile()
{
TrackingProfile myProfile = new TrackingProfile();
ActivityTrackingLocation stateActivityLocation = CreateActivityLocation(typeof(StateActivity));
AddActivityExecutionStatus(stateActivityLocation);
ActivityTrackingLocation eventDrivenActLoc = CreateActivityLocation(typeof(EventDrivenActivity));
AddActivityExecutionStatus(eventDrivenActLoc);
ActivityTrackPoint actPt = new ActivityTrackPoint();
actPt.MatchingLocations.Add(stateActivityLocation);
actPt.MatchingLocations.Add(eventDrivenActLoc);
myProfile.ActivityTrackPoints.Add(actPt);
WorkflowTrackPoint workflowTrack = CreateWorkflowTrackPoint();
myProfile.WorkflowTrackPoints.Add(workflowTrack);
UserTrackPoint utp = new UserTrackPoint();
UserTrackingLocation ul = new UserTrackingLocation();
ul.ActivityType = typeof(HandleExternalEventActivity);
ul.ArgumentType = typeof(object);
ul.MatchDerivedArgumentTypes = true;
ul.MatchDerivedActivityTypes = true;
utp.MatchingLocations.Add(ul);
myProfile.UserTrackPoints.Add(utp);
myProfile.Version = new Version("1.0.0.0");
return myProfile;
}
private static void StoreProfileToDB(TrackingProfile profile, string connString, Type wfType,string version)
{
TrackingProfileSerializer serializer = new TrackingProfileSerializer();
System.IO.StringWriter writer = new System.IO.StringWriter(new StringBuilder());
serializer.Serialize(writer, profile);
SqlConnection conn = null;
try
{
if (!String.IsNullOrEmpty(connString))
{
conn = new SqlConnection(connString);
string storedProc = "dbo.UpdateTrackingProfile";
SqlCommand cmd = new SqlCommand(storedProc, conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("#TypeFullName", SqlDbType.NVarChar, 128);
param.Direction = ParameterDirection.Input;
param.Value = wfType.FullName;
cmd.Parameters.Add(param);
param = new SqlParameter("#AssemblyFullName", SqlDbType.NVarChar, 256);
param.Direction = ParameterDirection.Input;
param.Value = wfType.Assembly.FullName;
cmd.Parameters.Add(param);
param = new SqlParameter("#Version", SqlDbType.VarChar, 32);
param.Direction = ParameterDirection.Input;
//Note that you should increment version number for your
//TrackingProfile to be able to use new TrackingProfile.
//Default version is "1.0.0.0, It uses the default profile if not increamented.
param.Value = version;
cmd.Parameters.Add(param);
param = new SqlParameter("#TrackingProfileXml", SqlDbType.NText);
param.Direction = ParameterDirection.Input;
param.Value = writer.ToString();
cmd.Parameters.Add(param);
conn.Open();
cmd.ExecuteNonQuery();
}//if
}// try
catch (Exception ex)
{
if (ex is SqlException)
{
//Check to see if it's a version error
if (ex.Message.Substring(0, 24) == "A version already exists")
{
EventLogger.Log("A profile with the same version already exists in database");
}//if
else
{
EventLogger.Log("Error writing profile to database : " + ex.ToString());
}
}
else
{
EventLogger.Log("Error writing profile to database : " + ex.ToString());
}
}
finally
{
if (conn != null) { conn.Close(); }
}
}