1 package org.codehaus.mojo.jaxb2.shared.environment;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.logging.Log;
23 import org.codehaus.mojo.jaxb2.shared.Validate;
24 import org.codehaus.mojo.jaxb2.shared.environment.classloading.ThreadContextClassLoaderBuilder;
25 import org.codehaus.mojo.jaxb2.shared.environment.classloading.ThreadContextClassLoaderHolder;
26 import org.codehaus.mojo.jaxb2.shared.environment.locale.LocaleFacet;
27 import org.codehaus.mojo.jaxb2.shared.environment.logging.LoggingHandlerEnvironmentFacet;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32
33
34
35
36
37
38 public class ToolExecutionEnvironment extends AbstractLogAwareFacet {
39
40
41 private ThreadContextClassLoaderBuilder builder;
42 private ThreadContextClassLoaderHolder holder;
43 private LoggingHandlerEnvironmentFacet loggingHandlerEnvironmentFacet;
44 private List<EnvironmentFacet> extraFacets;
45 private LocaleFacet localeFacet;
46
47
48
49
50
51
52
53
54
55
56
57 public ToolExecutionEnvironment(final Log mavenLog,
58 final ThreadContextClassLoaderBuilder builder,
59 final LoggingHandlerEnvironmentFacet loggingHandlerFacet,
60 final LocaleFacet localeFacet) {
61 super(mavenLog);
62
63
64 Validate.notNull(builder, "builder");
65 Validate.notNull(loggingHandlerFacet, "loggingHandlerFacet");
66
67
68 this.builder = builder;
69 this.loggingHandlerEnvironmentFacet = loggingHandlerFacet;
70 this.localeFacet = localeFacet;
71 extraFacets = new ArrayList<EnvironmentFacet>();
72 }
73
74
75
76
77
78
79 public void add(final EnvironmentFacet facet) {
80
81
82 Validate.notNull(facet, "facet");
83
84
85 extraFacets.add(facet);
86 }
87
88
89
90
91
92
93
94
95 public String getClassPathAsArgument() {
96
97
98 if (holder == null) {
99 throw new IllegalStateException("Cannot retrieve the classpath argument before calling 'setup'");
100 }
101
102
103 return holder.getClassPathAsArgument();
104 }
105
106
107
108
109 @Override
110 public final void setup() {
111
112
113 try {
114
115 if (log.isDebugEnabled()) {
116 log.debug("ToolExecutionEnvironment setup -- Starting.");
117 }
118
119
120 holder = builder.buildAndSet();
121
122
123 loggingHandlerEnvironmentFacet.setup();
124
125
126 if (localeFacet != null) {
127 localeFacet.setup();
128 }
129
130
131 for (EnvironmentFacet current : extraFacets) {
132 try {
133 current.setup();
134 } catch (Exception e) {
135 throw new IllegalStateException("Could not setup() EnvironmentFacet of type ["
136 + current.getClass().getName() + "]", e);
137 }
138 }
139
140 if (log.isDebugEnabled()) {
141 log.debug("ToolExecutionEnvironment setup -- Done.");
142 }
143
144 } catch (IllegalStateException e) {
145 throw e;
146 } catch (Exception e) {
147 throw new IllegalStateException("Could not setup() mandatory EnvironmentFacets.", e);
148 }
149 }
150
151
152
153
154 @Override
155 public final void restore() {
156
157 try {
158
159 if (log.isDebugEnabled()) {
160 log.debug("ToolExecutionEnvironment restore -- Starting.");
161 }
162
163 for (EnvironmentFacet current : extraFacets) {
164 try {
165 current.restore();
166 } catch (Exception e) {
167 throw new IllegalStateException("Could not restore() EnvironmentFacet of type ["
168 + current.getClass().getName() + "]", e);
169 }
170 }
171 } finally {
172
173
174 loggingHandlerEnvironmentFacet.restore();
175
176
177 if (localeFacet != null) {
178 localeFacet.restore();
179 }
180
181
182 holder.restoreClassLoaderAndReleaseThread();
183
184 if (log.isDebugEnabled()) {
185 log.debug("ToolExecutionEnvironment restore -- Done.");
186 }
187 }
188 }
189 }