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

The following examples show how to use org.osgi.framework.Bundle#getResource() . These examples are extracted from open source projects. 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
@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 2
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 3
Source Project: ovsdb   File: DockerOvs.java    License: 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 4
/**
 * 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 5
/**
 * 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 6
@Override
public URL getResource(String name) {

  URL resource = null;

  for (Bundle bundle : mBundles) {
    resource = bundle.getResource(name);
    if (resource != null) {
      break;
    }
  }
  return resource;
}
 
Example 7
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 8
Source Project: netbeans   File: OSGiClassLoader.java    License: 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 9
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 10
/**
 * 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 11
@Override
public <T> URL getResourceFromBundle(String type, Bundle b) {
    if (EmbeddedFelixFramework.isExtensionBundle(b)) {
        return getClass().getClassLoader().getResource(type);
    } else {
        return b.getResource(type);
    }
}
 
Example 12
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 13
Source Project: XPagesExtensionLibrary   File: Utils.java    License: 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 14
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 15
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 16
/**
 * 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 17
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 18
@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();
}
 
Example 19
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 20
private static boolean hasComponents(final Bundle bundle) {
  return bundle.getResource("META-INF/sisu/javax.inject.Named") != null;
}