KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > SetupHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.jar.JarEntry JavaDoc;
38 import java.util.jar.JarOutputStream JavaDoc;
39 import java.util.jar.Manifest JavaDoc;
40 import java.util.zip.CRC32 JavaDoc;
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 /** Some infrastructure for module system tests.
54  * @author Jesse Glick
55  */

56 abstract class SetupHid extends NbTestCase {
57
58     public SetupHid(String JavaDoc name) {
59         super(name);
60     }
61
62     /** directory full of JAR files to test */
63     protected File JavaDoc jars;
64
65     protected void setUp() throws Exception JavaDoc {
66         Locale.setDefault(Locale.US);
67         jars = new File JavaDoc(ModuleManagerTest.class.getResource("jars").getFile());
68         clearWorkDir();
69     }
70
71     protected static void deleteRec(File JavaDoc f) throws IOException JavaDoc {
72         if (f.isDirectory()) {
73             File JavaDoc[] kids = f.listFiles();
74             if (kids == null) throw new IOException JavaDoc("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 JavaDoc("Could not delete: " + f);
80     }
81
82     protected static void copyStreams(InputStream JavaDoc is, OutputStream JavaDoc os) throws IOException JavaDoc {
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 JavaDoc a, File JavaDoc b) throws IOException JavaDoc {
95         OutputStream JavaDoc os = new FileOutputStream JavaDoc(b);
96         try {
97             copyStreams(new FileInputStream JavaDoc(a), os);
98         } finally {
99             os.close();
100         }
101     }
102
103     protected static void copy(File JavaDoc a, FileObject b) throws IOException JavaDoc {
104         OutputStream JavaDoc os = b.getOutputStream();
105         try {
106             copyStreams(new FileInputStream JavaDoc(a), os);
107         } finally {
108             os.close();
109         }
110     }
111
112     protected static String JavaDoc slurp(String JavaDoc path) throws IOException JavaDoc {
113         Main.getModuleSystem(); // #26451
114
FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(path);
115         if (fo == null) return null;
116         InputStream JavaDoc is = fo.getInputStream();
117         StringBuffer JavaDoc text = new StringBuffer JavaDoc((int)fo.getSize());
118         byte[] buf = new byte[1024];
119         int read;
120         while ((read = is.read(buf)) != -1) {
121             text.append(new String JavaDoc(buf, 0, read, "US-ASCII"));
122         }
123         return text.toString();
124     }
125
126     protected static class FakeModuleInstaller extends ModuleInstaller {
127         // For examining results of what happened:
128
public final List JavaDoc<String JavaDoc> actions = new ArrayList JavaDoc<String JavaDoc>();
129         public final List JavaDoc<Object JavaDoc> args = new ArrayList JavaDoc<Object JavaDoc>();
130         public void clear() {
131             actions.clear();
132             args.clear();
133         }
134         // For adding invalid modules:
135
public final Set JavaDoc<Module> delinquents = new HashSet JavaDoc<Module>();
136         // For adding modules that don't want to close:
137
public final Set JavaDoc<Module> wontclose = new HashSet JavaDoc<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 JavaDoc<Module> modules) {
148             actions.add("load");
149             args.add(new ArrayList JavaDoc<Module>(modules));
150         }
151         public void unload(List JavaDoc<Module> modules) {
152             actions.add("unload");
153             args.add(new ArrayList JavaDoc<Module>(modules));
154         }
155         public boolean closing(List JavaDoc<Module> modules) {
156             actions.add("closing");
157             args.add(new ArrayList JavaDoc<Module>(modules));
158             Iterator JavaDoc<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 JavaDoc<Module> modules) {
165             actions.add("close");
166             args.add(new ArrayList JavaDoc<Module>(modules));
167         }
168     }
169
170     protected static final class FakeEvents extends org.netbeans.Events {
171         protected void logged(String JavaDoc message, Object JavaDoc[] args) {
172             // do nothing
173
// XXX is it better to test events or the installer??
174
}
175     }
176
177     protected static final class LoggedPCListener implements PropertyChangeListener JavaDoc {
178         private final Set JavaDoc<PropertyChangeEvent JavaDoc> changes = new HashSet JavaDoc<PropertyChangeEvent JavaDoc>(100);
179         public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
180             changes.add(evt);
181             notify();
182         }
183         public synchronized void waitForChanges() throws InterruptedException JavaDoc {
184             wait(5000);
185         }
186         public synchronized boolean hasChange(Object JavaDoc source, String JavaDoc prop) {
187             for (PropertyChangeEvent JavaDoc 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 JavaDoc source, String JavaDoc prop) throws InterruptedException JavaDoc {
197             while (! hasChange(source, prop)) {
198                 long start = System.currentTimeMillis();
199                 waitForChanges();
200                 if (System.currentTimeMillis() - start > 4000) {
201                     //System.err.println("changes=" + changes);
202
return false;
203                 }
204             }
205             return true;
206         }
207     }
208
209     protected static class LoggedFileListener implements FileChangeListener {
210         /** names of files that have changed: */
211         private final Set JavaDoc<String JavaDoc> files = new HashSet JavaDoc<String JavaDoc>(100);
212         private synchronized void change(FileEvent ev) {
213             files.add(ev.getFile().getPath());
214             notify();
215         }
216         public synchronized void waitForChanges() throws InterruptedException JavaDoc {
217             wait(5000);
218         }
219         public synchronized boolean hasChange(String JavaDoc fname) {
220             return files.contains(fname);
221         }
222         public synchronized boolean waitForChange(String JavaDoc fname) throws InterruptedException JavaDoc {
223             while (! hasChange(fname)) {
224                 long start = System.currentTimeMillis();
225                 waitForChanges();
226                 if (System.currentTimeMillis() - start > 4000) {
227                     //System.err.println("changes=" + changes);
228
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             // ignore?
244
}
245         public void fileRenamed(FileRenameEvent fe) {
246             change(fe);
247         }
248         public void fileChanged(FileEvent fe) {
249             change(fe);
250         }
251     }
252
253     /**
254      * Create a fresh JAR file.
255      * @param jar the file to create
256      * @param contents keys are JAR entry paths, values are text contents (will be written in UTF-8)
257      * @param manifest a manifest to store (key/value pairs for main section)
258      */

259     public static void createJar(File JavaDoc jar, Map JavaDoc<String JavaDoc,String JavaDoc> contents, Map JavaDoc<String JavaDoc,String JavaDoc> manifest) throws IOException JavaDoc {
260         Manifest JavaDoc m = new Manifest JavaDoc();
261         m.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug
262
for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> line : manifest.entrySet()) {
263             m.getMainAttributes().putValue(line.getKey(), line.getValue());
264         }
265         jar.getParentFile().mkdirs();
266         OutputStream JavaDoc os = new FileOutputStream JavaDoc(jar);
267         try {
268             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(os, m);
269             Iterator JavaDoc it = contents.entrySet().iterator();
270             while (it.hasNext()) {
271                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
272                 String JavaDoc path = (String JavaDoc) entry.getKey();
273                 byte[] data = ((String JavaDoc) entry.getValue()).getBytes("UTF-8");
274                 JarEntry JavaDoc je = new JarEntry JavaDoc(path);
275                 je.setSize(data.length);
276                 CRC32 JavaDoc crc = new CRC32 JavaDoc();
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