View Javadoc

1   /*
2    * Copyright (c) 2007, Ounce Labs, Inc.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *     * Redistributions of source code must retain the above copyright
8    *       notice, this list of conditions and the following disclaimer.
9    *     * Redistributions in binary form must reproduce the above copyright
10   *       notice, this list of conditions and the following disclaimer in the
11   *       documentation and/or other materials provided with the distribution.
12   *     * Neither the name of the <organization> nor the
13   *       names of its contributors may be used to endorse or promote products
14   *       derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS PROVIDED BY OUNCE LABS, INC. ``AS IS'' AND ANY
17   * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL OUNCE LABS, INC. BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.codehaus.mojo.ounce.core;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileWriter;
32  import java.io.IOException;
33  import java.io.ObjectOutputStream;
34  import java.util.Arrays;
35  import java.util.Collections;
36  import java.util.List;
37  import java.util.Map;
38  
39  import org.apache.maven.plugin.logging.Log;
40  import org.codehaus.mojo.ounce.ProjectOnlyMojo;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  import com.thoughtworks.xstream.XStream;
44  
45  /**
46   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
47   * @author <a href="mailto:sam.headrick@ouncelabs.com">Sam Headrick</a>
48   * @plexus.component role="org.codehaus.mojo.ounce.core.OunceCore" role-hint="test-xml"
49   */
50  public class OunceCoreXml
51      implements OunceCore
52  {
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see com.ouncelabs.plugins.OunceCore#createApplication(java.lang.String, java.io.File, java.util.List, boolean,
58       *      boolean)
59       */
60      public void createApplication( String baseDir, String theName, String theApplicationRoot, List theProjects,
61                                     Map ounceOptions, Log log )
62          throws OunceCoreException
63      {
64          // sort them to avoid implementation details messing
65          // up the order for testing.
66          Collections.sort( theProjects );
67          log.info( "Writing parameters to xml." );
68          OunceCoreApplication bean = new OunceCoreApplication( theName, theApplicationRoot, theProjects, ounceOptions );
69          XStream xs = new XStream();
70  
71          xs.alias( "application", OunceCoreApplication.class );
72          xs.alias( "project", OunceProjectBean.class );
73          try
74          {
75              File outFile = new File( baseDir + File.separator + "target", "application.xml" );
76              outFile.getParentFile().mkdirs();
77              outFile.createNewFile();
78              ObjectOutputStream out = xs.createObjectOutputStream( new FileWriter( outFile ) );
79              out.writeObject( bean );
80              out.close();
81          }
82          catch ( IOException e )
83          {
84              // TODO Auto-generated catch block
85              e.printStackTrace();
86          }
87      }
88  
89      /*
90       * (non-Javadoc)
91       * 
92       * @see com.ouncelabs.plugins.OunceCore#createProject(java.lang.String, java.io.File, java.util.List, java.io.File,
93       *      java.lang.String, java.lang.String, boolean, com.ouncelabs.plugins.CompilerOptions)
94       */
95      public void createProject( String baseDir, String theName, String theProjectRoot, List theSourceRoots,
96                                 String theWebRoot, String theClassPath, String theJdkName, String theCompilerOptions,
97                                 String packaging, Map ounceOptions, 
98                                 boolean analyzeStrutsFramework, boolean importStrutsValidation, Log log )
99          throws OunceCoreException
100     {
101         if ( StringUtils.isNotEmpty( theClassPath ) )
102         {
103             // the classpath order can change subtly from
104             // maven. Lets sort it alphabetically since we
105             // really
106             // only care about the contents for testing the
107             // plugin
108             String[] classp = theClassPath.split( ";" );
109             Arrays.sort( classp );
110 
111             StringBuffer sb = new StringBuffer();
112             if ( classp.length > 0 )
113             {
114                 // first one, no separator needed
115                 sb.append( classp[0] );
116 
117                 // separate the rest of them with
118                 // pathSeparator
119                 for ( int i = 1; i < classp.length; i++ )
120                 {
121                     sb.append( ProjectOnlyMojo.PATH_SEPARATOR );
122                     sb.append( classp[i] );
123                 }
124                 theClassPath = sb.toString();
125             }
126         }
127 
128         log.info( "Writing parameters to xml." );
129         OunceCoreProject bean =
130             new OunceCoreProject( theName, theProjectRoot, theSourceRoots, theWebRoot, theClassPath, theJdkName,
131                                   packaging, theCompilerOptions, ounceOptions );
132         XStream xs = new XStream();
133         xs.alias( "project", OunceCoreProject.class );
134         try
135         {
136             File outFile = new File( baseDir + File.separator + "target", "project.xml" );
137             outFile.getParentFile().mkdirs();
138             outFile.createNewFile();
139             ObjectOutputStream out = xs.createObjectOutputStream( new FileWriter( outFile ) );
140             out.writeObject( bean );
141             out.close();
142         }
143         catch ( IOException e )
144         {
145             // TODO Auto-generated catch block
146             e.printStackTrace();
147         }
148     }
149 
150     /*
151      * (non-Javadoc)
152      * 
153      * @see org.codehaus.mojo.ounce.core.OunceCore#readApplication(java.lang.String)
154      */
155     public OunceCoreApplication readApplication( String thePath, Log log )
156         throws OunceCoreException
157     {
158         XStream xs = new XStream();
159         xs.alias( "application", OunceCoreApplication.class );
160         xs.alias( "project", OunceProjectBean.class );
161 
162         File inFile = new File( thePath + File.separator + "target", "application.xml" );
163         try
164         {
165             FileInputStream is = new FileInputStream( inFile );
166             OunceCoreApplication app = (OunceCoreApplication) xs.fromXML( is );
167             is.close();
168             return app;
169         }
170         catch ( IOException e )
171         {
172             throw new OunceCoreException( "Unable to read file:" + inFile.getPath(), e );
173         }
174     }
175 
176     /*
177      * (non-Javadoc)
178      * 
179      * @see org.codehaus.mojo.ounce.core.OunceCore#readProject(java.lang.String)
180      */
181     public OunceCoreProject readProject( String thePath, Log log )
182         throws OunceCoreException
183     {
184         XStream xs = new XStream();
185         xs.alias( "project", OunceCoreProject.class );
186 
187         File inFile = new File( thePath + File.separator + "target", "project.xml" );
188         try
189         {
190             FileInputStream is = new FileInputStream( inFile );
191             OunceCoreProject project = (OunceCoreProject) xs.fromXML( is );
192             is.close();
193             return project;
194         }
195         catch ( IOException e )
196         {
197             throw new OunceCoreException( "Unable to read file:" + inFile.getPath(), e );
198         }
199     }
200 
201     /*
202      * (non-Javadoc)
203      * 
204      * @see org.codehaus.mojo.ounce.core.OunceCore#scan(java.lang.String, java.lang.String, java.lang.String,
205      *      java.lang.String, java.lang.String, java.lang.String, boolean, java.util.Map, java.lang.String, boolean,
206      *      org.apache.maven.plugin.logging.Log)
207      */
208     public void scan( String theApplicationFile, String theAssessmentName, String theAssessmentOutput,
209                       String theCaller, String theReportType, String theReportOutputType,
210                       String theReportOutputLocation, boolean thePublish, Map theOunceOptions, String installDir,
211                       boolean wait, Log theLog )
212         throws OunceCoreException
213     {
214 
215         OunceCoreScan bean =
216             new OunceCoreScan( theApplicationFile, theAssessmentName, theAssessmentOutput, theCaller, theReportType,
217                                theReportOutputType, theReportOutputLocation, thePublish, theOunceOptions );
218 
219         {
220             XStream xs = new XStream();
221             xs.alias( "scan", OunceCoreScan.class );
222             try
223             {
224                 File outFile = new File( "./target", "scan.xml" );
225                 outFile.getParentFile().mkdirs();
226                 outFile.createNewFile();
227                 ObjectOutputStream out = xs.createObjectOutputStream( new FileWriter( outFile ) );
228                 out.writeObject( bean );
229                 out.close();
230             }
231             catch ( IOException e )
232             {
233                 // TODO Auto-generated catch block
234                 e.printStackTrace();
235             }
236         }
237     }
238 
239     public void createPathVariables( Map pathVariableMap, String installDir, Log log )
240         throws OunceCoreException
241     {
242         // TODO Auto-generated method stub
243 
244     }
245 }