Coverage Report - org.kuali.maven.plugins.DeployUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DeployUtils
0%
0/86
0%
0/20
2.167
 
 1  
 package org.kuali.maven.plugins;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileOutputStream;
 5  
 import java.io.IOException;
 6  
 import java.io.OutputStream;
 7  
 import java.util.ArrayList;
 8  
 import java.util.Collections;
 9  
 import java.util.Iterator;
 10  
 import java.util.List;
 11  
 
 12  
 import org.apache.commons.io.IOUtils;
 13  
 import org.apache.commons.lang.StringUtils;
 14  
 import org.apache.maven.artifact.Artifact;
 15  
 import org.apache.maven.artifact.DefaultArtifact;
 16  
 import org.apache.maven.artifact.handler.ArtifactHandler;
 17  
 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
 18  
 
 19  0
 public class DeployUtils {
 20  
     public static void main(String[] args) {
 21  
         try {
 22  0
             String basedir = System.getProperty("user.home") + "/.oracle";
 23  0
             DeployUtils du = new DeployUtils();
 24  0
             List<Artifact> artifacts = du.getArtifacts(new File(basedir), "com.oracle", "11.2.0.2, 11.2.0.1");
 25  0
             String s = du.getShellScript(artifacts);
 26  0
             System.out.println(s);
 27  0
         } catch (Throwable t) {
 28  0
             t.printStackTrace();
 29  0
         }
 30  0
     }
 31  
 
 32  
     public void write(String filename, String content) throws IOException {
 33  0
         OutputStream out = null;
 34  
         try {
 35  0
             out = new FileOutputStream(filename);
 36  0
             IOUtils.write(content, out);
 37  
         } finally {
 38  0
             IOUtils.closeQuietly(out);
 39  0
         }
 40  0
     }
 41  
 
 42  
     public String getShellScript(List<Artifact> artifacts) throws IOException {
 43  0
         StringBuilder sb = new StringBuilder();
 44  0
         sb.append("#!/bin/sh\n");
 45  0
         for (Artifact artifact : artifacts) {
 46  0
             sb.append(getCommandLine(artifact) + "\n");
 47  
         }
 48  0
         return sb.toString();
 49  
     }
 50  
 
 51  
     protected String getCommandLine(Artifact artifact) throws IOException {
 52  0
         StringBuilder sb = new StringBuilder();
 53  0
         sb.append("mvn process-resources -Pprivate");
 54  0
         sb.append(" -Ddeployment.groupId=" + artifact.getGroupId());
 55  0
         sb.append(" -Ddeployment.version=" + artifact.getVersion());
 56  0
         sb.append(" -Ddeployment.artifactId=" + artifact.getArtifactId());
 57  0
         sb.append(" -Ddeployment.file=" + artifact.getFile().getCanonicalPath());
 58  0
         return sb.toString();
 59  
     }
 60  
 
 61  
     public String getGAVString(Artifact artifact) {
 62  0
         StringBuilder sb = new StringBuilder();
 63  0
         sb.append(artifact.getGroupId());
 64  0
         sb.append(":");
 65  0
         sb.append(artifact.getArtifactId());
 66  0
         sb.append(":");
 67  0
         sb.append(artifact.getVersion());
 68  0
         return sb.toString();
 69  
     }
 70  
 
 71  
     public List<Artifact> getArtifacts(File directory, String groupId, String versions) {
 72  0
         List<File> files = getFiles(directory);
 73  0
         List<Artifact> artifacts = new ArrayList<Artifact>();
 74  0
         for (File file : files) {
 75  0
             Artifact artifact = getArtifact(directory, file, groupId);
 76  0
             artifacts.add(artifact);
 77  0
         }
 78  0
         trimArtifacts(artifacts, versions);
 79  0
         Collections.sort(artifacts, new ArtifactComparator());
 80  0
         return artifacts;
 81  
     }
 82  
 
 83  
     protected boolean isMatch(String s, String[] tokens) {
 84  0
         for (String token : tokens) {
 85  0
             if (s.trim().equals(token.trim())) {
 86  0
                 return true;
 87  
             }
 88  
         }
 89  0
         return false;
 90  
     }
 91  
 
 92  
     protected void trimArtifacts(List<Artifact> artifacts, String includeVersions) {
 93  0
         if (StringUtils.isEmpty(includeVersions)) {
 94  0
             return;
 95  
         }
 96  0
         String[] versions = includeVersions.split(",");
 97  0
         Iterator<Artifact> itr = artifacts.iterator();
 98  0
         while (itr.hasNext()) {
 99  0
             Artifact artifact = itr.next();
 100  0
             if (!isMatch(artifact.getVersion(), versions)) {
 101  0
                 itr.remove();
 102  
             }
 103  0
         }
 104  0
     }
 105  
 
 106  
     protected Artifact getArtifact(File baseDirectory, File file, String groupId) {
 107  0
         ArtifactHandler handler = new DefaultArtifactHandler("jar");
 108  0
         String artifactId = getArtifactId(file);
 109  0
         String version = getVersion(baseDirectory, file);
 110  0
         Artifact artifact = new DefaultArtifact(groupId, artifactId, version, null, "jar", null, handler);
 111  0
         artifact.setFile(file);
 112  0
         return artifact;
 113  
     }
 114  
 
 115  
     protected String getVersion(File baseDirectory, File file) {
 116  0
         String path1 = baseDirectory.getAbsolutePath();
 117  0
         String path2 = file.getAbsolutePath();
 118  0
         int pos = path2.indexOf(path1) + path1.length();
 119  0
         String version = path2.substring(pos);
 120  0
         String filename = file.getName();
 121  0
         pos = version.indexOf(filename);
 122  0
         version = version.substring(0, pos);
 123  0
         String fileSeparator = System.getProperty("file.separator");
 124  0
         version = version.replace(fileSeparator, "");
 125  0
         return version;
 126  
     }
 127  
 
 128  
     protected String getArtifactId(File file) {
 129  0
         String filename = file.getName();
 130  0
         int pos = filename.indexOf(".");
 131  0
         return filename.substring(0, pos);
 132  
     }
 133  
 
 134  
     public List<File> getFiles(File directory) {
 135  0
         if (!directory.isDirectory()) {
 136  0
             throw new IllegalArgumentException(directory + " is not a directory");
 137  
         }
 138  0
         File[] fileList = directory.listFiles();
 139  0
         List<File> files = new ArrayList<File>();
 140  0
         for (File file : fileList) {
 141  0
             if (file.isDirectory()) {
 142  0
                 files.addAll(getFiles(file));
 143  
             } else {
 144  0
                 files.add(file);
 145  
             }
 146  
         }
 147  0
         return files;
 148  
     }
 149  
 
 150  
 }