use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class AriesApplicationManagerImpl method install.
public AriesApplicationContext install(AriesApplication app) throws BundleException, ManagementException, ResolverException {
if (!app.isResolved()) {
app = resolve(app);
}
// Register an Application Repository for this application if none exists
String appScope = app.getApplicationMetadata().getApplicationScope();
ServiceReference[] ref = null;
try {
String filter = "(" + BundleRepository.REPOSITORY_SCOPE + "=" + appScope + ")";
ref = _bundleContext.getServiceReferences(BundleRepository.class.getName(), filter);
} catch (InvalidSyntaxException e) {
// Something went wrong attempting to find a service so we will act as if
// there is no existing service.
}
if (ref == null || ref.length == 0) {
Dictionary dict = new Hashtable();
dict.put(BundleRepository.REPOSITORY_SCOPE, appScope);
ServiceRegistration serviceReg = _bundleContext.registerService(BundleRepository.class.getName(), new ApplicationRepository(app), dict);
serviceRegistrations.put(app, serviceReg);
}
AriesApplicationContext result = _applicationContextManager.getApplicationContext(app);
// When installing bundles in the .eba file we use the jar url scheme. This results in a
// JarFile being held open, which is bad as on windows we cannot delete the .eba file
// so as a work around we open a url connection to one of the bundles in the eba and
// if it is a jar url we close the associated JarFile.
Iterator<BundleInfo> bi = app.getBundleInfo().iterator();
if (bi.hasNext()) {
String location = bi.next().getLocation();
if (location.startsWith("jar")) {
try {
URL url = new URL(location);
JarURLConnection urlc = (JarURLConnection) url.openConnection();
// Make sure that we pick up the cached version rather than creating a new one
urlc.setUseCaches(true);
urlc.getJarFile().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return result;
}
use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class OBRAriesResolver method getBundleInfo.
@Override
public BundleInfo getBundleInfo(String bundleSymbolicName, Version bundleVersion) {
Map<String, String> attribs = new HashMap<String, String>();
// bundleVersion is an exact version - so ensure right version filter is generated
VersionRange range = ManifestHeaderProcessor.parseVersionRange(bundleVersion.toString(), true);
attribs.put(Resource.VERSION, range.toString());
String filterString = ManifestHeaderProcessor.generateFilter(Resource.SYMBOLIC_NAME, bundleSymbolicName, attribs);
Resource[] resources;
try {
resources = repositoryAdmin.discoverResources(filterString);
if (resources != null && resources.length > 0) {
return toBundleInfo(resources[0], false);
} else {
return null;
}
} catch (InvalidSyntaxException e) {
log.error("Invalid filter", e);
return null;
}
}
use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class CmUtils method callPlugins.
private static void callPlugins(final BundleContext bundleContext, final Dictionary<String, Object> props, final ServiceReference sr, final String configPid, final String factoryPid) {
ServiceReference[] plugins = null;
try {
final String targetPid = (factoryPid == null) ? configPid : factoryPid;
String filter = "(|(!(cm.target=*))(cm.target=" + targetPid + "))";
plugins = bundleContext.getServiceReferences(ConfigurationPlugin.class.getName(), filter);
} catch (InvalidSyntaxException ise) {
// no filter, no exception ...
}
// abort early if there are no plugins
if (plugins == null || plugins.length == 0) {
return;
}
// sort the plugins by their service.cmRanking
if (plugins.length > 1) {
Arrays.sort(plugins, CM_RANKING);
}
// call the plugins in order
for (ServiceReference pluginRef : plugins) {
ConfigurationPlugin plugin = (ConfigurationPlugin) bundleContext.getService(pluginRef);
if (plugin != null) {
try {
plugin.modifyConfiguration(sr, props);
} catch (Throwable t) {
// Ignore
} finally {
// ensure ungetting the plugin
bundleContext.ungetService(pluginRef);
}
setAutoProperties(props, configPid, factoryPid);
}
}
}
use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class Helper method getOsgiService.
public static <T> T getOsgiService(BundleContext bundleContext, Class<T> type, String filter, long timeout) {
ServiceTracker tracker = null;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bundleContext, osgiFilter, null);
tracker.open(true);
// Note that the tracker is not closed to keep the reference
// This is buggy, as the service reference may change i think
Object svc = tracker.waitForService(timeout);
if (svc == null) {
Dictionary<?, ?> dic = bundleContext.getBundle().getHeaders();
System.err.println("Test bundle headers: " + explode(dic));
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
System.err.println("ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
System.err.println("Filtered ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
}
throw new RuntimeException("Gave up waiting for service " + flt);
}
return type.cast(svc);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class Helper method getOsgiServiceReference.
public static <T> ServiceReference getOsgiServiceReference(BundleContext bundleContext, Class<T> type, String filter, long timeout) {
ServiceTracker tracker = null;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bundleContext, osgiFilter, null);
tracker.open(true);
// Note that the tracker is not closed to keep the reference
// This is buggy, as the service reference may change i think
Object svc = tracker.waitForService(timeout);
if (svc == null) {
Dictionary<?, ?> dic = bundleContext.getBundle().getHeaders();
System.err.println("Test bundle headers: " + explode(dic));
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
System.err.println("ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
System.err.println("Filtered ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
}
throw new RuntimeException("Gave up waiting for service " + flt);
}
return tracker.getServiceReference();
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
Aggregations