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 package org.jomc.ant;
32
33 import java.io.IOException;
34 import java.util.logging.Level;
35 import javax.xml.bind.JAXBContext;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.util.JAXBSource;
38 import javax.xml.transform.Source;
39 import org.apache.tools.ant.BuildException;
40 import org.jomc.model.Implementation;
41 import org.jomc.model.Module;
42 import org.jomc.model.Specification;
43 import org.jomc.modlet.Model;
44 import org.jomc.modlet.ModelContext;
45 import org.jomc.modlet.ModelException;
46 import org.jomc.modlet.ModelValidationReport;
47 import org.jomc.modlet.ObjectFactory;
48 import org.jomc.tools.ClassFileProcessor;
49
50
51
52
53
54
55
56 public final class ValidateClasspathTask extends ClassFileProcessorTask
57 {
58
59
60 public ValidateClasspathTask()
61 {
62 super();
63 }
64
65
66
67
68
69
70 @Override
71 public void processClassFiles() throws BuildException
72 {
73 ProjectClassLoader classLoader = null;
74 boolean suppressExceptionOnClose = true;
75
76 try
77 {
78 this.log( Messages.getMessage( "validatingClasspath", this.getModel() ) );
79
80 classLoader = this.newProjectClassLoader();
81 final ModelContext context = this.newModelContext( classLoader );
82 final ClassFileProcessor tool = this.newClassFileProcessor();
83 final JAXBContext jaxbContext = context.createContext( this.getModel() );
84 final Model model = this.getModel( context );
85 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
86 ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
87
88 this.logValidationReport( context, validationReport );
89 tool.setModel( model );
90
91 if ( validationReport.isModelValid() )
92 {
93 final Specification s = this.getSpecification( model );
94 final Implementation i = this.getImplementation( model );
95 final Module m = this.getModule( model );
96
97 if ( s != null )
98 {
99 validationReport = tool.validateModelObjects( s, context );
100
101 if ( validationReport != null )
102 {
103 this.logValidationReport( context, validationReport );
104
105 if ( !validationReport.isModelValid() )
106 {
107 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
108 }
109 }
110 }
111
112 if ( i != null )
113 {
114 validationReport = tool.validateModelObjects( i, context );
115
116 if ( validationReport != null )
117 {
118 this.logValidationReport( context, validationReport );
119
120 if ( !validationReport.isModelValid() )
121 {
122 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
123 }
124 }
125 }
126
127 if ( m != null )
128 {
129 validationReport = tool.validateModelObjects( m, context );
130
131 if ( validationReport != null )
132 {
133 this.logValidationReport( context, validationReport );
134
135 if ( !validationReport.isModelValid() )
136 {
137 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
138 }
139 }
140 }
141
142 if ( this.isModulesProcessingRequested() )
143 {
144 validationReport = tool.validateModelObjects( context );
145
146 if ( validationReport != null )
147 {
148 this.logValidationReport( context, validationReport );
149
150 if ( !validationReport.isModelValid() )
151 {
152 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
153 }
154 }
155 }
156
157 suppressExceptionOnClose = false;
158 }
159 else
160 {
161 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
162 }
163 }
164 catch ( final IOException e )
165 {
166 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
167 }
168 catch ( final JAXBException e )
169 {
170 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
171 }
172 catch ( final ModelException e )
173 {
174 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
175 }
176 finally
177 {
178 try
179 {
180 if ( classLoader != null )
181 {
182 classLoader.close();
183 }
184 }
185 catch ( final IOException e )
186 {
187 if ( suppressExceptionOnClose )
188 {
189 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
190 }
191 else
192 {
193 throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
194 }
195 }
196 }
197 }
198
199
200 @Override
201 public ValidateClasspathTask clone()
202 {
203 return (ValidateClasspathTask) super.clone();
204 }
205
206 }