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  
77          try
78          {
79              this.log( Messages.getMessage( "validatingClasspath", this.getModel() ) );
80  
81              classLoader = this.newProjectClassLoader();
82              final ModelContext context = this.newModelContext( classLoader );
83              final ClassFileProcessor tool = this.newClassFileProcessor();
84              final JAXBContext jaxbContext = context.createContext( this.getModel() );
85              final Model model = this.getModel( context );
86              final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
87              ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
88  
89              this.logValidationReport( context, validationReport );
90              tool.setModel( model );
91  
92              if ( validationReport.isModelValid() )
93              {
94                  final Specification s = this.getSpecification( model );
95                  final Implementation i = this.getImplementation( model );
96                  final Module m = this.getModule( model );
97  
98                  if ( s != null )
99                  {
100                     validationReport = tool.validateModelObjects( s, context );
101 
102                     if ( validationReport != null )
103                     {
104                         this.logValidationReport( context, validationReport );
105 
106                         if ( !validationReport.isModelValid() )
107                         {
108                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
109                         }
110                     }
111                 }
112 
113                 if ( i != null )
114                 {
115                     validationReport = tool.validateModelObjects( i, context );
116 
117                     if ( validationReport != null )
118                     {
119                         this.logValidationReport( context, validationReport );
120 
121                         if ( !validationReport.isModelValid() )
122                         {
123                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
124                         }
125                     }
126                 }
127 
128                 if ( m != null )
129                 {
130                     validationReport = tool.validateModelObjects( m, context );
131 
132                     if ( validationReport != null )
133                     {
134                         this.logValidationReport( context, validationReport );
135 
136                         if ( !validationReport.isModelValid() )
137                         {
138                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
139                         }
140                     }
141                 }
142 
143                 if ( this.isModulesProcessingRequested() )
144                 {
145                     validationReport = tool.validateModelObjects( context );
146 
147                     if ( validationReport != null )
148                     {
149                         this.logValidationReport( context, validationReport );
150 
151                         if ( !validationReport.isModelValid() )
152                         {
153                             throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
154                         }
155                     }
156                 }
157 
158                 classLoader.close();
159                 classLoader = null;
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                 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
190             }
191         }
192     }
193 
194     
195 
196 
197     @Override
198     public ValidateClasspathTask clone()
199     {
200         return (ValidateClasspathTask) super.clone();
201     }
202 
203 }