Skip to main content
summaryrefslogtreecommitdiffstats
blob: e84de542fc6a9546357fc76eff0bd84f3369c01d (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
/*******************************************************************************
 * Copyright (c) 2010 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.ote.container;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.osee.framework.ui.workspacebundleloader.JarChangeResourceListener;
import org.eclipse.osee.ote.core.BundleInfo;
import org.eclipse.osee.ote.core.OteBundleLocator;
import org.eclipse.osee.ote.runtimemanager.OteUserLibsNature;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 * @author Andrew M. Finkbeiner
 */
public class OteClasspathContainer implements IClasspathContainer, IUserLibListener {
   public final static Path ID = new Path("OTE Classpath Container from the extension point");

   private Collection<ClassPathDescription> classPaths;

   private List<BundleInfo> betterPaths;

   private ServiceTracker tracker;

   private OteBundleLocator locator;

   private final JavaProject javaProject;

   private LibraryChangeProvider<OteUserLibsNature> userLibListener;

   private JarChangeResourceListener<OteUserLibsNature> userLibResourceListener;

   public OteClasspathContainer(IPath path, IJavaProject javaProject) throws CoreException {
      this.javaProject = (JavaProject) javaProject;

      try {
         BundleContext context = OteContainerActivator.getDefault().getContext();
         tracker = new ServiceTracker(context, OteBundleLocator.class.getName(), null);
         tracker.open(true);
         Object obj = tracker.waitForService(10000);
         locator = (OteBundleLocator) obj;

         OteContainerActivator.getDefault().getLibraryChangeProvider().addListener(this);
      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }

   private String findProjectRoot(String path) {
      File fileForPath = new File(path);
      File projectFile = recursivelyFindProjectFile(fileForPath);
      return projectFile.getAbsolutePath();
   }

   private File recursivelyFindProjectFile(File file) {

      if (file == null) {
         return file;
      }

      if (fileIsDirectoryWithBin(file)) {
         return file;
      } else {
         return recursivelyFindProjectFile(file.getParentFile());
      }
   }

   private boolean fileIsDirectoryWithBin(File file) {
      if (file.isDirectory()) {
         File binChildFile = new File(file.getAbsoluteFile() + "/bin");
         if (binChildFile.exists()) {
            return true;
         }
      }
      return false;
   }

   private String getWorkspaceRelativePath(String absolutePath) {
      return absolutePath;
   }

   @Override
   public IClasspathEntry[] getClasspathEntries() {
      List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
      Collection<BundleInfo> runtimeLibUrls;
      try {
         runtimeLibUrls = locator.getRuntimeLibs();
         classPaths = new ArrayList<ClassPathDescription>();
         betterPaths = new ArrayList<BundleInfo>();
         for (BundleInfo info : runtimeLibUrls) {

            String binaryFilePath = info.getSystemLocation().getFile();

            if (info.isSystemLibrary()) {
               entries.add(JavaCore.newLibraryEntry(new Path(binaryFilePath), new Path(binaryFilePath), new Path("/")));
            } else {
               File projectFilePath = recursivelyFindProjectFile(new File(binaryFilePath));
               binaryFilePath = "/" + projectFilePath.getName();

               entries.add(JavaCore.newProjectEntry(new Path(binaryFilePath)));
            }
         }

      } catch (Exception ex) {
         ex.printStackTrace();
      }

      IClasspathEntry[] retVal = new IClasspathEntry[entries.size()];
      return entries.toArray(retVal);
   }

   @Override
   public String getDescription() {
      return "OTE Classpath Container";
   }

   @Override
   public int getKind() {
      return IClasspathContainer.K_APPLICATION;
   }

   @Override
   public IPath getPath() {
      return ID;
   }

   private class ClassPathDescription {
      private final String sourcePath, binaryPath;

      public ClassPathDescription(String binaryPath, String sourcePath) {
         super();
         this.sourcePath = sourcePath;
         this.binaryPath = binaryPath;
      }

      /**
       * @return the sourcePath
       */
      public String getSourcePath() {
         return sourcePath;
      }

      /**
       * @return the binaryPath
       */
      public String getBinaryPath() {
         return binaryPath;
      }

   }

   @SuppressWarnings("restriction")
   @Override
   public void libraryChanged() {
   }

}

Back to the top