Skip to content

Commit 422bd51

Browse files
author
jossonsmith
committed
Add distributed *.js loading support: http://a[eiouy]rchive.java2script.org
Fix bugs of ClazzLoader.
1 parent 862f026 commit 422bd51

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

sources/net.sf.j2s.java.core/src/java/lang/Class.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,12 @@ Clazz.decorateFunction = function (clazzFun, prefix, name) {
16251625
clazzFun[inF[i]] = Clazz.innerFunctions[inF[i]];
16261626
}
16271627

1628+
if (window["ClazzLoader"] != null) {
1629+
var node = ClazzLoader.findClass (qName);
1630+
if (node != null && node.status == ClazzNode.STATUS_KNOWN) {
1631+
ClazzLoader.updateNode (node);
1632+
}
1633+
}
16281634
};
16291635

16301636
/* proected */

sources/net.sf.j2s.java.core/src/java/lang/ClassLoader.js

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,35 @@ ClazzLoader.registerPackages = function (prefix, pkgs) {
398398
}
399399
};
400400

401+
/**
402+
* Using multiple sites to load *.js in multiple threads. Using multiple
403+
* sites may avoid 2 HTTP 1.1 connections recommendation limit.
404+
* Here is a default implementation for http://archive.java2script.org.
405+
* In site archive.java2script.org, there are 6 sites:
406+
* 1. http://archive.java2script.org
407+
* 2. http://erchive.java2script.org
408+
* 3. http://irchive.java2script.org
409+
* 4. http://orchive.java2script.org
410+
* 5. http://urchive.java2script.org
411+
* 6. http://yrchive.java2script.org
412+
*/
413+
/* protected */
414+
ClazzLoader.multipleSites = function (path) {
415+
var length = path.length;
416+
if (ClazzLoader.maxLoadingThreads > 1 && length > 15
417+
&& path.substring (0, 15) == "http://archive.") {
418+
var index = path.lastIndexOf ("/");
419+
if (index < length - 3) {
420+
var arr = ['a', 'e', 'i', 'o', 'u', 'y'];
421+
var c1 = path.charCodeAt (index + 1);
422+
var c2 = path.charCodeAt (index + 2);
423+
var idx = (length - index) * 3 + c1 * 5 + c2 * 7;
424+
return path.substring (0, 7) + arr[idx % 6] + path.substring (8);
425+
}
426+
}
427+
return path;
428+
};
429+
401430
/**
402431
* Return the *.js path of the given class. Maybe the class is contained
403432
* in a *.z.js jar file.
@@ -415,7 +444,7 @@ ClazzLoader.getClasspathFor = function (clazz, forRoot, ext) {
415444
var base = null;
416445
if (path != null) {
417446
if (!forRoot && ext == null) { // return directly
418-
return path;
447+
return ClazzLoader.multipleSites (path);
419448
} else {
420449
var idx = path.lastIndexOf (clazz.replace (/\./g, "/"));
421450
if (idx != -1) {
@@ -461,18 +490,19 @@ ClazzLoader.getClasspathFor = function (clazz, forRoot, ext) {
461490

462491
base = ClazzLoader.assureBase (base);
463492
if (forRoot) {
464-
return base;
493+
return ClazzLoader.multipleSites (base);
465494
}
466495
if (clazz.lastIndexOf (".*") == clazz.length - 2) {
467-
return base + clazz.substring (0, idx + 1).replace (/\./g, "/");
496+
return ClazzLoader.multipleSites (base + clazz.substring (0, idx + 1)
497+
.replace (/\./g, "/"));
468498
}
469499
if (ext == null) {
470500
ext = ".js";
471501
} else if (ext.charAt (0) != '.') {
472502
ext = "." + ext;
473503
}
474504
var jsPath = base + clazz.replace (/\./g, "/") + ext;
475-
return jsPath;
505+
return ClazzLoader.multipleSites (jsPath);
476506
};
477507

478508
/* private */
@@ -615,6 +645,9 @@ ClazzLoader.failedScripts = new Object ();
615645
/*-# failedHandles -> fhs #-*/
616646
ClazzLoader.failedHandles = new Object ();
617647

648+
/* protected */
649+
ClazzLoader.takeAnotherTry = false;
650+
618651
/**
619652
* Load *.js by adding script elements into head. Hook the onload event to
620653
* load the next class in dependency tree.
@@ -730,7 +763,7 @@ ClazzLoader.loadScript = function (file) {
730763
&& navigator.userAgent.indexOf("Opera") >= 0) {
731764
// Opera will not take another try.
732765
var fss = ClazzLoader.failedScripts;
733-
if (fss[path] == null) {
766+
if (fss[path] == null && ClazzLoader.takeAnotherTry) {
734767
// silently take another try for bad network
735768
fss[path] = 1;
736769
ClazzLoader.loadedScripts[path] = false;
@@ -761,7 +794,7 @@ ClazzLoader.loadScript = function (file) {
761794
this.onerror = null;
762795
var path = arguments.callee.path;
763796
var fss = ClazzLoader.failedScripts;
764-
if (fss[path] == null) {
797+
if (fss[path] == null && ClazzLoader.takeAnotherTry) {
765798
// silently take another try for bad network
766799
fss[path] = 1;
767800
ClazzLoader.loadedScripts[path] = false;
@@ -809,6 +842,9 @@ ClazzLoader.loadScript = function (file) {
809842
*/
810843
if (fss[path] == null) {
811844
var fun = function () {
845+
if (!ClazzLoader.takeAnotherTry) {
846+
return;
847+
}
812848
var path = arguments.callee.path;
813849
// next time in "loading" state won't get waiting!
814850
ClazzLoader.failedScripts[path] = 0;
@@ -818,6 +854,7 @@ ClazzLoader.loadScript = function (file) {
818854
ClazzLoader.inLoadingThreads--;
819855
}
820856
// Take another try!
857+
// log ("re - loading ... " + path);
821858
ClazzLoader.loadScript (path);
822859
};
823860
fun.path = path;
@@ -827,6 +864,7 @@ ClazzLoader.loadScript = function (file) {
827864
* What about big *.z.js need more than 1s to initialize?
828865
*/
829866
var waitingTime = (local ? 500 : 15000); // 0.5s : 15s
867+
//alert ("waiting:" + waitingTime + " . " + path);
830868
fhs[path] = window.setTimeout (fun, waitingTime);
831869
return;
832870
}
@@ -839,14 +877,15 @@ ClazzLoader.loadScript = function (file) {
839877
fhs[path] = null;
840878
}
841879
if ((local || state == "loaded") && !ClazzLoader.isInnerLoaded) {
842-
if (!local && (fss[path] == null || fss[path] == 0)) {
880+
if (!local && (fss[path] == null || fss[path] == 0)
881+
&& ClazzLoader.takeAnotherTry) {
843882
// failed! count down
844883
if (ClazzLoader.inLoadingThreads > 0) {
845884
ClazzLoader.inLoadingThreads--;
846885
}
847886
// silently take another try for bad network
848887
fss[path] = 1;
849-
// log ("reloading ... " + path);
888+
//log ("reloading ... " + path);
850889
ClazzLoader.loadedScripts[path] = false;
851890
ClazzLoader.loadScript (path);
852891
return;
@@ -1084,6 +1123,10 @@ ClazzLoader.checkOptionalCycle = function (node) {
10841123
ClazzLoader.updateNode (ts[i].parents[k]);
10851124
}
10861125
ts[i].parents = new Array ();
1126+
if (ts[i].optionalsLoaded != null) {
1127+
ts[i].optionalsLoaded ();
1128+
ts[i].optionalsLoaded = null;
1129+
}
10871130
}
10881131
ts.length = 0;
10891132
return true;
@@ -1134,6 +1177,7 @@ ClazzLoader.updateNode = function (node) {
11341177
var n = node.musts[i];
11351178
if (n.status < ClazzNode.STATUS_DECLARED) {
11361179
if (ClazzLoader.isClassDefined (n.name)) {
1180+
var nns = new Array (); // for optional loaded events!
11371181
n.status = ClazzNode.STATUS_OPTIONALS_LOADED;
11381182
ClazzLoader.updateNode (n);
11391183
/*
@@ -1150,12 +1194,30 @@ ClazzLoader.updateNode = function (node) {
11501194
nn.status = n.status;
11511195
nn.declaration = null;
11521196
ClazzLoader.updateNode (nn);
1197+
if (nn.optionalsLoaded != null) {
1198+
nns[nns.length] = nn;
1199+
}
11531200
}
11541201
}
11551202
n.declaration = null;
11561203
}
1157-
} else {
1158-
isMustsOK = false;
1204+
if (n.optionalsLoaded != null) {
1205+
nns[nns.length] = n;
1206+
}
1207+
for (var j = 0; j < nns.length; j++) {
1208+
if (nns[j].optionalsLoaded != null) {
1209+
nns[j].optionalsLoaded ();
1210+
nns[j].optionalsLoaded = null;
1211+
}
1212+
}
1213+
} else { // why not break? -Zhou Renjian @ Nov 28, 2006
1214+
if (n.status == ClazzNode.STATUS_CONTENT_LOADED) {
1215+
// may be lazy loading script!
1216+
ClazzLoader.updateNode (n);
1217+
}
1218+
if (n.status < ClazzNode.STATUS_DECLARED) {
1219+
isMustsOK = false;
1220+
}
11591221
}
11601222
}
11611223
}
@@ -1237,6 +1299,7 @@ ClazzLoader.updateNode = function (node) {
12371299
ClazzLoader.scriptCompleted (node.path);
12381300
if (node.optionalsLoaded != null) {
12391301
node.optionalsLoaded ();
1302+
node.optionalsLoaded = null;
12401303
if (!ClazzLoader.keepOnLoading) {
12411304
return false;
12421305
}
@@ -1256,6 +1319,7 @@ ClazzLoader.updateNode = function (node) {
12561319
ClazzLoader.scriptCompleted (nn.path);
12571320
if (nn.optionalsLoaded != null) {
12581321
nn.optionalsLoaded ();
1322+
nn.optionalsLoaded = null;
12591323
if (!ClazzLoader.keepOnLoading) {
12601324
return false;
12611325
}
@@ -1467,6 +1531,8 @@ ClazzLoader.load = function (musts, clazz, optionals, declaration) {
14671531

14681532
/*
14691533
* The following lines are commented intentionally.
1534+
* So lots of class is not declared until there is a must?
1535+
*
14701536
* TODO: Test the commented won't break up the dependency tree.
14711537
*/
14721538
/*
@@ -1483,6 +1549,9 @@ ClazzLoader.load = function (musts, clazz, optionals, declaration) {
14831549
declaration.clazzList = arguments[4];
14841550
}
14851551
node.declaration = declaration;
1552+
if (declaration != null) {
1553+
node.status = ClazzNode.STATUS_CONTENT_LOADED;
1554+
}
14861555

14871556
var isOptionalsOK = true;
14881557
if (optionals != null && optionals.length != 0) {

sources/net.sf.j2s.java.core/src/java/lang/String.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Clazz.load (["java.lang.CharSequence", "$.Comparable", "java.io.Serializable", "java.util.Comparator"], "java.lang.String", null, function () {
2+
if (String.$replace != null) { // already defined! Maybe ClazzLoader will try to load this again!
3+
return;
4+
}
25
java.lang.String = String;
36
//Clazz.decorateAsType (String, "String", null, [java.io.Serializable, CharSequence, Comparable]);
47
Clazz.implementOf (String, [java.io.Serializable, CharSequence, Comparable]);

0 commit comments

Comments
 (0)