|
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-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.autoupdate; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.*; import java.util.jar.*; import org.openide.ErrorManager; import org.openide.filesystems.*; import org.openide.xml.XMLUtil; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** This class represents one module update available on the web * * @author akemr * @version */ class SafeModule extends Object { public static final String PUBLIC_ID = "-//NetBeans//DTD Module Status 1.0//EN"; // NOI18N public static final String SYSTEM_ID = "http://www.netbeans.org/dtds/module-status-1_0.dtd"; // NOI18N static final String PROP_AUTOLOAD = "autoload"; // NOI18N static final String PROP_EAGER = "eager"; // NOI18N private static final String PROP_SPEC = "specversion"; // NOI18N private static final String PROP_ENABLED = "enabled"; // NOI18N private static final String PROP_JAR = "jar"; // NOI18N private static final String PROP_ORIGIN = "origin"; // NOI18N private static final String PROP_USER = "user"; // NOI18N private static final String PROP_INSTALL = "installation"; // NOI18N private static final String PROP_RELEASE = "release"; // NOI18N private static final String PROP_RELOADABLE = "reloadable"; // NOI18N private static final String ELEMENT_MODULE = "module"; // NOI18N private static final String ATTR_NAME = "name"; // NOI18N private static final String ATTR_PARAM = "param"; // NOI18N private static final String AUTOLOAD_SLASH = "autoload/"; // NOI18N private static final String EAGER_SLASH = "eager/"; // NOI18N private static final String FILE_SEPARATOR = System.getProperty ("file.separator"); private static Map prepared = new HashMap(); private static boolean register( ModuleUpdate mu ) { boolean success = true; Iterator it = mu.getJarList().iterator(); while ( success && it.hasNext() ) { String name = (String)it.next(); success = register( mu, name ); } return success; } static boolean write( Map installNow ) { boolean success = true; prepared = new HashMap(); Iterator it = installNow.entrySet().iterator(); while ( success && it.hasNext() ) { Map.Entry me = (Map.Entry)it.next(); ModuleUpdate mu = (ModuleUpdate)me.getKey(); File f = (File)me.getValue(); if ( f.exists() ) { success = false; } else { success = register( mu ); } } if ( !success ) return false; try { FileSystem deffs = Repository.getDefault().getDefaultFileSystem(); deffs.runAtomicAction( new DocAtomicAction( deffs ) ); } catch ( IOException ioe ) { return false; } return true; } private static boolean register( ModuleUpdate mu, String jarName ) { Module module = new Module(); try { loadManifest( jarName, module, mu ); } catch ( IOException ioe ) { return false; } Map m = new HashMap(); m.put( "name", module.getName() ); // NOI18N boolean autoloaded = false; if ( jarName.startsWith( AUTOLOAD_SLASH ) ) { autoloaded = true; jarName = jarName.substring( AUTOLOAD_SLASH.length() ); } m.put( PROP_AUTOLOAD, autoloaded ? Boolean.TRUE : Boolean.FALSE ); boolean eagered = false; if ( jarName.startsWith( EAGER_SLASH ) ) { eagered = true; jarName = jarName.substring( EAGER_SLASH.length() ); } m.put( PROP_EAGER, eagered ? Boolean.TRUE : Boolean.FALSE ); if ( !autoloaded && !eagered ) m.put( PROP_ENABLED, Boolean.TRUE ); m.put( PROP_JAR, jarName ); String origin = ""; // NOI18N if ( mu.isToInstallDir() ) origin = PROP_INSTALL; else origin = PROP_USER; if ( autoloaded ) origin = origin + '/' + PROP_AUTOLOAD; else if ( eagered ) origin = origin + '/' + PROP_EAGER; m.put( PROP_ORIGIN, origin ); if ( module.getRelease() != null ) { m.put( PROP_RELEASE, module.getRelease() ); } m.put( PROP_RELOADABLE, Boolean.FALSE ); if ( module.getSpecVersion() != null ) { m.put( PROP_SPEC, module.getSpecVersion() ); } String nameDashes = module.getName().replace('.', '-'); // NOI18N prepared.put( nameDashes, m ); return true; } private static void loadManifest(String jar, Module module, ModuleUpdate mu) throws IOException { String path = null; if ( mu.isToInstallDir() ) path = mu.findInstallDirectory ().toString (); else path = System.getProperty ("netbeans.user"); path = path + FILE_SEPARATOR + "modules" + FILE_SEPARATOR + jar; JarFile jarFile = new JarFile( path ); try { Manifest m = jarFile.getManifest(); String name = m.getMainAttributes().getValue( "OpenIDE-Module" ); // NOI18N int slash = name.indexOf('/'); // NOI18N if ( slash > -1 ) { module.setRelease( name.substring( slash + 1 ) ); name = name.substring( 0, slash ); } module.setName( name ); String spec = m.getMainAttributes().getValue( "OpenIDE-Module-Specification-Version" ); // NOI18N module.setSpecVersion( spec ); } finally { jarFile.close(); } } static class Module extends Object { /** Holds value of property name. */ private String name; /** Holds value of property specVersion. */ private String specVersion; /** Holds value of property release. */ private String release; /** Getter for property name. * @return Value of property name. */ public String getName() { return name; } /** Setter for property name. * @param name New value of property name. */ public void setName(String name) { this.name = name; } /** Getter for property specVersion. * @return Value of property specVersion. */ public String getSpecVersion() { return this.specVersion; } /** Setter for property specVersion. * @param specVersion New value of property specVersion. */ public void setSpecVersion(String specVersion) { this.specVersion = specVersion; } /** Getter for property release. * @return Value of property release. */ public String getRelease() { return this.release; } /** Setter for property release. * @param release New value of property release. */ public void setRelease(String release) { this.release = release; } } private static class DocAtomicAction implements FileSystem.AtomicAction { private FileSystem fs; public DocAtomicAction(FileSystem fs) { this.fs = fs; } public void run() throws IOException { fs.refresh (true); FileObject sysm = fs.findResource( "Modules" ); // NOI18N Iterator it = prepared.entrySet().iterator(); while ( it.hasNext() ) { Map.Entry me = (Map.Entry)it.next(); String filename = (String)me.getKey(); Map m = (Map)me.getValue(); if (sysm.getFileObject (filename + ".xml") != null) { // if the file is already created, skip its creation continue; } FileObject fo = sysm.createData( filename + ".xml" ); // NOI18N FileLock lock = fo.lock(); OutputStream os = null; try { os = fo.getOutputStream( lock ); writeStatus( m, os ); } finally { if ( os != null ) os.close(); lock.releaseLock(); } } } /* copied from core.modules.ModuleList from performance reasons * see #27293 for details */ private void writeStatus(Map m, OutputStream os) throws IOException { String codeName = (String)m.get("name"); // NOI18N if (codeName == null) throw new IllegalArgumentException("no code name present"); // NOI18N Writer w = new OutputStreamWriter(os, "UTF-8"); // NOI18N w.write("\n"); // NOI18N w.write("\n"); // NOI18N w.write(" |
... 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.