Search in sources :

Example 1 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class NbLoaderPool method readPool.

/**
 * Reads loader from the input stream.
 * @param ois object input stream to read from
 */
private static synchronized void readPool(ObjectInputStream ois, NbLoaderPool pool) throws IOException, ClassNotFoundException {
    /*installBefores = (Map)*/
    ois.readObject();
    /*installAfters = (Map)*/
    ois.readObject();
    HashSet<Class> classes = new HashSet<Class>();
    LinkedList<DataLoader> l = new LinkedList<DataLoader>();
    Iterator<? extends ModuleInfo> mit = Lookup.getDefault().lookupAll(ModuleInfo.class).iterator();
    Map<String, ModuleInfo> modules = new HashMap<String, ModuleInfo>();
    while (mit.hasNext()) {
        ModuleInfo m = mit.next();
        modules.put(m.getCodeNameBase(), m);
    }
    for (; ; ) {
        Object o1 = ois.readObject();
        if (o1 == null) {
            if (err.isLoggable(Level.FINE))
                err.fine("reading null");
            break;
        }
        NbMarshalledObject obj;
        if (o1 instanceof String) {
            String name = (String) o1;
            if (name.length() > 0 && name.charAt(0) == '=') {
                // NOI18N
                // #27190: unmodified loader, just here for the ordering.
                String cname = name.substring(1);
                DataLoader dl = names2Loaders.get(cname);
                if (dl != null) {
                    if (err.isLoggable(Level.FINE))
                        err.fine("reading unmodified " + cname);
                    l.add(dl);
                    classes.add(dl.getClass());
                } else {
                    // No such known loaded - presumably disabled module.
                    if (err.isLoggable(Level.FINE))
                        err.fine("skipping unmodified nonexistent " + cname);
                }
                continue;
            }
            // Module information.
            int rel = ois.readInt();
            String spec = (String) ois.readObject();
            obj = (NbMarshalledObject) ois.readObject();
            ModuleInfo m = modules.get(name);
            if (m == null) {
                if (err.isLoggable(Level.FINE))
                    err.fine("No known module " + name + ", skipping loader");
                continue;
            }
            if (!m.isEnabled()) {
                if (err.isLoggable(Level.FINE))
                    err.fine("Module " + name + " is disabled, skipping loader");
                continue;
            }
            if (m.getCodeNameRelease() < rel) {
                if (err.isLoggable(Level.FINE))
                    err.fine("Module " + name + " is too old (major vers.), skipping loader");
                continue;
            }
            if (spec != null) {
                SpecificationVersion v = m.getSpecificationVersion();
                if (v == null || v.compareTo(new SpecificationVersion(spec)) < 0) {
                    if (err.isLoggable(Level.FINE))
                        err.fine("Module " + name + " is too old (spec. vers.), skipping loader");
                    continue;
                }
            }
            if (err.isLoggable(Level.FINE))
                err.fine("Module " + name + " is OK, will try to restore loader");
        } else {
            // Loader with no known module, or backward compatibility.
            obj = (NbMarshalledObject) o1;
        }
        Exception t = null;
        try {
            DataLoader loader = (DataLoader) obj.get();
            if (loader == null) {
                // issue 38658)
                continue;
            }
            Class<?> clazz = loader.getClass();
            if (err.isLoggable(Level.FINE))
                err.fine("reading modified " + clazz.getName());
            l.add(loader);
            classes.add(clazz);
        } catch (IOException ex) {
            t = ex;
        } catch (ClassNotFoundException ex) {
            t = ex;
        }
    }
    // Read system loaders. But not into any particular order.
    for (; ; ) {
        NbMarshalledObject obj = (NbMarshalledObject) ois.readObject();
        if (obj == null) {
            if (err.isLoggable(Level.FINE))
                err.fine("reading null");
            break;
        }
        Exception t = null;
        try {
            // Just reads its shared state, nothing more.
            DataLoader loader = (DataLoader) obj.get();
            if (err.isLoggable(Level.FINE))
                err.fine("reading " + loader.getClass().getName());
        } catch (IOException ex) {
            t = ex;
        } catch (ClassNotFoundException ex) {
            t = ex;
        }
    }
    if (err.isLoggable(Level.FINE))
        err.fine("done reading");
    // Explanation: modules are permitted to restoreDefault () before
    // the loader pool is de-externalized. This means that all loader manifest
    // sections will add a default-instance entry to the pool at startup
    // time. Later, when the pool is restored, this may reorder existing ones,
    // as well as change properties. But if any loader is missing (typically
    // due to failed deserialization), it will nonetheless be added to the end
    // now (and the pool resorted just in case).
    Iterator it = loaders.iterator();
    while (it.hasNext()) {
        DataLoader loader = (DataLoader) it.next();
        if (!classes.contains(loader.getClass())) {
            l.add(loader);
        }
    }
    // NOI18N
    if (l.size() > new HashSet<DataLoader>(l).size())
        throw new IllegalStateException("Duplicates in " + l);
    loaders = l;
    // Always "resort": if the existing order was in fact compatible with the
    // current install-befores/afters, then this is no op (besides firing an
    // update event). Cf. #29671.
    resort(pool);
}
Also used : SpecificationVersion(org.openide.modules.SpecificationVersion) HashMap(java.util.HashMap) IOException(java.io.IOException) LinkedList(java.util.LinkedList) IOException(java.io.IOException) DataLoader(org.openide.loaders.DataLoader) ModuleInfo(org.openide.modules.ModuleInfo) NbMarshalledObject(org.openide.util.io.NbMarshalledObject) Iterator(java.util.Iterator) FileObject(org.openide.filesystems.FileObject) NbMarshalledObject(org.openide.util.io.NbMarshalledObject) HashSet(java.util.HashSet)

Example 2 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class NbLoaderPool method add2Deps.

/**
 * Add to loader ordering dependencies.
 * Only pays attention to dependencies among loaders that actually exist.
 * @param deps a map from loaders to lists of loaders they must come before
 * @param orderings either {@link #installBefore} or {@link #installAfter}
 * @param before true if orderings refers to before, false if to after
 * @see Utilities#topologicalSort
 */
private static void add2Deps(Map<DataLoader, List<DataLoader>> deps, Map orderings, boolean before) {
    Iterator it = orderings.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry e = (Map.Entry) it.next();
        String loaderClassName = (String) e.getKey();
        DataLoader l = names2Loaders.get(loaderClassName);
        if (l == null) {
            // NOI18N
            throw new IllegalStateException("No such loader: " + loaderClassName);
        }
        String[] repClassNames = (String[]) e.getValue();
        if (repClassNames == null) {
            // NOI18N
            throw new IllegalStateException("Null Install-" + (before ? "Before" : "After") + " for " + loaderClassName);
        }
        for (int i = 0; i < repClassNames.length; i++) {
            String repClassName = repClassNames[i];
            DataLoader l2 = repNames2Loaders.get(repClassName);
            if (l2 != null) {
                if (before) {
                    addDep(deps, l, l2);
                } else {
                    addDep(deps, l2, l);
                }
            } else {
                l2 = names2Loaders.get(repClassName);
                if (l2 != null) {
                    warn(loaderClassName, repClassName, l2.getRepresentationClassName());
                }
            }
        }
    }
}
Also used : DataLoader(org.openide.loaders.DataLoader) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class NbLoaderPool method writePool.

/**
 * Stores all the objects into stream.
 * @param oos object output stream to write to
 */
private static synchronized void writePool(ObjectOutputStream oos, NbLoaderPool pool) throws IOException {
    if (err.isLoggable(Level.FINE))
        err.fine("writePool");
    // No longer bother storing these (#29671):
    oos.writeObject(new HashMap());
    oos.writeObject(new HashMap());
    Iterator it = loaders.iterator();
    while (it.hasNext()) {
        DataLoader l = (DataLoader) it.next();
        if (!isModified(l)) {
            // #27190 - no real need to write this in detail.
            String c = l.getClass().getName();
            if (err.isLoggable(Level.FINE))
                err.fine("writing unmodified " + c);
            // '=' not a permissible part of a cnb, so this distinguishes it
            // NOI18N
            oos.writeObject("=" + c);
            continue;
        }
        NbMarshalledObject obj;
        try {
            obj = new NbMarshalledObject(l);
        } catch (IOException ex) {
            err.log(Level.WARNING, null, ex);
            obj = null;
        }
        if (obj != null) {
            if (err.isLoggable(Level.FINE))
                err.fine("writing modified " + l.getClass().getName());
            // Find its module, if any.
            boolean found = false;
            ModuleInfo m = Modules.getDefault().ownerOf(l.getClass());
            if (m != null && m.isEnabled()) {
                if (err.isLoggable(Level.FINE))
                    err.fine("belongs to module: " + m.getCodeNameBase());
                oos.writeObject(m.getCodeNameBase());
                int r = m.getCodeNameRelease();
                // might be -1, note
                oos.writeInt(r);
                SpecificationVersion v = m.getSpecificationVersion();
                if (v != null) {
                    oos.writeObject(v.toString());
                } else {
                    oos.writeObject(null);
                }
                found = true;
            }
            if (!found) {
                if (err.isLoggable(Level.FINE))
                    err.fine("does not belong to any module");
            // just write the NbMarshalledObject<DataLoader> itself;
            // we need to support that for compatibility of old loader
            // pools anyway
            }
            oos.writeObject(obj);
        }
    }
    if (err.isLoggable(Level.FINE))
        err.fine("writing null");
    oos.writeObject(null);
    // Write out system loaders now:
    Enumeration e = pool.allLoaders();
    while (e.hasMoreElements()) {
        DataLoader l = (DataLoader) e.nextElement();
        if (loaders.contains(l))
            continue;
        if (!isModified(l)) {
            // #27190 again. No need to write anything
            String c = l.getClass().getName();
            if (err.isLoggable(Level.FINE))
                err.fine("skipping unmodified " + c);
            continue;
        }
        NbMarshalledObject obj;
        try {
            obj = new NbMarshalledObject(l);
        } catch (IOException ex) {
            err.log(Level.WARNING, null, ex);
            obj = null;
        }
        if (obj != null) {
            if (err.isLoggable(Level.FINE))
                err.fine("writing " + l.getClass().getName());
            // No associated module, no need to write such info.
            oos.writeObject(obj);
        }
    }
    if (err.isLoggable(Level.FINE))
        err.fine("writing null");
    oos.writeObject(null);
    if (err.isLoggable(Level.FINE))
        err.fine("done writing");
}
Also used : DataLoader(org.openide.loaders.DataLoader) Enumeration(java.util.Enumeration) SpecificationVersion(org.openide.modules.SpecificationVersion) HashMap(java.util.HashMap) NbMarshalledObject(org.openide.util.io.NbMarshalledObject) ModuleInfo(org.openide.modules.ModuleInfo) Iterator(java.util.Iterator) IOException(java.io.IOException)

Example 4 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class NbLoaderPool method propertyChange.

/**
 * Listener to property changes.
 */
public void propertyChange(PropertyChangeEvent ev) {
    DataLoader l = (DataLoader) ev.getSource();
    String prop = ev.getPropertyName();
    if (DataLoader.PROP_ACTIONS.equals(prop) && ev.getNewValue() == null) {
        // skip this change as this means the loader is using new storage mechanism
        return;
    }
    modifiedLoaders.add(l);
    if (err.isLoggable(Level.FINE))
        err.fine("Got change in " + l.getClass().getName() + "." + prop);
    if (DataLoader.PROP_ACTIONS.equals(prop) || DataLoader.PROP_DISPLAY_NAME.equals(prop))
        // these are not important to the pool, i.e. to file recognition
        return;
    if (installationFinished) {
        superFireChangeEvent();
    }
}
Also used : DataLoader(org.openide.loaders.DataLoader)

Example 5 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class NbLoaderPool method findServicesFolder.

private final DataFolder findServicesFolder() throws Exception {
    // NOI18N
    Method m = DataLoaderPool.class.getDeclaredMethod("getFolderLoader");
    m.setAccessible(true);
    DataLoader dl = (DataLoader) m.invoke(null);
    // NOI18N
    FileObject services = FileUtil.getConfigFile("Services");
    return (DataFolder) dl.findDataObject(services, new HashSet<FileObject>());
}
Also used : DataLoader(org.openide.loaders.DataLoader) Method(java.lang.reflect.Method) FileObject(org.openide.filesystems.FileObject) DataFolder(org.openide.loaders.DataFolder) HashSet(java.util.HashSet)

Aggregations

DataLoader (org.openide.loaders.DataLoader)8 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Iterator (java.util.Iterator)3 FileObject (org.openide.filesystems.FileObject)3 Enumeration (java.util.Enumeration)2 HashSet (java.util.HashSet)2 ModuleInfo (org.openide.modules.ModuleInfo)2 SpecificationVersion (org.openide.modules.SpecificationVersion)2 NbMarshalledObject (org.openide.util.io.NbMarshalledObject)2 File (java.io.File)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 BackingStoreException (java.util.prefs.BackingStoreException)1 Preferences (java.util.prefs.Preferences)1 ModeImpl (org.netbeans.core.windows.ModeImpl)1 WindowManagerImpl (org.netbeans.core.windows.WindowManagerImpl)1 ModeConfig (org.netbeans.core.windows.persistence.ModeConfig)1