This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
/
Concierge.java
5275 lines (4550 loc) · 157 KB
/
Concierge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Jan S. Rellermeyer, IBM Research - initial API and implementation
*******************************************************************************/
package org.eclipse.concierge;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.nio.charset.Charset;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.concierge.BundleImpl.Revision;
import org.eclipse.concierge.BundleImpl.Revision.WovenClassImpl;
import org.eclipse.concierge.ConciergeCollections.MultiMap;
import org.eclipse.concierge.ConciergeCollections.ParseResult;
import org.eclipse.concierge.Resources.BundleCapabilityImpl;
import org.eclipse.concierge.Resources.ConciergeBundleWiring;
import org.eclipse.concierge.Resources.HostedBundleCapability;
import org.eclipse.concierge.compat.LegacyBundleProcessing;
import org.eclipse.concierge.compat.service.XargsFileLauncher;
import org.eclipse.concierge.service.log.LogServiceImpl;
import org.osgi.framework.AllServiceListener;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleListener;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceObjects;
import org.osgi.framework.ServicePermission;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.SynchronousBundleListener;
import org.osgi.framework.UnfilteredServiceListener;
import org.osgi.framework.Version;
import org.osgi.framework.dto.BundleDTO;
import org.osgi.framework.dto.FrameworkDTO;
import org.osgi.framework.dto.ServiceReferenceDTO;
import org.osgi.framework.hooks.bundle.CollisionHook;
import org.osgi.framework.hooks.bundle.EventHook;
import org.osgi.framework.hooks.resolver.ResolverHook;
import org.osgi.framework.hooks.resolver.ResolverHookFactory;
import org.osgi.framework.hooks.service.EventListenerHook;
import org.osgi.framework.hooks.service.FindHook;
import org.osgi.framework.hooks.service.ListenerHook;
import org.osgi.framework.hooks.service.ListenerHook.ListenerInfo;
import org.osgi.framework.hooks.weaving.WeavingException;
import org.osgi.framework.hooks.weaving.WeavingHook;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.namespace.AbstractWiringNamespace;
import org.osgi.framework.namespace.BundleNamespace;
import org.osgi.framework.namespace.HostNamespace;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.namespace.PackageNamespace;
import org.osgi.framework.startlevel.BundleStartLevel;
import org.osgi.framework.startlevel.FrameworkStartLevel;
import org.osgi.framework.startlevel.dto.FrameworkStartLevelDTO;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRequirement;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.framework.wiring.FrameworkWiring;
import org.osgi.resource.Capability;
import org.osgi.resource.Namespace;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.resource.Wire;
import org.osgi.resource.Wiring;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.LogService;
import org.osgi.service.resolver.HostedCapability;
import org.osgi.service.resolver.ResolutionException;
import org.osgi.service.resolver.ResolveContext;
import org.osgi.service.resolver.Resolver;
/**
* The core class of the Concierge OSGi framework. Implements the system bundle,
* maintains the central bundle and service registry.
*
* @author Jan S. Rellermeyer
* @author Jochen Hiller - added argument parsing
*/
public final class Concierge extends AbstractBundle implements Framework,
BundleRevision, FrameworkWiring, FrameworkStartLevel, BundleActivator {
// deprecated core framework constants.
@SuppressWarnings("deprecation")
private static final String FRAMEWORK_EXECUTIONENVIRONMENT = Constants.FRAMEWORK_EXECUTIONENVIRONMENT;
@SuppressWarnings("deprecation")
private static final String BUNDLE_REQUIREDEXECUTIONENVIRONMENT = Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT;
private static final String BUNDLE_SYMBOLIC_NAME = "org.eclipse.concierge";
/**
* Version displayed upon startup and returned by System Bundle
*/
private static final String FRAMEWORK_VERSION = "6.0.0.qualifier";
@SuppressWarnings("deprecation")
private static Class<?> SERVICE_EVENT_HOOK_CLASS = org.osgi.framework.hooks.service.EventHook.class;
// URLStreamHandlerFactory
/**
* This static variable contains a URL stream handler factory, which will be
* dispatched for the current running Concierge instance.
*/
private static ConciergeURLStreamHandlerFactory conciergeURLStreamHandlerFactory = new ConciergeURLStreamHandlerFactory();
// the runtime args
public static boolean SUPPORTS_EXTENSIONS;
/**
* framework basedir.
*/
private String BASEDIR;
/**
* bundle location.
*/
public String BUNDLE_LOCATION = "file:.";
private int BEGINNING_STARTLEVEL;
/**
* the location where the storage resides.
*/
String STORAGE_LOCATION;
/**
* classloader buffer size.
*/
static int CLASSLOADER_BUFFER_SIZE;
/**
* logging enabled.
*/
boolean LOG_ENABLED;
/**
* log service.
*/
LogService logger;
/**
* log buffer size.
*/
int LOG_BUFFER_SIZE;
/**
* log quiet ? (= no logging to System.out)
*/
boolean LOG_QUIET;
/**
* always decompress the bundles, great for testing
*
* FIXME: combine with decompress embedded into a single property, e.g.,
* with values: NEVER, EMBEDDED_JARS, ALWAYS
*/
boolean ALWAYS_DECOMPRESS;
/**
* decompress bundles with embedded jars.
*/
boolean DECOMPRESS_EMBEDDED;
/**
* log level.
*/
int LOG_LEVEL;
/**
* security.
*/
boolean SECURITY_ENABLED;
/**
* debug outputs from bundles ?
*/
boolean DEBUG_BUNDLES;
/**
* debug outputs from packages ?
*/
boolean DEBUG_PACKAGES;
/**
* debug outputs from class loading ?
*/
boolean DEBUG_CLASSLOADING;
/**
* debug outputs from services ?
*/
boolean DEBUG_SERVICES;
/**
* debug outputs from services ?
*/
boolean DEBUG_RESOLVER;
/**
* the profile.
*/
private String PROFILE;
private final String[] bootdelegationAbs;
private final String[] bootdelegationPrefix;
private String[] libraryExtensions;
private String execPermission;
private Pattern execPermissionPattern;
private static final int COLLISION_POLICY_SINGLE = -1;
private static final int COLLISION_POLICY_NONE = 0;
private static final int COLLISION_POLICY_MULTIPLE = 1;
private static final int COLLISION_POLICY_MANAGED = 2;
int collisionPolicy;
private Hashtable<String, String> headers;
private final Version version = new Version("1.5.0");
static final HashSet<String> SUPPORTED_EE = new HashSet<String>();
static Comparator<AbstractBundle> VERSION_COMPARATOR = new Comparator<AbstractBundle>() {
public int compare(final AbstractBundle b1, final AbstractBundle b2) {
return b2.getVersion().compareTo(b1.getVersion());
}
};
protected final static Pattern FILTER_ASSERT_MATCHER = Pattern
.compile("\\(([^&\\!|=<>~\\(\\)]*)[=|<=|>=|~=]");
// registry data structures
/**
* the bundles.
*/
List<AbstractBundle> bundles = new ArrayList<AbstractBundle>(2);
/**
* bundleID -> bundle.
*/
Map<Long, AbstractBundle> bundleID_bundles = new HashMap<Long, AbstractBundle>(
2);
/**
* location -> bundle.
*/
Map<String, AbstractBundle> location_bundles = Collections
.synchronizedMap(new HashMap<String, AbstractBundle>(2));
/**
* symbolicName -> List of bundles
*/
MultiMap<String, AbstractBundle> symbolicName_bundles = new MultiMap<String, AbstractBundle>(
2);
/**
* class name string -> service reference.
*/
final MultiMap<String, ServiceReference<?>> serviceRegistry = new MultiMap<String, ServiceReference<?>>(
3);
/**
* class name string -> service reference.
*/
final MultiMap<String, ServiceReference<?>> microServices = new MultiMap<String, ServiceReference<?>>(
3);
/**
* bundle listeners.
*/
protected final List<BundleListener> bundleListeners = new ArrayList<BundleListener>(
1);
/**
* synchronous bundle listeners.
*/
protected final List<SynchronousBundleListener> syncBundleListeners = new ArrayList<SynchronousBundleListener>(
1);
protected final MultiMap<BundleContext, BundleListener> bundleListenerMap = new MultiMap<BundleContext, BundleListener>();
/**
* service listeners.
*/
protected final List<ServiceListenerEntry> serviceListeners = new ArrayList<ServiceListenerEntry>(
1);
/**
* Map of unattached fragments in the system. HostName => List of fragments
*/
private final MultiMap<String, Revision> fragmentIndex = new MultiMap<String, Revision>(
1);
private final ArrayList<BundleImpl> extensionBundles = new ArrayList<BundleImpl>(
0);
/**
* framework listeners.
*/
protected final List<FrameworkListener> frameworkListeners = new ArrayList<FrameworkListener>(
1);
CapabilityRegistry capabilityRegistry = new CapabilityRegistry();
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
// the fields
/**
* next bundle ID.
*/
private long nextBundleID = 1;
/**
* the initial startlevel for installed bundles.
*/
int initStartlevel = 1;
/**
* restart ?
*/
public boolean restart = false;
// system bundle
/**
* the symbolicName of the system bundle
*/
public static final String FRAMEWORK_SYMBOLIC_NAME = "org.eclipse.concierge";
private final List<BundleCapability> systemBundleCapabilities = new ArrayList<BundleCapability>();
private static Properties defaultProperties;
String osname;
Version osversion;
String language;
String processor;
final Properties properties;
/*
* hook support
*/
// @formatter:off
// bundle hooks
private final List<ServiceReferenceImpl<CollisionHook>> bundleCollisionHooks = new ArrayList<ServiceReferenceImpl<CollisionHook>>(0);
private final List<ServiceReferenceImpl<org.osgi.framework.hooks.bundle.EventHook>> bundleEventHooks = new ArrayList<ServiceReferenceImpl<org.osgi.framework.hooks.bundle.EventHook>>(0);
protected final List<ServiceReferenceImpl<org.osgi.framework.hooks.bundle.FindHook>> bundleFindHooks = new ArrayList<ServiceReferenceImpl<org.osgi.framework.hooks.bundle.FindHook>>(0);
// resolver hook
protected List<ServiceReferenceImpl<ResolverHookFactory>> resolverHookFactories = new ArrayList<ServiceReferenceImpl<ResolverHookFactory>>(0);
// service hooks
@SuppressWarnings("deprecation")
protected List<ServiceReferenceImpl<org.osgi.framework.hooks.service.EventHook>> serviceEventHooks = new ArrayList<ServiceReferenceImpl<org.osgi.framework.hooks.service.EventHook>>(0);
protected List<ServiceReferenceImpl<ListenerHook>> serviceListenerHooks = new ArrayList<ServiceReferenceImpl<ListenerHook>>(0);
protected List<ServiceReferenceImpl<EventListenerHook>> serviceEventListenerHooks = new ArrayList<ServiceReferenceImpl<EventListenerHook>>(0);
protected List<ServiceReferenceImpl<FindHook>> serviceFindHooks = new ArrayList<ServiceReferenceImpl<FindHook>>(0);
// weaving hooks
private final List<ServiceReferenceImpl<WeavingHook>> weavingHooks = new ArrayList<ServiceReferenceImpl<WeavingHook>>(0);
// "hooks registry"
protected final HashMap<String, List<?>> hooks = new HashMap<String, List<?>>();
// @formatter:on
static final Dictionary<String, Object> props2Dict(final Properties props) {
final Hashtable<String, Object> table = new Hashtable<String, Object>();
for (final Object key : Collections.list(props.propertyNames())) {
final String keyStr = (String) key;
table.put(keyStr, props.getProperty(keyStr));
}
return table;
}
private FrameworkEvent stopEvent = new FrameworkEvent(
FrameworkEvent.STOPPED, this, null);
private final BundleStartLevel systemBundleStartLevel = new SystemBundleStartLevel();
private final ResolverImpl resolver = new ResolverImpl();
private final Method addURL;
final ClassLoader parentClassLoader;
final ClassLoader systemBundleClassLoader;
protected static final Comparator<? super Capability> BUNDLE_VERSION = new Comparator<Capability>() {
public int compare(final Capability cap1, final Capability cap2) {
final Version cap1Version = (Version) cap1.getAttributes().get(
AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
final Version cap2Version = (Version) cap2.getAttributes().get(
AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
return cap2Version.compareTo(cap1Version);
}
};
protected static final Comparator<? super Capability> EXPORT_ORDER = new Comparator<Capability>() {
// reverts the order so that we can
// retrieve the 0st item to get the best
// match
public int compare(final Capability c1, final Capability c2) {
if (!(c1 instanceof BundleCapability
&& c2 instanceof BundleCapability)) {
return 0;
}
final BundleCapability cap1 = (BundleCapability) c1;
final BundleCapability cap2 = (BundleCapability) c2;
final int cap1Resolved = cap1.getResource().getWiring() == null ? 0
: 1;
final int cap2Resolved = cap2.getResource().getWiring() == null ? 0
: 1;
int score = cap2Resolved - cap1Resolved;
if (score != 0) {
return score;
}
final Version cap1Version = (Version) cap1.getAttributes()
.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
final Version cap2Version = (Version) cap2.getAttributes()
.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
score = cap2Version.compareTo(cap1Version);
if (score != 0) {
return score;
}
final long cap1BundleId = cap1.getRevision().getBundle()
.getBundleId();
final long cap2BundleId = cap2.getRevision().getBundle()
.getBundleId();
return (int) (cap1BundleId - cap2BundleId);
}
};
public static final String DIR_INTERNAL = "x-int";
/** Return code from main when OK. */
private static final int MAIN_RC_OK = 0;
/** Return code from main when printing usage message. */
private static final int MAIN_RC_USAGE = 1;
private boolean firstInit = true;
/**
* Main method to start Concierge. This class will delegate that to an
* internal method, which will create framework instance, but will not wait
* until stopped. This allows better testability.
*/
public static void main(final String[] args) throws Exception {
final Concierge framework = doMain(args);
if (framework != null) {
// wait until framework stopped
framework.waitForStop(0);
// exit with OK
System.exit(MAIN_RC_OK);
} else {
// exit with usage message
System.exit(MAIN_RC_USAGE);
}
}
/**
* Processing of command line arguments. It will create a framework instance
* based on given arguments.
*
* @param args
* command line arguments.
* @throws Exception
*/
public static Concierge doMain(final String[] args) throws Exception {
// TODO: temporary solution to use xargs file launcher for argument
// processing
final XargsFileLauncher xargsLauncher = new XargsFileLauncher(System.err);
String xargsFile = null;
final StringBuffer argsBuf = new StringBuffer();
for (int i = 0; args != null && i < args.length; i++) {
if ("-help".equalsIgnoreCase(args[i])) {
// if -help show usage message
System.err.println(""
+ "Concierge usage: org.eclipse.concierge.Concierge {arguments}\n"
+ " {file.xargs} "
+ "Loads xargs file, must end with .xargs\n"
+ " {-install|-start|-istart} {bundle-jar-file} "
+ "Install and start one bundle (can be used multiple times, in specified order)\n"
+ " {-all} {directory} "
+ "Install and start all bundles from specified directory\n"
+ " {-Dprop=value} "
+ "Specify one or more props just for Concierge (can be used multiple times)\n"
+ "Sample: org.eclipse.concierge.Concierge -Dorg.osgi.framework.bootdelegation=sun.*,javax.* -istart mybundle.jar\n");
// TODO allow -all with directory location
return null;
} else if (args[i].endsWith(".xargs")) {
// if arg is name of an xargs file: use this, stop further
// processing
xargsFile = args[i];
break;
} else {
argsBuf.append(args[i]);
if (args[i].startsWith("-D")) {
argsBuf.append('\n');
} else if (args[i].equalsIgnoreCase("-profile")
|| args[i].equalsIgnoreCase("-install")
|| args[i].equalsIgnoreCase("-istart")
|| args[i].equalsIgnoreCase("-start")
|| args[i].equalsIgnoreCase("-all")) {
// append next argument to same line
if (i - 1 < args.length) {
i++;
argsBuf.append(' ');
argsBuf.append(args[i]);
argsBuf.append('\n');
}
}
}
}
// no arguments? Try to use init.xargs or other file
if (xargsFile == null && argsBuf.length() == 0) {
xargsFile = System.getProperty("org.eclipse.concierge.init.xargs");
if (xargsFile == null) {
xargsFile = "init.xargs";
}
}
// now start framework with given arguments
final Concierge fw;
if (xargsFile != null) {
// take args from file
final File xargs = new File(xargsFile);
if (xargs.exists()) {
fw = xargsLauncher.processXargsFile(xargs);
} else {
System.err.println("Concierge: xargs file '" + xargs.toString()
+ "' not found, starting without arguments");
fw = (Concierge) new Factory().newFramework(null);
fw.init();
fw.start();
}
} else {
// some arguments have been defined
InputStream inputStream = new ByteArrayInputStream(
argsBuf.toString().getBytes(Charset.forName("UTF-8")));
// TODO support really props as command line args?
// we have to preserve the properties for later variable and
// wildcard replacement
final Map<String, String> passedProperties = xargsLauncher
.getPropertiesFromXargsInputStream(inputStream);
// now process again for install/start options with given properties
inputStream = new ByteArrayInputStream(
argsBuf.toString().getBytes(Charset.forName("UTF-8")));
fw = xargsLauncher.processXargsInputStream(passedProperties,
inputStream);
}
return fw;
}
Concierge(final Map<String, String> passedProperties) {
hooks.put(CollisionHook.class.getName(), bundleCollisionHooks);
hooks.put(org.osgi.framework.hooks.bundle.FindHook.class.getName(),
bundleFindHooks);
hooks.put(EventHook.class.getName(), bundleEventHooks);
hooks.put(ResolverHookFactory.class.getName(), resolverHookFactories);
hooks.put(SERVICE_EVENT_HOOK_CLASS.getName(), serviceEventHooks);
hooks.put(EventListenerHook.class.getName(), serviceEventListenerHooks);
hooks.put(FindHook.class.getName(), serviceFindHooks);
hooks.put(ListenerHook.class.getName(), serviceListenerHooks);
hooks.put(WeavingHook.class.getName(), weavingHooks);
defaultProperties = new Properties(System.getProperties());
defaultProperties.setProperty(Constants.FRAMEWORK_BOOTDELEGATION,
"java.*, sun.*, com.sun.*");
defaultProperties.setProperty(Constants.FRAMEWORK_BUNDLE_PARENT,
Constants.FRAMEWORK_BUNDLE_PARENT_BOOT);
defaultProperties.setProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL,
"1");
final String feeStr = defaultProperties
.getProperty(FRAMEWORK_EXECUTIONENVIRONMENT);
final StringBuffer myEEs = new StringBuffer();
final StringBuffer seVersionList = new StringBuffer();
final StringBuffer compact1VersionList = new StringBuffer();
final StringBuffer compact2VersionList = new StringBuffer();
final StringBuffer compact3VersionList = new StringBuffer();
final StringBuffer minVersionList = new StringBuffer();
final int minor;
int parsed = 0;
final String specVersion = System
.getProperty("java.specification.version");
if (specVersion.indexOf('.') == -1) {
try {
parsed = Integer.parseInt(specVersion);
} catch (final NumberFormatException nfe) {
nfe.printStackTrace();
} finally {
minor = parsed;
}
} else {
try {
parsed = Integer.parseInt(specVersion.substring(2));
} catch (final NumberFormatException nfe) {
nfe.printStackTrace();
} finally {
minor = parsed;
}
}
// check for J2ME VMs. If not set (like in CEE-J) fallback to JavaSE
if (System.getProperty("java.specification.name", "")
.equals("J2ME Foundation Specification")) {
switch (minor) {
case 1:
myEEs.append("CDC-1.1/Foundation-1.1,");
case 0:
myEEs.append("CDC-1.0/Foundation-1.0");
}
} else {
switch (minor) {
case 11:
myEEs.append("JavaSE-11");
case 9:
myEEs.append("JavaSE-9");
case 8:
myEEs.append("J2SE-1.8,");
myEEs.append("JavaSE-1.8,");
// also add the valid compact profiles
try {
// Figure out the profile by loading some classes from the profile
// we do NOT use the $JAVA_HOME/release file like this is done by Equinox
// this would NOT be portable across different JVMs
myEEs.append("JavaSE-1.8/compact1,");
compact1VersionList.append("1.8,");
// we try to load the class from our own class loader without initializing the class
// be careful to not use this.getClass().getClassLoader().loadClass("...")
// as some JVM, e.g. CEE-J will return null as class loader when
// the framework has been loaded by the bootstrap class loader
// Class.forName("name", false, classLoader) can handle the case when class loader is null
Class.forName("org.w3c.dom.Document", false, this.getClass().getClassLoader());
myEEs.append("JavaSE-1.8/compact2,");
compact2VersionList.append("1.8,");
Class.forName("javax.management.Descriptor", false, this.getClass().getClassLoader());
myEEs.append("JavaSE-1.8/compact3,");
compact3VersionList.append("1.8,");
Class.forName("javax.imageio.ImageIO", false, this.getClass().getClassLoader());
} catch(ClassNotFoundException e){
}
seVersionList.append("1.8,");
case 7:
myEEs.append("J2SE-1.7,");
myEEs.append("JavaSE-1.7,");
seVersionList.append("1.7,");
case 6:
myEEs.append("J2SE-1.6,");
myEEs.append("JavaSE-1.6,");
seVersionList.append("1.6,");
case 5:
myEEs.append("J2SE-1.5,");
seVersionList.append("1.5,");
case 4:
myEEs.append("J2SE-1.4,");
myEEs.append("OSGi/Minimum-1.1,");
seVersionList.append("1.4,");
minVersionList.append("1.2,1.1,");
case 3:
myEEs.append("J2SE-1.3,");
seVersionList.append("1.3,");
case 2:
myEEs.append("J2SE-1.2,");
myEEs.append("OSGi/Minimum-1.0,");
seVersionList.append("1.2,");
minVersionList.append("1.0");
case 1:
myEEs.append("JRE-1.1");
seVersionList.append("1.1");
}
}
defaultProperties.setProperty(FRAMEWORK_EXECUTIONENVIRONMENT,
myEEs.toString());
// load micro-services
try {
final InputStream in = getClass().getClassLoader()
.getResourceAsStream("META-INF/micro-services");
final BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
try {
final String[] tokens = Utils.splitString(line, ' ');
final Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put(Constants.VERSION_ATTRIBUTE,
Version.parseVersion(tokens[2]));
final Class<?> cls = Class.forName(tokens[1]);
final Object service = cls.newInstance();
final ServiceReference<?> ref = new ServiceReferenceImpl<Object>(
this, this, service, props,
new String[] { tokens[0] });
microServices.insert(tokens[0], ref);
} catch (final Exception e) {
e.printStackTrace();
}
}
serviceRegistry.insertMap(microServices);
} catch (final IOException ioe) {
ioe.printStackTrace();
}
// populate osgi.ee namespace
try {
if (seVersionList.length() > 0) {
final BundleCapabilityImpl eeCap = new BundleCapabilityImpl(
this,
"osgi.ee; osgi.ee=\"JavaSE\"; version:List<Version>=\""
+ seVersionList.toString() + "\"");
systemBundleCapabilities.add(eeCap);
}
if(compact1VersionList.length() > 0){
final BundleCapabilityImpl eeCap = new BundleCapabilityImpl(
this,
"osgi.ee; osgi.ee=\"JavaSE/compact1\"; version:List<Version>=\""
+ compact1VersionList.toString() + "\"");
systemBundleCapabilities.add(eeCap);
}
if(compact2VersionList.length() > 0){
final BundleCapabilityImpl eeCap = new BundleCapabilityImpl(
this,
"osgi.ee; osgi.ee=\"JavaSE/compact2\"; version:List<Version>=\""
+ compact2VersionList.toString() + "\"");
systemBundleCapabilities.add(eeCap);
}
if(compact3VersionList.length() > 0){
final BundleCapabilityImpl eeCap = new BundleCapabilityImpl(
this,
"osgi.ee; osgi.ee=\"JavaSE/compact3\"; version:List<Version>=\""
+ compact3VersionList.toString() + "\"");
systemBundleCapabilities.add(eeCap);
}
if (minVersionList.length() > 0) {
final BundleCapabilityImpl eeCap = new BundleCapabilityImpl(
this,
"osgi.ee; osgi.ee=\"OSGi/Minimum\"; version:List<Version>=\""
+ minVersionList.toString() + "\"");
systemBundleCapabilities.add(eeCap);
}
} catch (final BundleException be) {
// too early for log service
be.printStackTrace();
}
if (feeStr != null) {
final LegacyBundleProcessing proc = getService(
LegacyBundleProcessing.class,
LegacyBundleProcessing.VERSION_TWO);
if (proc == null) {
warning("Framework execution environment is ignored because no bundle manifest version 2 processor is available in this deployment");
} else {
final List<BundleCapability> feeCaps = proc
.translateToCapability(this,
FRAMEWORK_EXECUTIONENVIRONMENT, feeStr);
systemBundleCapabilities.addAll(feeCaps);
}
}
defaultProperties.setProperty(Constants.FRAMEWORK_SYSTEMPACKAGES,
"org.osgi.framework;version=1.8,org.osgi.framework.dto;version=1.8,org.osgi.dto;version=1.0,org.osgi.framework.hooks.bundle;version=1.1,org.osgi.framework.hooks.resolver;version=1.0,org.osgi.framework.hooks.service;version=1.1,org.osgi.framework.hooks.weaving;version=1.1,org.osgi.framework.launch;version=1.2,org.osgi.framework.namespace;version=1.1,org.osgi.framework.startlevel;version=1.0,org.osgi.framework.startlevel.dto;version=1.0,org.osgi.framework.wiring;version=1.2,org.osgi.framework.wiring.dto;version=1.2,org.osgi.resource;version=1.0,org.osgi.resource.dto;version=1.0,org.osgi.service.log;version=1.3,org.osgi.service.packageadmin;version=1.2,org.osgi.service.startlevel;version=1.1,org.osgi.service.url;version=1.0,org.osgi.service.resolver;version=1.0.1,org.osgi.util.tracker;version=1.5.1,META-INF.services");
Object obj;
defaultProperties.put(Constants.FRAMEWORK_OS_NAME,
(obj = System.getProperty("os.name")) != null ? obj
: "undefined");
// Normalize to framework.processor according to OSGi R5 spec table 4.4
if ("Mac OS X".equals(System.getProperty("os.name"))) {
defaultProperties.put(Constants.FRAMEWORK_OS_NAME, "MacOSX");
} else if ("Mac OS".equals(System.getProperty("os.name"))) {
defaultProperties.put(Constants.FRAMEWORK_OS_NAME, "MacOS");
}
defaultProperties.put(Constants.FRAMEWORK_OS_VERSION,
(obj = System.getProperty("os.version")) != null ? obj
: "undefined");
defaultProperties.put(Constants.FRAMEWORK_PROCESSOR,
(obj = System.getProperty("os.arch")) != null ? obj
: "undefined");
final String lang = java.util.Locale.getDefault().getLanguage();
defaultProperties.setProperty(Constants.FRAMEWORK_LANGUAGE,
lang != null ? lang : "en");
defaultProperties.setProperty(Constants.FRAMEWORK_BEGINNING_STARTLEVEL,
"3");
defaultProperties.setProperty(Constants.FRAMEWORK_STORAGE, "storage");
// properties
properties = new Properties(defaultProperties) {
private static final long serialVersionUID = -3319768973242656809L;
public String getProperty(final String key) {
final Object launchO = get(key);
final String launchS = launchO instanceof String
? (String) launchO : null;
if (launchS != null) {
return launchS;
}
final String system = System.getProperty(key);
return system == null ? defaults.getProperty(key) : system;
}
};
if (passedProperties != null) {
for (final Map.Entry<String, String> entry : passedProperties
.entrySet()) {
if (entry.getValue() != null) {
properties.put(entry.getKey(), entry.getValue());
}
}
}
// set parent classloader
systemBundleClassLoader = getClass().getClassLoader();
final String p = properties
.getProperty(Constants.FRAMEWORK_BUNDLE_PARENT);
if (Constants.FRAMEWORK_BUNDLE_PARENT_APP.equals(p)) {
parentClassLoader = ClassLoader.getSystemClassLoader();
} else if (Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK.equals(p)) {
parentClassLoader = systemBundleClassLoader;
} else if (Constants.FRAMEWORK_BUNDLE_PARENT_EXT.equals(p)) {
ClassLoader c = ClassLoader.getSystemClassLoader();
while (c.getParent() != null) {
c = c.getParent();
}
parentClassLoader = c;
} else {
parentClassLoader = new ClassLoader(Object.class.getClassLoader()) {
};
}
properties.setProperty(Constants.SUPPORTS_FRAMEWORK_EXTENSION,
Boolean.toString(false));
Method m = null;
if (getClass().getClassLoader() instanceof URLClassLoader) {
try {
m = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[] { URL.class });
m.setAccessible(true);
properties.setProperty(Constants.SUPPORTS_FRAMEWORK_EXTENSION,
Boolean.toString(true));
SUPPORTS_EXTENSIONS = true;
} catch (final Exception e) {
logger.log(LogService.LOG_WARNING,
"Could not hijack classloader for framework extensions",
e);
}
}
addURL = m;
// apply constants
properties.setProperty(Constants.FRAMEWORK_VERSION, version.toString());
properties.setProperty(Constants.FRAMEWORK_VENDOR,
"Eclipse Foundation");
properties.setProperty(Constants.SUPPORTS_BOOTCLASSPATH_EXTENSION,
Boolean.toString(false));
properties.setProperty(Constants.SUPPORTS_FRAMEWORK_FRAGMENT,
Boolean.toString(true));
properties.setProperty(Constants.SUPPORTS_FRAMEWORK_REQUIREBUNDLE,
Boolean.toString(true));
// set instance properties
PROFILE = properties.getProperty("org.eclipse.concierge.profile",
"default");
BASEDIR = properties.getProperty("org.eclipse.concierge.basedir", ".");
BUNDLE_LOCATION = properties.getProperty("org.eclipse.concierge.jars",
"file:" + BASEDIR);
CLASSLOADER_BUFFER_SIZE = getProperty(
"org.eclipse.concierge.classloader.buffersize", 2048);
LOG_ENABLED = getProperty("org.eclipse.concierge.log.enabled", false);
LOG_QUIET = getProperty("org.eclipse.concierge.log.quiet", false);
LOG_BUFFER_SIZE = getProperty("org.eclipse.concierge.log.buffersize",
10);
LOG_LEVEL = getProperty("org.eclipse.concierge.log.level",
LogService.LOG_ERROR);
DEBUG_BUNDLES = getProperty("org.eclipse.concierge.debug.bundles",
false);
DEBUG_PACKAGES = getProperty("org.eclipse.concierge.debug.packages",
false);
DEBUG_SERVICES = getProperty("org.eclipse.concierge.debug.services",
false);
DEBUG_RESOLVER = getProperty("org.eclipse.concierge.debug.resolver",
false);
DEBUG_CLASSLOADING = getProperty(
"org.eclipse.concierge.debug.classloading", false);
if (getProperty("org.eclipse.concierge.debug", false)) {
System.out.println("SETTING ALL DEBUG FLAGS");
LOG_ENABLED = true;
LOG_LEVEL = LogService.LOG_DEBUG;
DEBUG_BUNDLES = true;
DEBUG_PACKAGES = true;
DEBUG_SERVICES = true;
DEBUG_CLASSLOADING = true;
DEBUG_RESOLVER = true;
LOG_LEVEL = 4;
}
ALWAYS_DECOMPRESS = getProperty(
"org.eclipse.concierge.alwaysDecompress", false);
DECOMPRESS_EMBEDDED = getProperty(
"org.eclipse.concierge.decompressEmbedded", true);