Java Code Examples for org.osgi.framework.Bundle#getResource()

The following examples show how to use org.osgi.framework.Bundle#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WizardTemplateChoicePage.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private URL getPreviewImageURL( String reportFileName, String key )
{
	URL url = null;

	Bundle bundle = Platform.getBundle( IResourceLocator.FRAGMENT_RESOURCE_HOST );
	if ( bundle == null )
	{
		return null;
	}
	url = bundle.getResource( key );

	if ( url == null )
	{
		String path = ReportPlugin.getDefault( ).getResourceFolder( );

		url = resolveURL( new File( path, key ) );

		if ( url == null )
		{
			url = resolveURL( new File( key ) );
		}
	}

	return url;
}
 
Example 2
Source File: TmfUITestPlugin.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Loads icons into the bundle's image registry
 *
 * @param bundle
 *          the bundle
 * @param url
 *          the icon url
 * @return the image
 */
public static @Nullable Image loadIcon(Bundle bundle, String url) {
    if (bundle == null) {
        return null;
    }
    Activator plugin = Activator.getDefault();
    String key = bundle.getSymbolicName() + "/" + url; //$NON-NLS-1$
    Image icon = plugin.getImageRegistry().get(key);
    if (icon == null) {
        URL imageURL = bundle.getResource(url);
        ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageURL);
        if (descriptor != null) {
            icon = descriptor.createImage();
            plugin.getImageRegistry().put(key, icon);
        }
    }
    return icon;
}
 
Example 3
Source File: BundleHttpContext.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * DOCUMENT ME!
 *
 * @param name DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public URL getResource(String name) {
    name = name.substring(1);

    String temp = name.substring(0, name.lastIndexOf("/"));
    String packageName = temp.replace('/', '.');

    for (int i = 0; i < exportedPackages.size(); i++) {
        Bundle b = ((Bundle) exportedPackages.get(i));
        ExportedPackage[] exp = packageAdmin.getExportedPackages(b);

        if (exp != null) {
            for (int j = 0; j < exp.length; j++) {
                if (exp[j].getName().equals(packageName)) {
                    return b.getResource(name);
                }
            }
        }
    }

    return null;
}
 
Example 4
Source File: DockerOvs.java    From ovsdb with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Since the docker-compose file is a resource in the bundle and docker-compose needs it.
 * in the file system, we copy it over - ugly but necessary.
 * @param yamlFileName File name
 * @return A File object for the newly created temporary yaml file.
 */
private File createTempDockerComposeFile(String yamlFileName) {
    Bundle bundle = FrameworkUtil.getBundle(this.getClass());
    Assert.assertNotNull("DockerOvs: bundle is null", bundle);
    URL url = bundle.getResource(envDockerComposeFile);
    Assert.assertNotNull("DockerOvs: URL is null", url);

    File tmpFile = null;
    try {
        tmpFile = File.createTempFile("ovsdb-it-tmp-", null);

        try (Reader in = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8);
                Writer out = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) {
            char[] buf = new char[1024];
            int read;
            while (-1 != (read = in.read(buf))) {
                out.write(buf, 0, read);
            }
        }

    } catch (IOException e) {
        Assert.fail(e.toString());
    }

    return tmpFile;
}
 
Example 5
Source File: BuiltInConfigurationType.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected URL resolveLocation(ICheckConfiguration checkConfiguration) {

  String contributorName = checkConfiguration.getAdditionalData().get(CONTRIBUTOR_KEY);

  Bundle contributor = Platform.getBundle(contributorName);
  URL locationUrl = FileLocator.find(contributor, new Path(checkConfiguration.getLocation()),
          null);

  // suggested by https://sourceforge.net/p/eclipse-cs/bugs/410/
  if (locationUrl == null) {
    locationUrl = contributor.getResource(checkConfiguration.getLocation());
  }

  return locationUrl;
}
 
Example 6
Source File: BundleClasspathUriResolver.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private URI findResourceInBundle(Bundle bundle, URI classpathUri)
        throws MalformedURLException, IOException {
    Path fullPath = new Path(classpathUri.path());
    if (bundle != null) {
        String projectRelativePath = "/" + fullPath;
        URL resourceUrl = bundle.getResource(projectRelativePath);
        if (resourceUrl != null) {
        	URL resolvedUrl = FileLocator.resolve(resourceUrl);
            URI normalizedURI = URI.createURI(
                    resolvedUrl.toString(), true);
            return normalizedURI;
        }
    }
    return classpathUri;
}
 
Example 7
Source File: ANBActivator.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public void start(BundleContext bundleContext) throws Exception {
	super.start(bundleContext);
	plugin = this;
	Bundle bundle = bundleContext.getBundle();
	IPHONE_PATH = new File(FileLocator.toFileURL(bundle.getResource("base")).getFile()).getAbsolutePath();
	if(bundle.getResource("base/load.ipa")==null){
		IPHONE_BASE=new File(FileLocator.toFileURL(bundle.getResource("base")).getFile()).getAbsolutePath()+"//load.ipa";
	}else{
		IPHONE_BASE = new File(FileLocator.toFileURL(bundle.getResource("base/load.ipa")).getFile()).getAbsolutePath();
	}
	IPHONE_VERSION_PATH = new File(FileLocator.toFileURL(bundle.getResource("base/version.txt")).getFile()).getAbsolutePath();
	IPHONE_CUSTOM_APPID = new File(FileLocator.toFileURL(bundle.getResource("base/customappid.txt")).getFile()).getAbsolutePath();
}
 
Example 8
Source File: AbstractButtonPart.java    From tesb-studio-se with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the image from bundle. Will return null without any Exception when
 * failed.
 *
 * @param bundle
 *            the bundle
 * @param path
 *            the path
 * @return the image from bundle
 */
protected static Image getImageFromBundle(String bundleId, String path) {
	try {
		Bundle bundle = Platform.getBundle(bundleId);
		URL resource = bundle.getResource(path);
		return ImageDescriptor.createFromURL(resource).createImage();
	} catch (Exception e) {
		// ignore when get icon failed.
	    ExceptionHandler.process(e);
	}
	return null;
}
 
Example 9
Source File: SyntheticBundleInstaller.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private static void addFileToArchive(Bundle bundle, String bundlePath, String fileInBundle,
        JarOutputStream jarOutputStream) throws IOException {
    String filePath = bundlePath + fileInBundle;
    URL resource = bundle.getResource(filePath);
    if (resource == null) {
        return;
    }
    ZipEntry zipEntry = new ZipEntry(fileInBundle);
    jarOutputStream.putNextEntry(zipEntry);
    resource.openStream().transferTo(jarOutputStream);
    jarOutputStream.closeEntry();
}
 
Example 10
Source File: PluginUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public static IFile copyFileToWorkspace(Plugin srcPlugin,
		String srcFilePath, IFile destFile) throws IOException,
		CoreException {
	Bundle bundle = srcPlugin.getBundle();
	URL bundleUrl = bundle.getResource(srcFilePath);
	URL fileUrl = FileLocator.toFileURL(bundleUrl);
	InputStream openStream = new BufferedInputStream(fileUrl.openStream());
	if (destFile.exists()) {
		destFile.delete(true, null);
	}
	destFile.create(openStream, true, null);
	return destFile;
}
 
Example 11
Source File: Utils.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
public static String getFileContents(final Bundle bundle, final String resName) throws Exception {
    URL resURL = bundle.getResource(resName);
    String content;
    
    InputStream is = resURL.openStream();
    try {
        java.util.Scanner s = new java.util.Scanner(is, "UTF-8").useDelimiter("\\A"); // $NON-NLS-1$ $NON-NLS-2$
        content = s.hasNext() ? s.next() : "";
    } finally {
        StreamUtil.close(is);
    }

    return content;
}
 
Example 12
Source File: EmbeddedFramework.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public <T> URL getResourceFromBundle(String type, Bundle b) {
    if (EmbeddedFelixFramework.isExtensionBundle(b)) {
        return getClass().getClassLoader().getResource(type);
    } else {
        return b.getResource(type);
    }
}
 
Example 13
Source File: ProjectUtils.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * Import the Eclipse projects found within the bundle containing {@code clazz} at the {@code
 * relativeLocation}. Return the list of projects imported.
 *
 * @throws IOException if the zip cannot be accessed
 * @throws CoreException if a project cannot be imported
 */
public static Map<String, IProject> importProjects(
    Class<?> clazz, String relativeLocation, boolean checkBuildErrors, IProgressMonitor monitor)
    throws IOException, CoreException {

  // Resolve the zip from within this bundle
  Bundle bundle = FrameworkUtil.getBundle(clazz);
  URL bundleLocation = bundle.getResource(relativeLocation);
  assertNotNull(bundleLocation);
  return importProjects(bundleLocation, checkBuildErrors, monitor);
}
 
Example 14
Source File: ChartExtensionPointManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public Map<String, Chart> getRegisteredCharts() {
	if (chartMap == null) {
		chartMap = new HashMap<>();
	}
	
	IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(extensionPointId);

	if (extensionPoint != null) {
		for (IExtension element : extensionPoint.getExtensions()) {
			String name = element.getContributor().getName();
			Bundle bundle = Platform.getBundle(name);

			for (IConfigurationElement ice : element.getConfigurationElements()) {
				String path = ice.getAttribute("json");
				if (path!= null) {
					// TODO: More validation is needed here, as it's very susceptible
					// to error. Only load the chart if it passes validation.
					URL url = bundle.getResource(path);
					JsonNode json;
					try {
						json = loadJsonFile(url);
					} catch (Exception e) {
						e.printStackTrace(); // FIXME
						continue;
					}
					chartMap.put(json.path("name").textValue(), new Chart(json));
				}
			}
		}
	}
	return chartMap;
}
 
Example 15
Source File: OSGiClassLoader.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected @Override URL findResource(String name) {
    for (Bundle b : bundles()) {
        URL resource = b.getResource(name);
        if (resource != null) {
            return resource;
        }
    }
    return super.findResource(name);
}
 
Example 16
Source File: SyntheticBundleInstaller.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private static Manifest getManifest(Bundle bundle, String bundlePath) throws IOException {
    String filePath = Paths.get(bundlePath, "META-INF/MANIFEST.MF").toString();
    URL resource = bundle.getResource(filePath);
    if (resource == null) {
        return null;
    }
    return new Manifest(resource.openStream());
}
 
Example 17
Source File: ExtensionClassLoader.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public URL getResource(String name) {

  URL resource = null;

  for (Bundle bundle : mBundles) {
    resource = bundle.getResource(name);
    if (resource != null) {
      break;
    }
  }
  return resource;
}
 
Example 18
Source File: RunAsAppFormMobileAction.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
public void run(IAction action) {
	CheckLoaderDialog loaderdialog = null;
	RunAsAppFormDeviceHandler deviceHandler=RunAsAppFormDeviceHandler.getInstance();
	List<AndroidDevice> aMobiles =deviceHandler.getAndroidDevice();
	List<IOSDevice> iMobiles =deviceHandler.getIosDevice();
	Activator
	.getDefault()
	.getLog()
	.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, 
			"when sync,  found ios device num = " + iMobiles.size(), null));
	if(aMobiles.size()<=0&&iMobiles.size()<=0){
		DeviceNotFoundDialog dialog = new DeviceNotFoundDialog(Display
				.getDefault().getActiveShell());
		dialog.open();
		return;
	}

	Bundle androidbundle = Platform
			.getBundle("com.apicloud.loader.platforms.android");
	Bundle iosbundle = Platform.getBundle("com.apicloud.loader.platforms.ios");

	CUSTOM_ANDROID_BASE = IDEUtil.getInstallPath() + "apploader/"
			+ getID() + "/load.apk";
	File appaloaderFile = new File(CUSTOM_ANDROID_BASE);
	if (!appaloaderFile.exists()) {
		setHasANDROIDAppLoader(false);
	} else {
		setHasANDROIDAppLoader(true);
	}

	CuSTOm_IOSROID_BASE = IDEUtil.getInstallPath() + "apploader/"
			+ getID() + "/load.ipa";
	File appiloaderFile = new File(CuSTOm_IOSROID_BASE);
	if (!appiloaderFile.exists()) {
		setHasIOSAppLoader(false);
	} else {
		setHasIOSAppLoader(true);
	}

	if (androidbundle.getResource("base/load.apk") == null) {
		setHasANDROIDBaseLoader(false);
	} else {
		setHasANDROIDBaseLoader(true);
	}
	if (iosbundle.getResource("base/load.ipa") == null) {
		setHasIOSBaseLoader(false);
	} else {
		setHasIOSBaseLoader(true);
	}

	if (hasANDROIDLoader() || hasIOSLoader()) {
		if (hasANDROIDLoader() && hasIOSLoader()) {
			loaderdialog = new CheckLoaderDialog(Display.getCurrent()
					.getActiveShell(), ALLLOADER, select);
		} else if (hasANDROIDLoader() && (!hasIOSLoader())) {
			loaderdialog = new CheckLoaderDialog(Display.getCurrent()
					.getActiveShell(), ALOADER, select);
		} else {
			loaderdialog = new CheckLoaderDialog(Display.getCurrent()
					.getActiveShell(), ILOADER, select);
		}
		loaderdialog.open();
		return;
	}

	Object obj = select.getFirstElement();

	Shell shell = Display.getDefault().getActiveShell();
	final SyncApplicationDialog sad = new SyncApplicationDialog(shell,
			aMobiles, iMobiles, obj);
	final CountDownLatch threadSignal = new CountDownLatch(aMobiles.size()
			+ iMobiles.size());
	sad.open();
	sad.run(threadSignal);
	Job job = new WorkspaceJob("") {
		@Override
		public IStatus runInWorkspace(IProgressMonitor monitor)
				throws CoreException {
			try {
				threadSignal.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			sad.finish();
			return Status.OK_STATUS;
		}
	};
	job.schedule();

}
 
Example 19
Source File: NexusBundleTracker.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean hasComponents(final Bundle bundle) {
  return bundle.getResource("META-INF/sisu/javax.inject.Named") != null;
}
 
Example 20
Source File: PackageAppItem.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run(IAction action) {
	
	CheckLoaderDialog loaderdialog = null;
	
	Bundle androidbundle = Platform
			.getBundle("com.apicloud.loader.platforms.android");
	Bundle iosbundle=Platform.getBundle("com.apicloud.loader.platforms.ios");
	
	 CUSTOM_ANDROID_BASE = IDEUtil.getInstallPath() + "apploader/load.apk";
	    File appaloaderFile=new File(CUSTOM_ANDROID_BASE);
	    if(!appaloaderFile.exists()){
	    	setHasANDROIDAppLoader(false);
	    }else{
	    setHasANDROIDAppLoader(true);
	    }
	    
	    CuSTOm_IOSROID_BASE = IDEUtil.getInstallPath() + "apploader/load.ipa";
	    File appiloaderFile=new File(CuSTOm_IOSROID_BASE);
	    if(!appiloaderFile.exists()){
	    	setHasIOSAppLoader(false);
	    }else{
	    	setHasIOSAppLoader(true);
	    }
	
	if (androidbundle.getResource("base/load.apk") == null) {
		setHasANDROIDBaseLoader(false);
	}else{
		setHasANDROIDBaseLoader(true);
	} if(iosbundle.getResource("base/load.ipa")==null){
		setHasIOSBaseLoader(false);
	}else{
		setHasIOSBaseLoader(true);
	}

	if (hasANDROIDLoader() || hasIOSLoader()) {

		if (hasANDROIDLoader() && hasIOSLoader()) {
			loaderdialog = new CheckLoaderDialog(Display.getCurrent()
					.getActiveShell(), ALLLOADER,select);
		} else if (hasANDROIDLoader() && (!hasIOSLoader())) {
			loaderdialog = new CheckLoaderDialog(Display.getCurrent()
					.getActiveShell(), ALOADER,select);
		} else {
			loaderdialog = new CheckLoaderDialog(Display.getCurrent()
					.getActiveShell(), ILOADER,select);
		}

		loaderdialog.open();
		return;
	}

	IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
	projects = FilterProject(projects);
	if(projects.length == 0) {
		MessageDialog.openError(null, Messages.AddFeatureDialog_INFORMATION, 
				Messages.CREATEAPP);
		return;
	}
	PackageAppItemDialog dialog = new PackageAppItemDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), projects);
	dialog.open();
}