2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.client
.ui
.commands
;
18 import java
.util
.HashMap
;
19 import java
.util
.Iterator
;
22 import javax
.jcr
.Node
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.slc
.BasicNameVersion
;
27 import org
.argeo
.slc
.NameVersion
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.deploy
.ModulesManager
;
30 import org
.argeo
.slc
.jcr
.SlcNames
;
31 import org
.argeo
.slc
.jcr
.SlcTypes
;
32 import org
.eclipse
.core
.commands
.AbstractHandler
;
33 import org
.eclipse
.core
.commands
.ExecutionEvent
;
34 import org
.eclipse
.core
.commands
.ExecutionException
;
35 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
36 import org
.eclipse
.core
.runtime
.IStatus
;
37 import org
.eclipse
.core
.runtime
.Status
;
38 import org
.eclipse
.core
.runtime
.jobs
.Job
;
39 import org
.eclipse
.jface
.viewers
.ISelection
;
40 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
41 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
43 /** Deletes one or many results */
44 public class UpdateModule
extends AbstractHandler
{
45 private final static Log log
= LogFactory
.getLog(UpdateModule
.class);
47 private ModulesManager modulesManager
;
49 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
50 final ISelection selection
= HandlerUtil
51 .getActiveWorkbenchWindow(event
).getActivePage().getSelection();
52 if (selection
!= null && selection
instanceof IStructuredSelection
) {
53 UpdateJob job
= new UpdateJob(selection
);
60 private class UpdateJob
extends Job
{
61 private final IStructuredSelection selection
;
63 public UpdateJob(ISelection selection
) {
64 super("Update modules");
65 this.selection
= ((IStructuredSelection
) selection
);
69 protected IStatus
run(IProgressMonitor monitor
) {
70 Iterator
<?
> it
= selection
.iterator();
73 Map
<String
, Node
> nodes
= new HashMap
<String
, Node
>();
74 nodes
: while (it
.hasNext()) {
76 if (obj
instanceof Node
) {
77 Node node
= (Node
) obj
;
78 Node executionModuleNode
= null;
79 while (executionModuleNode
== null) {
80 if (node
.isNodeType(SlcTypes
.SLC_EXECUTION_MODULE
)) {
81 executionModuleNode
= node
;
83 node
= node
.getParent();
84 if (node
.getPath().equals("/"))// root
88 if (!nodes
.containsKey(executionModuleNode
.getPath()))
89 nodes
.put(executionModuleNode
.getPath(),
94 monitor
.beginTask("Update modules", nodes
.size());
95 for (Node executionModuleNode
: nodes
.values()) {
96 monitor
.subTask("Update " + executionModuleNode
.getName());
97 NameVersion nameVersion
= new BasicNameVersion(
98 executionModuleNode
.getProperty(SlcNames
.SLC_NAME
)
99 .getString(), executionModuleNode
100 .getProperty(SlcNames
.SLC_VERSION
)
102 modulesManager
.upgrade(nameVersion
);
104 log
.info("Module " + nameVersion
+ " updated");
105 if (monitor
.isCanceled())
106 return Status
.CANCEL_STATUS
;
108 return Status
.OK_STATUS
;
109 } catch (Exception e
) {
110 throw new SlcException("Cannot update module " + obj
, e
);
111 // return Status.CANCEL_STATUS;
116 protected void canceling() {
117 getThread().interrupt();
123 public void setModulesManager(ModulesManager modulesManager
) {
124 this.modulesManager
= modulesManager
;