Skip to content

Commit

Permalink
improve Felix handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 14, 2024
1 parent b53b0b0 commit fdeef7b
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 53 deletions.
9 changes: 5 additions & 4 deletions core/src/main/java/default.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# OSGi settings
felix.log.level= "error"
# "onFirstInit|none"
org.osgi.framework.storage.clean="onFirstInit"
org.osgi.framework.bundle.parent= "framework"
felix.log.level=error
felix.cache.locking=false
org.osgi.framework.storage.clean=none
org.osgi.framework.bundle.parent=framework
felix.cache.bufsize=65536
org.osgi.framework.bootdelegation= \
coldfusion.xml.rpc,\
com.apple.laf,com.apple.laf.*,\
Expand Down
71 changes: 44 additions & 27 deletions core/src/main/java/lucee/runtime/engine/CFMLEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@
import lucee.runtime.video.VideoUtilImpl;
import lucee.servlet.http.HTTPServletImpl;
import lucee.transformer.dynamic.DynamicInvoker;
import lucee.transformer.library.function.FunctionLibException;
import lucee.transformer.library.function.FunctionLibFactory;
import lucee.transformer.library.tag.TagLibException;
import lucee.transformer.library.tag.TagLibFactory;

/**
* The CFMl Engine
Expand Down Expand Up @@ -220,36 +224,49 @@ public static CFMLEngineFactory FACTORY() {
}

private CFMLEngineImpl(CFMLEngineFactory factory, BundleCollection bc) {
String dumpPath = Caster.toString(SystemUtil.getSystemPropOrEnvVar("lucee.dump.threads", null), null);
if (!StringUtil.isEmpty(dumpPath, true)) {
int interval = Caster.toIntValue(SystemUtil.getSystemPropOrEnvVar("lucee.dump.threads.interval", null), 100);
long start = System.currentTimeMillis();
long max = Caster.toIntValue(SystemUtil.getSystemPropOrEnvVar("lucee.dump.threads.max", null), 10000);

// Create a new thread to run the task
Thread thread = new Thread(() -> {
while (true) {
try {
if ((start + max) < System.currentTimeMillis()) {
break;
}
// Call the dumpThreadPositions method
Resource target = ResourcesImpl.getFileResourceProvider().getResource(dumpPath);
Controler.dumpThreadPositions(target);
// Kick some stuff to get it started in parallel because it takes forever to load
ThreadUtil.getThread(() -> {
try {
Class.forName("lucee.runtime.type.util.KeyConstants");
}
catch (ClassNotFoundException e) {
}
}, true).start();

// Pause for the specified interval
SystemUtil.sleep(interval);
}
catch (IOException e) {
e.printStackTrace();
SystemUtil.sleep(1000);
}
}
});
ThreadUtil.getThread(() -> {
try {
FunctionLibFactory.loadFromSystem(null);
}
catch (FunctionLibException e) {
}
}, true).start();

// Start the thread
thread.start();
}
ThreadUtil.getThread(() -> {
try {
TagLibFactory.loadFromSystem(null);
}
catch (TagLibException e) {
}
}, true).start();

/*
* String dumpPath = Caster.toString(SystemUtil.getSystemPropOrEnvVar("lucee.dump.threads", null),
* null); if (!StringUtil.isEmpty(dumpPath, true)) { int interval =
* Caster.toIntValue(SystemUtil.getSystemPropOrEnvVar("lucee.dump.threads.interval", null), 100);
* long start = System.currentTimeMillis(); long max =
* Caster.toIntValue(SystemUtil.getSystemPropOrEnvVar("lucee.dump.threads.max", null), 10000);
*
* // Create a new thread to run the task Thread thread = new Thread(() -> { while (true) { try { if
* ((start + max) < System.currentTimeMillis()) { break; } // Call the dumpThreadPositions method
* Resource target = ResourcesImpl.getFileResourceProvider().getResource(dumpPath);
* Controler.dumpThreadPositions(target);
*
* // Pause for the specified interval SystemUtil.sleep(interval); } catch (IOException e) {
* e.printStackTrace(); SystemUtil.sleep(1000); } } });
*
* // Start the thread thread.start(); }
*/

FACTORY = this.factory = factory;
this.bundleCollection = bc;
Expand Down
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="7.0.0.81-SNAPSHOT"/>
<property name="version" value="7.0.0.82-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion 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>7.0.0.81-SNAPSHOT</version>
<version>7.0.0.82-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down
22 changes: 7 additions & 15 deletions loader/src/main/java/lucee/loader/engine/CFMLEngineFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import lucee.loader.util.ExtensionFilter;
import lucee.loader.util.Util;
import lucee.loader.util.ZipUtil;
import lucee.loader.util.log.Logging;
import lucee.runtime.config.ConfigServer;
import lucee.runtime.config.Identification;
import lucee.runtime.config.Password;
Expand Down Expand Up @@ -133,6 +134,7 @@ public class CFMLEngineFactory extends CFMLEngineFactorySupport {
private boolean embedded;

protected CFMLEngineFactory(final ServletConfig config) {
Logging.startupLog();
System.setProperty("org.apache.commons.logging.LogFactory.HashtableImpl", ConcurrentHashMapAsHashtable.class.getName());
File logFile = null;
this.config = config;
Expand Down Expand Up @@ -289,16 +291,7 @@ public void shutdownFelix() throws BundleException {
BundleCollection bc = singelton.getBundleCollection();
if (bc == null || bc.felix == null) return;

// stop
BundleLoader.removeBundles(bc);

// we give it some time
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
}

// BundleLoader.removeBundles(bc, false);
BundleUtil.stop(felix, false);
}

Expand Down Expand Up @@ -751,12 +744,12 @@ public Felix getFelix(final File cacheRootDir, Map<String, Object> config) throw
// Enables or disables bundle cache locking, which is used to prevent concurrent access to the
// bundle cache.

extend(config, "felix.cache.locking", null, false);
extend(config, "felix.cache.locking", "false", false);
extend(config, "org.osgi.framework.executionenvironment", null, false);
extend(config, "org.osgi.framework.storage", null, false);
extend(config, "org.osgi.framework.storage.clean", Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT, false);
extend(config, "org.osgi.framework.storage.clean", "none", false);
extend(config, Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK, false);

// felix.cache.bufsize
boolean isNew = false;
// felix.cache.rootdir
if (Util.isEmpty((String) config.get("felix.cache.rootdir"))) {
Expand All @@ -771,7 +764,7 @@ public Felix getFelix(final File cacheRootDir, Map<String, Object> config) throw
extend(config, Constants.FRAMEWORK_SYSTEMPACKAGES, null, true);
extend(config, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, null, true);
extend(config, "felix.cache.filelimit", null, false);
extend(config, "felix.cache.bufsize", null, false);
extend(config, "felix.cache.bufsize", "65536", false); // 64kb default is 8kb
extend(config, "felix.bootdelegation.implicit", null, false);
extend(config, "felix.systembundle.activators", null, false);
extend(config, "org.osgi.framework.startlevel.beginning", null, false);
Expand Down Expand Up @@ -803,7 +796,6 @@ public Felix getFelix(final File cacheRootDir, Map<String, Object> config) throw
e = it.next();
sb.append("\n- ").append(e.getKey()).append(':').append(e.getValue());
}
// log(Logger.LOG_INFO, sb.toString());

felix = new Felix(config);
try {
Expand Down
10 changes: 6 additions & 4 deletions loader/src/main/java/lucee/loader/osgi/BundleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public static void removeBundles(final BundleContext bc) throws BundleException
removeBundle(bundle);
}

public static void removeBundles(final BundleCollection bc) throws BundleException {
public static void removeBundles(final BundleCollection bc, boolean uninstall) throws BundleException {
BundleContext bcc = bc.getBundleContext();
final Bundle[] bundles = bcc == null ? new Bundle[0] : bcc.getBundles();

Expand All @@ -350,9 +350,11 @@ public static void removeBundles(final BundleCollection bc) throws BundleExcepti
}
}
// uninstall
for (final Bundle bundle: bundles) {
if (!BundleUtil.isSystemBundle(bundle)) {
uninstallBundle(bundle);
if (uninstall) {
for (final Bundle bundle: bundles) {
if (!BundleUtil.isSystemBundle(bundle)) {
uninstallBundle(bundle);
}
}
}
}
Expand Down
73 changes: 72 additions & 1 deletion loader/src/main/java/lucee/loader/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand All @@ -50,7 +54,10 @@ public class Util {

private static File tempFile;
// private static File homeFile;

public static final Charset UTF8;
static {
UTF8 = Charset.forName("UTF-8");
}
private static final int QUALIFIER_APPENDIX_SNAPSHOT = 1;
private static final int QUALIFIER_APPENDIX_BETA = 2;
private static final int QUALIFIER_APPENDIX_RC = 3;
Expand Down Expand Up @@ -380,6 +387,41 @@ public static void copy(final Resource in, final Resource out) throws IOExceptio
copy(is, os);
}

@Deprecated
public static void write(File file, String string, Charset charset, boolean append) throws IOException {
if (charset == null) {
charset = UTF8;
}

Writer writer = null;
try {
writer = getWriter(file, charset, append);
writer.write(string);
}
finally {
closeEL(writer);
}
}

@Deprecated
public static Writer getWriter(File file, Charset charset, boolean append) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(file, append);
}
catch (IOException ioe) {
closeEL(os);
throw ioe;
}
return getWriter(os, charset);
}

@Deprecated
public static Writer getWriter(OutputStream os, Charset charset) throws IOException {
if (charset == null) charset = UTF8;
return new BufferedWriter(new OutputStreamWriter(os, charset));
}

/**
* @deprecated use instead CFMLEngineFactory.getInstance().getStringUtil(). toVariableName (...)
* @param str input string
Expand Down Expand Up @@ -580,4 +622,33 @@ private static Throwable unwrap(Throwable t) {
if (cause != null && cause != t) return unwrap(cause);
return t;
}

public static String getSystemPropOrEnvVar(String name, String defaultValue) {
// env
String value = System.getenv(name);
if (!isEmpty(value)) return value;

// prop
value = System.getProperty(name);
if (!isEmpty(value)) return value;

// env 2
name = convertSystemPropToEnvVar(name);
value = System.getenv(name);
if (!isEmpty(value)) return value;

return defaultValue;
}

private static String convertSystemPropToEnvVar(String name) {
return name.replace('.', '_').toUpperCase();
}

public static void sleep(int time) {
try {
Thread.sleep(time);
}
catch (InterruptedException e) {
}
}
}

0 comments on commit fdeef7b

Please sign in to comment.