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