Search in sources :

Example 1 with StorageHookFactory

use of org.eclipse.osgi.internal.hookregistry.StorageHookFactory in project rt.equinox.framework by eclipse.

the class Storage method saveStorageHookData.

private void saveStorageHookData(DataOutputStream out, List<Generation> generations) throws IOException {
    List<StorageHookFactory<?, ?, ?>> factories = getConfiguration().getHookRegistry().getStorageHookFactories();
    out.writeInt(factories.size());
    for (StorageHookFactory<?, ?, ?> factory : factories) {
        out.writeUTF(factory.getKey());
        out.writeInt(factory.getStorageVersion());
        // create a temporary in memory stream so we can figure out the length
        ByteArrayOutputStream tempBytes = new ByteArrayOutputStream();
        DataOutputStream temp = new DataOutputStream(tempBytes);
        try {
            Object saveContext = factory.createSaveContext();
            for (Generation generation : generations) {
                if (generation.getBundleInfo().getBundleId() == 0) {
                    // ignore system bundle
                    continue;
                }
                @SuppressWarnings({ "rawtypes", "unchecked" }) StorageHook<Object, Object> hook = generation.getStorageHook((Class) factory.getClass());
                hook.save(saveContext, temp);
            }
        } finally {
            temp.close();
        }
        out.writeInt(tempBytes.size());
        out.write(tempBytes.toByteArray());
    }
}
Also used : Generation(org.eclipse.osgi.storage.BundleInfo.Generation) StorageHookFactory(org.eclipse.osgi.internal.hookregistry.StorageHookFactory) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with StorageHookFactory

use of org.eclipse.osgi.internal.hookregistry.StorageHookFactory in project rt.equinox.framework by eclipse.

the class Storage method loadStorageHookData.

private void loadStorageHookData(List<Generation> generations, DataInputStream in) throws IOException {
    List<StorageHookFactory<?, ?, ?>> factories = new ArrayList<>(getConfiguration().getHookRegistry().getStorageHookFactories());
    Map<Generation, List<StorageHook<?, ?>>> hookMap = new HashMap<>();
    int numFactories = in.readInt();
    for (int i = 0; i < numFactories; i++) {
        String factoryName = in.readUTF();
        int version = in.readInt();
        StorageHookFactory<Object, Object, StorageHook<Object, Object>> factory = null;
        for (Iterator<StorageHookFactory<?, ?, ?>> iFactories = factories.iterator(); iFactories.hasNext(); ) {
            @SuppressWarnings("unchecked") StorageHookFactory<Object, Object, StorageHook<Object, Object>> next = (StorageHookFactory<Object, Object, StorageHook<Object, Object>>) iFactories.next();
            if (next.getKey().equals(factoryName)) {
                factory = next;
                iFactories.remove();
                break;
            }
        }
        int dataSize = in.readInt();
        byte[] bytes = new byte[dataSize];
        in.readFully(bytes);
        if (factory != null) {
            DataInputStream temp = new DataInputStream(new ByteArrayInputStream(bytes));
            try {
                if (factory.isCompatibleWith(version)) {
                    Object loadContext = factory.createLoadContext(version);
                    for (Generation generation : generations) {
                        if (generation.getBundleInfo().getBundleId() == 0) {
                            // ignore system bundle
                            continue;
                        }
                        StorageHook<Object, Object> hook = factory.createStorageHookAndValidateFactoryClass(generation);
                        hook.load(loadContext, temp);
                        getHooks(hookMap, generation).add(hook);
                    }
                } else {
                    // recover by reinitializing the hook
                    for (Generation generation : generations) {
                        if (generation.getBundleInfo().getBundleId() == 0) {
                            // ignore system bundle
                            continue;
                        }
                        StorageHook<Object, Object> hook = factory.createStorageHookAndValidateFactoryClass(generation);
                        hook.initialize(generation.getHeaders());
                        getHooks(hookMap, generation).add(hook);
                    }
                }
            } catch (BundleException e) {
                throw new IOException(e);
            } finally {
                temp.close();
            }
        }
    }
    // now we need to recover for any hooks that are left
    for (Iterator<StorageHookFactory<?, ?, ?>> iFactories = factories.iterator(); iFactories.hasNext(); ) {
        @SuppressWarnings("unchecked") StorageHookFactory<Object, Object, StorageHook<Object, Object>> next = (StorageHookFactory<Object, Object, StorageHook<Object, Object>>) iFactories.next();
        // recover by reinitializing the hook
        for (Generation generation : generations) {
            if (generation.getBundleInfo().getBundleId() == 0) {
                // ignore system bundle
                continue;
            }
            StorageHook<Object, Object> hook = next.createStorageHookAndValidateFactoryClass(generation);
            try {
                hook.initialize(generation.getHeaders());
                getHooks(hookMap, generation).add(hook);
            } catch (BundleException e) {
                throw new IOException(e);
            }
        }
    }
    // now set the hooks to the generations
    for (Generation generation : generations) {
        generation.setStorageHooks(Collections.unmodifiableList(getHooks(hookMap, generation)), false);
    }
}
Also used : StorageHookFactory(org.eclipse.osgi.internal.hookregistry.StorageHookFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) StorageHook(org.eclipse.osgi.internal.hookregistry.StorageHookFactory.StorageHook) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) MRUBundleFileList(org.eclipse.osgi.storage.bundlefile.MRUBundleFileList) List(java.util.List) BundleException(org.osgi.framework.BundleException)

Aggregations

StorageHookFactory (org.eclipse.osgi.internal.hookregistry.StorageHookFactory)2 Generation (org.eclipse.osgi.storage.BundleInfo.Generation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 StorageHook (org.eclipse.osgi.internal.hookregistry.StorageHookFactory.StorageHook)1 MRUBundleFileList (org.eclipse.osgi.storage.bundlefile.MRUBundleFileList)1 BundleException (org.osgi.framework.BundleException)1