Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
guidospadotto-profesia committed Mar 31, 2024
1 parent 6b8b845 commit d7575d6
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package org.wso2.maven.p2;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -91,23 +90,22 @@ public static FeatureArtifact getFeatureArtifact(String featureArtifactDefinitio
}
public void resolveVersion(MavenProject project) throws MojoExecutionException{
if (version==null){
List dependencies = project.getDependencies();
for (Iterator iterator = dependencies.iterator(); iterator.hasNext();) {
Dependency dependancy = (Dependency) iterator.next();
if (dependancy.getGroupId().equalsIgnoreCase(getGroupId())&&dependancy.getArtifactId().equalsIgnoreCase(getArtifactId())){
setVersion(dependancy.getVersion());
//Check direct dependencies
List<Dependency> projectDependencies = project.getDependencies();
for (Dependency prjDependency : projectDependencies) {
if (prjDependency.getGroupId().equalsIgnoreCase(getGroupId())&&prjDependency.getArtifactId().equalsIgnoreCase(getArtifactId())){
setVersion(prjDependency.getVersion());
}

}

}
if (version==null) {
List dependencies = project.getDependencyManagement().getDependencies();
for (Iterator iterator = dependencies.iterator(); iterator.hasNext();) {
Dependency dependancy = (Dependency) iterator.next();
if (dependancy.getGroupId().equalsIgnoreCase(getGroupId())&&dependancy.getArtifactId().equalsIgnoreCase(getArtifactId())){
setVersion(dependancy.getVersion());
//Check inherited dependencies
List<Dependency> dependencies = project.getDependencyManagement().getDependencies();
for (Dependency dmDependency: dependencies) {
if (dmDependency.getGroupId().equalsIgnoreCase(getGroupId())&&dmDependency.getArtifactId().equalsIgnoreCase(getArtifactId())){
setVersion(dmDependency.getVersion());
}

}
}
if (version==null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
createAndSetupPaths();
rewriteEclipseIni();
// verifySetupP2RepositoryURL();
this.getLog().info("Running Equinox P2 Director Application");
installFeatures(getIUsToInstall());
//updating profile's config.ini p2.data.area property using relative path
Expand All @@ -130,7 +129,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
// createArchive();
// deployArtifact();
performMopUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.wso2.maven.p2.generate.utils.FileManagementUtil;
import org.wso2.maven.p2.generate.utils.MavenUtils;
import org.wso2.maven.p2.generate.utils.P2Utils;
import org.wso2.maven.p2.generate.utils.RepoSystemHolder;

/**
* Write environment information for the current build to file.
Expand Down Expand Up @@ -274,6 +275,7 @@ private List<FeatureArtifact> getProcessedFeatureArtifacts() throws MojoExecutio
return processedFeatureArtifacts;
if (featureArtifacts == null || featureArtifacts.size() == 0) return null;
processedFeatureArtifacts = new ArrayList<FeatureArtifact>();
RepoSystemHolder repoRefs = new RepoSystemHolder(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()));
Iterator iter = featureArtifacts.iterator();
while (iter.hasNext()) {
FeatureArtifact f = null;
Expand All @@ -287,7 +289,7 @@ private List<FeatureArtifact> getProcessedFeatureArtifacts() throws MojoExecutio
f = (FeatureArtifact) obj;
f.resolveVersion(getProject());
//getLog().info( "Resolving artifact " + f + " from " + remotePluginRepos );
f.setArtifact(MavenUtils.getResolvedArtifact(getLog(), repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()), f));
f.setArtifact(MavenUtils.getResolvedArtifact(getLog(), repoRefs , f));
processedFeatureArtifacts.add(f);
} catch (Exception e) {
throw new MojoExecutionException("Error occured when processing the Feature Artifact: " + obj.toString(), e);
Expand All @@ -310,6 +312,7 @@ private List<BundleArtifact> getProcessedBundleArtifacts() throws MojoExecutionE
return processedBundleArtifacts;
if (bundleArtifacts == null || bundleArtifacts.size() == 0) return null;
processedBundleArtifacts = new ArrayList<BundleArtifact>();
RepoSystemHolder repoRefs = new RepoSystemHolder(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()));
Iterator iter = bundleArtifacts.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Expand All @@ -322,7 +325,7 @@ private List<BundleArtifact> getProcessedBundleArtifacts() throws MojoExecutionE
f = (BundleArtifact) obj;
f.resolveVersion(getProject());
//getLog().info( "Resolving artifact " + f + " from " + remotePluginRepos );
f.setArtifact(MavenUtils.getResolvedArtifact(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()), f));
f.setArtifact(MavenUtils.getResolvedArtifact(repoRefs, f));
processedBundleArtifacts.add(f);
}
return processedBundleArtifacts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.wso2.maven.p2.generate.utils.MavenUtils;
import org.wso2.maven.p2.generate.utils.P2Utils;
import org.wso2.maven.p2.generate.utils.PropertyReplacer;
import org.wso2.maven.p2.generate.utils.RepoSystemHolder;

/**
* Write environment information for the current build to file.
Expand Down Expand Up @@ -276,6 +277,7 @@ private ArrayList<Bundle> getProcessedBundlesList() throws MojoExecutionExceptio
if (bundles == null || bundles.size() == 0)
return null;
processedBundles = new ArrayList<Bundle>();
RepoSystemHolder repoRefs = new RepoSystemHolder(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()));
Iterator iter = bundles.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Expand All @@ -289,7 +291,7 @@ private ArrayList<Bundle> getProcessedBundlesList() throws MojoExecutionExceptio
} else
b = (Bundle) obj;
b.resolveVersion(project);
b.setArtifact(MavenUtils.getResolvedArtifact(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()), b));
b.setArtifact(MavenUtils.getResolvedArtifact(repoRefs, b));
processedBundles.add(b);
}
return processedBundles;
Expand All @@ -301,6 +303,7 @@ private ArrayList<ImportBundle> getProcessedImportBundlesList() throws MojoExecu
if (importBundles == null || importBundles.size() == 0)
return null;
processedImportBundles = new ArrayList<ImportBundle>();
RepoSystemHolder repoRefs = new RepoSystemHolder(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()));
Iterator iter = importBundles.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Expand All @@ -315,7 +318,7 @@ private ArrayList<ImportBundle> getProcessedImportBundlesList() throws MojoExecu
b = (ImportBundle) obj;
b.resolveVersion(project);
if (!b.isExclude()) {
b.setArtifact(MavenUtils.getResolvedArtifact(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()), b));
b.setArtifact(MavenUtils.getResolvedArtifact(repoRefs, b));
}else
b.resolveOSGIInfo();
processedImportBundles.add(b);
Expand Down Expand Up @@ -353,7 +356,8 @@ private ArrayList<IncludedFeature> getIncludedFeatures() throws MojoExecutionExc

if (includedFeatures == null || includedFeatures.size() == 0)
return null;


RepoSystemHolder repoRefs = new RepoSystemHolder(repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()));
processedIncludedFeatures = new ArrayList<IncludedFeature>(includedFeatures.size());
for (Object obj : includedFeatures) {
if (obj instanceof String) {
Expand All @@ -362,7 +366,7 @@ private ArrayList<IncludedFeature> getIncludedFeatures() throws MojoExecutionExc
includedFeature.setFeatureVersion(project.getVersion());
DefaultArtifact artifact = new DefaultArtifact(includedFeature.getGroupId(), includedFeature.getArtifactId(), org.apache.maven.artifact.Artifact.SCOPE_RUNTIME, "zip", includedFeature.getArtifactVersion());
includedFeature.setArtifact(
MavenUtils.getResolvedArtifact(this.getLog(), repoSystem, repoSession, Stream.concat(remotePluginRepos.stream(), remoteProjectRepos.stream()).collect(Collectors.toList()), artifact));
MavenUtils.getResolvedArtifact(this.getLog(), repoRefs, artifact));
processedIncludedFeatures.add(includedFeature);
}
}
Expand Down Expand Up @@ -585,78 +589,55 @@ private void createManifestMFFile() throws MojoExecutionException {
}

private void createP2Inf() throws MojoExecutionException {
BufferedWriter out = null;
List<String> p2infStringList = null;
try {
ArrayList<Property> list = getProcessedAdviceProperties();

if (FILE_P2_INF.exists()) {
p2infStringList = readAdviceFile(FILE_P2_INF.getAbsolutePath()); // In memory storage of current p2.inf
// content
getLog().info("Updating Advice file (p2.inf)");
} else {
getLog().info("Generating Advice file (p2.inf)");
}
ArrayList<Property> list = getProcessedAdviceProperties();

out = new BufferedWriter(new FileWriter(FILE_P2_INF.getAbsolutePath()));
// re-writing the already availabled p2.inf lines
if (FILE_P2_INF.exists()) {
p2infStringList = readAdviceFile(FILE_P2_INF.getAbsolutePath()); // In memory storage of current p2.inf content
getLog().info("Updating Advice file (p2.inf)");
} else {
getLog().info("Generating Advice file (p2.inf)");
}

try ( //Use try-with-resources to manage autoclosing of writers
FileWriter fw = new FileWriter(FILE_P2_INF.getAbsolutePath());
BufferedWriter out = new BufferedWriter(fw)
){
// re-writing the already available p2.inf lines
Properties properties = new Properties();
properties.setProperty("feature.version", Bundle.getOSGIVersion(getVersion()));
if (p2infStringList != null && p2infStringList.size() > 0) {
for (String str : p2infStringList) {
out.write(PropertyReplacer.replaceProperties(str, properties) + "\n"); // writing the strings after
// replacing
// ${feature.version}
// writing the strings after replacing ${feature.version}
out.write(PropertyReplacer.replaceProperties(str, properties) + "\n");
}
}
if (list.size() == 0)
return; // finally block will take care of output stream closing.
return;
int nextIndex = P2Utils.getLastIndexOfProperties(FILE_P2_INF) + 1;
for (Object category : list) {
Property cat = (Property) category;
out.write("\nproperties." + nextIndex + ".name=" + cat.getKey());
out.write("\nproperties." + nextIndex + ".value=" + cat.getValue());
nextIndex++;
}
} catch (Exception e) {
} catch (IOException e) {
throw new MojoExecutionException("Unable to create/open p2.inf file", e);
} finally {
if (out != null)
try {
out.close();
} catch (IOException e) {
throw new MojoExecutionException("Unable to finalize p2.inf file", e);
}
}

}

private List<String> readAdviceFile(String absolutePath) throws MojoExecutionException {
List<String> stringList = new ArrayList<String>();
String inputLine = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(absolutePath));
while ((inputLine = br.readLine()) != null) {
stringList.add(inputLine);
}
br.close();

List<String> advices = new ArrayList<>();

try (FileReader fr = new FileReader(absolutePath); BufferedReader br = new BufferedReader(fr)){
br.lines().forEach(advices::add);
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Unable to create/open p2.inf file", e);
} catch (IOException e) {
throw new MojoExecutionException("Error while reading from p2.inf file", e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
throw new MojoExecutionException("Unable to finalize p2.inf file", e);
}
}
}
}

return stringList;
return advices;
}

private ArrayList<Object> getMissingImportPlugins(Document document) throws MojoExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void changeConfigIniProperty(File configIniFile, String propKey, S
}
}

public static void copyDirectory(File srcPath, File dstPath, List filesToBeCopied) throws IOException{
public static void copyDirectory(File srcPath, File dstPath, List<String> filesToBeCopied) throws IOException{
if (srcPath.isDirectory()){
if (!dstPath.exists()){
dstPath.mkdir();
Expand All @@ -81,8 +81,8 @@ public static void copyDirectory(File srcPath, File dstPath, List filesToBeCopie
}
}

public static List getAllFilesPresentInFolder(File srcPath){
List fileList=new ArrayList();
public static List<String> getAllFilesPresentInFolder(File srcPath){
List<String> fileList=new ArrayList<>();
if (srcPath.isDirectory()){
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@
*/
package org.wso2.maven.p2.generate.utils;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.LocalArtifactRequest;
import org.eclipse.aether.repository.LocalArtifactResult;
import org.eclipse.aether.repository.LocalRepositoryManager;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
Expand All @@ -43,45 +35,45 @@

public class MavenUtils {

private static Artifact performResolution(Log log, RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepositories, Artifact artifact) throws MojoExecutionException {
private static Artifact performResolution(Log log, RepoSystemHolder repoRefs, Artifact artifact) throws MojoExecutionException {

LocalRepositoryManager lrm = repoSession.getLocalRepositoryManager();
LocalArtifactRequest localReq = new LocalArtifactRequest(artifact, remoteRepositories, null);
LocalArtifactResult lat = lrm.find(repoSession,localReq);
// LocalRepositoryManager lrm = repoSession.getLocalRepositoryManager();
// LocalArtifactRequest localReq = new LocalArtifactRequest(artifact, remoteRepositories, null);
// LocalArtifactResult lat = lrm.find(repoSession,localReq);
//if (log!=null) log.info("Locally available? "+lat.isAvailable());

ArtifactRequest request = new ArtifactRequest();
request.setArtifact(artifact).setRepositories(remoteRepositories);
request.setArtifact(artifact).setRepositories(repoRefs.getRemoteRepositories());
//if (log!=null) log.info("Current Artifact Request: "+request);
ArtifactResult result = null;
try {
result = repoSystem.resolveArtifact( repoSession, request );
result = repoRefs.getRepoSystem().resolveArtifact( repoRefs.getRepoSession(), request );
} catch (ArtifactResolutionException e) {
log.error("Resolution error: "+e.getMessage());
if (log!=null) log.error("Resolution error: "+e.getMessage());
throw new MojoExecutionException("ERROR: "+e.getMessage(),e);
}
//if (log!=null) log.info("result.isResolved() = "+result.isResolved());
//TODO: use result.isResolved() to check the outcome of resolution
return result.getArtifact();
}

public static Artifact getResolvedArtifact(RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepositories, Bundle bundle) throws MojoExecutionException{
public static Artifact getResolvedArtifact(RepoSystemHolder repoRefs, Bundle bundle) throws MojoExecutionException{
Artifact artifact = new DefaultArtifact(bundle.getGroupId(), bundle.getArtifactId(), null /*org.apache.maven.artifact.Artifact.SCOPE_RUNTIME*/, "jar", bundle.getVersion());
return MavenUtils.performResolution(null, repoSystem, repoSession, remoteRepositories, artifact);
return MavenUtils.performResolution(null, repoRefs, artifact);
}

public static Artifact getResolvedArtifact(Log log, RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepositories, FeatureArtifact featureArtifact) throws MojoExecutionException{
public static Artifact getResolvedArtifact(Log log, RepoSystemHolder repoRefs, FeatureArtifact featureArtifact) throws MojoExecutionException{
Artifact artifact = new DefaultArtifact(featureArtifact.getGroupId(), featureArtifact.getArtifactId(), null /*org.apache.maven.artifact.Artifact.SCOPE_RUNTIME*/, "zip", featureArtifact.getVersion());
return MavenUtils.performResolution(log, repoSystem, repoSession, remoteRepositories, artifact);
return MavenUtils.performResolution(log, repoRefs, artifact);
}

public static Artifact getResolvedArtifact(Log log, RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepositories, P2Profile p2Profile) throws MojoExecutionException{
public static Artifact getResolvedArtifact(Log log, RepoSystemHolder repoRefs, P2Profile p2Profile) throws MojoExecutionException{
Artifact artifact = new DefaultArtifact(p2Profile.getGroupId(), p2Profile.getArtifactId(), null /*org.apache.maven.artifact.Artifact.SCOPE_RUNTIME*/, "zip", p2Profile.getVersion());
return MavenUtils.performResolution(log, repoSystem, repoSession, remoteRepositories, artifact);
return MavenUtils.performResolution(log, repoRefs, artifact);
}

public static Artifact getResolvedArtifact(Log log, RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepositories, Artifact artifact) throws MojoExecutionException{
return MavenUtils.performResolution(log, repoSystem, repoSession, remoteRepositories, artifact);
public static Artifact getResolvedArtifact(Log log, RepoSystemHolder repoRefs, Artifact artifact) throws MojoExecutionException{
return MavenUtils.performResolution(log, repoRefs, artifact);
}

public static Document getManifestDocument() throws MojoExecutionException {
Expand Down
Loading

0 comments on commit d7575d6

Please sign in to comment.