1 19 20 package org.netbeans.core.startup; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.ArrayList ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Locale ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import java.util.jar.JarEntry ; 38 import java.util.jar.JarOutputStream ; 39 import java.util.jar.Manifest ; 40 import java.util.zip.CRC32 ; 41 import org.netbeans.InvalidException; 42 import org.netbeans.Module; 43 import org.netbeans.ModuleInstaller; 44 import org.netbeans.junit.NbTestCase; 45 import org.openide.filesystems.FileAttributeEvent; 46 import org.openide.filesystems.FileChangeListener; 47 import org.openide.filesystems.FileEvent; 48 import org.openide.filesystems.FileLock; 49 import org.openide.filesystems.FileObject; 50 import org.openide.filesystems.FileRenameEvent; 51 import org.openide.filesystems.Repository; 52 53 56 abstract class SetupHid extends NbTestCase { 57 58 public SetupHid(String name) { 59 super(name); 60 } 61 62 63 protected File jars; 64 65 protected void setUp() throws Exception { 66 Locale.setDefault(Locale.US); 67 jars = new File (ModuleManagerTest.class.getResource("jars").getFile()); 68 clearWorkDir(); 69 } 70 71 protected static void deleteRec(File f) throws IOException { 72 if (f.isDirectory()) { 73 File [] kids = f.listFiles(); 74 if (kids == null) throw new IOException ("Could not list: " + f); 75 for (int i = 0; i < kids.length; i++) { 76 deleteRec(kids[i]); 77 } 78 } 79 if (! f.delete()) throw new IOException ("Could not delete: " + f); 80 } 81 82 protected static void copyStreams(InputStream is, OutputStream os) throws IOException { 83 byte[] buf = new byte[4096]; 84 try { 85 int i; 86 while ((i = is.read(buf)) != -1) { 87 os.write(buf, 0, i); 88 } 89 } finally { 90 is.close(); 91 } 92 } 93 94 protected static void copy(File a, File b) throws IOException { 95 OutputStream os = new FileOutputStream (b); 96 try { 97 copyStreams(new FileInputStream (a), os); 98 } finally { 99 os.close(); 100 } 101 } 102 103 protected static void copy(File a, FileObject b) throws IOException { 104 OutputStream os = b.getOutputStream(); 105 try { 106 copyStreams(new FileInputStream (a), os); 107 } finally { 108 os.close(); 109 } 110 } 111 112 protected static String slurp(String path) throws IOException { 113 Main.getModuleSystem(); FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(path); 115 if (fo == null) return null; 116 InputStream is = fo.getInputStream(); 117 StringBuffer text = new StringBuffer ((int)fo.getSize()); 118 byte[] buf = new byte[1024]; 119 int read; 120 while ((read = is.read(buf)) != -1) { 121 text.append(new String (buf, 0, read, "US-ASCII")); 122 } 123 return text.toString(); 124 } 125 126 protected static class FakeModuleInstaller extends ModuleInstaller { 127 public final List <String > actions = new ArrayList <String >(); 129 public final List <Object > args = new ArrayList <Object >(); 130 public void clear() { 131 actions.clear(); 132 args.clear(); 133 } 134 public final Set <Module> delinquents = new HashSet <Module>(); 136 public final Set <Module> wontclose = new HashSet <Module>(); 138 public void prepare(Module m) throws InvalidException { 139 if (delinquents.contains(m)) throw new InvalidException(m, "not supposed to be installed"); 140 actions.add("prepare"); 141 args.add(m); 142 } 143 public void dispose(Module m) { 144 actions.add("dispose"); 145 args.add(m); 146 } 147 public void load(List <Module> modules) { 148 actions.add("load"); 149 args.add(new ArrayList <Module>(modules)); 150 } 151 public void unload(List <Module> modules) { 152 actions.add("unload"); 153 args.add(new ArrayList <Module>(modules)); 154 } 155 public boolean closing(List <Module> modules) { 156 actions.add("closing"); 157 args.add(new ArrayList <Module>(modules)); 158 Iterator <Module> it = modules.iterator(); 159 while (it.hasNext()) { 160 if (wontclose.contains(it.next())) return false; 161 } 162 return true; 163 } 164 public void close(List <Module> modules) { 165 actions.add("close"); 166 args.add(new ArrayList <Module>(modules)); 167 } 168 } 169 170 protected static final class FakeEvents extends org.netbeans.Events { 171 protected void logged(String message, Object [] args) { 172 } 175 } 176 177 protected static final class LoggedPCListener implements PropertyChangeListener { 178 private final Set <PropertyChangeEvent > changes = new HashSet <PropertyChangeEvent >(100); 179 public synchronized void propertyChange(PropertyChangeEvent evt) { 180 changes.add(evt); 181 notify(); 182 } 183 public synchronized void waitForChanges() throws InterruptedException { 184 wait(5000); 185 } 186 public synchronized boolean hasChange(Object source, String prop) { 187 for (PropertyChangeEvent ev : changes) { 188 if (source == ev.getSource ()) { 189 if (prop.equals (ev.getPropertyName ())) { 190 return true; 191 } 192 } 193 } 194 return false; 195 } 196 public synchronized boolean waitForChange(Object source, String prop) throws InterruptedException { 197 while (! hasChange(source, prop)) { 198 long start = System.currentTimeMillis(); 199 waitForChanges(); 200 if (System.currentTimeMillis() - start > 4000) { 201 return false; 203 } 204 } 205 return true; 206 } 207 } 208 209 protected static class LoggedFileListener implements FileChangeListener { 210 211 private final Set <String > files = new HashSet <String >(100); 212 private synchronized void change(FileEvent ev) { 213 files.add(ev.getFile().getPath()); 214 notify(); 215 } 216 public synchronized void waitForChanges() throws InterruptedException { 217 wait(5000); 218 } 219 public synchronized boolean hasChange(String fname) { 220 return files.contains(fname); 221 } 222 public synchronized boolean waitForChange(String fname) throws InterruptedException { 223 while (! hasChange(fname)) { 224 long start = System.currentTimeMillis(); 225 waitForChanges(); 226 if (System.currentTimeMillis() - start > 4000) { 227 return false; 229 } 230 } 231 return true; 232 } 233 public void fileDeleted(FileEvent fe) { 234 change(fe); 235 } 236 public void fileFolderCreated(FileEvent fe) { 237 change(fe); 238 } 239 public void fileDataCreated(FileEvent fe) { 240 change(fe); 241 } 242 public void fileAttributeChanged(FileAttributeEvent fe) { 243 } 245 public void fileRenamed(FileRenameEvent fe) { 246 change(fe); 247 } 248 public void fileChanged(FileEvent fe) { 249 change(fe); 250 } 251 } 252 253 259 public static void createJar(File jar, Map <String ,String > contents, Map <String ,String > manifest) throws IOException { 260 Manifest m = new Manifest (); 261 m.getMainAttributes().putValue("Manifest-Version", "1.0"); for (Map.Entry <String ,String > line : manifest.entrySet()) { 263 m.getMainAttributes().putValue(line.getKey(), line.getValue()); 264 } 265 jar.getParentFile().mkdirs(); 266 OutputStream os = new FileOutputStream (jar); 267 try { 268 JarOutputStream jos = new JarOutputStream (os, m); 269 Iterator it = contents.entrySet().iterator(); 270 while (it.hasNext()) { 271 Map.Entry entry = (Map.Entry ) it.next(); 272 String path = (String ) entry.getKey(); 273 byte[] data = ((String ) entry.getValue()).getBytes("UTF-8"); 274 JarEntry je = new JarEntry (path); 275 je.setSize(data.length); 276 CRC32 crc = new CRC32 (); 277 crc.update(data); 278 je.setCrc(crc.getValue()); 279 jos.putNextEntry(je); 280 jos.write(data); 281 } 282 jos.close(); 283 } finally { 284 os.close(); 285 } 286 } 287 288 } 289 | Popular Tags |