Skip to content

Commit 6b7100d

Browse files
committed
review IDManager classes + few improvements
1 parent b6e220d commit 6b7100d

File tree

21 files changed

+224
-159
lines changed

21 files changed

+224
-159
lines changed

core.javaee/src/main/java/net/lecousin/core/javaee/BootstrapServlet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public void init() throws ServletException {
9090
properties,
9191
debugMode,
9292
threadFactory,
93-
new DefaultLibrariesManager()
93+
new DefaultLibrariesManager(),
94+
null
9495
);
9596
}
9697

net.lecousin.core/src/main/java/net/lecousin/framework/application/Application.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import net.lecousin.framework.locale.LocalizedProperties;
3535
import net.lecousin.framework.log.Logger;
3636
import net.lecousin.framework.log.LoggerFactory;
37+
import net.lecousin.framework.log.appenders.Appender;
3738
import net.lecousin.framework.util.AsyncCloseable;
3839
import net.lecousin.framework.util.ObjectUtil;
3940
import net.lecousin.framework.util.Pair;
@@ -274,7 +275,8 @@ public static ISynchronizationPoint<Exception> start(
274275
Map<String,String> properties,
275276
boolean debugMode,
276277
ThreadFactory threadFactory,
277-
LibrariesManager librariesManager
278+
LibrariesManager librariesManager,
279+
Appender defaultLogAppender
278280
) {
279281
Application app = new Application(artifact, commandLineArguments, properties, debugMode, threadFactory, librariesManager);
280282

@@ -307,8 +309,10 @@ public static ISynchronizationPoint<Exception> start(
307309
c.out("-----------------------------------------");
308310
}
309311

312+
LCCore.initEnvironment();
313+
310314
// init logging
311-
app.loggerFactory = new LoggerFactory(app);
315+
app.loggerFactory = new LoggerFactory(app, defaultLogAppender);
312316

313317
// init LCCore with this application
314318
LCCore.start(app);
@@ -355,7 +359,7 @@ public static ISynchronizationPoint<Exception> start(Artifact artifact, boolean
355359

356360
/** Method to call at the beginning of the application, typically in the main method. */
357361
public static ISynchronizationPoint<Exception> start(Artifact artifact, String[] args, boolean debugMode) {
358-
return start(artifact, args, null, debugMode, Executors.defaultThreadFactory(), new DefaultLibrariesManager());
362+
return start(artifact, args, null, debugMode, Executors.defaultThreadFactory(), new DefaultLibrariesManager(), null);
359363
}
360364

361365
/** Stop this application and release resources. */

net.lecousin.core/src/main/java/net/lecousin/framework/application/LCCore.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,7 @@ public static void set(Environment env) throws IllegalStateException {
7777
instance = env;
7878
}
7979

80-
/** Return true if the environment is already started. */
81-
public static boolean isStarted() {
82-
return started;
83-
}
84-
85-
/** Initialization. */
86-
public static synchronized void start() {
87-
if (started) throw new IllegalStateException("LCCore environment already started");
88-
started = true;
89-
80+
public static void initEnvironment() {
9081
// init logging system if not specified
9182
if (System.getProperty("org.apache.commons.logging.Log") == null)
9283
System.setProperty("org.apache.commons.logging.Log", "net.lecousin.framework.log.bridges.ApacheCommonsLogging");
@@ -99,6 +90,19 @@ public static synchronized void start() {
9990
protocols += "net.lecousin.framework.protocols";
10091
System.setProperty("java.protocol.handler.pkgs", protocols);
10192
}
93+
}
94+
95+
/** Return true if the environment is already started. */
96+
public static boolean isStarted() {
97+
return started;
98+
}
99+
100+
/** Initialization. */
101+
public static synchronized void start() {
102+
if (started) throw new IllegalStateException("LCCore environment already started");
103+
started = true;
104+
105+
initEnvironment();
102106

103107
instance.start();
104108
new Task.Cpu<Void,Exception>("Initializing framework tools", Task.PRIORITY_NORMAL) {

net.lecousin.core/src/main/java/net/lecousin/framework/application/launcher/AppLauncher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ else if (args[i].startsWith("-maven-repository=")) {
163163
cfg.properties,
164164
false,
165165
Executors.defaultThreadFactory(),
166-
librariesManager
166+
librariesManager,
167+
null
167168
);
168169
start.block(0);
169170
if (start.hasError()) {

net.lecousin.core/src/main/java/net/lecousin/framework/application/launcher/DevLauncher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ else if (args[i].startsWith("-maven-repository=")) {
190190
cfg.properties,
191191
true,
192192
Executors.defaultThreadFactory(),
193-
librariesManager
193+
librariesManager,
194+
null
194195
);
195196
start.block(0);
196197
if (start.hasError()) {

net.lecousin.core/src/main/java/net/lecousin/framework/application/libraries/classpath/LoadLibraryPluginsFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected void processLine(UnprotectedStringBuffer line) throws IOException {
4848
throw new Exception("Invalid plugin class: " + line.asString() + " must extend Plugin");
4949
Plugin pi = (Plugin)cl.newInstance();
5050
ExtensionPoints.add(epName, pi);
51-
} catch (Exception e) {
51+
} catch (Throwable e) {
5252
throw new IOException("Error loading plugins of library: " + getSourceDescription(), e);
5353
}
5454
}

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/StandaloneTaskPriorityManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final synchronized boolean remove(Task<?, ?> task) {
6666
long wait = lastIdle + 1000 - System.currentTimeMillis();
6767
if (wait > 50) {
6868
if (!taskManager.isStopping() && taskManager.getTransferTarget() == null) {
69-
try { this.wait(); }
69+
try { this.wait(wait); }
7070
catch (InterruptedException e) { /* ignore */ }
7171
return null;
7272
}

net.lecousin.core/src/main/java/net/lecousin/framework/io/text/Decoder.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.lecousin.framework.concurrent.CancelException;
1414
import net.lecousin.framework.concurrent.synch.AsyncWork;
1515
import net.lecousin.framework.concurrent.synch.ISynchronizationPoint;
16+
import net.lecousin.framework.concurrent.synch.SynchronizationPoint;
1617
import net.lecousin.framework.io.IO;
1718
import net.lecousin.framework.mutable.MutableBoolean;
1819
import net.lecousin.framework.util.AsyncCloseable;
@@ -70,14 +71,20 @@ public void transferTo(Decoder newDecoder) {
7071
protected AsyncWork<ByteBuffer, IOException> nextBuffer;
7172
protected ByteBuffer currentBuffer = null;
7273

74+
/** When the decode method returns -2, this method can be used to know when new data is available to be decoded. */
75+
public ISynchronizationPoint<IOException> canDecode() {
76+
if (currentBuffer != null) return new SynchronizationPoint<>(true);
77+
return nextBuffer;
78+
}
79+
7380
/**
7481
* Decode characters from the IO.
7582
* @param chars array to fill
7683
* @param pos offset in the array to fill
7784
* @param len maximum number of characters to fill
7885
* @param interrupt when true, the decoding will stop as soon as possible
7986
* @param min minimum number of characters to fill, regardless of the interrupt value
80-
* @return number of decoded characters, -1 if no more characters can be decoded
87+
* @return number of decoded characters, -1 if no more characters can be decoded, -2 if we need to wait for next buffer from the IO
8188
* @throws IOException error reading from IO
8289
* @throws CancelException IO has been cancelled
8390
*/
@@ -86,7 +93,10 @@ public int decode(char[] chars, int pos, int len, MutableBoolean interrupt, int
8693
int nb = 0;
8794
do {
8895
if (currentBuffer == null) {
89-
currentBuffer = nextBuffer.blockResult(0);
96+
if (nextBuffer.isUnblocked())
97+
currentBuffer = nextBuffer.blockResult(0);
98+
else
99+
return nb > 0 ? nb : -2;
90100
if (currentBuffer != null)
91101
nextBuffer = io.readNextBufferAsync();
92102
}

net.lecousin.core/src/main/java/net/lecousin/framework/io/text/ProgressiveBufferedReadableCharStream.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,22 @@ public Void run() throws CancelException {
516516
}
517517
return null;
518518
}
519+
if (nb == -2) {
520+
synchronized (buffers) {
521+
if (buffer.length > 0) {
522+
lastBufferReady = myBuffer;
523+
if (firstBufferReady == -1) firstBufferReady = myBuffer;
524+
myBuffer = -1;
525+
buffer = null;
526+
interruptFillBuffer.set(false);
527+
if (iNeedABuffer != null)
528+
iNeedABuffer.unblock();
529+
}
530+
}
531+
taskFillBuffer = new TaskFillBuffer();
532+
decoder.canDecode().listenAsync(taskFillBuffer, true);
533+
return null;
534+
}
519535
if (nb == -1) {
520536
synchronized (buffers) {
521537
if (buffer.length > 0) {

net.lecousin.core/src/main/java/net/lecousin/framework/log/LoggerFactory.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public class LoggerFactory {
3333
private Map<String,Appender> appenders = new HashMap<String,Appender>();
3434

3535
/** Constructor. */
36-
public LoggerFactory(Application app) {
36+
public LoggerFactory(Application app, Appender defaultAppender) {
3737
application = app;
3838
thread = new LoggerThread(app);
39-
defaultAppender = new ConsoleAppender(this, app.isReleaseMode() ? Level.INFO : Level.DEBUG,
40-
new LogPattern("%d{HH:mm:ss.SSS} [%level] <%logger{20}> %m"));
39+
if (defaultAppender == null)
40+
defaultAppender = new ConsoleAppender(app.getConsole(), app.isReleaseMode() ? Level.INFO : Level.DEBUG,
41+
new LogPattern("%d{HH:mm:ss.SSS} [%level] <%logger{20}> %m"));
42+
this.defaultAppender = defaultAppender;
4143
defaultLogger = new Logger(this, "default", defaultAppender, null);
4244
loggers.put("default", defaultLogger);
4345

0 commit comments

Comments
 (0)