Search in sources :

Example 6 with BundleException

use of org.osgi.framework.BundleException in project atlas by alibaba.

the class Framework method installNewBundle.

/**
     * install a bundle from input file, the file will move to bundle storage directory.
     *
     * @param location the bundle location.
     * @param file     the input file.
     * @return a Bundle object.
     * @throws BundleException if the installation failed.
     * @throws
     */
static BundleImpl installNewBundle(final String location, final File file) throws BundleException {
    File bundleDir = null;
    try {
        bundleDir = new File(STORAGE_LOCATION, location);
        if (!bundleDir.exists()) {
            bundleDir.mkdirs();
        }
        BundleLock.WriteLock(location);
        AtlasFileLock.getInstance().LockExclusive(bundleDir);
        /*
	         * <specs page="58">Every bundle is uniquely identified by its location string. If an installed bundle is using
	         * the specified location, the installBundle method must return the Bundle object for that installed bundle and
	         * not install a new bundle.</specs>
	         */
        final BundleImpl cached;
        if ((cached = (BundleImpl) getBundle(location)) != null) {
            return cached;
        }
        Log.e("BundleInstaller", "real install " + location);
        BundleImpl bundle = null;
        BundleListing.BundleInfo info = AtlasBundleInfoManager.instance().getBundleInfo(location);
        String version = info != null ? info.getVersion() : "-1";
        bundle = new BundleImpl(bundleDir, location, new BundleContext(), null, file, version, true, -1);
        storeMetadata();
        return bundle;
    } catch (IOException e) {
        BundleException e1 = new BundleException("Failed to install bundle." + FileUtils.getAvailableDisk(), e);
        if (bundleDir != null)
            Framework.deleteDirectory(bundleDir);
        if (FileUtils.getUsableSpace(Environment.getDataDirectory()) < LowDiskException.thredshold) {
            throw new LowDiskException(FileUtils.getAvailableDisk(), e);
        }
        throw new BundleException("Failed to install bundle.", e);
    } catch (BundleException e) {
        BundleException e1 = new BundleException("Failed to install bundle." + FileUtils.getAvailableDisk(), e);
        if (bundleDir != null)
            Framework.deleteDirectory(bundleDir);
        throw e1;
    } finally {
        BundleLock.WriteUnLock(location);
        if (bundleDir != null) {
            AtlasFileLock.getInstance().unLock(bundleDir);
        }
    }
}
Also used : BundleListing(android.taobao.atlas.bundleInfo.BundleListing) LowDiskException(android.taobao.atlas.runtime.LowDiskException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) File(java.io.File)

Example 7 with BundleException

use of org.osgi.framework.BundleException in project atlas by alibaba.

the class PatchInstaller method install.

public void install() throws BundleException {
    if (mergeOutputs.isEmpty()) {
        throw new BundleException("merge bundles is empty");
    }
    //
    //        buildBundleInventory(updateInfo);
    //
    //        File fileDir = new File(RuntimeVariables.androidApplication.getFilesDir(), "bundlelisting");
    //        String fileName = String.format("%s%s.json", new Object[]{"bundleInfo-", updateInfo.dexPatchVersion>0 ? updateInfo.dexPatchVersion+"" : updateInfo.updateVersion});
    //
    //        if (!fileDir.exists() || !new File(fileDir, fileName).exists()) {
    //            throw new BundleException("bundle file list is empty");
    //        }
    Iterator entries = mergeOutputs.entrySet().iterator();
    String[] packageName = new String[mergeOutputs.size()];
    File[] bundleFilePath = new File[mergeOutputs.size()];
    String[] versions = new String[mergeOutputs.size()];
    int index = 0;
    while (entries.hasNext()) {
        Map.Entry entry = (Map.Entry) entries.next();
        packageName[index] = (String) entry.getKey();
        Pair<String, String> bundlePair = (Pair<String, String>) entry.getValue();
        bundleFilePath[index] = new File(bundlePair.first);
        if (!bundleFilePath[index].exists()) {
            throw new BundleException("bundle input is wrong");
        }
        versions[index] = bundlePair.second;
        index++;
    }
    List<String> realInstalledBundle = Arrays.asList(packageName);
    for (UpdateInfo.Item bundle : updateInfo.updateBundles) {
        if (!realInstalledBundle.contains(bundle.name) && AtlasBundleInfoManager.instance().isInternalBundle(bundle.name)) {
            throw new BundleException("bundle  " + bundle.name + " is error");
        }
    }
    Atlas.getInstance().installOrUpdate(packageName, bundleFilePath, versions, updateInfo.dexPatchVersion);
    List<String> rollbackBundles = new ArrayList<String>();
    for (UpdateInfo.Item item : updateInfo.updateBundles) {
        if (item != null) {
            String bundleCodeVersion = item.version.split("@")[1];
            if (bundleCodeVersion.trim().equals("-1")) {
                rollbackBundles.add(item.name);
            }
        }
    }
    if (rollbackBundles.size() > 0) {
        Atlas.getInstance().restoreBundle(rollbackBundles.toArray(new String[rollbackBundles.size()]));
    }
    saveBaselineInfo(updateInfo, realInstalledBundle);
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) BundleException(org.osgi.framework.BundleException) File(java.io.File) Map(java.util.Map) UpdateInfo(com.taobao.atlas.update.model.UpdateInfo) Pair(android.util.Pair)

Example 8 with BundleException

use of org.osgi.framework.BundleException in project jersey by jersey.

the class ExtendedWadlWebappOsgiTest method testExtendedWadl.

/**
     * Test checks that the WADL generated using the WadlGenerator api doesn't
     * contain the expected text.
     *
     * @throws java.lang.Exception in case of a test error.
     */
@Test
public void testExtendedWadl() throws Exception {
    // TODO - temporary workaround
    // This is a workaround related to issue JERSEY-2093; grizzly (1.9.5) needs to have the correct context
    // class loader set
    ClassLoader myClassLoader = this.getClass().getClassLoader();
    for (Bundle bundle : bundleContext.getBundles()) {
        if ("webapp".equals(bundle.getSymbolicName())) {
            myClassLoader = bundle.loadClass("org.glassfish.jersey.examples.extendedwadl.resources.MyApplication").getClassLoader();
            break;
        }
    }
    Thread.currentThread().setContextClassLoader(myClassLoader);
    // END of workaround - the entire block can be deleted after grizzly is updated to recent version
    // List all the OSGi bundles
    StringBuilder sb = new StringBuilder();
    sb.append("-- Bundle list -- \n");
    for (Bundle b : bundleContext.getBundles()) {
        sb.append(String.format("%1$5s", "[" + b.getBundleId() + "]")).append(" ").append(String.format("%1$-70s", b.getSymbolicName())).append(" | ").append(String.format("%1$-20s", b.getVersion())).append(" |");
        try {
            b.start();
            sb.append(" STARTED  | ");
        } catch (BundleException e) {
            sb.append(" *FAILED* | ").append(e.getMessage());
        }
        sb.append(b.getLocation()).append("\n");
    }
    sb.append("-- \n\n");
    LOGGER.fine(sb.toString());
    final ResourceConfig resourceConfig = createResourceConfig();
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    final Client client = ClientBuilder.newClient();
    final Response response = client.target(baseUri).path("application.wadl").request(MediaTypes.WADL_TYPE).buildGet().invoke();
    String wadl = response.readEntity(String.class);
    LOGGER.info("RESULT = " + wadl);
    assertTrue("Generated wadl is of null length", !wadl.isEmpty());
    assertTrue("Generated wadl doesn't contain the expected text", wadl.contains("This is a paragraph"));
    assertFalse(wadl.contains("application.wadl/xsd0.xsd"));
    server.shutdownNow();
}
Also used : Response(javax.ws.rs.core.Response) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) HttpServer(org.glassfish.grizzly.http.server.HttpServer) BundleException(org.osgi.framework.BundleException) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 9 with BundleException

use of org.osgi.framework.BundleException in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkTest method shouldMarkThePluginAsInvalidIfAnyExceptionOccursAfterLoad.

@Test
public void shouldMarkThePluginAsInvalidIfAnyExceptionOccursAfterLoad() throws BundleException {
    final Bundle bundle = mock(Bundle.class);
    spy.addPluginChangeListener(new PluginChangeListener() {

        @Override
        public void pluginLoaded(GoPluginDescriptor pluginDescriptor) {
            throw new RuntimeException("some error");
        }

        @Override
        public void pluginUnLoaded(GoPluginDescriptor pluginDescriptor) {
        }
    });
    when(bundleContext.installBundle(any(String.class))).thenReturn(bundle);
    final GoPluginDescriptor goPluginDescriptor = new GoPluginDescriptor(TEST_SYMBOLIC_NAME, "1.0", null, "location", new File(""), false);
    spy.start();
    try {
        spy.loadPlugin(goPluginDescriptor);
        fail("should throw exception");
    } catch (Exception e) {
        assertTrue(goPluginDescriptor.getStatus().isInvalid());
    }
}
Also used : Bundle(org.osgi.framework.Bundle) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) File(java.io.File) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Test(org.junit.Test)

Example 10 with BundleException

use of org.osgi.framework.BundleException in project lwjgl by LWJGL.

the class Activator method start.

/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
@Override
public void start(BundleContext context) throws Exception {
    super.start(context);
    try {
        String path = LibraryPathUtil.getLWJGLLibraryPath(context);
        Status status = new Status(Status.INFO, PLUGIN_ID, Status.INFO, "Set org.lwjgl.librarypath to " + path, null);
        getLog().log(status);
    } catch (Throwable ex) {
        Status status = new Status(Status.ERROR, PLUGIN_ID, Status.ERROR, "Error setting native LWJGL libraries path: " + ex.toString(), ex);
        getLog().log(status);
        throw new BundleException(status.getMessage(), ex);
    }
}
Also used : Status(org.eclipse.core.runtime.Status) BundleException(org.osgi.framework.BundleException)

Aggregations

BundleException (org.osgi.framework.BundleException)99 Bundle (org.osgi.framework.Bundle)54 IOException (java.io.IOException)31 Test (org.junit.Test)19 File (java.io.File)15 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 FileInputStream (java.io.FileInputStream)9 BundleContext (org.osgi.framework.BundleContext)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Hashtable (java.util.Hashtable)5 Manifest (java.util.jar.Manifest)5 ServiceReference (org.osgi.framework.ServiceReference)5 Version (org.osgi.framework.Version)5 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)5 LowDiskException (android.taobao.atlas.runtime.LowDiskException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4