1 package org.andromda.ant.task;
2
3 import java.io.FileNotFoundException;
4 import java.net.URL;
5 import java.util.Map;
6 import org.andromda.core.AndroMDA;
7 import org.andromda.core.common.ResourceUtils;
8 import org.apache.commons.lang.StringUtils;
9 import org.apache.commons.lang.exception.ExceptionUtils;
10 import org.apache.tools.ant.BuildException;
11 import org.apache.tools.ant.taskdefs.MatchingTask;
12
13
14
15
16
17
18
19
20
21
22
23
24 public class AndroMDAGenTask
25 extends MatchingTask
26 {
27
28
29
30 static
31 {
32 initializeContextClassLoader();
33 }
34
35
36
37
38 private URL configurationUri;
39
40
41
42
43
44
45 public void setConfigurationUri(final URL configurationUri)
46 {
47 this.configurationUri = configurationUri;
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 final AndroMDA andromda = AndroMDA.newInstance();
72 if (andromda != null)
73 {
74 andromda.run(
75 this.replaceProperties(ResourceUtils.getContents(configurationUri)));
76 andromda.shutdown();
77 }
78 }
79 catch (Throwable throwable)
80 {
81 final Throwable cause = ExceptionUtils.getCause(throwable);
82 if (cause != null)
83 {
84 throwable = cause;
85 }
86 if (throwable instanceof FileNotFoundException)
87 {
88 throw new BuildException("No configuration could be loaded from --> '" + configurationUri + '\'');
89 }
90 throw new BuildException(throwable);
91 }
92 finally
93 {
94
95
96
97 Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
98 }
99 }
100
101
102
103
104
105
106
107
108
109 protected String replaceProperties(String string)
110 {
111 @SuppressWarnings("unchecked")
112 final Map<String, Object> properties = this.getProject().getProperties();
113 if (properties != null)
114 {
115 for (Map.Entry<String, Object> entry : properties.entrySet())
116 {
117 final String name = entry.getKey();
118 final String value = (String)entry.getValue();
119 final String property = "${" + name + '}';
120 string = StringUtils.replace(string, property, value);
121 }
122 }
123
124
125 string = this.removePropertyReferences(string);
126 return string;
127 }
128
129
130
131
132 private static final String PROPERTY_REFERENCE = "\\$\\{.*\\}";
133
134
135
136
137
138
139
140
141
142 public String removePropertyReferences(String string)
143 {
144 if (string != null)
145 {
146 string = string.replaceAll(PROPERTY_REFERENCE, "");
147 }
148 return string;
149 }
150
151
152
153
154
155 private static void initializeContextClassLoader()
156 {
157 Thread.currentThread().setContextClassLoader(AndroMDAGenTask.class.getClassLoader());
158 }
159 }