Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b6a0432cf8ab4f3974ec887a4a936d2e3abf101 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.workspacebundleloader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
import java.util.logging.Level;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Robert A. Fisher
 */
public class JarCollectionNature implements IProjectNature {

   private final Name BUNDLE_PATH_ATTRIBUTE;
   private boolean isClosing;

   protected IProject project;

   /**
    * @param BUNDLE_PATH_ATTRIBUTE the name of the attribute in the MANIFEST.MF to look at when looking for the path to
    * the jars being provided.
    */
   public JarCollectionNature(String BUNDLE_PATH_ATTRIBUTE) {
      super();
      this.BUNDLE_PATH_ATTRIBUTE = new Name(BUNDLE_PATH_ATTRIBUTE);
      this.isClosing = false;
   }

   /**
    * @return the isClosing
    */
   public boolean isClosing() {
      return isClosing;
   }

   /**
    * @param isClosing the isClosing to set
    */
   public void setClosing(boolean isClosing) {
      this.isClosing = isClosing;
   }

   @Override
   public void configure() {
   }

   @Override
   public void deconfigure() {
   }

   @Override
   public IProject getProject() {
      return project;
   }

   @Override
   public void setProject(IProject project) {
      this.project = project;
   }

   public Collection<URL> getBundles() {
      Collection<URL> urls = new ArrayList<URL>();

      if (isClosing) {
         return urls;
      }

      IPath[] paths = getProjectRelativeBundlePaths();
      for (IPath path : paths) {
         IPath pluginsPath = project.getLocation().append(path);
         File pluginDir = pluginsPath.toFile();
         File[] jars = pluginDir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
               return name.endsWith(".jar");
            }
         });

         if (jars != null) {
            for (File jar : jars) {
               try {
                  urls.add(jar.toURI().toURL());
               } catch (MalformedURLException ex) {
                  OseeLog.log(JarCollectionNature.class, Level.SEVERE, ex);
               }
            }
         }
      }
      return urls;
   }

   public IPath[] getProjectRelativeBundlePaths() {
      Manifest manifest = getManifestFile();
      Path[] paths;
      Attributes mainAttributes = manifest.getMainAttributes();
      String pathString;
      if (mainAttributes.containsKey(BUNDLE_PATH_ATTRIBUTE)) {
         pathString = mainAttributes.getValue(BUNDLE_PATH_ATTRIBUTE);
         String[] tempPaths = pathString.split(",");
         paths = new Path[tempPaths.length];
         for (int i = 0; i < paths.length; i++) {
            paths[i] = new Path(tempPaths[i].trim());
         }
      } else {
         paths = new Path[1];
         paths[0] = new Path("plugins");
      }
      return paths;
   }

   /**
    * @return
    */
   private Manifest getManifestFile() {
      try {
         File manifestFile = project.getLocation().append("META-INF").append("MANIFEST.MF").toFile();
         if (manifestFile.exists()) {
            return new Manifest(new FileInputStream(manifestFile));
         } else {
            return null;
         }
      } catch (Exception ex) {
         ex.printStackTrace();
         return null;
      }
   }

   @SuppressWarnings("unchecked")
   protected static <T extends JarCollectionNature> Collection<T> getWorkspaceProjects(String natureId, Class<T> clazz) throws CoreException {
      IWorkspace workspace = ResourcesPlugin.getWorkspace();
      IWorkspaceRoot workspaceRoot = workspace.getRoot();
      IProject[] projects = workspaceRoot.getProjects();

      Collection<T> natures = new LinkedList<T>();

      for (IProject project : projects) {
         if (project.isOpen()) {
            IProjectNature nature = project.getNature(natureId);
            if (nature != null) {
               JarCollectionNature jarNature = (JarCollectionNature) nature;
               natures.add((T) jarNature);
            }
         }
      }

      return natures;
   }
}

Back to the top