diff --git a/core/src/main/java/lucee/commons/io/res/util/ResourceUtil.java b/core/src/main/java/lucee/commons/io/res/util/ResourceUtil.java index 1bf9a41622..f179b86bef 100755 --- a/core/src/main/java/lucee/commons/io/res/util/ResourceUtil.java +++ b/core/src/main/java/lucee/commons/io/res/util/ResourceUtil.java @@ -816,9 +816,14 @@ public static String getMimeType(Resource res, String fileName, String defaultVa * @return is inside or not */ public static boolean isChildOf(Resource file, Resource dir) { - while (file != null) { - if (file.equals(dir)) return true; - file = file.getParentResource(); + if (dir == null || !file.getResourceProvider().getScheme().equals(dir.getResourceProvider().getScheme())) return false; + // + if ((file.getAbsolutePath()).startsWith(dir.getAbsolutePath())) { + return true; + } + + if ((getCanonicalPathEL(file)).startsWith(getCanonicalPathEL(dir))) { + return true; } return false; } @@ -829,18 +834,19 @@ public static boolean isChildOf(Resource file, Resource dir) { * @param file file to search * @param dir directory to search */ - public static String getPathToChild(Resource file, Resource dir) { + public static String getPathToChild(Resource file, final Resource dir) { if (dir == null || !file.getResourceProvider().getScheme().equals(dir.getResourceProvider().getScheme())) return null; - boolean isFile = file.isFile(); - String str = "/"; - while (file != null) { - if (file.equals(dir)) { - if (isFile) return str.substring(0, str.length() - 1); - return str; - } - str = "/" + file.getName() + str; - file = file.getParentResource(); + + String strFile, strDir; + // + if ((strFile = file.getAbsolutePath()).startsWith(strDir = dir.getAbsolutePath())) { + return strFile.substring(strDir.length()); + } + + if ((strFile = getCanonicalPathEL(file)).startsWith(strDir = getCanonicalPathEL(dir))) { + return strFile.substring(strDir.length()); } + return null; } diff --git a/core/src/main/java/lucee/runtime/osgi/OSGiUtil.java b/core/src/main/java/lucee/runtime/osgi/OSGiUtil.java index 72fd4dc775..b3aece8bc3 100644 --- a/core/src/main/java/lucee/runtime/osgi/OSGiUtil.java +++ b/core/src/main/java/lucee/runtime/osgi/OSGiUtil.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; @@ -81,7 +82,7 @@ import lucee.runtime.type.util.ListUtil; public class OSGiUtil { - + private static boolean[] checkBundleRanges = new boolean[] { true, false }; private static final int QUALIFIER_APPENDIX_SNAPSHOT = 1; private static final int QUALIFIER_APPENDIX_BETA = 2; private static final int QUALIFIER_APPENDIX_RC = 3; @@ -99,6 +100,12 @@ protected Set initialValue() { private static class Filter implements FilenameFilter, ResourceNameFilter { + private BundleRange bundleRange; + + public Filter(BundleRange bundleRange) { + this.bundleRange = bundleRange; + } + @Override public boolean accept(File dir, String name) { return accept(name); @@ -110,11 +117,22 @@ public boolean accept(Resource dir, String name) { } private boolean accept(String name) { - return name.endsWith(".jar"); + if (!name.endsWith(".jar")) return false; + if (bundleRange == null) return true; + Version v; + String[] patterns = new String[] { bundleRange.getName() + "-", bundleRange.getName().replace('.', '-') + "-", bundleRange.getName().replace('-', '.') + "-" }; + for (String pattern: patterns) { + if (name.startsWith(pattern)) { + v = toVersion(name.substring(pattern.length(), name.length() - 4), null); + if (v != null) return true; + } + } + + return false; } } - private static final Filter JAR_EXT_FILTER = new Filter(); + private static final Filter JAR_EXT_FILTER = new Filter(null); private static String[] bootDelegation; private static Map packageBundleMapping = new LinkedHashMap(); @@ -751,13 +769,18 @@ public static Bundle _loadBundle(final BundleRange bundleRange, Identification i if (bundleRange.getVersionRange() != null && !bundleRange.getVersionRange().isEmpty()) { // TODO not only check for from version, request a range, but that needs an adjustment with the // provider - BundleFile _bf = BundleFile.getInstance(factory.downloadBundle(bundleRange.getName(), bundleRange.getVersionRange().getFrom().getVersion().toString(), id)); + BundleFile _bf = improveFileName( + BundleFile.getInstance(factory.downloadBundle(bundleRange.getName(), bundleRange.getVersionRange().getFrom().getVersion().toString(), id))); resetJarsFromBundleDirectory(factory); b = _loadBundle(bc, _bf); } else { // MUST find out why this breaks at startup with commandbox if version exists Resource r = downloadBundle(factory, bundleRange.getName(), null, id); + BundleFile src = BundleFile.getInstance(r); + BundleFile trg = improveFileName(src); + if (src != trg) r = ResourceUtil.toResource(trg.getFile()); + resetJarsFromBundleDirectory(factory); b = _loadBundle(bc, r); } @@ -835,9 +858,11 @@ private static Resource downloadBundle(CFMLEngineFactory factory, final String s throw re; } } - + // print.e("xxxxxxxxxxxxxxxxxxxxxxx"); + // print.e(symbolicName + ":" + symbolicVersion); final Resource jarDir = ResourceUtil.toResource(factory.getBundleDirectory()); final URL updateProvider = factory.getUpdateLocation(); + if (symbolicVersion == null) symbolicVersion = "latest"; final URL updateUrl = new URL(updateProvider, "/rest/update/provider/download/" + symbolicName + "/" + symbolicVersion + "/" + (id != null ? id.toQueryString() : "") + (id == null ? "?" : "&") + "allowRedirect=true" @@ -1078,7 +1103,7 @@ private static BundleFile _getBundleFile(CFMLEngineFactory factory, BundleRange BundleFile mbf = null; Resource dir = ResourceUtil.toResource(factory.getBundleDirectory()); // first we check if there is a file match (fastest solution) - List jars = createPossibleNameMatches(dir, addional, bundleRange.getName()); + List jars = createPossibleNameMatches(dir, addional, bundleRange); for (Resource jar: jars) { if (jar.isFile()) { match = jar; @@ -1088,30 +1113,41 @@ private static BundleFile _getBundleFile(CFMLEngineFactory factory, BundleRange } } } - if (mbf != null) return mbf; - + if (mbf != null) { + return improveFileName(mbf); + } List children = listFiles(dir, addional, JAR_EXT_FILTER); - // now we check by Manifest comparsion, name not necessary reflect the correct bundle info - BundleFile bf; - for (Resource child: children) { - match = child; - bf = BundleFile.getInstance(child); - if (bf.isBundle()) { - if (bf.getSymbolicName().equals(bundleRange.getName())) { - if (bundleRange.matches(bf)) { - if (mbf == null || OSGiUtil.isNewerThan(bf.getVersion(), mbf.getVersion())) mbf = bf; - } - else { - if (versionsFound != null) { - if (versionsFound.length() > 0) versionsFound.append(", "); - versionsFound.append(bf.getVersionAsString()); + // now we check all jar files + { + + // now we check by Manifest comparsion, name not necessary reflect the correct bundle info + BundleFile bf; + for (boolean checkBundleRange: checkBundleRanges) { + mbf = null; + for (Resource child: children) { + if (checkBundleRange && !new Filter(bundleRange).accept(child.getName())) continue; + match = child; + bf = BundleFile.getInstance(child); + if (bf.isBundle()) { + if (bf.getSymbolicName().equals(bundleRange.getName())) { + if (bundleRange.matches(bf)) { + if (mbf == null || OSGiUtil.isNewerThan(bf.getVersion(), mbf.getVersion())) mbf = bf; + } + else { + if (versionsFound != null) { + if (versionsFound.length() > 0) versionsFound.append(", "); + versionsFound.append(bf.getVersionAsString()); + } + } } } } + if (mbf != null) { + return improveFileName(mbf); + } } } - if (mbf != null) return mbf; } catch (Exception e) { @@ -1137,15 +1173,60 @@ private static BundleFile _getBundleFile(CFMLEngineFactory factory, BundleRange return null; } - private static List createPossibleNameMatches(Resource dir, List addional, String name) { - String[] patterns = new String[] { name + "-", name.replace('.', '-'), name.replace('-', '.') }; + /** + * rename file to match the Manifest information + * + * @param bf + */ + private static BundleFile improveFileName(BundleFile bf) { + File f = bf.getFile(); + String preferedName = bf.getSymbolicName() + "-" + bf.getVersionAsString() + ".jar"; + if (!preferedName.equals(f.getName())) { + try { + File nf = new File(f.getParentFile(), preferedName); + + if (f.renameTo(nf)) { + return BundleFile.getInstance(nf); + } + else { + IOUtil.copy(new FileInputStream(f), new FileOutputStream(nf), true, true); + if (!f.delete()) { + f.deleteOnExit(); + } + else { + return BundleFile.getInstance(nf); + } + } + } + catch (Exception e) { + LogUtil.log("OSGi", e); + } + } + return bf; + } + private static List createPossibleNameMatches(Resource dir, List addional, BundleRange bundleRange) { List resources = new ArrayList(); - for (String pattern: patterns) { - resources.add(dir.getRealResource(pattern)); + + // do we have a from match + VersionRange vr = bundleRange.getVersionRange(); + if (vr != null) { + VersionDefinition from = vr.getFrom(); + if (from != null && from.op == VersionDefinition.EQ || from.op == VersionDefinition.GTE) { + String name = bundleRange.getName(); + String[] patterns = new String[] { name + "-" + from.version + ".jar", name.replace('.', '-') + "-" + (from.version.toString().replace('.', '-')) + ".jar", + name.replace('-', '.') + "-" + from.version + ".jar" }; + + for (String pattern: patterns) { + resources.add(dir.getRealResource(pattern)); + } + } } if (addional != null && !addional.isEmpty()) { + String name = bundleRange.getName(); + String[] patterns = new String[] { name + "-", name.replace('.', '-'), name.replace('-', '.') }; + Iterator it = addional.iterator(); Resource res; while (it.hasNext()) { diff --git a/loader/build.xml b/loader/build.xml index d7e3457f6a..1dee5c7594 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index 9d81826416..4862002a58 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.0.1.4-SNAPSHOT + 6.0.1.5-SNAPSHOT jar Lucee Loader Build