summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/2.0-M1/tools/eclipse/plugins/core/org/apache/tuscany/sca/plugin/core/classpath/BaseClasspathContainer.java
blob: ee9a3d2de9885b2b060f2c01fa01723158e549fc (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.plugin.core.classpath;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.tuscany.sca.plugin.core.classpath.RuntimeClasspathContainer;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
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.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.osgi.framework.Bundle;

/**
 * A base classpath container implementation.
 * 
 * @version $Rev$ $Date$
 */
public class BaseClasspathContainer implements RuntimeClasspathContainer {
    
    private String pluginID;
    private String libraryID;
    private String libraryName;
    private String distributionName;
    private String sourceDistributionName;
    private String distributionVersion;
    private String homeProperty;
    private String sourceProperty;
    
    /**
     * Constructs a new Classpath container.
     * 
     * @param pluginID
     * @param libraryID
     * @param libraryName
     * @param distributionName
     * @param distributionVersion
     * @param homeProperty
     * @param sourceProperty
     */
    public BaseClasspathContainer(String pluginID, String libraryID, String libraryName,
                                  String distributionName, String sourceDistributionName, String distributionVersion,
                                  String homeProperty, String sourceProperty) {
        this.pluginID = pluginID;
        this.libraryID = libraryID;
        this.libraryName = libraryName;
        this.distributionName = distributionName;
        this.sourceDistributionName = sourceDistributionName;
        this.distributionVersion = distributionVersion;
        this.homeProperty = homeProperty;
        this.sourceProperty = sourceProperty;
    }

    public IClasspathEntry[] getClasspathEntries() {
        List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
        
        // Get the runtime distribution location
        IPath runtimePath = runtimePath();
        
        // Get the source distribution location
        IPath sourcePath = sourcePath();
        
        // Add the JARs from runtime/lib and runtime/modules as classpath entries
        if (runtimePath != null) {
            
            // Add the jars from runtime/modules
            File modulesDirectory = runtimePath.append("modules").toFile();
            if (modulesDirectory != null && modulesDirectory.exists()) {
                for (File file : modulesDirectory.listFiles()) {
                    IPath path = new Path(file.getPath());
                    String name = path.lastSegment();
                    String extension = path.getFileExtension();
                    
                    // Only include API and launcher JARs
                    if (!"jar".equals(extension)) {
                        continue;
                    }
                    if (name.indexOf("-api-") == -1 && name.indexOf("-launcher-") == -1) {
                        continue;
                    }

                    list.add(JavaCore.newLibraryEntry(path, sourcePath, null));
                }
            }

            // Add the jars from runtime/lib
            File libDirectory = runtimePath.append("lib").toFile();
            if (libDirectory != null && libDirectory.exists()) {
                for (File file : libDirectory.listFiles()) {
                    IPath path = new Path(file.getPath());
                    String name = path.lastSegment();
                    String extension = path.getFileExtension();
                    
                    // Only include jaxb, jaxws and jsr API JARs
                    if (!"jar".equals(extension)) {
                        continue;
                    }
                    if (name.indexOf("-api-") != -1) {
                        if (name.startsWith("jaxb") || name.startsWith("jaxws") || name.startsWith("jsr")) {
                            list.add(JavaCore.newLibraryEntry(path, sourcePath, null));
                        }
                    }
                }
            }
        }
        
        return (IClasspathEntry[])list.toArray(new IClasspathEntry[list.size()]);
    }

    public IClasspathEntry[] getRuntimeClasspathEntries() {
        
        // Get the runtime distribution location
        IPath runtimePath = runtimePath();

        if (runtimePath != null) {
            return new IClasspathEntry[] {JavaCore.newLibraryEntry(runtimePath, null, null)};
        } else {
            return new IClasspathEntry[0];
        }
    }

    public String getDescription() {
        return libraryName;
    }

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

    public IPath getPath() {
        return new Path(libraryID);
    }

    /**
     * Returns the location of the runtime distribution.
     * 
     * @return
     */
    private IPath runtimePath() {
        IPath path = artifactLocation(pluginID, distributionName, distributionVersion, null, null);
        
        if (path == null) {
            
            // Try to get the location of the distribution from
            // the HOME property or environment variable
            String home = System.getProperty(homeProperty);
            if (home == null || home.length() == 0) {
                home = System.getenv(homeProperty);
            }
            if (home != null && home.length() != 0) {
                if (new File(home).exists()) {
                    path = new Path(home);
                }
            }
        }
        return path;
    }
    
    /**
     * Returns the location of the source distribution.
     * 
     * @return
     */
    private IPath sourcePath() {
        IPath path = artifactLocation(pluginID, sourceDistributionName, distributionVersion, "src", ".zip");

        if (path == null) {
            
            // Try to get the location of the source distribution from
            // the SRC property or environment variable
            String source = System.getProperty(sourceProperty);
            if (source == null || source.length() == 0) {
                source = System.getenv(sourceProperty);
            }
            if (source != null && source.length() != 0) {
                if (new File(source).exists()) {
                    path = new Path(source);
                }
            }
        }
        return path;
    }

    /**
     * Returns the location of the specified artifact.
     *  
     * @param pluginId
     * @param artifactId
     * @param version
     * @param classifier
     * @param extension
     * @return
     */
    private IPath artifactLocation(String pluginId, String artifactId, String version, String classifier, String extension) {
        String artifactName;
        if (classifier != null) {
            artifactName = artifactId + '-' + version + '-' + classifier; 
        } else {
            artifactName = artifactId + '-' + version; 
        }
        if (extension != null) {
            artifactName += extension;
        }
        try {
            Bundle bundle = Platform.getBundle(pluginId);
            URL location = FileLocator.find(bundle, new Path(artifactName), null);
            location = FileLocator.toFileURL(location);
            IPath path = new Path(new File(location.toURI()).getPath());
            return path;
        } catch (Exception e) {
            error("Artifact not found: " + artifactName, e);
            return null;
        }
    }

    /**
     * Log an error.
     * 
     * @param msg
     * @param e
     */
    private void error(String msg, Exception e) {
        Platform.getLog(
                        Platform.getBundle(pluginID)).log(
                        new Status(IStatus.ERROR, pluginID, IStatus.OK, msg, e));
    }

}