View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 pw999
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  package net.sf.oqt.maven;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import javax.persistence.Entity;
23  import javax.persistence.EntityManager;
24  
25  import net.sf.oqt.core.CoreFactory;
26  import net.sf.oqt.core.CoreProperties;
27  import net.sf.oqt.model.EntityVO;
28  import net.sf.oqt.model.PackageVO;
29  import net.sf.oqt.model.QueryVO;
30  import net.sf.oqt.model.ResultVO;
31  
32  import org.apache.maven.plugin.AbstractMojo;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.openjpa.persistence.EntityManagerImpl;
37  import org.apache.openjpa.persistence.OpenJPAQuery;
38  
39  
40  public abstract class AbstractQueryTranslatorTranslateMojo extends AbstractMojo {
41  
42      /**
43       * @parameter expression="${project}"
44       * @required
45       * @readonly
46       */
47      protected MavenProject project;
48  
49      /**
50       * The JDBC database url.
51       * <p>
52       * The URL to the database must be valid and the database must be up and running and acception connections. The
53       * database may be empty and the user which is used to connect to this database does not required write access.
54       * 
55       * @parameter
56       * @required
57       */
58      private String url = "";
59      /**
60       * The username for the databases connection
61       * 
62       * @parameter
63       */
64      private String username = "";
65      /**
66       * The password for the database connection
67       * 
68       * @parameter
69       */
70      private String password = "";
71      /**
72       * The drivername for the database connection.
73       * <p>
74       * The driver must be on the classpath. It is advised to provide the driver as a dependency for this plugin.
75       * 
76       * @parameter
77       * @required
78       */
79      private String driverName = "";
80      /**
81       * The databse dictionary is it's known by the openJPA version on the classpath.
82       * 
83       * @parameter
84       * @required
85       */
86      private String dictionary = "";
87      /**
88       * The list of packages where the plugin must go look for entities.
89       * <p>
90       * The plugin will automatically look in all the subpackages.
91       * <p>
92       * If this plugin is ran outside the model-module, then it's advised to give the model as a dependency of this
93       * plugin.
94       * 
95       * @parameter
96       * @required
97       */
98      private final List<String> packageNames = new ArrayList<String>();
99  
100     /**
101      * Defines what should be outputed to text files. 
102      * <p>
103      * Currently supported types are:
104      * <ul>
105      *  <li>JPQL</li>
106      *  <li>SQL</li>
107      * </ul>
108      * 
109      * @parameter
110      * @since 0.3
111      */
112     private final List<String> reportTypes = new ArrayList<String>();
113 
114     public final String getUrl() {
115         return url;
116     }
117 
118     public final void setUrl(final String url) {
119         this.url = url;
120     }
121 
122     public final String getUsername() {
123         return username;
124     }
125 
126     public final void setUsername(final String username) {
127         this.username = username;
128     }
129 
130     public final String getPassword() {
131         return password;
132     }
133 
134     public final void setPassword(final String password) {
135         this.password = password;
136     }
137 
138     public final String getDriverName() {
139         return driverName;
140     }
141 
142     public final void setDriverName(final String driverName) {
143         this.driverName = driverName;
144     }
145 
146     public final String getDictionary() {
147         return dictionary;
148     }
149 
150     public final void setDictionary(final String dictionary) {
151         this.dictionary = dictionary;
152     }
153 
154     public final List<String> getPackageNames() {
155         return packageNames;
156     }
157 
158     /**
159      * @return the reportTypes
160      */
161     public final List<String> getReportTypes() {
162         return reportTypes;
163     }
164 
165     void printOutputToDebug(final ResultVO result) {
166         if (getLog().isDebugEnabled()) {
167             final Collection<PackageVO> packages = result.getPackages();
168             for (final PackageVO packageVO : packages) {
169                 getLog().debug("Package: " + packageVO.getName());
170                 final Collection<EntityVO> entities = packageVO.getEntities();
171                 for (final EntityVO entityVO : entities) {
172                     getLog().debug(">Entity: " + entityVO.getName());
173                     final Collection<QueryVO> queries = entityVO.getQueries();
174                     for (final QueryVO queryVO : queries) {
175                         getLog().debug(">>NamedQuery: " + queryVO.getName());
176                         getLog().debug(">>> Number of SQL queries: " + queryVO.getSQLQuery().size());
177                     }
178                 }
179             }
180         }
181     }
182 
183     /**
184      * Checks if the required classes are on the classpath.
185      * <p>
186      * The checked classes are:
187      * <ul>
188      * <li>javax.persistence.EntityManager (javaee-api)
189      * <li>javax.persistence.Entity (javaee-api)
190      * <li>org.apache.openjpa.persistence.EntityManagerImpl (openJPA)
191      * <li>org.apache.openjpa.persistence.OpenJPAQuery (openJPA)
192      * <li>org.apache.openjpa.persistence.PersistenceProviderImpl (openJPA)
193      * <li> {@link #driverName} (user provided)
194      * </ul>
195      * 
196      * @throws MojoExecutionException if not all classes are on the classpath.
197      */
198     void validateClassPath() throws MojoExecutionException {
199         final List<String> missing = new ArrayList<String>();
200 
201         try {
202             Class.forName(EntityManager.class.getName());
203             Class.forName(Entity.class.getName());
204         } catch (final ClassNotFoundException e) {
205             missing.add("Ensure that javaee-api is on the classpath");
206         }
207 
208         try {
209             Class.forName(EntityManagerImpl.class.getName());
210             Class.forName(OpenJPAQuery.class.getName());
211             Class.forName(org.apache.openjpa.persistence.PersistenceProviderImpl.class.getName());
212         } catch (final ClassNotFoundException e) {
213             missing.add("Ensure that openjpa is on the classpath");
214         }
215 
216         try {
217             Class.forName(getDriverName());
218         } catch (final ClassNotFoundException e) {
219             missing.add("Failed to find the driver" + getDriverName() + " on the classpath. Ensure that you provided it as a dependency");
220         }
221         if (!missing.isEmpty()) {
222             for (final String string : missing) {
223                 getLog().error(string);
224             }
225             throw new MojoExecutionException("Failed to find all dependencies, see log output for more info");
226         }
227 
228     }
229 
230     /**
231      * Copies the properties from this plugin to the {@link CoreProperties} and verifies that the ClassPath is correct.
232      */
233     @Override
234     public void execute() throws MojoExecutionException, MojoFailureException {
235         // Setting properties passed via pom into the Core
236         CoreFactory.getProperties().setUrl(getUrl());
237         CoreFactory.getProperties().setUsername(getUsername());
238         CoreFactory.getProperties().setPassword(getPassword());
239         CoreFactory.getProperties().setDriverName(getDriverName());
240         CoreFactory.getProperties().setDictionary(getDictionary());
241         CoreFactory.getProperties().getFqns().clear();
242         CoreFactory.getProperties().getPackageNames().addAll(getPackageNames());
243         validateClassPath();
244     }
245 
246     void setProject(final MavenProject project) {
247         this.project = project;
248     }
249 
250 }