org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eclipselink Show documentation
Show all versions of eclipselink Show documentation
EclipseLink build based upon Git transaction f2b9fc5
The newest version!
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.tools.weaving.jpa;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.eclipse.persistence.exceptions.StaticWeaveException;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* Description: This is the static weave ant task definition class that verifies the value of specified attributes and invokes
* StaticWeaveProcessor to weave classes.
*
* Usage:
*
* - Ensure the classpath contains all the classes necessary to load the classes in the source.
*
- The lib containing this weaving Ant task must be added into the classpath by using the -lib option on Ant command line instead of using the classpath attribute of the taskdef Ant task.
*
- Define weaving Ant task and Ant target by using following attributes:
*
- source - specify source location. In the default configuration StaticWeaveAntTask assumes the source contains the persistence.xml,if this is not the case, the location containing the persistence.xml must be explicitly identified by the attribute 'persistenceinfo'.
*
- target - specify the output location (either a directory or a jar).
*
- persistenceinfo - specify the location containing the persistence.xml. This is optional and should only be specified if the source does not contain the persistence.xml.
* Weaving is not required for composite persistence unit - only for its members.
* If composite persistence unit member can't be used independently from its composite (specifies {@literal
})
* then it must specify both its source and persistenceInfo of its composite.
* Otherwise composite persistence unit member may or may not specify persistenceInfo of its composite.
* - log - specify a logging file. This is optional.
*
- loglevel - specify a literal value of EclipseLink logging level(OFF,SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST) The default value is OFF(8). This is optional.
*
* - The weaving will be performed in place if source and target point to the same location. Weaving in place is ONLY applicable for directory-based sources.
*
*Example:
*
*
*<target name="define.task" description="New task definition for EclipseLink static weaving"/>
* <taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask"/>
*</target>
*<target name="weaving" description="perform weaving." depends="define.task">
* <weave source= "c:\foo.jar" target = "c:\wovenfoo.jar" persistenceinfo="c:\foo-containing-persistenceinfo.jar">
* <classpath>
* <pathelement path="c:\foo-dependent.jar"/>
* </classpath>
* </weave>
*</target>
*
*/
public class StaticWeaveAntTask extends Task{
private String source;
private String persistenceinfo;
private String persistencexml;
private String target;
private List classPaths = new ArrayList<>();
private int logLevel = SessionLog.OFF;
private Writer logWriter;
/**
* Default constructor.
*/
public StaticWeaveAntTask() {
}
/**
* Set the input archive to be used to weave.
*/
public void setSource(String source) {
this.source = source;
}
/**
* Set output archive to be used to weave to.
*/
public void setTarget(String target) {
this.target = target;
}
/**
* Set the log file.
*/
public void setLog(String logFile) throws IOException {
try{
this.logWriter = new FileWriter(logFile);
}catch(Exception e){
throw StaticWeaveException.openLoggingFileException(logFile,e);
}
}
public void setLogLevel(String logLevel) {
if (logLevel.equalsIgnoreCase("OFF") ||
logLevel.equalsIgnoreCase("SEVERE") ||
logLevel.equalsIgnoreCase("WARNING") ||
logLevel.equalsIgnoreCase("INFO") ||
logLevel.equalsIgnoreCase("CONFIG") ||
logLevel.equalsIgnoreCase("FINE") ||
logLevel.equalsIgnoreCase("FINER") ||
logLevel.equalsIgnoreCase("FINEST") ||
logLevel.equalsIgnoreCase("ALL")) {
this.logLevel = AbstractSessionLog.translateStringToLoggingLevel(logLevel.toUpperCase());
} else{
throw StaticWeaveException.illegalLoggingLevel(logLevel);
}
}
public void setPersistenceinfo(String persistenceinfo) {
this.persistenceinfo = persistenceinfo;
}
public void setpersistencexml(String persistenceXMLLocation) {
this.persistencexml = persistenceXMLLocation;
}
/**
* Add the dependent classpath in order to load classes from the specified input jar.
*/
public void addClasspath(Path path) {
classPaths.add(path);
}
/**
* Parse the class path element and store them into vector.
*/
private List getPathElement(){
List pathElements = new ArrayList<>();
for (int i = 0; i < classPaths.size(); i++) {
String thisPath = classPaths.get(i).toString();
if (thisPath != null) {
String[] thisSplitedPath = thisPath.split(File.pathSeparator);
pathElements.addAll(Arrays.asList(thisSplitedPath));
}
}
return pathElements;
}
/**
* Convert the path element into the URL which further pass into
* the classloader.
*/
private URL[] getURLs(){
List pathElements = getPathElement();
URL[] urls = new URL[pathElements.size()];
for(int i=0;i