Skip to content

Commit

Permalink
add experimental version for debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Nov 9, 2023
1 parent 2074d6a commit e3eb6d5
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 14 deletions.
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.0.1.21-SNAPSHOT"/>
<property name="version" value="6.0.1.22-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
48 changes: 46 additions & 2 deletions loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.0.1.21-SNAPSHOT</version>
<version>6.0.1.22-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down Expand Up @@ -629,7 +629,51 @@
<artifactId>janino-commons-compiler</artifactId>
<version>3.1.9</version>
</dependency>


<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>10.1.15</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>10.1.15</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.1.15</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<version>10.1.15</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>10.1.15</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>10.1.15</version>
</dependency>




<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>



</dependencies>

<repositories>
Expand Down
166 changes: 155 additions & 11 deletions loader/src/main/java/lucee/loader/engine/CFMLEngineFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -46,6 +48,7 @@
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -118,6 +121,8 @@ public class CFMLEngineFactory extends CFMLEngineFactorySupport {
// do not remove/ranme, grapped by core directly
protected ServletConfig config;

private boolean embedded;

protected CFMLEngineFactory(final ServletConfig config) {
System.setProperty("org.apache.commons.logging.LogFactory.HashtableImpl", ConcurrentHashMapAsHashtable.class.getName());
File logFile = null;
Expand Down Expand Up @@ -219,6 +224,13 @@ public static CFMLEngine getInstance(final ServletConfig config, final EngineCha
void readInitParam(final ServletConfig config) {
if (luceeServerRoot != null) return;

String strEmbedded = config.getInitParameter("embedded");
embedded = false;
if (!Util.isEmpty(strEmbedded, true)) {
strEmbedded.trim().toLowerCase();
embedded = strEmbedded.equals("true") || strEmbedded.equals("yes");
}

String initParam = config.getInitParameter("lucee-server-directory");
if (Util.isEmpty(initParam)) initParam = config.getInitParameter("lucee-server-root");
if (Util.isEmpty(initParam)) initParam = config.getInitParameter("lucee-server-dir");
Expand Down Expand Up @@ -281,7 +293,126 @@ public void shutdownFelix() throws BundleException {
BundleUtil.stop(felix, false);
}

public static void main(String[] args) throws IOException {
createBundleFromSource();

}

public static File createBundleFromSource() throws IOException {

// Users/mic/Projects/Lucee/Lucee6/core/target/classes/META-INF/MANIFEST.MF

File classesDirectory = new File("/Users/mic/Projects/Lucee/Lucee6/core/target/classes/");
File sourceDirectory = new File("/Users/mic/Projects/Lucee/Lucee6/core/src/main/cfml/");
File pomFile = new File("/Users/mic/Projects/Lucee/Lucee6/loader/pom.xml");
File manifestFile = new File(classesDirectory, "META-INF/MANIFEST.MF");

Manifest manifest = new Manifest();
// Assuming the manifest file is correct and fully prepared for OSGi
try (InputStream is = new FileInputStream(manifestFile)) {
manifest.read(is);
}
Attributes main = manifest.getMainAttributes();
String bundleName = main.getValue("Bundle-SymbolicName");
String bundleVersion = readVersionFromPOM(pomFile);
System.out.println(bundleVersion);

main.put(new Attributes.Name("Bundle-Version"), bundleVersion);

File bundleFile = File.createTempFile(bundleName + "-", ".lco");
bundleFile = new File("/Users/mic/tmp8/" + bundleName + "-" + bundleVersion + "-" + System.currentTimeMillis() + ".lco");

System.out.println("- temp core file: " + bundleFile);

// Output file for the JAR
JarOutputStream jos = null;
// FileInputStream fis = null;

try {
jos = new JarOutputStream(new FileOutputStream(bundleFile), manifest);
addDirectoryToJar(jos, classesDirectory, "", new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
return !name.equals(".DS_Store");
// return !name.equals(".DS_Store") && !name.endsWith(".java");
}
});
System.out.println("----------------------------");

addDirectoryToJar(jos, sourceDirectory, "resource/", new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
return !name.endsWith(".DS_Store") && !name.endsWith(".java");
}
});

}
finally {
// Util.closeEL(fis);
Util.closeEL(jos);
}
return bundleFile;
}

private static String readVersionFromPOM(File pomFile) throws IOException {
String versionTagStart = "<version>";
String versionTagEnd = "</version>";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Util.copy(new FileInputStream(pomFile), baos, true, true);
String xml = new String(baos.toByteArray());
int startIndex = xml.indexOf(versionTagStart) + versionTagStart.length();
int endIndex = xml.indexOf(versionTagEnd);

if (startIndex < versionTagStart.length() || endIndex == -1) {
throw new IOException("Version tag not found");
}

return xml.substring(startIndex, endIndex).trim();
}

private static void addDirectoryToJar(JarOutputStream jos, File folder, String parentEntryName, FilenameFilter filter) throws IOException {

// Recursively add files to the jar
String name;
for (File file: folder.listFiles()) {
if (file.isDirectory()) {
name = (parentEntryName + file.getName() + "/").replace("//", "/");
// System.out.println("dir:" + name);
addDirectoryToJar(jos, file, name, filter); // Recursive call
}
else {
name = (parentEntryName + file.getName()).replace("//", "/");
if ((filter != null && !filter.accept(folder, name)) || name.equals(JarFile.MANIFEST_NAME)) {
continue; // Skip the manifest file since it's already added
}
// System.out.println(s);
JarEntry entry = new JarEntry(parentEntryName + file.getName());
entry.setTime(file.lastModified());
try {
jos.putNextEntry(entry);
// Write file to JAR
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
byte[] buffer = new byte[1024];
int count;
while ((count = bis.read(buffer)) != -1) {
jos.write(buffer, 0, count);
}
}
jos.closeEntry();
System.out.println(name);

}
catch (Exception e) {

}
}
}
}

private void initEngine() throws ServletException {

final Version coreVersion = VersionInfo.getIntVersion();
final long coreCreated = VersionInfo.getCreateTime();

Expand All @@ -294,23 +425,34 @@ private void initEngine() throws ServletException {
catch (final IOException e) {
throw new ServletException(e);
}

final File[] patches = PATCH_ENABLED ? patcheDir.listFiles(new ExtensionFilter(new String[] { ".lco" })) : null;
File lucee = null;
if (patches != null) {
for (final File patch: patches) {
if (patch.getName().startsWith("tmp.lco")) patch.delete();
else if (patch.lastModified() < coreCreated) patch.delete();
else if (patch.length() < 1000000L) patch.delete();
else if (lucee == null || Util.isNewerThan(toVersion(patch.getName(), VERSION_ZERO), toVersion(lucee.getName(), VERSION_ZERO))) lucee = patch;
// create lucee core from source
if (embedded) {
try {
lucee = createBundleFromSource();
}
catch (IOException e) {
throw new ServletException(e);
}
}
if (lucee != null && Util.isNewerThan(coreVersion, toVersion(lucee.getName(), VERSION_ZERO))) lucee = null;

// read lucee core from patch directory
if (lucee == null) {
final File[] patches = PATCH_ENABLED ? patcheDir.listFiles(new ExtensionFilter(new String[] { ".lco" })) : null;
if (patches != null) {
for (final File patch: patches) {
if (patch.getName().startsWith("tmp.lco")) patch.delete();
else if (patch.lastModified() < coreCreated) patch.delete();
else if (patch.length() < 1000000L) patch.delete();
else if (lucee == null || Util.isNewerThan(toVersion(patch.getName(), VERSION_ZERO), toVersion(lucee.getName(), VERSION_ZERO))) lucee = patch;
}
}
if (lucee != null && Util.isNewerThan(coreVersion, toVersion(lucee.getName(), VERSION_ZERO))) lucee = null;
}
// Load Lucee
// URL url=null;
try {
// Load core version when no patch available
// Load core version from jar when no patch available
if (lucee == null) {
log(Logger.LOG_DEBUG, "Load built-in Core");

Expand Down Expand Up @@ -384,14 +526,14 @@ private void initEngine() throws ServletException {
engine = _getCore(lucee);
}
else {

// TODO: LDEV-2805 set engine's classloader to use local class files
// engine =
}

setEngine(engine);
}
else {

bundleCollection = BundleLoader.loadBundles(this, getFelixCacheDirectory(), getBundleDirectory(), lucee, bundleCollection);
// bundle=loadBundle(lucee);
log(Logger.LOG_DEBUG, "Loaded bundle: [" + bundleCollection.core.getSymbolicName() + "]");
Expand All @@ -403,11 +545,13 @@ private void initEngine() throws ServletException {
log(Logger.LOG_DEBUG, "Loaded Lucee Version [" + singelton.getInfo().getVersion() + "]");
}
catch (final InvocationTargetException e) {
e.printStackTrace();
log(e.getTargetException());
// e.getTargetException().printStackTrace();
throw new ServletException(e.getTargetException());
}
catch (final Exception e) {
e.printStackTrace();
throw new ServletException(e);
}

Expand Down

0 comments on commit e3eb6d5

Please sign in to comment.