Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f461b552effe1e33d93de028078cb7fa391af45c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, 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
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.m2e.core.ui.internal.actions;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.ui.part.FileEditorInput;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.index.IIndex;
import org.eclipse.m2e.core.index.IndexedArtifact;
import org.eclipse.m2e.core.index.IndexedArtifactFile;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectRegistry;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog;


/**
 * Open POM Action
 * 
 * @author Eugene Kuleshov
 */
public class OpenPomAction extends ActionDelegate implements IWorkbenchWindowActionDelegate {
  private static final Logger log = LoggerFactory.getLogger(OpenPomAction.class);

  String type = IIndex.SEARCH_ARTIFACT;

  private IStructuredSelection selection;

  /* (non-Javadoc)
   * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
   */
  public void init(IWorkbenchWindow window) {
  }

  public void selectionChanged(IAction action, ISelection selection) {
    if(selection instanceof IStructuredSelection) {
      this.selection = (IStructuredSelection) selection;
    } else {
      this.selection = null;
    }
  }

  /* (non-Javadoc)
   * @see org.eclipse.ui.actions.ActionDelegate#run(org.eclipse.jface.action.IAction)
   */
  public void run(IAction action) {
    //TODO mkleint: this asks for rewrite.. having one action that does 2 quite different things based
    // on something as vague as selection passed in is unreadable..
    if(selection != null) {
      Object element = this.selection.getFirstElement();
      if(IIndex.SEARCH_ARTIFACT.equals(type) && element != null) {
        try {
          final ArtifactKey ak = SelectionUtil.getArtifactKey(element);
          if(ak != null) {
            new Job(Messages.OpenPomAction_job_opening) {
              protected IStatus run(IProgressMonitor monitor) {
                openEditor(ak.getGroupId(), ak.getArtifactId(), ak.getVersion(), monitor);
                return Status.OK_STATUS;
              }
            }.schedule();
            return;
          }
        } catch(CoreException ex) {
          log.error(ex.getMessage(), ex);
          PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
            public void run() {
              MessageDialog.openInformation(Display.getDefault().getActiveShell(), //
                  Messages.OpenPomAction_open_error_title, Messages.OpenPomAction_open_error_message);
            }
          });
        }
      }
    }

    String title = Messages.OpenPomAction_title_pom;

    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    MavenRepositorySearchDialog dialog = MavenRepositorySearchDialog.createOpenPomDialog(shell, title);
    if(dialog.open() == Window.OK) {
      final IndexedArtifactFile iaf = (IndexedArtifactFile) dialog.getFirstResult();
      new Job(Messages.OpenPomAction_job_opening) {
        protected IStatus run(IProgressMonitor monitor) {
          if(iaf != null) {
            openEditor(iaf.group, iaf.artifact, iaf.version, monitor);
          }
          return Status.OK_STATUS;
        }
      }.schedule();
    }
  }

  public static void openEditor(IndexedArtifact ia, IndexedArtifactFile f, IProgressMonitor monitor) {
    if(f == null || ia.getClassname() == null || ia.getPackageName() == null) {
      return;
    }

    String groupId = f.getDependency().getGroupId();
    String artifactId = f.getDependency().getArtifactId();
    String version = f.getDependency().getVersion();

    String name = ia.getClassname();
    String fileName = ia.getPackageName().replace('.', '/') + "/" + ia.getClassname() + ".java"; //$NON-NLS-1$ //$NON-NLS-2$
    String tooltip = groupId + ":" + artifactId + ":" + version + "/" + fileName; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    try {
      IMaven maven = MavenPlugin.getMaven();

      List<ArtifactRepository> artifactRepositories = maven.getArtifactRepositories();

      Artifact artifact = maven.resolve(groupId, artifactId, version, "java-source", "sources", artifactRepositories, //$NON-NLS-1$ //$NON-NLS-2$
          monitor);

      final File file = artifact.getFile();
      if(file == null) {
        openDialog(NLS.bind(Messages.OpenPomAction_error_download_source, tooltip));
        return;
      }

      // that won't work if source archive have subfolders before actual source tree
      String url = "jar:" + file.toURL().toString() + "!/" + fileName; //$NON-NLS-1$ //$NON-NLS-2$
      InputStream is = new URL(url).openStream();
      byte[] buff = readStream(is);

      openEditor(new MavenPathStorageEditorInput(name + ".java", tooltip, url, buff), name + ".java"); //$NON-NLS-1$ //$NON-NLS-2$

    } catch(IOException ex) {
      String msg = NLS.bind(Messages.OpenPomAction_error_open_editor, name);
      log.error(msg, ex);
      openDialog(msg + "\n" + ex.toString()); //$NON-NLS-1$
    } catch(CoreException ex) {
      log.error(ex.getMessage(), ex);
      openDialog(ex.getMessage() + "\n" + ex.toString()); //$NON-NLS-1$
    }
  }

  public static IEditorPart openEditor(String groupId, String artifactId, String version, IProgressMonitor monitor) {
    if(groupId.length() > 0 && artifactId.length() > 0) {
      final String name = groupId + ":" + artifactId + ":" + version + ".pom"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

      try {
        IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry();
        IMavenProjectFacade projectFacade = projectManager.getMavenProject(groupId, artifactId, version);
        if(projectFacade != null) {
          final IFile pomFile = projectFacade.getPom();
          return openEditor(new FileEditorInput(pomFile), name);
        }

        IMaven maven = MavenPlugin.getMaven();

        List<ArtifactRepository> artifactRepositories = maven.getArtifactRepositories();

        Artifact artifact = maven.resolve(groupId, artifactId, version, "pom", null, artifactRepositories, monitor); //$NON-NLS-1$

        File file = artifact.getFile();
        if(file != null) {
          return openEditor(new MavenPathStorageEditorInput(name, name, file.getAbsolutePath(),
              readStream(new FileInputStream(file))), name);
        }

        openDialog(NLS.bind(Messages.OpenPomAction_error_download, name));

      } catch(IOException ex) {
        String msg = NLS.bind(Messages.OpenPomAction_error_open_pom, name);
        log.error(msg, ex);
        openDialog(msg + "\n" + ex.toString()); //$NON-NLS-1$
      } catch(CoreException ex) {
        log.error(ex.getMessage(), ex);
        openDialog(ex.getMessage() + "\n" + ex.toString()); //$NON-NLS-1$
      }
    }

    return null;
  }

  public static IEditorPart openEditor(final IEditorInput editorInput, final String name) {
    final IEditorPart[] part = new IEditorPart[1];
    PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
      public void run() {
        IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
        IContentType contentType = contentTypeManager.findContentTypeFor(name);
        IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry();
        IEditorDescriptor editor = editorRegistry.getDefaultEditor(name, contentType);
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if(window != null) {
          IWorkbenchPage page = window.getActivePage();
          if(page != null) {
            try {
              part[0] = page.openEditor(editorInput, editor.getId());
            } catch(PartInitException ex) {
              MessageDialog.openInformation(Display.getDefault().getActiveShell(), //
                  Messages.OpenPomAction_open_title, NLS.bind(Messages.OpenPomAction_33, editorInput.getName(), ex.toString())); 
            }
          }
        }
      }
    });
    return part[0];
  }

  private static void openDialog(final String msg) {
    PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
      public void run() {
        MessageDialog.openInformation(Display.getDefault().getActiveShell(), //
            Messages.OpenPomAction_open_title, msg);
      }
    });
  }

  private static byte[] readStream(InputStream is) throws IOException {
    byte[] b = new byte[is.available()];
    int len = 0;
    while(true) {
      int n = is.read(b, len, b.length - len);
      if(n == -1) {
        if(len < b.length) {
          byte[] c = new byte[len];
          System.arraycopy(b, 0, c, 0, len);
          b = c;
        }
        return b;
      }
      len += n;
      if(len == b.length) {
        byte[] c = new byte[b.length + 1000];
        System.arraycopy(b, 0, c, 0, len);
        b = c;
      }
    }
  }

  /**
   * Storage editor input implementation for Maven poms
   */
  public static class MavenStorageEditorInput implements IStorageEditorInput {

    private final String name;

    private final String path;

    private final String tooltip;

    private final byte[] content;

    public MavenStorageEditorInput(String name, String tooltip, String path, byte[] content) {
      this.name = name;
      this.path = path;
      this.tooltip = tooltip;
      this.content = content;
    }

    // IStorageEditorInput

    public boolean exists() {
      return true;
    }

    public String getName() {
      return this.name;
    }

    public String getToolTipText() {
      return this.tooltip;
    }

    public IStorage getStorage() {
      return new MavenStorage(name, path, content);
    }

    public ImageDescriptor getImageDescriptor() {
      return null;
    }

    public IPersistableElement getPersistable() {
      return null;
    }

    @SuppressWarnings("rawtypes")
    public Object getAdapter(Class adapter) {
      return null;
    }

    public IPath getPath() {
      return path == null ? null : new Path(path);
    }

  }

  public static class MavenPathStorageEditorInput extends MavenStorageEditorInput implements IPathEditorInput {
    public MavenPathStorageEditorInput(String name, String tooltip, String path, byte[] content) {
      super(name, tooltip, path, content);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    //implemented as hinted by IPathEditorInput javadoc.
    public boolean equals(Object obj) {
      IPath path = getPath();
      if (path != null && obj instanceof MavenPathStorageEditorInput) {
        return path.equals(((MavenPathStorageEditorInput)obj).getPath());
      }
      return super.equals(obj);
    }
  }
  
  private static class MavenStorage implements IStorage {
    private String name;

    private final String path;

    private final byte[] content;

    public MavenStorage(String name, String path, byte[] content) {
      this.name = name;
      this.path = path;
      this.content = content;
    }

    public String getName() {
      return name;
    }

    public IPath getFullPath() {
      return path == null ? null : new Path(path);
    }

    public InputStream getContents() {
      return new ByteArrayInputStream(content);
    }

    public boolean isReadOnly() {
      return true;
    }

    @SuppressWarnings("rawtypes")
    public Object getAdapter(Class adapter) {
      return null;
    }
  }
}

Back to the top