KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > packageAdmin > ExportedPackageImpl


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube.packageAdmin;
28
29 import com.opensugar.cube.BundleClassLoader;
30
31 import org.osgi.service.packageadmin.ExportedPackage;
32 import org.osgi.framework.Bundle;
33
34 import java.util.Vector JavaDoc;
35 import java.net.URL JavaDoc;
36
37 public class ExportedPackageImpl implements ExportedPackage, HasVersion {
38
39    private String JavaDoc name;
40    private String JavaDoc specificationVersion;
41    private Bundle exportingBundle;
42    private BundleClassLoader classLoader;
43    private Vector JavaDoc importingBundles;
44    private boolean removalPending;
45    private boolean open;
46
47    public ExportedPackageImpl( String JavaDoc name, String JavaDoc specificationVersion, Bundle exportingBundle, BundleClassLoader classLoader ) {
48       this.name = name;
49       this.specificationVersion = specificationVersion;
50       this.exportingBundle = exportingBundle;
51       this.classLoader = classLoader;
52
53       importingBundles = new Vector JavaDoc();
54       importingBundles.addElement( exportingBundle );
55       removalPending = false;
56       open = false;
57    }
58
59    public String JavaDoc getName() {
60       return name;
61    }
62
63    public String JavaDoc getSpecificationVersion() {
64       return specificationVersion;
65    }
66
67    public Bundle getExportingBundle() {
68       return exportingBundle;
69    }
70
71    public Bundle[] getImportingBundles() {
72       Vector JavaDoc clone = (Vector JavaDoc)importingBundles.clone();
73       Bundle[] ret = new Bundle[ clone.size() ];
74       clone.copyInto( ret );
75       return ret;
76    }
77
78    public boolean isRemovalPending() {
79       return removalPending;
80    }
81
82    protected void addImportingBundle( Bundle bundle ) {
83       if ( importingBundles.indexOf( bundle ) == -1 ) {
84          importingBundles.addElement( bundle );
85       }
86    }
87
88    protected void removeImportingBundle( Bundle bundle ) {
89       // a bundle that exports a package implicitly imports it
90
// therefore the spec says that getImportingBundles should return a list that
91
// always contains the exporting bundle
92
// but the spec also says the getImportingBundles method should return resolved
93
// bundles that import the package
94
//
95
// therefore, I think it is ok to remove the exporting bundle from the list of
96
// importers if the exporting bundle is being uninstalled
97
// if the exporting bundle is being updated, then if its updated version exports
98
// the same package version again, it will go back immediately in the list of
99
// importing bundles, so the removal will be very brief and will pause no problems
100
// if the exporting bundle is being updated, and its updated version exports a
101
// larger version of the package, it is correct that the bundle should not be
102
// in the list of importers of this package because it will not be resolved...
103
//
104
// the spec seems to require some clarification here.
105
//
106
// in the meantime, I will allow the exporting package to be removed from the list
107
// of importers, and leave it to PackageAdminImpl to take care when calling this
108
// method
109
//
110
//if ( bundle.getBundleId() == exportingBundle.getBundleId() && bundle.getState() != bundle.UNINSTALLED ) {
111
// throw new IllegalArgumentException( "Package exporter cannot be removed from the package's list of importers (unless the package exporter is uninstalled)" );
112
//}
113

114       importingBundles.removeElement( bundle );
115    }
116
117    protected boolean isImportingBundle( Bundle bundle ) {
118       return ( importingBundles.indexOf( bundle ) != -1 );
119    }
120
121    protected void setRemovalPending() {
122       removalPending = true;
123    }
124
125    protected void setStale() {
126       exportingBundle = null;
127       classLoader = null;
128       importingBundles = null;
129       removalPending = true ;
130    }
131
132    protected boolean isOpen() {
133       return open;
134    }
135
136    protected Class JavaDoc loadClass( String JavaDoc className ) throws ClassNotFoundException JavaDoc {
137       try {
138          Class JavaDoc clazz = classLoader.loadOwnClass( className );
139          open = true;
140          return clazz;
141       }
142       catch ( ClassNotFoundException JavaDoc e ) {
143          throw e;
144       }
145    }
146
147    protected URL JavaDoc getResource( String JavaDoc name ) {
148       URL JavaDoc url = classLoader.getOwnResource( name );
149       if ( url != null ) {
150          open = true;
151       }
152       return url;
153    }
154
155 }
156
Popular Tags