|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.convertor; import java.io.IOException; import java.io.InputStream; import java.lang.ClassLoader; import java.net.URL; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.jar.Attributes; import java.util.jar.Manifest; import org.netbeans.api.convertor.ConvertorDescriptor; import org.netbeans.api.convertor.ConvertorException; import org.netbeans.api.convertor.Convertors; import org.netbeans.spi.convertor.Convertor; import org.openide.ErrorManager; import org.openide.modules.ModuleInfo; import org.openide.util.Lookup; import org.openide.util.LookupEvent; import org.openide.util.LookupListener; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * * @author David Konecny */ public class ConvertorsPool implements LookupListener { private static final ConvertorsPool DEFAULT = new ConvertorsPool(); private Set convertorDescriptors = new HashSet(); private boolean initialized = false; private Lookup.Result modules; private ConvertorsPool() { modules = Lookup.getDefault().lookup(new Lookup.Template(ModuleInfo.class)); modules.addLookupListener(this); } public static ConvertorsPool getDefault() { return DEFAULT; } public ConvertorDescriptor getReadConvertor(String namespace, String element) { assert namespace != null && element != null; initConvertors(); Iterator it = convertorDescriptors.iterator(); while (it.hasNext()) { ConvertorDescriptor cd = (ConvertorDescriptor)it.next(); if (namespace.equals(cd.getNamespace()) && element.equals(cd.getElementName())) { return cd; } } return null; } public ConvertorDescriptor getWriteConvertor(Object o) { initConvertors(); Convertor convertor = null; Class clazz = o.getClass(); Iterator it = convertorDescriptors.iterator(); while (it.hasNext()) { ConvertorDescriptor cd = (ConvertorDescriptor)it.next(); if (cd.getClassName() == null || (!cd.getClassName().equals(o.getClass().getName()))) { continue; } Class cls; try { cls = InstanceUtils.findClass(cd.getClassName()); if (!(cls.getClassLoader().loadClass(o.getClass().getName()) == o.getClass())) { ErrorManager.getDefault().log(ErrorManager.WARNING, "Object "+o+" cannot be stored by convertor "+cd+", because of classloader mismatch. Skipping convertor."); continue; } } catch (Exception e) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); continue; } if (clazz.equals(cls)) { return cd; } } return null; } public Set getDescriptors() { initConvertors(); return new HashSet(convertorDescriptors); } private synchronized void initConvertors() { if (initialized) { return; } loadConvertors(); initialized = true; } public synchronized void resultChanged(LookupEvent ev) { loadConvertors(); } private void loadConvertors() { Set old = convertorDescriptors; ClassLoader currentClassLoader = (ClassLoader)Lookup.getDefault().lookup(ClassLoader.class); Set convs = new HashSet(); Enumeration en = null; try { en = currentClassLoader.getResources("META-INF/MANIFEST.MF"); } catch (IOException ex) { ex.printStackTrace(); ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); return; } while (en.hasMoreElements ()) { URL u = (URL)en.nextElement(); Manifest mf; try { InputStream is = u.openStream(); try { mf = new Manifest(is); loadConvertors(mf, convs); } finally { is.close(); } } catch (IOException ex) { ErrorManager.getDefault().log(ErrorManager.ERROR, "Cannot read file "+u+". The file will be ignored."); } } convertorDescriptors = convs; Accessor.DEFAULT.firePropertyChange(Convertors.CONVERTOR_DESCRIPTORS, old, new HashSet(convertorDescriptors)); } private void loadConvertors(Manifest m, Set convs) { Iterator it = m.getEntries().entrySet().iterator(); // Iterator |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.