KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > installer > artifact > AbstractJarArtifact


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar.installer.artifact;
37
38 import java.io.*;
39 import java.util.Map JavaDoc;
40 import java.util.jar.JarEntry JavaDoc;
41 import java.util.jar.JarInputStream JavaDoc;
42
43 import org.ungoverned.oscar.installer.Install;
44 import org.ungoverned.oscar.installer.Status;
45 import org.ungoverned.oscar.installer.StringProperty;
46
47 public abstract class AbstractJarArtifact extends AbstractArtifact
48 {
49     public AbstractJarArtifact(StringProperty sourceName)
50     {
51         this(sourceName, null);
52     }
53
54     public AbstractJarArtifact(StringProperty sourceName, StringProperty destDir)
55     {
56         this(sourceName, destDir, false);
57     }
58
59     public AbstractJarArtifact(
60         StringProperty sourceName, StringProperty destDir, boolean localize)
61     {
62         super(sourceName, destDir, localize);
63     }
64
65     public boolean process(Status status, Map JavaDoc propMap)
66     {
67         try
68         {
69             InputStream JavaDoc is = getInputStream(status);
70
71             if (is == null)
72             {
73                 return true;
74             }
75
76             JarInputStream JavaDoc jis = new JarInputStream JavaDoc(is);
77             status.setText("Extracting...");
78             unjar(jis, propMap);
79             jis.close();
80         }
81         catch (Exception JavaDoc ex)
82         {
83             System.err.println(this);
84             System.err.println(ex);
85             return false;
86         }
87
88         return true;
89     }
90
91     protected void unjar(JarInputStream JavaDoc jis, Map JavaDoc propMap)
92         throws IOException
93     {
94         String JavaDoc installDir =
95             ((StringProperty) propMap.get(Install.INSTALL_DIR)).getStringValue();
96
97         // Loop through JAR entries.
98
for (JarEntry JavaDoc je = jis.getNextJarEntry();
99              je != null;
100              je = jis.getNextJarEntry())
101         {
102             if (je.getName().startsWith("/"))
103             {
104                 throw new IOException("JAR resource cannot contain absolute paths.");
105             }
106
107             File target =
108                 new File(installDir, getDestinationDirectory().getStringValue());
109             target = new File(target, je.getName());
110
111             // Check to see if the JAR entry is a directory.
112
if (je.isDirectory())
113             {
114                 if (!target.exists())
115                 {
116                     if (!target.mkdirs())
117                     {
118                         throw new IOException("Unable to create target directory: "
119                             + target);
120                     }
121                 }
122                 // Just continue since directories do not have content to copy.
123
continue;
124             }
125
126             int lastIndex = je.getName().lastIndexOf('/');
127             String JavaDoc name = (lastIndex >= 0) ?
128                 je.getName().substring(lastIndex + 1) : je.getName();
129             String JavaDoc destination = (lastIndex >= 0) ?
130                 je.getName().substring(0, lastIndex) : "";
131
132             // JAR files use '/', so convert it to platform separator.
133
destination = destination.replace('/', File.separatorChar);
134
135             if (localize())
136             {
137                 copyAndLocalize(jis, installDir, name, destination, propMap);
138             }
139             else
140             {
141                 copy(jis, installDir, name, destination);
142             }
143         }
144     }
145 }
Popular Tags