KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > BundleRepository


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.core;
13
14 import java.util.*;
15 import org.eclipse.osgi.framework.util.KeyedHashSet;
16 import org.osgi.framework.Version;
17
18 /**
19  * The BundleRepository holds all installed Bundle object for the
20  * Framework. The BundleRepository is also used to mark and unmark
21  * bundle dependancies.
22  *
23  * <p>
24  * This class is not synchronized. Any access to the bundle
25  * repository must be synchronized by the caller.
26  */

27 public class BundleRepository {
28     /** bundles by install order */
29     private ArrayList bundlesByInstallOrder;
30
31     /** bundles keyed by bundle Id */
32     private KeyedHashSet bundlesById;
33
34     /** bundles keyed by SymbolicName */
35     private HashMap bundlesBySymbolicName;
36
37     public BundleRepository(int initialCapacity) {
38         bundlesByInstallOrder = new ArrayList(initialCapacity);
39         bundlesById = new KeyedHashSet(initialCapacity, true);
40         bundlesBySymbolicName = new HashMap(initialCapacity);
41     }
42
43     /**
44      * Gets a list of bundles ordered by install order.
45      * @return List of bundles by install order.
46      */

47     public List getBundles() {
48         return bundlesByInstallOrder;
49     }
50
51     /**
52      * Gets a bundle by its bundle Id.
53      * @param bundleId
54      * @return a bundle with the specified id or null if one does not exist
55      */

56     public AbstractBundle getBundle(long bundleId) {
57         Long JavaDoc key = new Long JavaDoc(bundleId);
58         return (AbstractBundle) bundlesById.getByKey(key);
59     }
60
61     public AbstractBundle[] getBundles(String JavaDoc symbolicName) {
62         if (Constants.OSGI_SYSTEM_BUNDLE.equals(symbolicName))
63             symbolicName = Constants.getInternalSymbolicName();
64         return (AbstractBundle[]) bundlesBySymbolicName.get(symbolicName);
65     }
66
67     public AbstractBundle getBundle(String JavaDoc symbolicName, Version version) {
68         AbstractBundle[] bundles = getBundles(symbolicName);
69         if (bundles != null) {
70             if (bundles.length > 0) {
71                 for (int i = 0; i < bundles.length; i++) {
72                     if (bundles[i].getVersion().equals(version)) {
73                         return bundles[i];
74                     }
75                 }
76             }
77         }
78         return null;
79     }
80
81     public void add(AbstractBundle bundle) {
82         bundlesByInstallOrder.add(bundle);
83         bundlesById.add(bundle);
84         String JavaDoc symbolicName = bundle.getSymbolicName();
85         if (symbolicName != null) {
86             AbstractBundle[] bundles = (AbstractBundle[]) bundlesBySymbolicName.get(symbolicName);
87             if (bundles == null) {
88                 // making the initial capacity on this 1 since it
89
// should be rare that multiple version exist
90
bundles = new AbstractBundle[1];
91                 bundles[0] = bundle;
92                 bundlesBySymbolicName.put(symbolicName, bundles);
93                 return;
94             }
95
96             ArrayList list = new ArrayList(bundles.length + 1);
97             // find place to insert the bundle
98
Version newVersion = bundle.getVersion();
99             boolean added = false;
100             for (int i = 0; i < bundles.length; i++) {
101                 AbstractBundle oldBundle = bundles[i];
102                 Version oldVersion = oldBundle.getVersion();
103                 if (!added && newVersion.compareTo(oldVersion) >= 0) {
104                     added = true;
105                     list.add(bundle);
106                 }
107                 list.add(oldBundle);
108             }
109             if (!added) {
110                 list.add(bundle);
111             }
112
113             bundles = new AbstractBundle[list.size()];
114             list.toArray(bundles);
115             bundlesBySymbolicName.put(symbolicName, bundles);
116         }
117     }
118
119     public boolean remove(AbstractBundle bundle) {
120         // remove by bundle ID
121
boolean found = bundlesById.remove(bundle);
122         if (!found)
123             return false;
124
125         // remove by install order
126
bundlesByInstallOrder.remove(bundle);
127         // remove by symbolic name
128
String JavaDoc symbolicName = bundle.getSymbolicName();
129         if (symbolicName == null)
130             return true;
131
132         AbstractBundle[] bundles = (AbstractBundle[]) bundlesBySymbolicName.get(symbolicName);
133         if (bundles == null)
134             return true;
135
136         // found some bundles with the global name.
137
// remove all references to the specified bundle.
138
int numRemoved = 0;
139         for (int i = 0; i < bundles.length; i++) {
140             if (bundle == bundles[i]) {
141                 numRemoved++;
142                 bundles[i] = null;
143             }
144         }
145         if (numRemoved > 0) {
146             if (bundles.length - numRemoved <= 0) {
147                 // no bundles left in the array remove the array from the hash
148
bundlesBySymbolicName.remove(symbolicName);
149             } else {
150                 // create a new array with the null entries removed.
151
AbstractBundle[] newBundles = new AbstractBundle[bundles.length - numRemoved];
152                 int indexCnt = 0;
153                 for (int i = 0; i < bundles.length; i++) {
154                     if (bundles[i] != null) {
155                         newBundles[indexCnt] = bundles[i];
156                         indexCnt++;
157                     }
158                 }
159                 bundlesBySymbolicName.put(symbolicName, newBundles);
160             }
161         }
162
163         return true;
164     }
165
166     public void removeAllBundles() {
167         bundlesByInstallOrder.clear();
168         bundlesById = new KeyedHashSet();
169         bundlesBySymbolicName.clear();
170     }
171 }
172
Popular Tags