Index: src/org/netbeans/core/modules/Module.java =================================================================== RCS file: /cvs/core/src/org/netbeans/core/modules/Module.java,v --- src/org/netbeans/core/modules/Module.java 14 Apr 2005 19:17:34 -0000 1.57 +++ src/org/netbeans/core/modules/Module.java 6 May 2005 18:21:13 -0000 @@ -19,6 +19,7 @@ // (NbBundle.getLocalizedValue is OK here.) import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.AllPermission; @@ -28,6 +29,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -43,6 +45,7 @@ import java.util.jar.JarFile; import java.util.jar.Manifest; import java.util.zip.ZipEntry; +import org.openide.xml.XMLUtil; import org.netbeans.JarClassLoader; import org.netbeans.ProxyClassLoader; import org.openide.ErrorManager; @@ -51,6 +54,13 @@ import org.openide.modules.SpecificationVersion; import org.openide.util.NbBundle; import org.openide.util.WeakSet; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; /** Object representing one module, possibly installed. * Responsible for opening of module JAR file; reading @@ -68,6 +78,19 @@ public static final String PROP_VALID = "valid"; // NOI18N public static final String PROP_PROBLEMS = "problems"; // NOI18N + private static final String ELEMENT_MODULES = "installed_modules"; // NOI18N + private static final String ELEMENT_MODULE = "module"; // NOI18N + private static final String ATTR_CODENAMEBASE = "codename"; // NOI18N + private static final String ELEMENT_VERSION = "module_version"; // NOI18N + private static final String ATTR_VERSION = "specification_version"; // NOI18N + private static final String ATTR_ORIGIN = "origin"; // NOI18N + private static final String ATTR_LAST = "last"; // NOI18N + private static final String ATTR_INSTALL = "install_time"; // NOI18N + private static final String ELEMENT_FILE = "file"; // NOI18N + private static final String ATTR_FILE_NAME = "name"; // NOI18N + private static final String ATTR_CRC = "crc"; // NOI18N + private static final String UPDATE_TRACKING = "update_tracking"; // NOI18N + /** manager which owns this module */ private final ModuleManager mgr; /** event logging (should not be much here) */ @@ -1076,8 +1099,124 @@ OneModuleClassLoader l = (OneModuleClassLoader)it.next(); l.releaseLocks(); } + Util.err.log ("Module " + getCodeNameBase () + " is being deleted. All module's file will be removed."); + removeModuleFiles (); moduleJARs.remove(jar); } + + // support for module uninstall + + private void removeModuleFiles () { + Iterator it = clusters ().iterator (); + while (it.hasNext ()) { + removeModuleFilesInCluster ((File)it.next ()); + } + return; + } + + private void removeModuleFilesInCluster (File cluster) { + File updateTracking = new File (cluster + File.separator + UPDATE_TRACKING); + if (!updateTracking.isDirectory ()) return; + File moduleUpdateTracking = new File (updateTracking, getCodeNameBase ().replace ('.', '-') + ".xml"); + Util.err.log (moduleUpdateTracking + " found."); + if (!moduleUpdateTracking.exists ()) return; + Set/**/ moduleFiles = getModuleFiles (moduleUpdateTracking); + Iterator it = moduleFiles.iterator (); + while (it.hasNext ()) { + String fileName = (String) it.next (); + File file = new File (cluster + File.separator + fileName); + assert file.exists () : "File " + file + " exists."; + Util.err.log ("File " + file + " is being deleted."); + file.delete (); + } + } + + // XXX replace with file locator + private static File getPlatformDir () { + return new File (System.getProperty ("netbeans.home")); // NOI18N + } + + private static List/**/ clusters () { + ArrayList/**/ files = new ArrayList (); + + File ud = new File (System.getProperty ("netbeans.user")); // NOI18N + files.add (ud); + + String dirs = System.getProperty ("netbeans.dirs"); // NOI18N + + if (dirs != null) { + Enumeration en = new StringTokenizer (dirs, File.pathSeparator); + while (en.hasMoreElements ()) { + File f = new File ((String)en.nextElement ()); + files.add (f); + } + } + + + File id = getPlatformDir (); + files.add (id); + + return java.util.Collections.unmodifiableList (files); + } + + private Set/**/ getModuleFiles (File moduleUpdateTracking) { + return readFromUpdateTracking (moduleUpdateTracking); + } + + private Set/**/ readFromUpdateTracking (File moduleUpdateTracking) { + Document document = null; + InputStream is; + try { + is = new FileInputStream (moduleUpdateTracking); + InputSource xmlInputSource = new InputSource (is); + document = XMLUtil.parse (xmlInputSource, false, false, null, org.openide.xml.EntityCatalog.getDefault ()); + if (is != null) { + is.close (); + } + } catch (SAXException saxe) { + ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, saxe); + return Collections.EMPTY_SET; + } catch (IOException ioe) { + ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ioe); + } + + assert document.getDocumentElement () != null : "File " + moduleUpdateTracking + " must contain element."; + return readModuleElement (document.getDocumentElement ()); + } + + private Set/**/ readModuleElement (Element element) { + Set/**/ files = new HashSet (); + assert ELEMENT_MODULE.equals (element.getTagName ()) : "The root element is: " + ELEMENT_MODULE + " but was: " + element.getTagName (); + NodeList listModuleVersions = element.getElementsByTagName (ELEMENT_VERSION); + for (int i = 0; i < listModuleVersions.getLength (); i++) { + files.addAll (readModuleVersion (listModuleVersions.item (i))); + } + return files; + } + + private Set/**/ readModuleVersion (Node version) { + Node attrLast = version.getAttributes ().getNamedItem (ATTR_LAST); + assert attrLast != null : "ELEMENT_VERSION must contain ATTR_LAST attribute."; + if (Boolean.valueOf (attrLast.getNodeValue ()).booleanValue ()) { + return readModuleFiles (version); + } else { + return Collections.EMPTY_SET; + } + } + + private Set/**/ readModuleFiles (Node version) { + Set/**/ files = new HashSet (); + NodeList fileNodes = version.getChildNodes (); + for (int i = 0; i < fileNodes.getLength (); i++) { + if (fileNodes.item (i).hasAttributes ()) { + NamedNodeMap map = fileNodes.item (i).getAttributes (); + files.add (map.getNamedItem (ATTR_FILE_NAME).getNodeValue ()); + } + } + return files; + } + + // end of module uninstall /** Get the JAR manifest. * Should never be null, even if disabled.