KickJava   Java API By Example, From Geeks To Geeks.

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


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
41 import org.ungoverned.oscar.installer.*;
42 import org.ungoverned.oscar.installer.property.*;
43
44 public abstract class AbstractArtifact implements Artifact
45 {
46     private StringProperty m_sourceName = null;
47     private StringProperty m_destDir = null;
48     private boolean m_localize = false;
49
50     // This following shared buffer assumes that there is
51
// no concurrency when processing resources.
52
protected static byte[] s_buffer = new byte[2048];
53
54     public AbstractArtifact(
55         StringProperty sourceName, StringProperty destDir, boolean localize)
56     {
57         if (destDir == null)
58         {
59             destDir = new StringPropertyImpl("empty", "");
60         }
61         m_sourceName = sourceName;
62         m_destDir = destDir;
63         m_localize = localize;
64     }
65
66     public StringProperty getSourceName()
67     {
68         return m_sourceName;
69     }
70
71     public StringProperty getDestinationDirectory()
72     {
73         return m_destDir;
74     }
75
76     public boolean localize()
77     {
78         return m_localize;
79     }
80
81     protected static void copy(
82         InputStream is, String JavaDoc installDir, String JavaDoc destName, String JavaDoc destDir)
83         throws IOException
84     {
85         if (destDir == null)
86         {
87             destDir = "";
88         }
89
90         // Make sure the target directory exists and
91
// that is actually a directory.
92
File targetDir = new File(installDir, destDir);
93         if (!targetDir.exists())
94         {
95             if (!targetDir.mkdirs())
96             {
97                 throw new IOException("Unable to create target directory: "
98                     + targetDir);
99             }
100         }
101         else if (!targetDir.isDirectory())
102         {
103             throw new IOException("Target is not a directory: "
104                 + targetDir);
105         }
106
107         BufferedOutputStream bos = new BufferedOutputStream(
108             new FileOutputStream(new File(targetDir, destName)));
109         int count = 0;
110         while ((count = is.read(s_buffer)) > 0)
111         {
112             bos.write(s_buffer, 0, count);
113         }
114         bos.close();
115     }
116
117     protected static void copyAndLocalize(
118         InputStream is, String JavaDoc installDir, String JavaDoc destName,
119         String JavaDoc destDir, Map JavaDoc propMap)
120         throws IOException
121     {
122         if (destDir == null)
123         {
124             destDir = "";
125         }
126
127         // Make sure the target directory exists and
128
// that is actually a directory.
129
File targetDir = new File(installDir, destDir);
130         if (!targetDir.exists())
131         {
132             if (!targetDir.mkdirs())
133             {
134                 throw new IOException("Unable to create target directory: "
135                     + targetDir);
136             }
137         }
138         else if (!targetDir.isDirectory())
139         {
140             throw new IOException("Target is not a directory: "
141                 + targetDir);
142         }
143
144         BufferedOutputStream bos = new BufferedOutputStream(
145             new FileOutputStream(new File(targetDir, destName)));
146         int i = 0;
147         while ((i = is.read()) > 0)
148         {
149             // Parameters start with "%%", so check to see if
150
// we have a parameter.
151
if ((char)i == '%')
152             {
153                 // One of three possibilities, we have a parameter,
154
// we have an end of file, or we don't have a parameter.
155
int i2 = is.read();
156                 if ((char) i2 == '%')
157                 {
158                     Object JavaDoc obj = readParameter(is);
159
160                     // If the byte sequence was not a parameter afterall,
161
// then a byte array is returned, otherwise a string
162
// containing the parameter m_name is returned.
163
if (obj instanceof byte[])
164                     {
165                         bos.write(i);
166                         bos.write(i2);
167                         bos.write((byte[]) obj);
168                     }
169                     else
170                     {
171                         Property prop = (Property) propMap.get(obj);
172                         String JavaDoc value = (prop == null) ? "" : prop.toString();
173                         bos.write(value.getBytes());
174                     }
175                 }
176                 else if (i2 == -1)
177                 {
178                     bos.write(i);
179                 }
180                 else
181                 {
182                     bos.write(i);
183                     bos.write(i2);
184                 }
185             }
186             else
187             {
188                 bos.write(i);
189             }
190         }
191         bos.close();
192     }
193
194     protected static Object JavaDoc readParameter(InputStream is)
195         throws IOException
196     {
197         int count = 0;
198         int i = 0;
199         while ((count < s_buffer.length) && ((i = is.read()) > 0))
200         {
201             if ((char) i == '%')
202             {
203                 // One of three possibilities, we have the end of
204
// the parameter, we have an end of file, or we
205
// don't have the parameter end.
206
int i2 = is.read();
207                 if ((char) i2 == '%')
208                 {
209                     return new String JavaDoc(s_buffer, 0, count);
210                 }
211                 else if (i2 == -1)
212                 {
213                     s_buffer[count] = (byte) i;
214                     byte[] b = new byte[count];
215                     for (int j = 0; j < count; j++)
216                         b[j] = s_buffer[j];
217                     return b;
218                 }
219                 else
220                 {
221                     s_buffer[count++] = (byte) i;
222                     s_buffer[count++] = (byte) i2;
223                 }
224             }
225             else
226             {
227                 s_buffer[count++] = (byte) i;
228             }
229         }
230
231         byte[] b = new byte[count - 1];
232         for (int j = 0; j < (count - 1); j++)
233             b[j] = s_buffer[j];
234
235         return b;
236     }
237
238     public static String JavaDoc getPath(String JavaDoc s, char separator)
239     {
240         return (s.lastIndexOf(separator) < 0)
241             ? "" : s.substring(0, s.lastIndexOf(separator));
242     }
243
244     public static String JavaDoc getPathHead(String JavaDoc s, char separator)
245     {
246         return (s.lastIndexOf(separator) < 0)
247             ? s : s.substring(s.lastIndexOf(separator) + 1);
248     }
249 }
Popular Tags