Source Code Cross Referenced for Archive.java in  » IDE-Netbeans » library » org » netbeans » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » library » org.netbeans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans;
043:
044:        import java.io.DataOutputStream;
045:        import java.io.IOException;
046:        import java.io.UnsupportedEncodingException;
047:        import java.nio.ByteBuffer;
048:        import java.util.HashMap;
049:        import java.util.LinkedHashMap;
050:        import java.util.Map;
051:        import org.netbeans.JarClassLoader.JarSource;
052:
053:        /**
054:         * A shared startup-needed resource archive.
055:         * File format of the archive:
056:         *  [Header]
057:         *  ([Source entry]|[File entry])*
058:         * 
059:         * Header:
060:         *   8B [Magic]
061:         *   8B [timestamp]
062:         * 
063:         * Source entry (describes a data source for following entries):
064:         *   1B 0x01 type identifier
065:         *   xB id   utf8 String identifier of the source (file name)
066:         *
067:         * File entry (keeps content of a file with name and source ref):
068:         *   1B 0x02 type identifier (0x03 for general)
069:         *   2B src  number of the source (sources are counted in file from 0)
070:         *   4B len  length of the data (or -1 for no such file for source)
071:         *   xB name utf8 String name of the file
072:         * lenB data file content
073:         * 
074:         * Utf8 string
075:         *   2B len  length of the following String in bytes
076:         * lenB data utf8 encoded string
077:         *
078:         * @author nenik
079:         */
080:        class Archive implements  Stamps.Updater {
081:            // increment on format change
082:            private static final long magic = 6836742066851800321l;
083:
084:            private volatile boolean saved;
085:            private final boolean prepopulated;
086:
087:            private volatile boolean gathering;
088:            private final Object gatheringLock = new Object();
089:            // these two collections are guarded either by the gatheringLock
090:            // or by the "gathering" volatile flag transitions
091:            private Map<String, Boolean> requests = new LinkedHashMap<String, Boolean>();
092:            private Map<String, JarSource> knownSources = new HashMap<String, JarSource>();
093:
094:            private volatile boolean active;
095:            // These two collections are guarded by the "active" volatile flag transition.
096:            // They are modified from a single thread only, when "active" flag is false
097:            private Map<String, Integer> sources = new HashMap<String, Integer>();
098:            private Map<Entry, Entry> entries = new HashMap<Entry, Entry>();
099:
100:            public Archive() {
101:                gathering = false;
102:                active = false;
103:                prepopulated = false;
104:            }
105:
106:            /** Creates a new instance of Archive that reads data from given cache
107:             */
108:            Archive(Stamps cache) {
109:                ByteBuffer master = cache.asByteBuffer("all-resources.dat");
110:                try {
111:                    parse(master, cache.lastModified());
112:                } catch (Exception e) {
113:                    sources.clear();
114:                    entries.clear();
115:                }
116:                prepopulated = entries.size() > 0;
117:
118:                active = true;
119:                gathering = true;
120:            }
121:
122:            final boolean isActive() {
123:                return active;
124:            }
125:
126:            /**
127:             * Sweep through the master buffer and remember all the entries
128:             */
129:            private void parse(ByteBuffer master, long after) throws Exception {
130:                if (master.remaining() < 16)
131:                    throw new IllegalStateException("Cache invalid");
132:                if (master.getLong() != magic)
133:                    throw new IllegalStateException("Wrong format");
134:                if (master.getLong() < after)
135:                    throw new IllegalStateException("Cache outdated");
136:
137:                int srcCounter = 0;
138:
139:                while (master.remaining() > 0) {
140:                    int type = master.get();
141:                    switch (type) {
142:                    case 1: // source header
143:                        String name = parseString(master);
144:                        sources.put(name, srcCounter++);
145:                        break;
146:                    case 2:
147:                        Entry en = new Entry(master); // shifts the buffer
148:                        entries.put(en, en);
149:                        break;
150:                    default:
151:                        throw new IllegalStateException("Cache invalid");
152:                    }
153:                }
154:                master.rewind();
155:            }
156:
157:            private static String parseString(ByteBuffer src) {
158:                int len = src.getChar();
159:                byte data[] = new byte[len];
160:                src.get(data);
161:                try {
162:                    return new String(data, "UTF8");
163:                } catch (UnsupportedEncodingException uee) {
164:                    throw new InternalError(); // UTF8 must be supported
165:                }
166:            }
167:
168:            private static void writeString(DataOutputStream dos, String str)
169:                    throws UnsupportedEncodingException, IOException {
170:                byte[] data = str.getBytes("UTF8");
171:                dos.writeChar(data.length);
172:                dos.write(data);
173:            }
174:
175:            private Entry getEntry(JarSource source, String name) {
176:                Integer src = sources.get(source.getIdentifier());
177:                if (src == null)
178:                    return null;
179:
180:                return entries.get(new Template(src, name)); // or null
181:            }
182:
183:            public byte[] getData(JarSource source, String name)
184:                    throws IOException {
185:                Entry e = null;
186:                if (active) {
187:                    e = getEntry(source, name);
188:                    if (e == null && gathering) {
189:                        String srcId = source.getIdentifier();
190:                        String key = srcId + "!/" + name;
191:
192:                        synchronized (gatheringLock) {
193:                            if (!knownSources.containsKey(srcId))
194:                                knownSources.put(srcId, source);
195:                            if (!requests.containsKey(key))
196:                                requests.put(key, Boolean.TRUE);
197:                        }
198:                    }
199:                }
200:                if (e == null) {
201:                    byte[] data = source.resource(name);
202:                    // maybe store it now? No.
203:                    return data;
204:                }
205:
206:                return e.getContent();
207:            }
208:
209:            public void stopGathering() {
210:                gathering = false;
211:            }
212:
213:            public void stopServing() {
214:                active = false;
215:                // thread-safe, the only place using the field after
216:                // construction is guarded by above-cleared volatile flag
217:                // and this free happens-after clearing the flag
218:                entries = null;
219:            }
220:
221:            public void save(Stamps cache) throws IOException {
222:                if (saved) {
223:                    return;
224:                }
225:                saved = true;
226:                cache.scheduleSave(this , "all-resources.dat", prepopulated);
227:            }
228:
229:            public void flushCaches(DataOutputStream dos) throws IOException {
230:                assert !gathering;
231:                assert !active;
232:
233:                if (!prepopulated) { // write header
234:                    dos.writeLong(magic);
235:                    dos.writeLong(System.currentTimeMillis());
236:                }
237:
238:                // no need to really synchronize on this collection, gathering flag
239:                // is already cleared
240:                for (String s : requests.keySet()) {
241:                    String[] parts = s.split("!/");
242:                    JarSource src = knownSources.get(parts[0]);
243:                    byte[] data = src.resource(parts[1]);
244:                    Integer srcId = sources.get(parts[0]);
245:                    if (srcId == null) {
246:                        srcId = sources.size();
247:                        sources.put(parts[0], srcId);
248:                        dos.write(1);
249:                        writeString(dos, src.getIdentifier());
250:                    }
251:
252:                    dos.write(2);
253:                    dos.writeChar(srcId);
254:                    dos.writeInt(data == null ? -1 : data.length); // store a marker to avoid openning
255:                    writeString(dos, parts[1]);
256:                    if (data != null)
257:                        dos.write(data);
258:                }
259:                dos.close();
260:
261:                // clean up
262:                requests = null;
263:                knownSources = null;
264:                sources = null;
265:            }
266:
267:            public void cacheReady() {
268:                // nothing needs to be done
269:            }
270:
271:            /* Entry layout in the buffer:
272:             * -1    1B 0x02 type identifier (0x03 for general)
273:             *  0 -> 2B src  number of the source (sources are counted in file from 0)
274:             *  2    4B len  length of the data
275:             *  6    2B x    Length of name (in bytes)
276:             *  8    xB name utf8 String name of the file
277:             *x+8    yB data file content
278:             */
279:
280:            private static class Entry {
281:                private final int offset;
282:                private final ByteBuffer master;
283:
284:                Entry(ByteBuffer m) {
285:                    master = m;
286:                    offset = master.position();
287:                    int fLen = master.getInt(offset + 2);
288:                    int nLen = master.getChar(offset + 6);
289:                    if (fLen < 0)
290:                        fLen = 0;
291:                    master.position(offset + 8 + nLen + fLen);
292:                }
293:
294:                String getName() {
295:                    ByteBuffer my = master.duplicate();
296:                    my.position(offset + 6);
297:                    return parseString(my);
298:                }
299:
300:                int getSource() {
301:                    return master.getChar(offset);
302:                }
303:
304:                byte[] getContent() {
305:                    int fLen = master.getInt(offset + 2);
306:                    int nLen = master.getChar(offset + 6);
307:                    if (fLen < 0)
308:                        return null;
309:
310:                    ByteBuffer clone = master.duplicate();
311:                    clone.position(offset + 8 + nLen);
312:                    byte[] content = new byte[fLen];
313:                    clone.get(content);
314:                    return content;
315:                }
316:
317:                public @Override
318:                int hashCode() {
319:                    ByteBuffer clone = master.duplicate();
320:                    clone.position(offset + 8);
321:                    clone.limit(offset + 8 + master.getChar(offset + 6));
322:
323:                    int code = 53 * master.getChar(offset);
324:                    while (clone.hasRemaining())
325:                        code = code * 53 + clone.get();
326:                    return code;
327:                }
328:
329:                public @Override
330:                boolean equals(Object obj) {
331:                    if (obj instanceof  Template)
332:                        return obj.equals(this );
333:                    return obj == this ;
334:                }
335:
336:                public @Override
337:                String toString() {
338:                    return "#" + getSource() + ":" + getName() + "=[" + offset
339:                            + "]";
340:                }
341:            }
342:
343:            // template
344:            private class Template {
345:                private int source;
346:                private byte[] utf;
347:
348:                Template(int src, String name) {
349:                    try {
350:                        this .source = src;
351:                        utf = name.getBytes("UTF8");
352:                    } catch (UnsupportedEncodingException ex) {
353:                        throw new InternalError();
354:                    }
355:                }
356:
357:                public @Override
358:                boolean equals(Object o) {
359:                    if (!(o instanceof  Entry))
360:                        return false;
361:                    Entry e = (Entry) o;
362:
363:                    if (source != e.master.getChar(e.offset))
364:                        return false;
365:                    if (utf.length != e.master.getChar(e.offset + 6))
366:                        return false;
367:
368:                    ByteBuffer clone = e.master.duplicate();
369:                    clone.position(((Entry) o).offset + 8);
370:
371:                    for (byte b : utf)
372:                        if (b != clone.get())
373:                            return false;
374:
375:                    return true;
376:                }
377:
378:                public @Override
379:                int hashCode() {
380:                    int code = 53 * source;
381:                    for (byte b : utf)
382:                        code = code * 53 + b;
383:                    return code;
384:                }
385:            }
386:
387:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.