summaryrefslogtreecommitdiffstats
path: root/branches/pre-spec-changes/services/plugins/plugin.itest/src/main/java/org/apache/tuscany/sca/plugin/itest/TuscanyStartMojo.java
blob: 178561b87081a9c02fc15245eddd6d8f5a6f4ce9 (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
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
/*
 * 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.itest;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.tuscany.host.runtime.InitializationException;

import org.osoa.sca.CompositeContext;
import org.osoa.sca.CurrentCompositeContext;

/**
 * @version $Rev$ $Date$
 * @goal start
 * @phase pre-integration-test
 */
public class TuscanyStartMojo extends AbstractMojo {

    public class MavenEmbeddedArtifactRepository implements org.apache.tuscany.spi.services.artifact.ArtifactRepository {
        public static final String COMPONENT_NAME = "MavenEmbeddedArtifactRepository";

        public void resolve(org.apache.tuscany.spi.services.artifact.Artifact artifact) {
            resolveTransitively(artifact);
        }

        public void resolve(Collection artifacts) {
            for (Object a : artifacts) {
                resolve((Artifact)a);
            }
        }

        /**
         * Resolves the dependencies transitively.
         * 
         * @param rootArtifact Artifact whose dependencies need to be resolved.
         */
        public boolean resolveTransitively(org.apache.tuscany.spi.services.artifact.Artifact rootArtifact) {

            org.apache.maven.artifact.Artifact mavenRootArtifact =
                artifactFactory.createArtifact(rootArtifact.getGroup(), rootArtifact.getName(), rootArtifact
                    .getVersion(), org.apache.maven.artifact.Artifact.SCOPE_RUNTIME, rootArtifact.getType());

            try {

                if (resolve(mavenRootArtifact)) {
                    rootArtifact.setUrl(mavenRootArtifact.getFile().toURL());
                    if (resolveDependencies(rootArtifact, mavenRootArtifact)) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } catch (MalformedURLException ex) {
                throw new IllegalArgumentException(ex);
            }

        }

        /*
         * Resolves the artifact.
         */
        private boolean resolve(org.apache.maven.artifact.Artifact mavenRootArtifact) {

            try {
                resolver.resolve(mavenRootArtifact, remoteRepositories, localRepository);
                return true;
            } catch (ArtifactResolutionException ex) {
                return false;
            } catch (ArtifactNotFoundException ex) {
                return false;
            }

        }

        /*
         * Resolves transitive dependencies.
         */
        private boolean resolveDependencies(org.apache.tuscany.spi.services.artifact.Artifact rootArtifact,
                                            org.apache.maven.artifact.Artifact mavenRootArtifact) {

            try {

                ResolutionGroup resolutionGroup = null;
                ArtifactResolutionResult result = null;

                resolutionGroup = metadataSource.retrieve(mavenRootArtifact, localRepository, remoteRepositories);
                result =
                    resolver.resolveTransitively(resolutionGroup.getArtifacts(),
                                                 mavenRootArtifact,
                                                 remoteRepositories,
                                                 localRepository,
                                                 metadataSource);

                // Add the artifacts to the deployment unit
                for (Object obj : result.getArtifacts()) {
                    org.apache.maven.artifact.Artifact depArtifact = (org.apache.maven.artifact.Artifact)obj;
                    org.apache.tuscany.spi.services.artifact.Artifact artifact =
                        new org.apache.tuscany.spi.services.artifact.Artifact();
                    artifact.setName(depArtifact.getArtifactId());
                    artifact.setGroup(depArtifact.getGroupId());
                    artifact.setType(depArtifact.getType());
                    artifact.setClassifier(depArtifact.getClassifier());
                    artifact.setUrl(depArtifact.getFile().toURL());
                    artifact.setVersion(depArtifact.getVersion());
                    rootArtifact.addDependency(artifact);
                }

            } catch (ArtifactMetadataRetrievalException ex) {
                return false;
            } catch (MalformedURLException ex) {
                throw new IllegalArgumentException(ex);
            } catch (ArtifactResolutionException ex) {
                return false;
            } catch (ArtifactNotFoundException ex) {
                return false;
            }

            return true;

        }

    }

    /**
     * @parameter
     */
    private URL systemScdl;

    /**
     * @parameter
     */
    private URL applicationScdl;

    /**
     * @parameter expression="${project.testClasspathElements}"
     * @required
     * @readonly
     */
    private List testClassPath;

    /**
     * Extensions
     * 
     * @parameter
     */
    private Dependency[] extensions = new Dependency[0];

    /**
     * Used to look up Artifacts in the remote repository.
     * 
     * @parameter expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
     * @required
     * @readonly
     */
    private ArtifactResolver resolver;

    /**
     * Used to look up Artifacts in the remote repository.
     * 
     * @parameter expression="${component.org.apache.maven.artifact.metadata.ArtifactMetadataSource}"
     * @required
     * @readonly
     */
    private ArtifactMetadataSource metadataSource;

    /**
     * Location of the local repository.
     * 
     * @parameter expression="${localRepository}"
     * @readonly
     * @required
     */
    private ArtifactRepository localRepository;

    /**
     * List of Remote Repositories used by the resolver
     * 
     * @parameter expression="${project.remoteArtifactRepositories}"
     * @readonly
     * @required
     */
    private List remoteRepositories;

    /**
     * Used to look up Artifacts in the remote repository.
     * 
     * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
     * @required
     * @readonly
     */
    private ArtifactFactory artifactFactory;

    static ThreadLocal<ClassLoader> foo = new ThreadLocal<ClassLoader>();

    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("Starting Tuscany...");

        ClassLoader hostClassLoader = getClass().getClassLoader();
        if (systemScdl == null) {
            systemScdl = hostClassLoader.getResource("META-INF/tuscany/embeddedMaven.scdl");
        }

        MavenRuntimeInfo runtimeInfo = new MavenRuntimeInfo();
        MavenEmbeddedRuntime runtime = new MavenEmbeddedRuntime();
        runtime.setArtifactRepository(new MavenEmbeddedArtifactRepository());

        for (Dependency d : extensions) {
            try {
                Artifact artifact = d.getArtifact(artifactFactory);
                resolver.resolve(artifact, remoteRepositories, localRepository);
                URL url = artifact.getFile().toURL();
                getLog().info(url.toString());
                runtime.addExtension(artifact.getGroupId() + ":" + artifact.getArtifactId(), url);
            } catch (Exception e) {
                throw new MojoExecutionException("Fail to resolve an extension", e);
            }
        }

        runtime.setSystemScdl(systemScdl);
        runtime.setHostClassLoader(hostClassLoader);

        ClassLoader applicationClassLoader = createApplicationClassLoader(hostClassLoader);
        if (applicationScdl == null) {
            Enumeration resources;
            try {
                resources = applicationClassLoader.getResources("META-INF/sca/default.scdl");
            } catch (IOException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
            if (!resources.hasMoreElements()) {
                throw new MojoExecutionException("No SCDL found on test classpath");
            }
            applicationScdl = (URL)resources.nextElement();
            if (resources.hasMoreElements()) {
                StringBuffer msg = new StringBuffer();
                msg.append("Multiple SCDL files found on test classpath:\n");
                msg.append("  ").append(applicationScdl).append('\n');
                do {
                    msg.append("  ").append(resources.nextElement()).append('\n');
                } while (resources.hasMoreElements());
                throw new MojoExecutionException(msg.toString());
            }
        } else {
            if (applicationScdl.getProtocol() == null) {
                String resource = applicationScdl.getPath();
                applicationScdl = applicationClassLoader.getResource(resource);
                if (applicationScdl == null) {
                    throw new MojoExecutionException("Application SCDL cannot be resolved: " + resource);
                }
            }
        }
        runtime.setApplicationName("application");
        runtime.setApplicationScdl(applicationScdl);
        runtime.setApplicationClassLoader(applicationClassLoader);
        runtime.setRuntimeInfo(runtimeInfo);
        try {
            runtime.initialize();
        } catch (InitializationException e) {
            throw new MojoExecutionException("Error initializing", e);
        }
        CompositeContext context = runtime.getContext();
        CurrentCompositeContext.setContext(context);

        foo.set(applicationClassLoader);
    }

    public ClassLoader createApplicationClassLoader(ClassLoader parent) {
        URL[] urls = new URL[testClassPath.size()];
        int idx = 0;
        for (Iterator i = testClassPath.iterator(); i.hasNext();) {
            File pathElement = new File((String)i.next());
            try {
                URL url = pathElement.toURI().toURL();
                getLog().debug("Adding application URL: " + url);
                urls[idx++] = url;
            } catch (MalformedURLException e) {
                // toURI should have encoded the URL
                throw new AssertionError();
            }

        }
        return new URLClassLoader(urls, parent);
    }
}