diff --git a/src/main/java/org/myrobotlab/framework/repo/IvyWrapper.java b/src/main/java/org/myrobotlab/framework/repo/IvyWrapper.java index aae1ace772..20c823bb7f 100644 --- a/src/main/java/org/myrobotlab/framework/repo/IvyWrapper.java +++ b/src/main/java/org/myrobotlab/framework/repo/IvyWrapper.java @@ -74,6 +74,7 @@ public void rawlog(String msg, int level) { static String ivysettingsXmlTemplate = null; static String ivyXmlTemplate = null; + transient static IvyWrapper localInstance = null; public static final Filter NO_FILTER = NoFilter.INSTANCE; @@ -404,97 +405,119 @@ public void installDependency(String location, ServiceDependency library) { } @Override - synchronized public void install(String location, String[] serviceTypes) { - - try { + synchronized public void install(String location, String[] serviceTypes) throws IOException { - Set targetLibraries = getUnfulfilledDependencies(serviceTypes); + Set targetLibraries = getUnfulfilledDependencies(serviceTypes); - if (targetLibraries.size() == 0) { - StringBuilder sb = new StringBuilder(); - for (String type : serviceTypes) { - if (type.lastIndexOf(".") > 0) { - sb.append(type.substring(type.lastIndexOf(".") + 1)); - } else { - sb.append(type); - } - sb.append(" "); + if (targetLibraries.size() == 0) { + StringBuilder sb = new StringBuilder(); + for (String type : serviceTypes) { + if (type.lastIndexOf(".") > 0) { + sb.append(type.substring(type.lastIndexOf(".") + 1)); + } else { + sb.append(type); } - info("%s already installed", sb.toString()); - return; + sb.append(" "); } + info("%s already installed", sb.toString()); + return; + } - publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.INFO, Repo.INSTALL_START, String.format("starting install of %s", (Object[]) serviceTypes))); + publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.INFO, Repo.INSTALL_START, String.format("starting install of %s", (Object[]) serviceTypes))); - log.info("installing {} services into {}", serviceTypes.length, location); + log.info("installing {} services into {}", serviceTypes.length, location); - // create build files - generates appropriate ivy.xml and settings files - // this service file should be marked as dependencies all others - // should be marked as provided - // ??? do "provided" get incorporate in the resolve ? - createBuildFiles(location, serviceTypes); + // create build files - generates appropriate ivy.xml and settings files + // this service file should be marked as dependencies all others + // should be marked as provided + // ??? do "provided" get incorporate in the resolve ? + createBuildFiles(location, serviceTypes); - Platform platform = Platform.getLocalInstance(); + Platform platform = Platform.getLocalInstance(); - // templates [originalname](-[classifier])(-[revision]).[ext] parens are "optional" - String[] cmd = new String[] { "-settings", location + "/ivysettings.xml", "-ivy", location + "/ivy.xml", "-retrieve", location + "/jar" + "/[originalname].[ext]" }; + // templates [originalname](-[classifier])(-[revision]).[ext] parens are + // "optional" - StringBuilder sb = new StringBuilder(); - sb.append("wget https://repo1.maven.org/maven2/org/apache/ivy/ivy/" + IVY_VERSION + "/ivy-" + IVY_VERSION + ".jar\n"); - sb.append("java -jar ivy-" + IVY_VERSION + ".jar"); - for (String s : cmd) { - sb.append(" "); - sb.append(s); - } + List cmd = new ArrayList<>(); + cmd.add("-settings"); + cmd.add(location + "/ivysettings.xml"); + cmd.add("-ivy"); + cmd.add(location + "/ivy.xml"); + cmd.add("-retrieve"); + cmd.add(location + "/jar" + "/[originalname].[ext]"); - sb.append("\n"); + int msgLevel = Message.MSG_WARN; + if (log.isWarnEnabled() || log.isErrorEnabled()) { + msgLevel = Message.MSG_WARN; + cmd.add("-warn"); + } else { + msgLevel = Message.MSG_INFO; + } - log.info("cmd {}", sb); - FileIO.toFile("libraries/install.sh", sb.toString().getBytes()); + StringBuilder sb = new StringBuilder(); + sb.append("wget https://repo1.maven.org/maven2/org/apache/ivy/ivy/" + IVY_VERSION + "/ivy-" + IVY_VERSION + ".jar\n"); + sb.append("java -jar ivy-" + IVY_VERSION + ".jar"); + for (String s : cmd) { + sb.append(" "); + sb.append(s); + } - Ivy ivy = Ivy.newInstance(); // <-- for future 2.5.x release - ivy.getLoggerEngine().pushLogger(new IvyWrapperLogger(Message.MSG_INFO)); + sb.append("\n"); - ResolveReport report = null; - List err = new ArrayList<>(); - try { - report = Main.run(cmd); - } catch(Exception e) { - err.add(e.toString()); - } + log.info("cmd {}", sb); + FileIO.toFile("libraries/install.sh", sb.toString().getBytes()); - // if no errors -h - // mark "service" as installed - // mark all libraries as installed + Ivy ivy = Ivy.newInstance(); // <-- for future 2.5.x release + ivy.getLoggerEngine().pushLogger(new IvyWrapperLogger(msgLevel)); - if (report != null) { - List problems = report.getAllProblemMessages(); - for (String problem : problems) { - if (!problem.startsWith("WARN: symlinkmass")) { - err.add(problem); - } + ResolveReport report = null; + List err = new ArrayList<>(); + try { + report = Main.run(cmd.toArray(new String[0])); + } catch (Exception e) { + err.add(e.toString()); + } + + // if no errors -h + // mark "service" as installed + // mark all libraries as installed + + if (report != null) { + List problems = report.getAllProblemMessages(); + for (String problem : problems) { + if (!problem.startsWith("WARN: symlinkmass")) { + err.add(problem); } } + } - if (err.size() > 0) { - log.error("had errors - repo will not be updated. Errors:\n{}", err); - } else { - - // TODO - promote to Repo.setInstalled - for (ServiceDependency library : targetLibraries) { - // set as installed & save state - library.setInstalled(true); - installedLibraries.put(library.toString(), library); - info("installed %s platform %s", library, platform.getPlatformId()); - } - save(); + if (err.size() > 0) { + log.error("had errors - repo will not be updated. Errors:\n{}", err); + } else { + + // TODO - promote to Repo.setInstalled + for (ServiceDependency library : targetLibraries) { + // set as installed & save state + library.setInstalled(true); + installedLibraries.put(library.toString(), library); + info("installed %s platform %s", library, platform.getPlatformId()); } + save(); + } + + if (report == null) { + String errorDetail = String.format("There were problems resolving dependencies %s", (Object[]) serviceTypes); + log.error(errorDetail); + publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.ERROR, Repo.INSTALL_FINISHED, errorDetail)); + throw new RuntimeException(errorDetail); + } else { ArtifactDownloadReport[] artifacts = report.getAllArtifactsReports(); for (int i = 0; i < artifacts.length; ++i) { ArtifactDownloadReport ar = artifacts[i]; Artifact artifact = ar.getArtifact(); - // String filename = IvyPatternHelper.substitute("[originalname].[ext]", + // String filename = + // IvyPatternHelper.substitute("[originalname].[ext]", // artifact); File file = ar.getLocalFile(); @@ -508,17 +531,13 @@ synchronized public void install(String location, String[] serviceTypes) { info("unzipped %s", filename); } catch (Exception e) { log.error("unable to unzip file {}", filename, e); + throw new IOException(String.format("unable to unzip file %s", filename)); } } } publishStatus(Status.newInstance(Repo.class.getSimpleName(), StatusLevel.INFO, Repo.INSTALL_FINISHED, String.format("finished install of %s", (Object[]) serviceTypes))); - - } catch (Exception e) { - error(e.getMessage()); - log.error(e.getMessage(), e); } - } private void publishStatus(String msg, int level) { diff --git a/src/main/java/org/myrobotlab/framework/repo/Repo.java b/src/main/java/org/myrobotlab/framework/repo/Repo.java index 2253e4753f..9262834da1 100644 --- a/src/main/java/org/myrobotlab/framework/repo/Repo.java +++ b/src/main/java/org/myrobotlab/framework/repo/Repo.java @@ -55,9 +55,9 @@ public abstract class Repo { */ protected static String LOCATION = null; - List errors = new ArrayList(); + protected List errors = new ArrayList(); - Map installedLibraries = new TreeMap(); + protected Map installedLibraries = new TreeMap(); public String getRepoPath() { return LOCATION + File.separator + REPO_STATE_FILE_NAME; @@ -360,7 +360,7 @@ static public void info(String format, Object... args) { publishStatus(Status.info(format, args)); } - synchronized public void install() { + synchronized public void install() throws Exception { // if a runtime exits we'll broadcast we are starting to install ServiceData sd = ServiceData.getLocalInstance(); info("starting installation of %s services", sd.getServiceTypeNames().length); @@ -368,11 +368,11 @@ synchronized public void install() { info("finished installing %d services", sd.getServiceTypeNames().length); } - synchronized public void install(String serviceType) { + synchronized public void install(String serviceType) throws Exception { install(getInstallDir(), serviceType); } - synchronized public void install(String location, String serviceType) { + synchronized public void install(String location, String serviceType) throws Exception { String[] types = null; if (serviceType == null) { @@ -403,18 +403,18 @@ synchronized public void installDependency(String libraries, String[] installDep abstract public void installDependency(String location, ServiceDependency serviceTypes); - abstract public void install(String location, String[] serviceTypes); + abstract public void install(String location, String[] serviceTypes) throws IOException; - synchronized public void install(String[] serviceTypes) { + synchronized public void install(String[] serviceTypes) throws Exception { install(getInstallDir(), serviceTypes); } - public void installEach() { + public void installEach() throws Exception { String workDir = String.format(String.format("libraries.ivy.services.%d", System.currentTimeMillis())); installEachTo(workDir); } - public void installEachTo(String location) { + public void installEachTo(String location) throws Exception { // if a runtime exits we'll broadcast we are starting to install ServiceData sd = ServiceData.getLocalInstance(); String[] serviceNames = sd.getServiceTypeNames(); @@ -423,7 +423,7 @@ public void installEachTo(String location) { info("finished installing %d services", sd.getServiceTypeNames().length); } - public void installTo(String location) { + public void installTo(String location) throws Exception { // if a runtime exits we'll broadcast we are starting to install ServiceData sd = ServiceData.getLocalInstance(); info("starting installation of %s services", sd.getServiceTypeNames().length); diff --git a/src/test/java/org/myrobotlab/framework/repo/RepoTest.java b/src/test/java/org/myrobotlab/framework/repo/RepoTest.java index 17a7a63c65..f2715dc90d 100644 --- a/src/test/java/org/myrobotlab/framework/repo/RepoTest.java +++ b/src/test/java/org/myrobotlab/framework/repo/RepoTest.java @@ -4,8 +4,6 @@ import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.IOException; -import java.text.ParseException; import java.util.ArrayList; import java.util.Set; @@ -50,7 +48,7 @@ public void setUp() throws Exception { } @Test - public void testAddStatusListener() throws ParseException, IOException { + public void testAddStatusListener() throws Exception { Repo repo = Repo.getInstance(); repo.addStatusPublisher(this); repo.install("Arduino"); @@ -81,7 +79,7 @@ public void testGetUnfulfilledDependencies() { } @Test - public void testIsInstalled() throws ParseException, IOException { + public void testIsInstalled() throws Exception { Repo repo = Repo.getInstance(); repo.clear(); repo.install("Arduino");