Repository: tomee Updated Branches: refs/heads/master 4e94dc8ae -> 1adec5221
TOMEE-1870 avoid to scan when scan.xml gives all the info we need Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/1adec522 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/1adec522 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/1adec522 Branch: refs/heads/master Commit: 1adec5221c960b9447eb7d8d8666d234efffe225 Parents: 4e94dc8 Author: Romain manni-Bucau <rmannibu...@gmail.com> Authored: Sat Jul 16 20:04:50 2016 +0200 Committer: Romain manni-Bucau <rmannibu...@gmail.com> Committed: Sat Jul 16 20:04:50 2016 +0200 ---------------------------------------------------------------------- .../org/apache/openejb/config/ScanUtil.java | 8 +++ .../openejb/config/WebappAggregatedArchive.java | 32 ++++++++++- .../config/WebappAggregatedArchiveTest.java | 58 ++++++++++++++++++++ .../resources/WebappAggregatedArchiveTest.xml | 23 ++++++++ 4 files changed, 118 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/1adec522/container/openejb-core/src/main/java/org/apache/openejb/config/ScanUtil.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/ScanUtil.java b/container/openejb-core/src/main/java/org/apache/openejb/config/ScanUtil.java index 7a0d452..244f09d 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/ScanUtil.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/ScanUtil.java @@ -49,6 +49,7 @@ public final class ScanUtil { private final Set<String> classes = new HashSet<String>(); private final Set<String> packages = new HashSet<String>(); private Set<String> current; + private boolean optimized = true; @Override public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { @@ -56,6 +57,9 @@ public final class ScanUtil { current = classes; } else if (qName.equals("package")) { current = packages; + } else if (qName.equals("scan")) { + final String optimized = attributes.getValue("optimized"); + this.optimized = optimized == null || Boolean.parseBoolean(optimized); } } @@ -71,6 +75,10 @@ public final class ScanUtil { current = null; } + public boolean isOptimized() { + return optimized; + } + public Set<String> getPackages() { return packages; } http://git-wip-us.apache.org/repos/asf/tomee/blob/1adec522/container/openejb-core/src/main/java/org/apache/openejb/config/WebappAggregatedArchive.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/WebappAggregatedArchive.java b/container/openejb-core/src/main/java/org/apache/openejb/config/WebappAggregatedArchive.java index 047270a..54d8dc2 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/WebappAggregatedArchive.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/WebappAggregatedArchive.java @@ -18,6 +18,7 @@ package org.apache.openejb.config; import org.apache.xbean.finder.archive.Archive; +import org.apache.xbean.finder.archive.ClassesArchive; import org.apache.xbean.finder.archive.CompositeArchive; import org.apache.xbean.finder.archive.FilteredArchive; import org.apache.xbean.finder.filter.Filter; @@ -26,18 +27,20 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import static java.util.Collections.singletonList; +import static org.apache.openejb.loader.JarLocation.jarLocation; + public class WebappAggregatedArchive implements Archive, ScanConstants { private final Map<URL, List<String>> map = new HashMap<URL, List<String>>(); private ScanUtil.ScanHandler handler; private boolean scanXmlExists; // faster than using an empty handler - private final Archive archive; + private Archive archive; public WebappAggregatedArchive(final Module module, final Iterable<URL> urls, final Filter filter) { final List<Archive> archives = new ArrayList<Archive>(); @@ -47,6 +50,28 @@ public class WebappAggregatedArchive implements Archive, ScanConstants { try { handler = ScanUtil.read(scanXml); scanXmlExists = true; + if ((handler.getPackages() == null || handler.getPackages().isEmpty()) + && handler.getClasses() != null && !handler.getClasses().isEmpty() + && handler.isOptimized()) { // only classes, skip scanning + final Collection<Class<?>> loaded = new ArrayList<>(handler.getClasses().size()); + for (final String clazz : handler.getClasses()) { + try { + final Class<?> aClass = module.getClassLoader().loadClass(clazz); + loaded.add(aClass); + final URL jar = jarLocation(aClass).toURI().toURL(); + List<String> list = map.get(jar); + if (list == null) { + list = new ArrayList<>(); + map.put(jar, list); + } + list.add(clazz); + } catch (final ClassNotFoundException e) { + throw new IllegalArgumentException(e); + } + } + archive = new ClassesArchive(loaded.toArray(new Class[loaded.size()])); + return; + } } catch (final IOException e) { // ignored, will not use filtering with scan.xml } @@ -55,7 +80,8 @@ public class WebappAggregatedArchive implements Archive, ScanConstants { for (final URL url : urls) { final List<String> classes = new ArrayList<String>(); final Archive archive = new FilteredArchive( - new ConfigurableClasspathArchive(module.getClassLoader(), Arrays.asList(url)), new ScanXmlSaverFilter(scanXmlExists, handler, classes, filter)); + new ConfigurableClasspathArchive(module.getClassLoader(), singletonList(url)), + new ScanXmlSaverFilter(scanXmlExists, handler, classes, filter)); map.put(url, classes); archives.add(archive); } http://git-wip-us.apache.org/repos/asf/tomee/blob/1adec522/container/openejb-core/src/test/java/org/apache/openejb/config/WebappAggregatedArchiveTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/WebappAggregatedArchiveTest.java b/container/openejb-core/src/test/java/org/apache/openejb/config/WebappAggregatedArchiveTest.java new file mode 100644 index 0000000..4f4596f --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/config/WebappAggregatedArchiveTest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openejb.config; + +import org.junit.Test; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; +import java.util.Map; + +import static java.util.Arrays.asList; +import static java.util.Collections.singletonMap; +import static org.apache.openejb.loader.JarLocation.jarLocation; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class WebappAggregatedArchiveTest { + @Test + public void allClassesDefined() throws MalformedURLException { + final WebappAggregatedArchive aggregatedArchive = new WebappAggregatedArchive( + new Module(false) { + @Override + public ClassLoader getClassLoader() { + return Thread.currentThread().getContextClassLoader(); + } + + @Override + public Map<String, Object> getAltDDs() { + return Map.class.cast( + singletonMap("scan.xml", Thread.currentThread().getContextClassLoader().getResource("WebappAggregatedArchiveTest.xml"))); + } + }, + asList(jarLocation(WebappAggregatedArchive.class).toURI().toURL(), + jarLocation(WebappAggregatedArchiveTest.class).toURI().toURL())); + assertEquals(1, aggregatedArchive.getClassesMap().size()); + + final URL key = jarLocation(WebappAggregatedArchiveTest.class).toURI().toURL(); + final List<String> classes = aggregatedArchive.getClassesMap().get(key); + assertNotNull(classes); + assertEquals(1, classes.size()); + assertEquals(WebappAggregatedArchiveTest.class.getName(), classes.iterator().next()); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/1adec522/container/openejb-core/src/test/resources/WebappAggregatedArchiveTest.xml ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/resources/WebappAggregatedArchiveTest.xml b/container/openejb-core/src/test/resources/WebappAggregatedArchiveTest.xml new file mode 100644 index 0000000..f237d8f --- /dev/null +++ b/container/openejb-core/src/test/resources/WebappAggregatedArchiveTest.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<scan> + <classes> + <class>org.apache.openejb.config.WebappAggregatedArchiveTest</class> + </classes> +</scan>