Is it dangerous to change the bundlecontext of a bundle to public?
The reason why I would like to do is, is to access the Context from a different package.
Of course a workaround would be to pass the context as a parameter when instantiate an object. But if it wouldn't be too dangerous to make the context public, this way would be more easy.
I would not store BundleContext in a public static variable for several reasons.
From the Javadoc of BundleContext:
The BundleContext object is only valid during the execution of its context bundle; that is, during the period from when the context bundle is in the STARTING, STOPPING, and ACTIVE bundle states. [...]
If you do so, you might forget to set the static variable when the bundle stops. In that case others can access the bundle context when it is in marked for removal state. In this state, others are stille wired to this bundle.
A BundleContext object is generally for the private use of its associated bundle and is not meant to be shared with other bundles in the OSGi environment.
By assigning the BundleContext to a static variable, you let others to access it. Well, if the class is in a non-exported package, it is harder, but still possible to get it.
A Bundle can be adapted to its BundleContext. In order for this to succeed, the caller must have the appropriate AdminPermission[bundle,CONTEXT] if the Java Runtime Environment supports permissions.
If you accidently take this public static variable into a class that is in an exported package, others can access it without permission checks.
Issue if the package is exported and imported
Imagine that you have package A and B. Package A is exported but also imported. It can happen that classes from package B will see package A classes from a different bundle. In that case classes from package B will use the bundle context of a different bundle.
All of the examples are special ones above but they show how easily you can get into trouble if you store your BundleContext in a static variable. If you are careful, you will not cause any trouble, but remember, you might not be the only one, who changes the code of the project.
In general, it is not a good practice to use static variables (except constants) or functions, especially in OSGi. If you want to use any of them, it is time to ask if you should refactor your code.