下面列出了org.osgi.framework.startlevel.FrameworkStartLevel#setStartLevel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Representation put(final Representation r,
final Variant variant) {
try {
final FrameworkStartLevelPojo sl = fromRepresentation(r, r.getMediaType());
final FrameworkStartLevel fsl = getFrameworkStartLevel();
if (sl.getStartLevel() != 0) {
fsl.setStartLevel(sl.getStartLevel());
}
if (sl.getInitialBundleStartLevel() != 0) {
fsl.setInitialBundleStartLevel(sl.getInitialBundleStartLevel());
}
return SUCCESS(Status.SUCCESS_NO_CONTENT);
} catch (final Exception e) {
return ERROR(e, variant);
}
}
public int cmdStartlevel(Dictionary<String, ?> opts,
Reader in,
PrintWriter out,
Session session)
{
final FrameworkStartLevel fsl = bc.getBundle(0)
.adapt(FrameworkStartLevel.class);
final String levelStr = (String) opts.get("level");
try {
if (levelStr != null) {
final int level = Integer.parseInt(levelStr);
fsl.setStartLevel(level);
} else {
out.println("current start level: " + fsl.getStartLevel());
out.println("initial bundle start level: "
+ fsl.getInitialBundleStartLevel());
}
return 0;
} catch (final Exception e) {
out.println("Failed to show/set startlevel=" + levelStr);
e.printStackTrace(out);
return -1;
}
}
/**
* Install selected features.
*/
private void installNexusFeatures(final String featureNames) throws Exception {
final Set<Feature> features = new LinkedHashSet<>();
for (final String name : Splitter.on(',').trimResults().omitEmptyStrings().split(featureNames)) {
final Feature feature = featuresService.getFeature(name);
if (feature != null) {
features.add(feature);
}
else {
log.warn("Missing: {}", name);
}
}
log.info("Installing: {}", features);
Set<String> featureIds = new LinkedHashSet<>(features.size());
for (final Feature f : features) {
// feature might already be installed in the cache; if so then skip installation
if (!featuresService.isInstalled(f)) {
featureIds.add(f.getId());
}
}
if (!featureIds.isEmpty()) {
// avoid auto-refreshing bundles as that could trigger unwanted restart/lifecycle events
EnumSet<Option> options = EnumSet.of(NoAutoRefreshBundles, NoAutoRefreshManagedBundles);
featuresService.installFeatures(featureIds, options);
}
log.info("Installed: {}", features);
// feature bundles have all been installed, so raise framework start level to finish activation
FrameworkStartLevel frameworkStartLevel = bundleContext.getBundle(0).adapt(FrameworkStartLevel.class);
if (frameworkStartLevel.getStartLevel() < NEXUS_PLUGIN_START_LEVEL) {
frameworkStartLevel.setStartLevel(NEXUS_PLUGIN_START_LEVEL, this);
// activation continues asynchronously in frameworkEvent method...
}
}
void setStartLevel(final int level)
{
final Thread t = new Thread() {
@Override
public void run()
{
final FrameworkStartLevel fsl = getFrameworkStartLevel();
if (null != fsl) {
fsl.setStartLevel(level);
}
}
};
t.start();
}
/**
* Create a new framework, initialize and start it. If the init
* parameter is true, or if there is no stored framework state, bundle
* Jar file data will be read from the "jars" folder of the provided
* AssetManager. If the init parameter is false and there is a stored
* framework state, the framework will be started and its state will
* be the stored state.
*
* @param localStorage Path to application's storage location in the
* file system, framework state will be stored here
* @param init If true, any stored framework state is ignored.
* @param am AssetManager for the application, will be used to read
* bundle Jar file data when the framework is started without
* using a stored state.
* @return a framework instance or null if the framework could not be
* started or initialized.
* @throws IOException if there was a problem reading bundle Jar file
* data or framework/system properties using the AssestManager.
*/
public static Framework newFramework(String localStorage,
boolean init,
AssetManager am)
throws IOException
{
config = getConfiguration(am);
String fwDir = localStorage + File.separator + FWDIR;
config.put(Constants.FRAMEWORK_STORAGE, fwDir);
boolean doInit = init || !new File(fwDir).exists();
if (doInit) {
config.put(Constants.FRAMEWORK_STORAGE_CLEAN,
Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
}
Framework fw = new Main().getFrameworkFactory().newFramework(config);
try {
fw.init();
int runlevel;
if (doInit) {
runlevel = scanBundles(fw, am);
saveRunlevel(runlevel, fwDir);
} else {
runlevel = loadRunlevel(fwDir);
}
fw.start();
// set target start level for framework start-up
final FrameworkStartLevel fsl = fw.adapt(FrameworkStartLevel.class);
if (fsl!=null) {
fsl.setStartLevel(runlevel);
}
} catch (BundleException be) {
Log.e(KfApk.class.getName(), "Failed to init/start framework", be);
return null;
}
return fw;
}
@Override
public void setStartLevel(int startlevel) {
FrameworkStartLevel fsl = getFrameworkStartLevel0();
fsl.setStartLevel(startlevel);
}
public void setFrameworkStartLevel(FrameworkStartLevelDTO startLevel) {
Bundle fw = context.getBundle(0);
FrameworkStartLevel fwsl = fw.adapt(FrameworkStartLevel.class);
fwsl.setInitialBundleStartLevel(startLevel.initialBundleStartLevel);
fwsl.setStartLevel(startLevel.startLevel);
}