1 36 package org.ungoverned.oscar.installer.artifact; 37 38 import java.io.*; 39 import java.util.Map ; 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 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 installDir, String destName, String destDir) 83 throws IOException 84 { 85 if (destDir == null) 86 { 87 destDir = ""; 88 } 89 90 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 installDir, String destName, 119 String destDir, Map propMap) 120 throws IOException 121 { 122 if (destDir == null) 123 { 124 destDir = ""; 125 } 126 127 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 if ((char)i == '%') 152 { 153 int i2 = is.read(); 156 if ((char) i2 == '%') 157 { 158 Object obj = readParameter(is); 159 160 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 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 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 int i2 = is.read(); 207 if ((char) i2 == '%') 208 { 209 return new String (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 getPath(String s, char separator) 239 { 240 return (s.lastIndexOf(separator) < 0) 241 ? "" : s.substring(0, s.lastIndexOf(separator)); 242 } 243 244 public static String getPathHead(String s, char separator) 245 { 246 return (s.lastIndexOf(separator) < 0) 247 ? s : s.substring(s.lastIndexOf(separator) + 1); 248 } 249 } | Popular Tags |