3838import org .osgi .framework .wiring .BundleRequirement ;
3939import org .osgi .framework .wiring .BundleRevision ;
4040
41+ import java .lang .ref .WeakReference ;
4142import java .util .ArrayList ;
4243import java .util .Collections ;
4344import java .util .HashMap ;
4748import java .util .List ;
4849import java .util .Map ;
4950import java .util .Map .Entry ;
51+ import java .util .Objects ;
5052import java .util .Set ;
53+ import java .util .WeakHashMap ;
54+ import java .util .concurrent .atomic .AtomicLong ;
55+ import java .util .function .Function ;
56+ import java .util .stream .Collectors ;
5157
5258public class ManifestParser
5359{
@@ -67,6 +73,32 @@ public class ManifestParser
6773 private volatile List <NativeLibraryClause > m_libraryClauses ;
6874 private volatile boolean m_libraryHeadersOptional = false ;
6975
76+ private static final Map <Object , WeakReference <Object >> objectCache = new WeakHashMap <>();
77+ private static final Function <Object , Object > cache = (foo ) ->
78+ {
79+ if (foo instanceof String )
80+ {
81+ return ((String ) foo ).intern ();
82+ }
83+ else if (foo != null )
84+ {
85+ synchronized (objectCache )
86+ {
87+ WeakReference <Object > ref = objectCache .get (foo );
88+ if (ref != null )
89+ {
90+ Object refValue = ref .get ();
91+ if (refValue != null )
92+ {
93+ return refValue ;
94+ }
95+ }
96+ objectCache .put (foo , new WeakReference <>(foo ));
97+ }
98+ }
99+ return foo ;
100+ };
101+
70102 public ManifestParser (Logger logger , Map <String , Object > configMap , BundleRevision owner , Map <String , Object > headerMap )
71103 throws BundleException
72104 {
@@ -108,6 +140,8 @@ public ManifestParser(Logger logger, Map<String, Object> configMap, BundleRevisi
108140 }
109141 }
110142
143+ m_bundleVersion = (Version ) cache .apply (m_bundleVersion );
144+
111145 //
112146 // Parse bundle symbolic name.
113147 //
@@ -269,25 +303,25 @@ public ManifestParser(Logger logger, Map<String, Object> configMap, BundleRevisi
269303 }
270304
271305 List <BundleRequirement > nativeCodeReqs = convertNativeCode (owner , m_libraryClauses , m_libraryHeadersOptional );
272-
306+
273307 // Combine all requirements.
274308 m_requirements = new ArrayList <BundleRequirement >(
275309 hostReqs .size () + importReqs .size () + rbReqs .size ()
276310 + requireReqs .size () + dynamicReqs .size () + breeReqs .size ());
277- m_requirements .addAll (hostReqs );
278- m_requirements .addAll (importReqs );
279- m_requirements .addAll (rbReqs );
280- m_requirements .addAll (requireReqs );
281- m_requirements .addAll (dynamicReqs );
282- m_requirements .addAll (breeReqs );
283- m_requirements .addAll (nativeCodeReqs );
311+ m_requirements .addAll (hostReqs . stream (). map ( req -> BundleRequirementImpl . createFrom (( BundleRequirementImpl ) req , cache )). collect ( Collectors . toList ()) );
312+ m_requirements .addAll (importReqs . stream (). map ( req -> BundleRequirementImpl . createFrom (( BundleRequirementImpl ) req , cache )). collect ( Collectors . toList ()) );
313+ m_requirements .addAll (rbReqs . stream (). map ( req -> BundleRequirementImpl . createFrom (( BundleRequirementImpl ) req , cache )). collect ( Collectors . toList ()) );
314+ m_requirements .addAll (requireReqs . stream (). map ( req -> BundleRequirementImpl . createFrom (( BundleRequirementImpl ) req , cache )). collect ( Collectors . toList ()) );
315+ m_requirements .addAll (dynamicReqs . stream (). map ( req -> BundleRequirementImpl . createFrom (( BundleRequirementImpl ) req , cache )). collect ( Collectors . toList ()) );
316+ m_requirements .addAll (breeReqs . stream (). map ( req -> BundleRequirementImpl . createFrom (( BundleRequirementImpl ) req , cache )). collect ( Collectors . toList ()) );
317+ m_requirements .addAll (nativeCodeReqs . stream (). map ( req -> BundleRequirementImpl . createFrom (( BundleRequirementImpl ) req , cache )). collect ( Collectors . toList ()) );
284318
285319 // Combine all capabilities.
286320 m_capabilities = new ArrayList <BundleCapability >(
287321 capList .size () + exportCaps .size () + provideCaps .size ());
288- m_capabilities .addAll (capList );
289- m_capabilities .addAll (exportCaps );
290- m_capabilities .addAll (provideCaps );
322+ m_capabilities .addAll (capList . stream (). map ( cap -> BundleCapabilityImpl . createFrom (( BundleCapabilityImpl ) cap , cache )). collect ( Collectors . toList ()) );
323+ m_capabilities .addAll (exportCaps . stream (). map ( cap -> BundleCapabilityImpl . createFrom (( BundleCapabilityImpl ) cap , cache )). collect ( Collectors . toList ()) );
324+ m_capabilities .addAll (provideCaps . stream (). map ( cap -> BundleCapabilityImpl . createFrom (( BundleCapabilityImpl ) cap , cache )). collect ( Collectors . toList ()) );
291325
292326 //
293327 // Parse activation policy.
0 commit comments