1 package org.andromda.ant.task;
2
3 import java.io.FileNotFoundException;
4 import java.net.URL;
5 import org.andromda.core.AndroMDAServer;
6 import org.andromda.core.configuration.Configuration;
7 import org.apache.commons.lang.exception.ExceptionUtils;
8 import org.apache.tools.ant.BuildException;
9 import org.apache.tools.ant.taskdefs.MatchingTask;
10
11
12
13
14
15
16
17
18
19
20 public class AndroMDAServerStartTask
21 extends MatchingTask
22 {
23
24
25
26 static
27 {
28 initializeContextClassLoader();
29 }
30
31
32
33
34 private URL configurationUri;
35
36
37
38
39
40
41 public void setConfigurationUri(final URL configurationUri)
42 {
43 this.configurationUri = configurationUri;
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public void execute()
60 throws BuildException
61 {
62
63
64 initializeContextClassLoader();
65 try
66 {
67 if (this.configurationUri == null)
68 {
69 throw new BuildException("Configuration is not a valid URI --> '" + this.configurationUri + '\'');
70 }
71
72
73 final Configuration configuration = Configuration.getInstance(this.configurationUri);
74
75
76 final AndroMDAServer andromdaServer = AndroMDAServer.newInstance();
77 if (andromdaServer != null)
78 {
79 andromdaServer.start(configuration);
80 }
81 }
82 catch (Throwable throwable)
83 {
84 final Throwable cause = ExceptionUtils.getCause(throwable);
85 if (cause != null)
86 {
87 throwable = cause;
88 }
89 if (throwable instanceof FileNotFoundException)
90 {
91 throw new BuildException("No configuration could be loaded from --> '" + configurationUri + '\'');
92 }
93 throw new BuildException(throwable);
94 }
95 finally
96 {
97
98
99
100 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
101 }
102 }
103
104
105
106
107
108 private static void initializeContextClassLoader()
109 {
110 Thread.currentThread().setContextClassLoader(AndroMDAServerStartTask.class.getClassLoader());
111 }
112 }