Source Code Cross Referenced for JarIndex.java in  » 6.0-JDK-Modules » j2me » sun » misc » 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 » 6.0 JDK Modules » j2me » sun.misc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)JarIndex.java	1.10 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package sun.misc;
029:
030:        import java.io.*;
031:        import java.util.*;
032:        import java.util.jar.*;
033:        import java.util.zip.*;
034:
035:        /**
036:         * This class is used to maintain mappings from packages, classes 
037:         * and resources to their enclosing JAR files. Mappings are kept
038:         * at the package level except for class or resource files that 
039:         * are located at the root directory. URLClassLoader uses the mapping
040:         * information to determine where to fetch an extension class or 
041:         * resource from. 
042:         *
043:         * @author  Zhenghua Li
044:         * @version 1.4, 02/02/00
045:         * @since   1.3
046:         */
047:
048:        public class JarIndex {
049:
050:            /**
051:             * The hash map that maintains mappings from 
052:             * package/classe/resource to jar file list(s)
053:             */
054:            private HashMap indexMap;
055:
056:            /** 
057:             * The hash map that maintains mappings from
058:             * jar file to package/class/resource lists
059:             */
060:            private HashMap jarMap;
061:
062:            /*
063:             * An ordered list of jar file names.
064:             */
065:            private String[] jarFiles;
066:
067:            /**
068:             * The index file name.
069:             */
070:            public static final String INDEX_NAME = "META-INF/INDEX.LIST";
071:
072:            /**
073:             * Constructs a new, empty jar index.
074:             */
075:            public JarIndex() {
076:                indexMap = new HashMap();
077:                jarMap = new HashMap();
078:            }
079:
080:            /**
081:             * Constructs a new index from the specified input stream.
082:             * 
083:             * @param is the input stream containing the index data
084:             */
085:            public JarIndex(InputStream is) throws IOException {
086:                this ();
087:                read(is);
088:            }
089:
090:            /**
091:             * Constructs a new index for the specified list of jar files.
092:             * 
093:             * @param files the list of jar files to construct the index from.
094:             */
095:            public JarIndex(String[] files) throws IOException {
096:                this ();
097:                this .jarFiles = files;
098:                parseJars(files);
099:            }
100:
101:            /**
102:             * Returns the jar index, or <code>null</code> if none.
103:             *
104:             * @param jar the JAR file to get the index from.
105:             * @exception IOException if an I/O error has occurred.
106:             */
107:            public static JarIndex getJarIndex(JarFile jar) throws IOException {
108:                JarIndex index = null;
109:                JarEntry e = jar.getJarEntry(INDEX_NAME);
110:                // if found, then load the index
111:                if (e != null) {
112:                    index = new JarIndex(jar.getInputStream(e));
113:                }
114:                return index;
115:            }
116:
117:            /**
118:             * Returns the jar files that are defined in this index.
119:             */
120:            public String[] getJarFiles() {
121:                return jarFiles;
122:            }
123:
124:            /*
125:             * Add the key, value pair to the hashmap, the value will
126:             * be put in a linked list which is created if necessary.
127:             */
128:            private void addToList(String key, String value, HashMap t) {
129:                LinkedList list = (LinkedList) t.get(key);
130:                if (list == null) {
131:                    list = new LinkedList();
132:                    list.add(value);
133:                    t.put(key, list);
134:                } else if (!list.contains(value)) {
135:                    list.add(value);
136:                }
137:            }
138:
139:            /**
140:             * Returns the list of jar files that are mapped to the file.
141:             *
142:             * @param fileName the key of the mapping
143:             */
144:            public LinkedList get(String fileName) {
145:                LinkedList jarFiles = null;
146:                if ((jarFiles = (LinkedList) indexMap.get(fileName)) == null) {
147:                    /* try the package name again */
148:                    int pos;
149:                    if ((pos = fileName.lastIndexOf("/")) != -1) {
150:                        jarFiles = (LinkedList) indexMap.get(fileName
151:                                .substring(0, pos));
152:                    }
153:                }
154:                return jarFiles;
155:            }
156:
157:            /**
158:             * Add the mapping from the specified file to the specified
159:             * jar file. If there were no mapping for the package of the 
160:             * specified file before, a new linked list will be created,
161:             * the jar file is added to the list and a new mapping from 
162:             * the package to the jar file list is added to the hashmap. 
163:             * Otherwise, the jar file will be added to the end of the 
164:             * existing list.
165:             *
166:             * @param fileName the file name
167:             * @param jarName the jar file that the file is mapped to
168:             *
169:             */
170:            public void add(String fileName, String jarName) {
171:                String packageName;
172:                int pos;
173:                if ((pos = fileName.lastIndexOf("/")) != -1) {
174:                    packageName = fileName.substring(0, pos);
175:                } else {
176:                    packageName = fileName;
177:                }
178:
179:                // add the mapping to indexMap
180:                addToList(packageName, jarName, indexMap);
181:
182:                // add the mapping to jarMap
183:                addToList(jarName, packageName, jarMap);
184:            }
185:
186:            /**
187:             * Go through all the jar files and construct the
188:             * index table.
189:             */
190:            private void parseJars(String[] files) throws IOException {
191:                if (files == null) {
192:                    return;
193:                }
194:
195:                String currentJar = null;
196:
197:                for (int i = 0; i < files.length; i++) {
198:                    currentJar = files[i];
199:                    ZipFile zrf = new ZipFile(currentJar.replace('/',
200:                            File.separatorChar));
201:
202:                    Enumeration entries = zrf.entries();
203:                    while (entries.hasMoreElements()) {
204:                        String fileName = ((ZipEntry) (entries.nextElement()))
205:                                .getName();
206:                        if (fileName.startsWith("META-INF")) {
207:                            continue;
208:                        }
209:                        add(fileName, currentJar);
210:                    }
211:                    zrf.close();
212:                }
213:            }
214:
215:            /**
216:             * Writes the index to the specified OutputStream
217:             *
218:             * @param out the output stream
219:             * @exception IOException if an I/O error has occurred
220:             */
221:            public void write(OutputStream out) throws IOException {
222:                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
223:                        out, "UTF8"));
224:                bw.write("JarIndex-Version: 1.0\n\n");
225:
226:                if (jarFiles != null) {
227:                    for (int i = 0; i < jarFiles.length; i++) {
228:                        /* print out the jar file name */
229:                        String jar = jarFiles[i];
230:                        bw.write(jar + "\n");
231:                        LinkedList jarlist = (LinkedList) jarMap.get(jar);
232:                        if (jarlist != null) {
233:                            Iterator listitr = jarlist.iterator();
234:                            while (listitr.hasNext()) {
235:                                bw.write((String) (listitr.next()) + "\n");
236:                            }
237:                        }
238:                        bw.write("\n");
239:                    }
240:                    bw.flush();
241:                }
242:            }
243:
244:            /**
245:             * Reads the index from the specified InputStream.
246:             *
247:             * @param is the input stream 
248:             * @exception IOException if an I/O error has occurred
249:             */
250:            public void read(InputStream is) throws IOException {
251:                BufferedReader br = new BufferedReader(new InputStreamReader(
252:                        is, "UTF8"));
253:                String line = null;
254:                String currentJar = null;
255:
256:                /* an ordered list of jar file names */
257:                Vector jars = new Vector();
258:
259:                /* read until we see a .jar line */
260:                while ((line = br.readLine()) != null && !line.endsWith(".jar"))
261:                    ;
262:
263:                for (; line != null; line = br.readLine()) {
264:                    if (line.length() == 0)
265:                        continue;
266:
267:                    if (line.endsWith(".jar")) {
268:                        currentJar = line;
269:                        jars.add(currentJar);
270:                    } else {
271:                        String name = line;
272:                        addToList(name, currentJar, indexMap);
273:                        addToList(currentJar, name, jarMap);
274:                    }
275:                }
276:
277:                jarFiles = (String[]) jars.toArray(new String[jars.size()]);
278:            }
279:
280:            /**
281:             * Merges the current index into another index, taking into account
282:             * the relative path of the current index.
283:             *
284:             * @param toIndex The destination index which the current index will
285:             *                merge into.
286:             * @param path    The relative path of the this index to the destination
287:             *                index.
288:             *
289:             */
290:            public void merge(JarIndex toIndex, String path) {
291:                Iterator itr = indexMap.entrySet().iterator();
292:                while (itr.hasNext()) {
293:                    Map.Entry e = (Map.Entry) itr.next();
294:                    String packageName = (String) e.getKey();
295:                    LinkedList from_list = (LinkedList) e.getValue();
296:                    Iterator listItr = from_list.iterator();
297:                    while (listItr.hasNext()) {
298:                        String jarName = (String) listItr.next();
299:                        if (path != null) {
300:                            jarName = path.concat(jarName);
301:                        }
302:                        toIndex.add(packageName, jarName);
303:                    }
304:                }
305:            }
306:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.