1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
3 import javax
.jcr
.Binary
;
4 import javax
.jcr
.Credentials
;
6 import javax
.jcr
.NodeIterator
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.Repository
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.RepositoryFactory
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.nodetype
.NodeType
;
13 import javax
.jcr
.query
.Query
;
14 import javax
.jcr
.query
.QueryResult
;
15 import javax
.jcr
.util
.TraversingItemVisitor
;
17 import org
.argeo
.api
.cms
.CmsLog
;
18 import org
.argeo
.cms
.security
.Keyring
;
19 import org
.argeo
.eclipse
.ui
.jcr
.EclipseJcrMonitor
;
20 import org
.argeo
.jcr
.JcrMonitor
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.argeo
.slc
.NameVersion
;
23 import org
.argeo
.slc
.SlcException
;
24 import org
.argeo
.slc
.SlcNames
;
25 import org
.argeo
.slc
.SlcTypes
;
26 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
27 import org
.argeo
.slc
.repo
.ArtifactIndexer
;
28 import org
.argeo
.slc
.repo
.JarFileIndexer
;
29 import org
.argeo
.slc
.repo
.RepoConstants
;
30 import org
.argeo
.slc
.repo
.RepoUtils
;
31 import org
.argeo
.slc
.repo
.maven
.AetherUtils
;
32 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
33 import org
.argeo
.slc
.repo
.osgi
.NormalizeGroup
;
34 import org
.eclipse
.aether
.artifact
.Artifact
;
35 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
36 import org
.eclipse
.core
.commands
.AbstractHandler
;
37 import org
.eclipse
.core
.commands
.ExecutionEvent
;
38 import org
.eclipse
.core
.commands
.ExecutionException
;
39 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
40 import org
.eclipse
.core
.runtime
.IStatus
;
41 import org
.eclipse
.core
.runtime
.Status
;
42 import org
.eclipse
.core
.runtime
.jobs
.Job
;
43 import org
.eclipse
.jface
.dialogs
.Dialog
;
44 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
45 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
46 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
47 import org
.eclipse
.swt
.SWT
;
48 import org
.eclipse
.swt
.graphics
.Point
;
49 import org
.eclipse
.swt
.layout
.GridData
;
50 import org
.eclipse
.swt
.layout
.GridLayout
;
51 import org
.eclipse
.swt
.widgets
.Button
;
52 import org
.eclipse
.swt
.widgets
.Composite
;
53 import org
.eclipse
.swt
.widgets
.Control
;
54 import org
.eclipse
.swt
.widgets
.Label
;
55 import org
.eclipse
.swt
.widgets
.Shell
;
56 import org
.eclipse
.swt
.widgets
.Text
;
57 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
59 /** Legacy - Make sure than Maven and OSGi metadata are consistent */
60 public class NormalizeDistribution
extends AbstractHandler
implements SlcNames
{
61 private final static CmsLog log
= CmsLog
62 .getLog(NormalizeDistribution
.class);
64 public final static String ID
= DistPlugin
.PLUGIN_ID
65 + ".normalizeDistribution";
66 public final static String DEFAULT_LABEL
= "Legacy Normalization...";
67 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
68 .getImageDescriptor("icons/normalize.gif");
70 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
71 public final static String PARAM_TARGET_REPO_PATH
= "targetRepoPath";
73 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
75 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
76 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
78 // DEPENDENCY INJECTION
79 private RepositoryFactory repositoryFactory
;
80 private Keyring keyring
;
81 private Repository nodeRepository
;
83 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
85 String targetRepoPath
= event
.getParameter(PARAM_TARGET_REPO_PATH
);
86 String wkspName
= event
.getParameter(PARAM_WORKSPACE_NAME
);
88 Session nodeSession
= null;
92 NormalizationDialog dialog
= new NormalizationDialog(
93 HandlerUtil
.getActiveShell(event
));
94 if (dialog
.open() != Dialog
.OK
)
97 nodeSession
= nodeRepository
.login();
98 Node repoNode
= nodeSession
.getNode(targetRepoPath
);
99 Repository repository
= RepoUtils
.getRepository(repositoryFactory
,
101 Credentials credentials
= RepoUtils
.getRepositoryCredentials(
104 String version
= dialog
.getVersion();
105 Boolean overridePoms
= dialog
.getOverridePoms();
107 job
= new NormalizeJob(repository
.login(credentials
, wkspName
),
108 version
, overridePoms
);
111 } catch (RepositoryException e
) {
112 throw new SlcException("Cannot normalize " + wkspName
, e
);
114 JcrUtils
.logoutQuietly(nodeSession
);
119 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
120 Binary origBinary
= null;
121 Binary osgiBinary
= null;
123 Session session
= sourcesNode
.getSession();
124 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(
125 sourcesNode
.getPath(), null);
127 // read name version from manifest
128 Artifact osgiArtifact
= new DefaultArtifact(
129 sourcesArtifact
.getGroupId(),
130 sourcesArtifact
.getArtifactId(),
131 sourcesArtifact
.getExtension(),
132 sourcesArtifact
.getVersion());
133 String osgiPath
= MavenConventionsUtils
.artifactPath(
134 artifactBasePath
, osgiArtifact
);
135 osgiBinary
= session
.getNode(osgiPath
).getNode(Node
.JCR_CONTENT
)
136 .getProperty(Property
.JCR_DATA
).getBinary();
138 NameVersion nameVersion
= RepoUtils
.readNameVersion(osgiBinary
141 // create PDe sources artifact
142 Artifact pdeSourceArtifact
= new DefaultArtifact(
143 sourcesArtifact
.getGroupId(),
144 sourcesArtifact
.getArtifactId() + ".source",
145 sourcesArtifact
.getExtension(),
146 sourcesArtifact
.getVersion());
147 String targetSourceParentPath
= MavenConventionsUtils
148 .artifactParentPath(artifactBasePath
, pdeSourceArtifact
);
149 String targetSourceFileName
= MavenConventionsUtils
150 .artifactFileName(pdeSourceArtifact
);
151 String targetSourceJarPath
= targetSourceParentPath
+ '/'
152 + targetSourceFileName
;
154 Node targetSourceParentNode
= JcrUtils
.mkfolders(session
,
155 targetSourceParentPath
);
156 origBinary
= sourcesNode
.getNode(Node
.JCR_CONTENT
)
157 .getProperty(Property
.JCR_DATA
).getBinary();
158 byte[] targetJarBytes
= RepoUtils
.packageAsPdeSource(
159 origBinary
.getStream(), nameVersion
);
160 JcrUtils
.copyBytesAsFile(targetSourceParentNode
,
161 targetSourceFileName
, targetJarBytes
);
164 Node targetSourceJarNode
= session
.getNode(targetSourceJarPath
);
165 artifactIndexer
.index(targetSourceJarNode
);
166 jarFileIndexer
.index(targetSourceJarNode
);
167 } catch (RepositoryException e
) {
168 throw new SlcException("Cannot add PDE sources for " + sourcesNode
,
171 JcrUtils
.closeQuietly(origBinary
);
172 JcrUtils
.closeQuietly(osgiBinary
);
177 private class NormalizeJob
extends Job
{
178 private Session session
;
179 private String version
;
180 private Boolean overridePoms
;
182 public NormalizeJob(Session session
, String version
,
183 Boolean overridePoms
) {
184 super("Normalize Distribution");
185 this.session
= session
;
186 this.version
= version
;
187 this.overridePoms
= overridePoms
;
191 protected IStatus
run(IProgressMonitor progressMonitor
) {
194 JcrMonitor monitor
= new EclipseJcrMonitor(progressMonitor
);
195 // normalize artifacts
196 Query countQuery
= session
199 .createQuery("select file from [nt:file] as file",
201 QueryResult result
= countQuery
.execute();
202 Long expectedCount
= result
.getNodes().getSize();
203 monitor
.beginTask("Normalize artifacts of "
204 + session
.getWorkspace().getName(),
205 expectedCount
.intValue());
206 NormalizingTraverser tiv
= new NormalizingTraverser(monitor
);
207 session
.getNode(artifactBasePath
).accept(tiv
);
210 Query groupQuery
= session
214 "select group from [" + SlcTypes
.SLC_GROUP_BASE
215 + "] as group", Query
.JCR_SQL2
);
216 NodeIterator groups
= groupQuery
.execute().getNodes();
217 monitor
.beginTask("Normalize groups of "
218 + session
.getWorkspace().getName(),
219 (int) groups
.getSize());
220 while (groups
.hasNext()) {
221 NormalizeGroup
.processGroupNode(groups
.nextNode(), version
,
222 overridePoms
, monitor
);
224 } catch (Exception e
) {
225 return new Status(IStatus
.ERROR
, DistPlugin
.PLUGIN_ID
,
226 "Cannot normalize distribution "
227 + session
.getWorkspace().getName(), e
);
229 JcrUtils
.logoutQuietly(session
);
231 return Status
.OK_STATUS
;
236 private class NormalizingTraverser
extends TraversingItemVisitor
{
239 public NormalizingTraverser(JcrMonitor monitor
) {
241 this.monitor
= monitor
;
245 protected void entering(Property property
, int level
)
246 throws RepositoryException
{
250 protected void entering(Node node
, int level
)
251 throws RepositoryException
{
252 if (node
.isNodeType(NodeType
.NT_FILE
)) {
253 if (node
.getName().endsWith("-sources.jar")) {
254 monitor
.subTask(node
.getName());
255 packageSourcesAsPdeSource(node
);
256 node
.getSession().save();
258 if (log
.isDebugEnabled())
259 log
.debug("Processed source artifact " + node
.getPath());
260 } else if (node
.getName().endsWith(".jar")) {
261 if (jarFileIndexer
.support(node
.getPath()))
262 if (artifactIndexer
.support(node
.getPath())) {
263 monitor
.subTask(node
.getName());
264 artifactIndexer
.index(node
);
265 jarFileIndexer
.index(node
);
266 node
.getSession().save();
268 if (log
.isDebugEnabled())
269 log
.debug("Processed artifact "
279 protected void leaving(Property property
, int level
)
280 throws RepositoryException
{
284 protected void leaving(Node node
, int level
) throws RepositoryException
{
289 public class NormalizationDialog
extends TitleAreaDialog
{
290 private static final long serialVersionUID
= -3103886455862638580L;
292 private Text versionT
;
293 private String version
;
294 private Button overridePomsC
;
295 private Boolean overridePoms
;
297 public NormalizationDialog(Shell parentShell
) {
301 protected Point
getInitialSize() {
302 return new Point(300, 250);
305 protected Control
createDialogArea(Composite parent
) {
306 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
307 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
309 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
310 composite
.setLayout(new GridLayout(2, false));
311 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
313 versionT
= createLT(composite
, "Version");
314 overridePomsC
= createLC(composite
, "Override POMs");
315 setMessage("Configure normalization", IMessageProvider
.NONE
);
322 protected void okPressed() {
323 version
= versionT
.getText();
324 overridePoms
= overridePomsC
.getSelection();
328 /** Creates label and text. */
329 protected Text
createLT(Composite parent
, String label
) {
330 new Label(parent
, SWT
.NONE
).setText(label
);
331 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.BORDER
333 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
337 /** Creates label and check. */
338 protected Button
createLC(Composite parent
, String label
) {
339 new Label(parent
, SWT
.NONE
).setText(label
);
340 Button check
= new Button(parent
, SWT
.CHECK
);
341 check
.setSelection(false);
342 check
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
346 protected void configureShell(Shell shell
) {
347 super.configureShell(shell
);
348 shell
.setText("Normalize...");
351 public String
getVersion() {
355 public Boolean
getOverridePoms() {
361 /* DEPENDENCY INJECTION */
362 public void setNodeRepository(Repository nodeRepository
) {
363 this.nodeRepository
= nodeRepository
;
366 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
367 this.repositoryFactory
= repositoryFactory
;
370 public void setKeyring(Keyring keyring
) {
371 this.keyring
= keyring
;