Return-Path: X-Original-To: apmail-ant-notifications-archive@minotaur.apache.org Delivered-To: apmail-ant-notifications-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 59F5FD877 for ; Mon, 27 Aug 2012 10:15:25 +0000 (UTC) Received: (qmail 22447 invoked by uid 500); 27 Aug 2012 10:15:24 -0000 Delivered-To: apmail-ant-notifications-archive@ant.apache.org Received: (qmail 22374 invoked by uid 500); 27 Aug 2012 10:15:22 -0000 Mailing-List: contact notifications-help@ant.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ant.apache.org Delivered-To: mailing list notifications@ant.apache.org Received: (qmail 22343 invoked by uid 99); 27 Aug 2012 10:15:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Aug 2012 10:15:21 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Aug 2012 10:15:16 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2FF082388994 for ; Mon, 27 Aug 2012 10:14:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1377632 - in /ant/sandbox/antdsl/branches/import-experiment: ./ org.apache.ant.antdsl/src/org/apache/ant/antdsl/ org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/ test/ test/test-module/ test/test-module/org/ test/test-module/org/apac... Date: Mon, 27 Aug 2012 10:14:31 -0000 To: notifications@ant.apache.org From: hibou@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120827101432.2FF082388994@eris.apache.org> Author: hibou Date: Mon Aug 27 10:14:30 2012 New Revision: 1377632 URL: http://svn.apache.org/viewvc?rev=1377632&view=rev Log: - remove classloader hack by using BundleWiring - implement a function to try to find the classloader of the bundle holding a resource (an ant script) - stack the classloader properly when importing a script - make the import module work on a basic exemple Added: ant/sandbox/antdsl/branches/import-experiment/test/build.xml (with props) ant/sandbox/antdsl/branches/import-experiment/test/test-module/ ant/sandbox/antdsl/branches/import-experiment/test/test-module.bnd ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/ ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/ ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/ ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/ ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant (with props) Removed: ant/sandbox/antdsl/branches/import-experiment/test/getAntUnitOSGi.sh Modified: ant/sandbox/antdsl/branches/import-experiment/build.xml ant/sandbox/antdsl/branches/import-experiment/ivy-fixed.xml ant/sandbox/antdsl/branches/import-experiment/ivy.xml ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AbstractAntDslProjectHelper.java ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/OSGiFrameworkManager.java ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java ant/sandbox/antdsl/branches/import-experiment/test/ (props changed) ant/sandbox/antdsl/branches/import-experiment/test/build.ant Modified: ant/sandbox/antdsl/branches/import-experiment/build.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/build.xml?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/build.xml (original) +++ ant/sandbox/antdsl/branches/import-experiment/build.xml Mon Aug 27 10:14:30 2012 @@ -44,8 +44,8 @@ - - + + Modified: ant/sandbox/antdsl/branches/import-experiment/ivy-fixed.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/ivy-fixed.xml?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/ivy-fixed.xml (original) +++ ant/sandbox/antdsl/branches/import-experiment/ivy-fixed.xml Mon Aug 27 10:14:30 2012 @@ -4,7 +4,7 @@ module="ant-dsl" revision="1.0.0" status="integration" - publication="20120822212844" + publication="20120826210539" /> @@ -157,7 +157,7 @@ + - Modified: ant/sandbox/antdsl/branches/import-experiment/ivy.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/ivy.xml?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/ivy.xml (original) +++ ant/sandbox/antdsl/branches/import-experiment/ivy.xml Mon Aug 27 10:14:30 2012 @@ -17,7 +17,7 @@ - + Modified: ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AbstractAntDslProjectHelper.java URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AbstractAntDslProjectHelper.java?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AbstractAntDslProjectHelper.java (original) +++ ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AbstractAntDslProjectHelper.java Mon Aug 27 10:14:30 2012 @@ -373,6 +373,12 @@ public abstract class AbstractAntDslProj throw new BuildException("Unable to install the bundle " + resource.getName() + " (" + e.getMessage() + ")", e); } } + + try { + osgiFrameworkManager.start(); + } catch (BundleException e) { + throw new BuildException("Unable to start the OSGi framework (" + e.getMessage() + ")", e); + } } protected void importAntlib(Project project, AntDslContext context, String name, String resource) { @@ -408,9 +414,9 @@ public abstract class AbstractAntDslProj ProjectHelper subHelper = ProjectHelperRepository.getInstance().getProjectHelperForBuildFile(urlResource); - // TODO - // add to the classloader stack the classloader of the imported build - + ClassLoader childCl = getOSGiFrameworkManager(project).getClassLoader(buildModule, buildUrl); + getClassloaderStack(project).push(childCl); + // push current stacks into the sub helper subHelper.getImportStack().addAll(this.getImportStack()); subHelper.getExtensionStack().addAll(this.getExtensionStack()); @@ -419,6 +425,7 @@ public abstract class AbstractAntDslProj subHelper.parse(project, urlResource); // push back the stack from the sub helper to the main one + getClassloaderStack(project).pop(); project.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, this); getImportStack().clear(); getImportStack().addAll(subHelper.getImportStack()); Modified: ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/OSGiFrameworkManager.java URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/OSGiFrameworkManager.java?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/OSGiFrameworkManager.java (original) +++ ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/OSGiFrameworkManager.java Mon Aug 27 10:14:30 2012 @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -18,33 +17,52 @@ import org.osgi.framework.BundleExceptio import org.osgi.framework.Constants; import org.osgi.framework.launch.Framework; import org.osgi.framework.launch.FrameworkFactory; +import org.osgi.framework.wiring.BundleWiring; public class OSGiFrameworkManager { - private static final String ANT_PACKAGES = "org.apache.tools.ant," + "org.apache.tools.ant.types," + "org.apache.tools.ant.taskdefs.condition," - + "org.apache.tools.ant.taskdefs," + "org.apache.tools.ant.util"; + //@formatter:off + private static final String ANT_PACKAGES = + "org.apache.tools.ant," + + "org.apache.tools.ant.types," + + "org.apache.tools.ant.taskdefs.condition," + + "org.apache.tools.ant.taskdefs," + + "org.apache.tools.ant.util"; + //@formatter:on private Framework framework; - private List bundleToStart = new ArrayList(); + private List bundles = new ArrayList(); - private Map classloaders = new LinkedHashMap(); - - private GodClassLoader godClassLoader; + private GodClassLoader godClassLoader = new GodClassLoader(); public OSGiFrameworkManager() throws BundleException { Map configMap = new HashMap(); configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, ANT_PACKAGES); framework = getFrameworkFactory().newFramework(configMap); framework.init(); - godClassLoader = new GodClassLoader(); } private FrameworkFactory getFrameworkFactory() { - URL url = OSGiFrameworkManager.class.getClassLoader().getResource("META-INF/services/org.osgi.framework.launch.FrameworkFactory"); - if (url == null) { + Enumeration urls; + try { + urls = OSGiFrameworkManager.class.getClassLoader().getResources("META-INF/services/org.osgi.framework.launch.FrameworkFactory"); + } catch (IOException e) { + throw new BuildException(e); + } + if (urls == null || !urls.hasMoreElements()) { throw new BuildException("No OSGi framework could be found in Ant's classpath"); } + URL url = null; + ArrayList urlList = Collections.list(urls); + for (URL candidate : urlList) { + if (candidate.toExternalForm().contains("felix")) { + url = candidate; + } + } + if (url == null) { + url = urlList.iterator().next(); + } BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(url.openStream())); @@ -71,9 +89,8 @@ public class OSGiFrameworkManager { public void install(String bundleURI) throws BundleException { Bundle bundle = framework.getBundleContext().installBundle(bundleURI); - classloaders.put(bundleURI, new BundleClassLoader(bundle)); if (!isFragment(bundle)) { - bundleToStart.add(bundle); + bundles.add(bundle); } } @@ -95,42 +112,34 @@ public class OSGiFrameworkManager { } } }); - for (Bundle bundle : bundleToStart) { + for (Bundle bundle : bundles) { bundle.start(); } } - public ClassLoader getClassloader(String bundleURI) { - return classloaders.get(bundleURI); - } - public GodClassLoader getGodClassLoader() { return godClassLoader; } - private static class BundleClassLoader extends ClassLoader { - - private Bundle bundle; - - private BundleClassLoader(Bundle bundle) { - super(null); - this.bundle = bundle; - } - - @Override - public URL getResource(String name) { - return bundle.getResource(name); - } - - @Override - public Enumeration getResources(String name) throws IOException { - return bundle.getResources(name); - } + public List getBundles() { + return bundles; + } - @Override - public Class< ? > loadClass(String name) throws ClassNotFoundException { - return bundle.loadClass(name); + /** + * Find which classloader is responsible for resolving holding url + * + * @param url + * @return + */ + public ClassLoader getClassLoader(String resource, URL url) { + for (Bundle bundle : bundles) { + BundleWiring wiring = bundle.adapt(BundleWiring.class); + List entries = wiring.findEntries(resource, null, 0); + if (!entries.isEmpty()) { + return wiring.getClassLoader(); + } } + return null; } private class GodClassLoader extends ClassLoader { @@ -141,7 +150,9 @@ public class OSGiFrameworkManager { @Override public URL getResource(String name) { - for (ClassLoader cl : classloaders.values()) { + for (Bundle bundle : bundles) { + BundleWiring wiring = bundle.adapt(BundleWiring.class); + ClassLoader cl = wiring.getClassLoader(); URL url = cl.getResource(name); if (url != null) { return url; @@ -153,7 +164,9 @@ public class OSGiFrameworkManager { @Override public Enumeration getResources(String name) throws IOException { List urls = new ArrayList(); - for (ClassLoader cl : classloaders.values()) { + for (Bundle bundle : bundles) { + BundleWiring wiring = bundle.adapt(BundleWiring.class); + ClassLoader cl = wiring.getClassLoader(); Enumeration resources = cl.getResources(name); if (resources != null) { urls.addAll(Collections.list(resources)); @@ -164,7 +177,9 @@ public class OSGiFrameworkManager { @Override public Class< ? > loadClass(String name) throws ClassNotFoundException { - for (ClassLoader cl : classloaders.values()) { + for (Bundle bundle : bundles) { + BundleWiring wiring = bundle.adapt(BundleWiring.class); + ClassLoader cl = wiring.getClassLoader(); try { return cl.loadClass(name); } catch (ClassNotFoundException e) { Modified: ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext (original) +++ ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext Mon Aug 27 10:14:30 2012 @@ -43,7 +43,7 @@ EAntlibImport: ; EBuildImport: - 'module' name=QualifiedName + 'module' file=StringLiteral ; EExtensionPoint: Modified: ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java (original) +++ ant/sandbox/antdsl/branches/import-experiment/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java Mon Aug 27 10:14:30 2012 @@ -181,7 +181,7 @@ public class AntDslXTextProjectHelper ex importAntlib(project, context, readIdentifier(eAntlibImport.getName()), readString(eAntlibImport.getResource())); } else if (eImport instanceof EBuildImport) { EBuildImport eBuildImport = (EBuildImport) eImport; - // TODO + importBuildModule(project, context, readString(eBuildImport.getFile())); } else { throw new IllegalArgumentException("Unsupported import " + eImport.getClass().getName()); } Propchange: ant/sandbox/antdsl/branches/import-experiment/test/ ------------------------------------------------------------------------------ --- svn:ignore (original) +++ svn:ignore Mon Aug 27 10:14:30 2012 @@ -1,2 +1,4 @@ org.apache.ant.antunit-1.2.jar ant-antunit-1.2.jar +biz.aQute.bnd-1.50.0.jar +org.apache.ant.antdsl.test-1.0.jar Modified: ant/sandbox/antdsl/branches/import-experiment/test/build.ant URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/test/build.ant?rev=1377632&r1=1377631&r2=1377632&view=diff ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/test/build.ant (original) +++ ant/sandbox/antdsl/branches/import-experiment/test/build.ant Mon Aug 27 10:14:30 2012 @@ -3,10 +3,13 @@ default : build antpath { file(file = "test/org.apache.ant.antunit-1.2.jar") + file(file = "test/org.apache.ant.antdsl.test-1.0.jar") } import antlib "org/apache/ant/antunit/antlib.xml" as antunit +import module "org/apache/ant/antdsl/test/build.ant" target build + depends imported { antunit:assertEquals(expected = "test", actual = "test") antunit:assertEquals(expected = "test", actual = "nok") Added: ant/sandbox/antdsl/branches/import-experiment/test/build.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/test/build.xml?rev=1377632&view=auto ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/test/build.xml (added) +++ ant/sandbox/antdsl/branches/import-experiment/test/build.xml Mon Aug 27 10:14:30 2012 @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file Propchange: ant/sandbox/antdsl/branches/import-experiment/test/build.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ant/sandbox/antdsl/branches/import-experiment/test/build.xml ------------------------------------------------------------------------------ svn:keywords = Date Revision Author HeadURL Id Propchange: ant/sandbox/antdsl/branches/import-experiment/test/build.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Added: ant/sandbox/antdsl/branches/import-experiment/test/test-module.bnd URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/test/test-module.bnd?rev=1377632&view=auto ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/test/test-module.bnd (added) +++ ant/sandbox/antdsl/branches/import-experiment/test/test-module.bnd Mon Aug 27 10:14:30 2012 @@ -0,0 +1,6 @@ +version=1.0 + +Bundle-Version: ${version} +Bundle-SymbolicName: org.apache.ant.antdsl.test +Include-Resource: test-module +Export-Package: org.apache.ant.antdsl.test.* \ No newline at end of file Added: ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant URL: http://svn.apache.org/viewvc/ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant?rev=1377632&view=auto ============================================================================== --- ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant (added) +++ ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant Mon Aug 27 10:14:30 2012 @@ -0,0 +1,8 @@ + +import antlib "org/apache/ant/antunit/antlib.xml" as antunit + +target imported +{ + antunit:assertEquals(expected = "test", actual = "test") + echo(message = "imported!") +} Propchange: ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant ------------------------------------------------------------------------------ svn:keywords = Date Revision Author HeadURL Id Propchange: ant/sandbox/antdsl/branches/import-experiment/test/test-module/org/apache/ant/antdsl/test/build.ant ------------------------------------------------------------------------------ svn:mime-type = text/xml