1 /*
2 * $Id: FindResourceJar.java 2992 2011-11-24 19:25:54Z andrewinkler $
3 * ============================================================================
4 * Project gluehloch-homepage-resource
5 * Copyright (c) 2004-2010 by Andre Winkler. All rights reserved.
6 * ============================================================================
7 * GNU LESSER GENERAL PUBLIC LICENSE
8 * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26 package de.awtools.homegen;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.net.URL;
33
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.commons.lang.Validate;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40 * Sucht nach dem Resourcen-JAR. In dem Resourcen-JAR befindet sich die Datei
41 * {@link FindResourceJar#JAR_MARKER}. Diese liegt in der Wurzel des Archivs.
42 *
43 * @version $LastChangedRevision: 2992 $ $LastChangedDate: 2011-11-24 20:25:54 +0100 (Thu, 24 Nov 2011) $
44 * @author by Andre Winkler, $LastChangedBy: andrewinkler $
45 *
46 * @since 1.4
47 */
48 public final class FindResourceJar {
49
50 /** Das Archiv, welches diese Resource enthält, ist das gesuchte Archiv. */
51 public static final String JAR_MARKER = "/RootResource.txt";
52
53 /** Der Logger der Klasse. */
54 private final Logger log = LoggerFactory.getLogger(FindResourceJar.class);
55
56 /**
57 * Liefert die URL für das Resourcen-Archiv.
58 *
59 * @return Die URI für das Resourcen-Archiv. Kann <code>null</code> sein,
60 * dann wurde das Archiv nicht gefunden.
61 * @throws URISyntaxException URI Syntax nicht korrekt.
62 * @throws IOException Die Marker-Resource konnte nicht gefunden werden.
63 */
64 public URI call() throws URISyntaxException, IOException {
65 URI result = null;
66 URL url = getClass().getResource(JAR_MARKER);
67 if (url == null) {
68 throw new IOException("Resource '" + JAR_MARKER + "' not found.");
69 }
70
71 String filePath = url.getFile();
72 // @todo Ein Problem mit der URI Generierung.
73 // Manschmal (Bsp. mit Maven test run) wird ein File-Pfad ohne '%20'
74 // Ersetzung angelegt.
75 filePath = StringUtils.replace(filePath, " ", "%20");
76
77 if (log.isDebugEnabled()) {
78 log.debug("*** JAR URL: " + url);
79 }
80
81 if (StringUtils.contains(filePath, ".jar!")) {
82 filePath = StringUtils.substringBefore(filePath, "!");
83 URI tmpUrl = new URI(filePath);
84 File tmp = new File(tmpUrl);
85
86 if (log.isDebugEnabled()) {
87 log.debug("Jar resource file: " + tmp);
88 }
89
90 Validate.isTrue(tmp.exists());
91 result = tmp.toURI();
92 } else {
93 filePath = StringUtils.substringBefore(filePath, JAR_MARKER);
94 URI tmpUrl = new URI("file://" + filePath);
95 File tmp = new File(tmpUrl);
96
97 if (log.isDebugEnabled()) {
98 log.debug("Resource directory: " + tmp);
99 }
100
101 Validate.isTrue(tmp.exists());
102 result = tmp.toURI();
103
104 log.debug("Jar not found!");
105 }
106
107 if (log.isDebugEnabled()) {
108 log.debug("Jar resource URI: " + (result == null ? "<null>"
109 : result));
110 }
111
112 return result;
113 }
114
115 }