PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Current Directory ermitteln


eViL_oNe
27.07.2005, 21:52
Hallo,

ich nutze derzeit commons-digester um Beans aus XML Dateien zu erzeugen und sie später in ein gewünschtes Ausgabeformat (Email, HTML etc) umzuwandeln.

Das klappt so weit ganz gut, nur ist dieser Vorgang relativ zeitraubend. Da sich die entsprechenden Dateien selten bis nie ändern (Dokument und Email Templates), wollte ich sie unter WEB-INF/cache einmal erzeugen, und nur dann aktualisieren, wenn lastModified() von der XML-Datei > lastModified() von der Cache-Datei.

Mein Problem ist: wie lese ich das aktuelle Verzeichnis eines Servlets aus?


<% File dir = new File( "." ); %>
<%= dir.getCanonicalPath() %>


führt nicht zum Ziel, sondern gibt mir c:/programme/eclipse aus (eher kurios imo)


<%= this.getServletContext().getRealPath( "" ) %>


sieht etwas besser aus, da kommt <Eclipse Workplace Path>/<project name>/.deployables/<project name>

Ist dies der richtige Weg, das aktuelle Verzeichnis zu ermitteln? Man könnte ja den Pfad leicht durch die Konkatenation von "/WEB-INF/cache/<filename>" erweitern, und dann mit dem Cachen beginnen - eine kleine Utility-Klasse der Form CacheOutput.store( String relativePathName, String content, long lastModified ) ist schnell geschrieben!


MrEasy
28.07.2005, 14:17
ja, ist der richtige Weg, aber warum nicht gleich das /cache/<filename> übergeben? dann bekommste gleich dein Ergebnis:

getRealPath
public String getRealPath(String*path)
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:
path - a String specifying a virtual path
Returns:
a String specifying the real path, or null if the translation cannot be performed

Vielleicht auch praktisch:


getResourcePaths

public Set getResourcePaths(String path)
Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument. Paths indicating subdirectory paths end with a '/'. The returned paths are all relative to the root of the web application and have a leading '/'. For example, for a web application containing

/welcome.html
/catalog/index.html
/catalog/products.html
/catalog/offers/books.html
/catalog/offers/music.html
/customer/login.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com.acme.OrderServlet.class,

getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}
getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.


Parameters:
path - the partial path used to match the resources, which must start with a /
Returns:
a Set containing the directory listing, or null if there are no resources in the web application whose path begins with the supplied path.
Since:
Servlet 2.3

eViL_oNe
28.07.2005, 14:34
hmm stimmt, habe ich nicht bedacht, danke ;)

jetzt habe ich aber zu meinem Erstaunen festgestellt, dass das Parsen von XMLs schneller geht als gedacht (mein kleiner Debug Timer gibt vom Anfang des Starts bis zum JSP Footer als Zeit 120 - 200 ms an, und da sind noch andere Prozesse am laufen), daher werde ich das Cachen erstmal zurückstellen

allerdings gut zu wissen, wie das geht!