Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
#481650 Fixed bug with wrong bundle initial start level
Browse files Browse the repository at this point in the history
* added test cases to check start level
* set current init level also before start of bundle
* minor refactorings of XargsFileLauncher

Change-Id: I671533016aba0487914ddc79852bb12a48c2deae
Signed-off-by: Jochen Hiller <j.hiller@telekom.de>
  • Loading branch information
Jochen Hiller committed Nov 7, 2015
1 parent a4c2c1b commit d14501c
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 83 deletions.
2 changes: 1 addition & 1 deletion distribution/src/main/dist/samples/equinox-console.xargs
@@ -1,7 +1,7 @@
# xargs sample file to load Equinox Console

# uncomment to clean storage first
-Dorg.osgi.framework.storage.clean=onFirstInit
# -Dorg.osgi.framework.storage.clean=onFirstInit

# use our own profile
-Dorg.eclipse.concierge.profile=equinox-console
Expand Down
21 changes: 15 additions & 6 deletions distribution/src/main/dist/samples/felix.xargs
@@ -1,4 +1,5 @@
# xargs sample file to load some Felix bundles
# We will start the bundles in different start levels to show that capability

# uncomment to clean storage first
# -Dorg.osgi.framework.storage.clean=onFirstInit
Expand All @@ -7,17 +8,25 @@
-Dorg.eclipse.concierge.profile=felix

# repos to load bundles from
-Drepo=https://archive.apache.org/dist/felix/
-Drepo=https://archive.apache.org/dist/felix

# load bundles
# load shell with start level 1
-initlevel 1
-istart bundles/org.eclipse.concierge.shell-5.0.0.*.jar

# start DS with start level 2
-initlevel 2
-istart ${repo}/org.apache.felix.scr-1.8.0.jar
-istart ${repo}/org.apache.felix.eventadmin-1.4.2.jar

# start other services with level 3
# First install (will use current start level 2), then start with level 3
-install ${repo}/org.apache.felix.eventadmin-1.4.2.jar
-install ${repo}/org.apache.felix.metatype-1.0.12.jar
-install ${repo}/org.apache.felix.configadmin-1.8.4.jar

-level 1
-initlevel 3
-start ${repo}/org.apache.felix.eventadmin-1.4.2.jar
-start ${repo}/org.apache.felix.metatype-1.0.12.jar

-level 2
-start ${repo}/org.apache.felix.configadmin-1.8.4.jar

# check in shell with command "startlevel <bundle-id>"
Expand Up @@ -32,20 +32,21 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.concierge.BundleImpl;
import org.eclipse.concierge.BundleImpl.Revision;
import org.eclipse.concierge.Concierge;
import org.eclipse.concierge.Factory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.startlevel.BundleStartLevel;
import org.osgi.framework.wiring.BundleRevision;

public class XargsFileLauncher {

protected static final boolean WIN = System.getProperty("os.name").toLowerCase().startsWith("win");

protected static final boolean WIN = System.getProperty("os.name")
.toLowerCase().startsWith("win");

/**
* process an init.xargs-style file.
*
Expand All @@ -58,12 +59,13 @@ public class XargsFileLauncher {
* if something goes wrong. For example, if strict startup is
* set and the installation of a bundle fails.
*/
public Concierge processXargsFile(final File file) throws BundleException,
FileNotFoundException {
public Concierge processXargsFile(final File file)
throws BundleException, FileNotFoundException {
InputStream inputStream = new FileInputStream(file);
// we have to preserve the properties for later variable and wildcard
// replacement
final Map<String, String> passedProperties = getPropertiesFromXargsInputStream(inputStream);
final Map<String, String> passedProperties = getPropertiesFromXargsInputStream(
inputStream);

// now process again for install/start options with given properties
inputStream = new FileInputStream(file);
Expand All @@ -72,8 +74,8 @@ public Concierge processXargsFile(final File file) throws BundleException,

public Concierge processXargsInputStream(
final Map<String, String> passedProperties,
final InputStream inputStream) throws BundleException,
FileNotFoundException {
final InputStream inputStream)
throws BundleException, FileNotFoundException {

// create framework with given properties
final Concierge concierge = (Concierge) new Factory()
Expand All @@ -88,13 +90,11 @@ public Concierge processXargsInputStream(
if (concierge.restart) {
return concierge;
}

final BundleContext context = concierge.getBundleContext();

int maxLevel = 1;
final BundleContext context = concierge.getBundleContext();

final BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
final BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));

try {
final HashMap<String, Bundle> memory = new HashMap<String, Bundle>(
Expand All @@ -112,9 +112,6 @@ public Concierge processXargsInputStream(
} else if (token.startsWith("-initlevel")) {
token = getArg(token, 10);
initLevel = Integer.parseInt(token);
if (initLevel > maxLevel) {
maxLevel = initLevel;
}
continue;
} else if (token.startsWith("-all")) {
token = getArg(token, 4);
Expand All @@ -127,64 +124,79 @@ public Concierge processXargsInputStream(
}
final File files[];
files = jardir.listFiles(new FilenameFilter() {
public boolean accept(File arg0, String arg1) {
return arg1.toLowerCase().endsWith(".jar")
|| arg1.toLowerCase().endsWith(".zip");
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jar")
|| name.toLowerCase().endsWith(".zip");
}
});
if (files == null) {
logError("NO FILES FOUND IN "
+ concierge.BUNDLE_LOCATION);
logError("NO FILES FOUND IN " + jardir.getPath());
break;
}
// first install all bundles
final List<Bundle> bundlesToStart = new ArrayList<Bundle>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
continue;
}
final BundleImpl b = (BundleImpl) context
final Bundle b = (Bundle) context
.installBundle(files[i].getPath());
b.setStartLevel(initLevel);
// adapt to BundleStartLevel
final BundleStartLevel bundleStartLevel = b
.adapt(BundleStartLevel.class);
bundleStartLevel.setStartLevel(initLevel);
bundlesToStart.add(b);
}
// TODO start bundles in start level order?
// then start all bundles (if not a fragment)
for (Iterator<Bundle> iter = bundlesToStart.iterator(); iter
.hasNext();) {
Bundle b = iter.next();
// is it a fragment?
final Revision rev = (Revision) b
.adapt(BundleRevision.class);
if (!rev.isFragment()) {
b.start();
}
}

continue;
} else if (token.startsWith("-istart")) {
token = getArg(token, 7);
token = replaceVariable(token, passedProperties);
token = resolveWildcardName(token);
final BundleImpl bundle = (BundleImpl) context
.installBundle(token);
bundle.setStartLevel(initLevel);
String bundleLocation = getArg(token, 7);
bundleLocation = replaceVariable(bundleLocation,
passedProperties);
bundleLocation = resolveWildcardName(bundleLocation);
final Bundle bundle = context.installBundle(bundleLocation);
// adapt to BundleStartLevel
final BundleStartLevel bundleStartLevel = bundle
.adapt(BundleStartLevel.class);
bundleStartLevel.setStartLevel(initLevel);
bundle.start();
} else if (token.startsWith("-install")) {
token = getArg(token, 8);
token = replaceVariable(token, passedProperties);
token = resolveWildcardName(token);
final BundleImpl bundle = (BundleImpl) context
.installBundle(token);
bundle.setStartLevel(initLevel);
memory.put(token, bundle);
String bundleLocation = getArg(token, 8);
bundleLocation = replaceVariable(bundleLocation,
passedProperties);
bundleLocation = resolveWildcardName(bundleLocation);
final Bundle bundle = context.installBundle(bundleLocation);
// adapt to BundleStartLevel
final BundleStartLevel bundleStartLevel = bundle
.adapt(BundleStartLevel.class);
bundleStartLevel.setStartLevel(initLevel);
memory.put(bundleLocation, bundle);
} else if (token.startsWith("-start")) {
token = getArg(token, 6);
token = replaceVariable(token, passedProperties);
token = resolveWildcardName(token);
final Bundle bundle = (Bundle) memory.remove(token);
String bundleLocation = getArg(token, 6);
bundleLocation = replaceVariable(bundleLocation,
passedProperties);
bundleLocation = resolveWildcardName(bundleLocation);
final Bundle bundle = memory.remove(bundleLocation);
if (bundle == null) {
logError("Bundle " + token
logError("Bundle " + bundleLocation
+ " is marked to be started but has not been "
+ "installed before. Ignoring the command !");
} else {
// set start level again in case it has been changed
// meanwhile
final BundleStartLevel bundleStartLevel = bundle
.adapt(BundleStartLevel.class);
bundleStartLevel.setStartLevel(initLevel);
bundle.start();
}
} else if (token.startsWith("-skip")) {
Expand All @@ -211,8 +223,8 @@ public boolean accept(File arg0, String arg1) {
public Map<String, String> getPropertiesFromXargsInputStream(
final InputStream inputStream) {
final Map<String, String> properties = new HashMap<String, String>();
final BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
final BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));

try {
String line;
Expand Down Expand Up @@ -259,8 +271,8 @@ public Map<String, String> getPropertiesFromXargsInputStream(
value = replaceVariable(value, properties);
if (doAdd) {
String oldValue = properties.get(key);
properties.put(key, (oldValue == null ? ""
: oldValue) + value);
properties.put(key,
(oldValue == null ? "" : oldValue) + value);
} else {
properties.put(key, value);
}
Expand Down Expand Up @@ -344,20 +356,20 @@ String resolveWildcardName(final String bundleName) {
return bundleName;
}
// TODO how to check http protocol?
final File dir = new File(bundleName.substring(0,
bundleName.lastIndexOf("/")));
final File dir = new File(
bundleName.substring(0, bundleName.lastIndexOf("/")));
// try to use a file filter
final FileFilter filter = new FileFilter() {
public boolean accept(final File pathname) {
final String preStar = bundleName.substring(0,
bundleName.lastIndexOf("*"));
final String postStar = bundleName.substring(bundleName
.lastIndexOf("*") + 1);
final String path = WIN ? pathname.getPath().replace('\\', '/') : pathname.getPath();

return path.startsWith(preStar)
&& path.endsWith(postStar);
final String postStar = bundleName
.substring(bundleName.lastIndexOf("*") + 1);

final String path = WIN ? pathname.getPath().replace('\\', '/')
: pathname.getPath();

return path.startsWith(preStar) && path.endsWith(postStar);
}
};
final File foundFiles[] = dir.listFiles(filter);
Expand All @@ -366,6 +378,7 @@ public boolean accept(final File pathname) {
} else if (foundFiles.length == 1) {
return foundFiles[0].getPath(); // exact match
} else if (foundFiles.length > 1) {
// sort the list of found files, takes the "newest" one
final ArrayList<String> sortedFiles = new ArrayList<String>();
for (int i = 0; i < foundFiles.length; i++) {
sortedFiles.add(foundFiles[i].getPath());
Expand Down
Expand Up @@ -25,7 +25,7 @@
/**
* Tests the XargsLauncher with its pattern replacement and wildcard support.
*/
public class XargsFileLauncherTest {
public class XargsFileLauncherPropertiesTest {

@Test
public void testConstructor() {
Expand Down

0 comments on commit d14501c

Please sign in to comment.