KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > util > io > lib > DirJavaExplorer


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.util.io.lib;
25
26 import org.objectweb.jorm.util.io.api.PathExplorer;
27 import org.objectweb.jorm.util.api.Loggable;
28 import org.objectweb.util.monolog.api.BasicLevel;
29 import org.objectweb.util.monolog.api.Logger;
30 import org.objectweb.util.monolog.api.LoggerFactory;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.util.jar.JarFile JavaDoc;
42 import java.util.zip.ZipEntry JavaDoc;
43
44 /**
45  * DirJavaExplorer defines methods to create object able
46  * to manage a list of paths and furnish input and output stream
47  * on files
48  */

49 public class DirJavaExplorer implements PathExplorer, Loggable {
50     /**
51      * the vector which contains all the paths
52      */

53     private Collection JavaDoc paths;
54     private Logger logger;
55     private LoggerFactory loggerFactory;
56
57     static final String JavaDoc LOGGER_NAME = "org.objectweb.jorm.io.pathexplorer";
58     static String JavaDoc fileSeparator = System.getProperty("file.separator");
59     static String JavaDoc pathSeparator = System.getProperty("path.separator");
60
61     /**
62      * Builds a new DirJavaExplorer.
63      */

64     public DirJavaExplorer() {
65         paths = new ArrayList JavaDoc();
66     }
67
68     public void setLogger(Logger logger) {
69         this.logger = logger;
70     }
71
72     /**
73      * Returns a logger to an component that wants to log things.
74      */

75     public Logger getLogger() {
76         return logger;
77     }
78
79     /**
80      * Returns a logger factory that allows the creation of new loggers.
81      */

82     public LoggerFactory getLoggerFactory() {
83         return loggerFactory;
84     }
85
86     /**
87      * Assigns a logger factory that allows the creation of new loggers.
88      *
89      * @param loggerfactory the LoggerFactory object to obtain new loggers
90      */

91     public void setLoggerFactory(LoggerFactory loggerfactory) {
92         this.loggerFactory = loggerFactory;
93         if (logger == null && this.loggerFactory != null)
94             logger = loggerFactory.getLogger(LOGGER_NAME);
95     }
96
97     /**
98      * Adds a classpath and its path separator to the current explorer.
99      * This classpath is added to the list of path already defines.
100      *
101      * @param cpath the classpath which contains a list of paths
102      */

103     public void addPath(String JavaDoc cpath) {
104         int previous, current;
105         previous = 0;
106         current = cpath.indexOf(pathSeparator, previous);
107         while (current != -1) {
108             paths.add(cpath.substring(previous, current));
109
110             previous = current + 1;
111             current = cpath.indexOf(pathSeparator, previous);
112         }
113         paths.add(cpath.substring(previous));
114     }
115
116     /**
117      * Creates the paths with the given path.
118      *
119      * @param path the vector of paths (which are String)
120      */

121     public void addPath(Collection JavaDoc path) {
122         paths.addAll(path);
123     }
124
125     /**
126      * Gets an input stream from a given file. This file is opened from the
127      * defined path.
128      *
129      * @param file the string representation of the name of the file
130      * @return an InputStream for the given file
131      */

132     public InputStream JavaDoc getInputStream(String JavaDoc file) {
133         if (logger != null)
134             logger.log(BasicLevel.DEBUG, "Search " + file);
135         try {
136             File JavaDoc f = null;
137             for (Iterator JavaDoc it = paths.iterator(); it.hasNext();) {
138                 String JavaDoc path = (String JavaDoc) it.next();
139                 if (logger != null)
140                     logger.log(BasicLevel.DEBUG, "Try with " + path);
141                 File JavaDoc p = new File JavaDoc(path);
142                 if (p.exists()) {
143                     if (p.isDirectory()) {
144                         f = new File JavaDoc(p, file);
145                         if (f.exists()) {
146                             if (logger != null)
147                                 logger.log(BasicLevel.DEBUG, "Found " + f.getAbsolutePath());
148                             return new FileInputStream JavaDoc(f);
149                         }
150                     } else if (p.isFile()) {
151                         JarFile JavaDoc jarfile = null;
152                         try {
153                             jarfile = new JarFile JavaDoc(path);
154                             ZipEntry JavaDoc zipentry = jarfile.getEntry(file);
155                             if (zipentry == null)
156                                 zipentry = jarfile.getEntry(file.replace('\\', '/'));
157                             if (zipentry == null)
158                                 zipentry = jarfile.getEntry(file.replace('/', '\\'));
159                             if (zipentry != null) {
160                                 if (logger != null)
161                                     logger.log(BasicLevel.DEBUG, "Found " + file + " in " + path);
162                                 return jarfile.getInputStream(zipentry);
163                             } else {
164                                 if (logger != null) {
165                                     logger.log(BasicLevel.DEBUG, "Entry not Found " + file + " in jar file " + path);
166                                     for (Enumeration JavaDoc e = jarfile.entries(); e.hasMoreElements();) {
167                                         logger.log(BasicLevel.DEBUG, "entry: " + e.nextElement());
168                                     }
169                                 }
170                             }
171                         } catch (IOException JavaDoc e) {
172                             if (logger != null)
173                                 logger.log(BasicLevel.WARN, "Unmanaged file entry: " + path);
174                         }
175                     } else {
176                         if (logger != null)
177                             logger.log(BasicLevel.WARN, "Unmanaged path entry: " + path);
178                     }
179                 } else {
180                     System.out.println("WARN: Unreachable path entry: " + path);
181                 }
182             }
183             f = new File JavaDoc(file);
184             if (f.exists()) {
185                 if (logger != null)
186                     logger.log(BasicLevel.DEBUG, "Found " + f.getAbsolutePath());
187                 return new FileInputStream JavaDoc(f);
188             }
189
190             return null;
191         } catch (Exception JavaDoc e) {
192             if (logger != null)
193                 logger.log(BasicLevel.ERROR, "", e);
194             return null;
195         }
196     }
197
198     /**
199      * Gets an output stream from a given file. This file is opened from the
200      * defined path.
201      *
202      * @param file the string representation of the name of the file
203      * @return an OutputStream for the given file
204      */

205     public OutputStream JavaDoc getOutputStream(String JavaDoc file) {
206         return null;
207     }
208
209     public String JavaDoc getClassPath() {
210         String JavaDoc result = "";
211         boolean first = true;
212         for (Iterator JavaDoc it = paths.iterator(); it.hasNext();) {
213             result += (first ? "" : File.pathSeparator) + it.next();
214             first = false;
215         }
216         return result;
217     }
218 }
219
Popular Tags