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.logging.LoggingHandlerEnvironmentFacet;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31
32
33
34
35
36
37 public class ToolExecutionEnvironment extends AbstractLogAwareFacet {
38
39
40 private ThreadContextClassLoaderBuilder builder;
41 private ThreadContextClassLoaderHolder holder;
42 private LoggingHandlerEnvironmentFacet loggingHandlerEnvironmentFacet;
43 private List<EnvironmentFacet> extraFacets;
44
45
46
47
48
49
50
51
52
53 public ToolExecutionEnvironment(final Log mavenLog,
54 final ThreadContextClassLoaderBuilder builder,
55 final LoggingHandlerEnvironmentFacet loggingHandlerFacet) {
56 super(mavenLog);
57
58
59 Validate.notNull(builder, "builder");
60 Validate.notNull(loggingHandlerFacet, "loggingHandlerFacet");
61
62
63 this.builder = builder;
64 this.loggingHandlerEnvironmentFacet = loggingHandlerFacet;
65 extraFacets = new ArrayList<EnvironmentFacet>();
66 }
67
68
69
70
71
72
73 public void add(final EnvironmentFacet facet) {
74
75
76 Validate.notNull(facet, "facet");
77
78
79 extraFacets.add(facet);
80 }
81
82
83
84
85
86
87
88
89 public String getClassPathAsArgument() {
90
91
92 if (holder == null) {
93 throw new IllegalStateException("Cannot retrieve the classpath argument before calling 'setup'");
94 }
95
96
97 return holder.getClassPathAsArgument();
98 }
99
100
101
102
103 @Override
104 public final void setup() {
105
106
107 try {
108
109 if (log.isDebugEnabled()) {
110 log.debug("ToolExecutionEnvironment setup -- Starting.");
111 }
112
113
114 holder = builder.buildAndSet();
115
116
117 loggingHandlerEnvironmentFacet.setup();
118
119
120 for (EnvironmentFacet current : extraFacets) {
121 try {
122 current.setup();
123 } catch (Exception e) {
124 throw new IllegalStateException("Could not setup() EnvironmentFacet of type ["
125 + current.getClass().getName() + "]", e);
126 }
127 }
128
129 if (log.isDebugEnabled()) {
130 log.debug("ToolExecutionEnvironment setup -- Done.");
131 }
132
133 } catch (IllegalStateException e) {
134 throw e;
135 } catch (Exception e) {
136 throw new IllegalStateException("Could not setup() mandatory EnvironmentFacets.", e);
137 }
138 }
139
140
141
142
143 @Override
144 public final void restore() {
145
146 try {
147
148 if (log.isDebugEnabled()) {
149 log.debug("ToolExecutionEnvironment restore -- Starting.");
150 }
151
152 for (EnvironmentFacet current : extraFacets) {
153 try {
154 current.restore();
155 } catch (Exception e) {
156 throw new IllegalStateException("Could not restore() EnvironmentFacet of type ["
157 + current.getClass().getName() + "]", e);
158 }
159 }
160 } finally {
161
162
163 loggingHandlerEnvironmentFacet.restore();
164
165
166 holder.restoreClassLoaderAndReleaseThread();
167
168 if (log.isDebugEnabled()) {
169 log.debug("ToolExecutionEnvironment restore -- Done.");
170 }
171 }
172 }
173 }