You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2019/10/13 21:50:32 UTC

[tomee] 02/02: Start of a simpler approach to bootstrapping an embedded server TOMEE-2706 New TomEE Embedded Bootstrap

This is an automated email from the ASF dual-hosted git repository.

dblevins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit c3f3b0e14bea3d27aa2010f37d77e937ec79f15e
Author: David Blevins <da...@gmail.com>
AuthorDate: Sun Oct 13 14:18:38 2019 -0700

    Start of a simpler approach to bootstrapping an embedded server
    TOMEE-2706 New TomEE Embedded Bootstrap
---
 tomee/tomee-bootstrap/pom.xml                      |  59 ++++++
 .../java/org/apache/tomee/bootstrap/Start.java     |  44 ++++
 .../test/java/org/apache/tomee/bootstrap/Misc.java | 232 +++++++++++++++++++++
 3 files changed, 335 insertions(+)

diff --git a/tomee/tomee-bootstrap/pom.xml b/tomee/tomee-bootstrap/pom.xml
new file mode 100644
index 0000000..86eea17
--- /dev/null
+++ b/tomee/tomee-bootstrap/pom.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+-->
+
+<!-- $Rev: 600338 $ $Date: 2007-12-02 09:08:04 -0800 (Sun, 02 Dec 2007) $ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <artifactId>tomee</artifactId>
+    <groupId>org.apache.tomee</groupId>
+    <version>8.0.1-SNAPSHOT</version>
+  </parent>
+
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>tomee-bootstrap</artifactId>
+  <packaging>jar</packaging>
+  <name>TomEE :: TomEE :: Bootstrap</name>
+  <description>This module contains the classes that will be added to the catalina class loader</description>
+
+  <properties>
+    <tomee.build.name>${project.groupId}.tomee.catalina</tomee.build.name>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.tomee.bom</groupId>
+      <artifactId>tomee-microprofile</artifactId>
+      <version>${project.version}</version>
+      <type>pom</type>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.projectlombok</groupId>
+      <artifactId>lombok</artifactId>
+      <version>1.18.8</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
+
diff --git a/tomee/tomee-bootstrap/src/main/java/org/apache/tomee/bootstrap/Start.java b/tomee/tomee-bootstrap/src/main/java/org/apache/tomee/bootstrap/Start.java
new file mode 100644
index 0000000..0ab8129
--- /dev/null
+++ b/tomee/tomee-bootstrap/src/main/java/org/apache/tomee/bootstrap/Start.java
@@ -0,0 +1,44 @@
+/*
+ * 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.tomee.bootstrap;
+
+import org.apache.catalina.startup.Catalina;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+public class Start {
+
+    public static void main(String[] args) {
+        start();
+    }
+    public static void start() {
+
+        final long start = System.currentTimeMillis();
+        System.setProperty("catalina.home", "/tmp/apache-tomee-microprofile-8.0.0-M3");
+        System.setProperty("catalina.base", "/tmp/apache-tomee-microprofile-8.0.0-M3");
+        final URLClassLoader loader = new URLClassLoader(new URL[0], Start.class.getClassLoader());
+
+        final Catalina catalina = new Catalina();
+        catalina.setParentClassLoader(loader);
+        catalina.setAwait(false);
+        catalina.load();
+        catalina.start();
+        final long elapsed = System.currentTimeMillis() - start;
+        System.out.println("Elapsed "+elapsed);
+    }
+}
diff --git a/tomee/tomee-bootstrap/src/test/java/org/apache/tomee/bootstrap/Misc.java b/tomee/tomee-bootstrap/src/test/java/org/apache/tomee/bootstrap/Misc.java
new file mode 100644
index 0000000..09370be
--- /dev/null
+++ b/tomee/tomee-bootstrap/src/test/java/org/apache/tomee/bootstrap/Misc.java
@@ -0,0 +1,232 @@
+/*
+ * 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.tomee.bootstrap;
+
+import lombok.Getter;
+import lombok.ToString;
+import org.apache.openejb.loader.Files;
+import org.apache.openejb.loader.Zips;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class Misc {
+
+    public static void main(String[] args) throws IOException {
+
+        final Repository repository = Repository.build();
+
+        final File file = new File("/Users/dblevins/work/apache/downloads/apache-tomee-8.0.0-webprofile.zip");
+        final File tmpdir = Files.tmpdir();
+        Zips.unzip(file, tmpdir);
+
+        final List<File> jars = Files.collect(tmpdir, ".*.jar");
+
+        final List<Artifact> collect = jars.stream()
+                .filter(jar -> !jar.getName().equals("bootstrap.jar"))
+                .filter(jar -> !jar.getName().equals("catalina-ant.jar"))
+                .filter(jar -> !jar.getName().startsWith("tomcat-i18n"))
+                .map(repository::from)
+                .sorted()
+                .peek(artifact -> System.out.print(artifact.asDep()))
+                .collect(Collectors.toList());
+
+
+        collect.stream().forEach(System.out::println);
+
+
+        System.out.println(tmpdir.getAbsolutePath());
+    }
+
+    public static class Repository {
+        private final Map<String, File> artifacts = new HashMap<>();
+        private final File path = new File("/Users/dblevins/.m2/repository/");
+
+        public static Repository build() {
+            final Repository repository = new Repository();
+
+            final List<File> jars = Files.collect(repository.path, ".*\\.jar");
+            for (final File jar : jars) {
+                repository.artifacts.put(jar.getName(), jar);
+            }
+
+            return repository;
+        }
+
+        public Artifact from(final File jar) {
+            if (jar.getName().equals("commons-daemon.jar")) {
+                return new Artifact("commons-daemon", "commons-daemon", "1.1.0");
+            }
+
+            if (jar.getName().equals("tomcat-juli.jar")) {
+                return new Artifact("org.apache.tomee", "tomee-juli", "${project.version}");
+            }
+
+            if (jar.getName().equals("catalina-ha.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-catalina-ha", "9.0.22");
+            }
+
+            if (jar.getName().equals("catalina-storeconfig.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-storeconfig", "9.0.22");
+            }
+
+            if (jar.getName().equals("catalina-tribes.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-tribes", "9.0.22");
+            }
+
+            if (jar.getName().equals("catalina.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-catalina", "9.0.22");
+            }
+
+            if (jar.getName().equals("el-api.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-el-api", "9.0.22");
+            }
+
+            if (jar.getName().equals("jasper-el.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-jasper-el", "9.0.22");
+            }
+
+            if (jar.getName().equals("jasper.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-jasper", "9.0.22");
+            }
+
+            if (jar.getName().equals("jaspic-api.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-jaspic-api", "9.0.22");
+            }
+
+            if (jar.getName().equals("servlet-api.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-servlet-api", "9.0.22");
+            }
+            if (jar.getName().equals("websocket-api.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-websocket-api", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-coyote.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-coyote", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-dbcp.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-dbcp", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-api.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-api", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-websocket.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-websocket", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-util.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-util", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-util-scan.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-util-scan", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-jni.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-jni", "9.0.22");
+            }
+            if (jar.getName().equals("tomcat-jdbc.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-jdbc", "9.0.22");
+            }
+            if (jar.getName().equals("jsp-api.jar")) {
+                return new Artifact("org.apache.tomcat", "tomcat-jsp-api", "9.0.22");
+            }
+
+            if (jar.getName().equals("ecj-4.12.jar")) {
+                return new Artifact("org.eclipse.jdt", "ecj", "3.18.0");
+            }
+
+            if (jar.getName().startsWith("openejb-")) {
+                final String artifact = jar.getName().replaceAll("-8.0.0.*", "");
+                return new Artifact("org.apache.tomee", artifact, "${project.version}");
+            }
+            if (jar.getName().startsWith("tomee-")) {
+                final String artifact = jar.getName().replaceAll("-8.0.0.*", "");
+                return new Artifact("org.apache.tomee", artifact, "${project.version}");
+            }
+
+
+            // /Users/dblevins/.m2/repository//org/apache/xbean/xbean-naming/4.14/xbean-naming-4.14.jar
+            final File file = getFile(jar);
+            final File versionDir = file.getParentFile();
+            final File artifactDir = versionDir.getParentFile();
+
+            final String groupId = artifactDir.getParentFile()
+                    .getAbsolutePath()
+                    .substring(path.getAbsolutePath().length() + 1)
+                    .replace("/", ".");
+
+            return Artifact.builder()
+                    .artifactId(artifactDir.getName())
+                    .version(versionDir.getName())
+                    .groupId(groupId)
+                    .build();
+        }
+
+        private File getFile(final File jar) {
+            {
+                final File file = artifacts.get(jar.getName());
+                if (file != null) return file;
+            }
+            {
+                final String name = jar.getName();
+                final String relativeName = name.substring(name.length() - 4);
+
+                for (final Map.Entry<String, File> entry : artifacts.entrySet()) {
+                    if (entry.getKey().startsWith(relativeName)) return entry.getValue();
+                }
+            }
+
+            throw new IllegalStateException(jar.getName());
+        }
+    }
+
+    @lombok.Builder
+    @Getter
+    @ToString
+    public static class Artifact implements Comparable<Artifact> {
+        private final String groupId;
+        private final String artifactId;
+        private final String version;
+
+        @Override
+        public int compareTo(final Artifact o) {
+            final String a = this.getGroupId() + ":" + this.artifactId;
+            final String b = o.getGroupId() + ":" + o.artifactId;
+            return a.compareTo(b);
+        }
+
+        public String asDep() {
+            final String g = groupId;
+            final String a = artifactId;
+            final String v = version;
+            return "" +
+                    "    <dependency>\n" +
+                    "      <groupId>" + g + "</groupId>\n" +
+                    "      <artifactId>" + a + "</artifactId>\n" +
+                    "      <version>" + v + "</version>\n" +
+                    "      <exclusions>\n" +
+                    "        <exclusion>\n" +
+                    "          <artifactId>*</artifactId>\n" +
+                    "          <groupId>*</groupId>\n" +
+                    "        </exclusion>\n" +
+                    "      </exclusions>\n" +
+                    "    </dependency>\n";
+        }
+    }
+
+}