1 31 32 package org.jdesktop.jdic.packager.impl; 33 34 import java.io.IOException ; 35 import java.io.File ; 36 import java.io.PrintWriter ; 37 import java.io.BufferedReader ; 38 import java.io.BufferedWriter ; 39 import java.io.FileWriter ; 40 import java.io.InputStreamReader ; 41 42 43 54 public class RpmPackageGenerator implements PackageGenerator { 55 private String homePath; 56 private String topPath; 57 private String buildPath; 58 private String rpmsPath; 59 private String specsPath; 60 61 public RpmPackageGenerator() { 62 homePath = System.getProperty("user.home"); 63 topPath = homePath + "/.rpm/"; 64 buildPath = homePath + "/.rpm/BUILD/"; 65 rpmsPath = homePath + "/.rpm/RPMS/"; 66 specsPath = homePath + "/.rpm/SPECS/"; 67 } 68 69 72 public void generatePackage(JnlpPackageInfo pkgInfo) throws IOException { 73 File spec = null; 74 BufferedReader in = null; 75 String [] cmdArray = new String [3]; 76 77 try { 78 setTopdir(pkgInfo); 79 spec = createSpec(pkgInfo); 80 String rpmVersion = getRpmVersion(); 81 if (rpmVersion != null && rpmVersion.compareToIgnoreCase("4.0") >= 0) 82 cmdArray[0] = "rpmbuild"; 83 else 84 cmdArray[0] = "rpm"; 85 cmdArray[1] = "-bb"; 86 cmdArray[2] = spec.getAbsolutePath(); 87 Process p = Runtime.getRuntime().exec(cmdArray); 88 in = new BufferedReader ( 89 new InputStreamReader (p.getErrorStream())); 90 String line; 91 while((line = in.readLine()) != null) { 92 System.out.println(line); 93 } 94 95 } finally { 96 if(in != null) 97 in.close(); 98 99 100 File macros = new File (homePath + "/.rpmmacros"); 101 if (macros.exists()) { 102 if (!macros.delete()) 103 System.out.println("Cannot delete ~/.rpmmacros"); 104 } 105 } 106 107 } 108 109 private String getRpmVersion() throws IOException { 110 String [] cmdArray = {"rpm", "--version"}; 111 BufferedReader in = null; 112 try { 113 Process p = Runtime.getRuntime().exec(cmdArray); 114 in = new BufferedReader ( 115 new InputStreamReader (p.getInputStream())); 116 String line = in.readLine(); 117 if (line != null) { 118 line = line.trim(); 119 return line.substring(line.lastIndexOf(" ")).trim(); 120 } 121 else 122 return null; 123 } finally { 124 if(in != null) 125 in.close(); 126 } 127 } 128 129 private File createSpec(JnlpPackageInfo pkgInfo) throws IOException { 130 int i = 0; 131 132 PrintWriter pw = null; 133 String appName = pkgInfo.getPackageName(); 134 String dest = pkgInfo.getOutputDirPath(); 135 136 if (dest == null) { 137 dest = System.getProperty("user.dir"); 138 File destDir = new File (dest); 139 if (!destDir.canWrite()) 140 throw new IOException ("Cannot write to the current dir, please specify the PackagePath property"); 141 } 142 143 String installationPath = "/tmp/.jnlp/" + appName + "/"; 144 String resourcePath = pkgInfo.getResourceDirPath() + "/"; 145 String description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[0], JnlpConstants.JNLP_FIELD_DESCRIPTION); 146 String release = pkgInfo.getRelease(); 147 String version = pkgInfo.getVersion(); 148 String license = "NONE"; 149 String licensePath = pkgInfo.getLicenseDirPath(); 150 boolean hasLicense = false; 151 if (licensePath != null) { 152 hasLicense = true; 153 license = "License files are located at /usr/share/doc/packages/" + appName + "/License"; 154 } 155 String group = "System Environment/Base"; 156 boolean shortcut = pkgInfo.getShortcutEnabled(); 157 boolean association = pkgInfo.getAssociationEnabled(); 158 boolean result = false; 159 boolean systemCacheEnabled = pkgInfo.getSystemCacheEnabled(); 160 161 try { 162 163 File spec = new File (specsPath + appName + ".spec"); 164 pw = new PrintWriter (new BufferedWriter (new FileWriter (spec))); 165 166 167 String input = new String (); 168 input = "Summary: " + description + "\n"; 169 for(i=1; i<10; i++) { 170 if((description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[i], JnlpConstants.JNLP_FIELD_DESCRIPTION)) != null) { 171 input += "Summary(" + JnlpConstants.LOCALES[i] + "): " + description + "\n"; 172 } 173 } 174 input += "name: " + appName + "\n"; 175 input += "Release: " + release + "\n"; 176 input += "Version: " + version + "\n"; 177 input += "License: " + license + "\n"; 178 input += "Group: " + group + "\n"; 179 input += "BuildRoot: " + buildPath + "\n"; 180 input += "\n"; 181 pw.println(input); 182 183 184 description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[0], JnlpConstants.JNLP_FIELD_DESCRIPTION); 185 input = "%description\n"; 186 input += description + "\n"; 187 input += "\n"; 188 for(i=1; i<10; i++) { 189 if((description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[i], JnlpConstants.JNLP_FIELD_DESCRIPTION)) != null) { 190 input += "%description -l " + JnlpConstants.LOCALES[i] + "\n"; 191 input += description + "\n"; 192 } 193 } 194 pw.println(input); 195 196 197 try { 198 FileOperUtility.copyLocalFile(pkgInfo.getResourceDirPath(), buildPath + installationPath); 199 if (hasLicense) 200 FileOperUtility.copyLocalFile(licensePath, buildPath + File.separator + "License"); 201 } catch (IOException ioE) { 202 throw new IOException ("Cannot copy resource files to BuildRoot: " + ioE.getMessage()); 203 } 204 205 206 207 input = "%pre\n"; 208 String [] checkScript; 209 checkScript = JnlpUtility.javawsCheckScript(); 210 for(i=0; i<checkScript.length; i++) { 211 input += checkScript[i] + "\n"; 212 } 213 input += "echo preinstall finished\n"; 214 pw.println(input); 215 216 217 input = "%post\n"; 218 input += "JNLP_ASSOCIATION=`grep '^[^#]*application/x-java-jnlp-file' /etc/mailcap`\n"; 219 input += "JAVAWS_PATH=`echo $JNLP_ASSOCIATION | awk -F\\; '{print $2}' | awk '{print $1}'`\n"; 220 input += "echo JAVAWS_PATH: $JAVAWS_PATH\n"; 221 input += "$JAVAWS_PATH "; 222 if (systemCacheEnabled) 223 input += "-system "; 224 input += "-silent -import "; 225 if (shortcut) 226 input += "-shortcut "; 227 if (association) 228 input += "-association "; 229 input += "-codebase " 230 + "file://" 231 + installationPath 232 + " " 233 + installationPath 234 + "/" 235 + pkgInfo.getJnlpFileName() 236 + "\n"; 237 input += "\n"; 238 pw.println(input); 239 240 241 input = "%preun\n"; 242 243 for(i=0; i<checkScript.length; i++) { 244 input += checkScript[i] + "\n"; 245 } 246 247 pw.println(input); 248 249 250 input = "%postun\n"; 251 input += "JNLP_ASSOCIATION=`grep '^[^#]*application/x-java-jnlp-file' /etc/mailcap`\n"; 252 input += "JAVAWS_PATH=`echo $JNLP_ASSOCIATION | awk -F\\; '{print $2}' | awk '{print $1}'`\n"; 253 input += "\n"; 254 input += "$JAVAWS_PATH "; 255 if (systemCacheEnabled) 256 input += "-system "; 257 input += "-silent -uninstall " 258 + pkgInfo.getJnlpFileHref() 259 + "\n"; 260 input += "\n"; 261 pw.println(input); 262 263 264 input = "%clean\n"; 265 input += "mv " + "`find " + rpmsPath + " -name \"*.rpm\"` " + dest + "\n"; 266 input += "cd " + topPath + "\n"; 267 input += "cd ..\n"; 268 input += "rm -rf " + topPath + "\n"; 269 input += "\n"; 270 pw.println(input); 271 272 273 input = "%files\n"; 274 input += installationPath + "\n"; 275 if (hasLicense) 276 input += "%doc License\n"; 277 pw.println(input); 278 279 pw.close(); 280 return spec; 281 } finally { 282 if (pw != null) 283 pw.close(); 284 } 285 } 286 287 private void setTopdir(JnlpPackageInfo pkgInfo) throws IOException { 288 PrintWriter pw = null; 289 290 try { 291 boolean result = false; 292 293 294 File buildDir = new File (buildPath); 295 result = buildDir.mkdirs(); 296 if(result == false) { 297 result = buildDir.exists(); 298 if(result == false) 299 throw new IOException ("Cannot create BUILD dir in $HOME directory"); 300 } 301 File installDir = new File (buildPath + pkgInfo.getUniqueTmpDirPath() + "/"); 302 result = installDir.mkdirs(); 303 if(result == false) { 304 result = installDir.exists(); 305 if(result == false) 306 throw new IOException ("Cannot create install dir in BuildRoot"); 307 } 308 File rpmsDir = new File (rpmsPath); 309 result = rpmsDir.mkdirs(); 310 if(result == false) { 311 result = rpmsDir.exists(); 312 if(result == false) 313 throw new IOException ("Cannot create RPMS dir in $HOME directory"); 314 } 315 File specsDir = new File (specsPath); 316 result = specsDir.mkdirs(); 317 if(result == false) { 318 result = specsDir.exists(); 319 if(result == false) 320 throw new IOException ("Cannot create SPECS dir in $HOME directory"); 321 } 322 323 324 pw = new PrintWriter ( 325 new BufferedWriter (new FileWriter (homePath + "/.rpmmacros"))); 326 pw.println("%_topdir " + topPath); 327 pw.close(); 328 } finally { 329 if(pw != null) 330 pw.close(); 331 } 332 } 333 } 334 | Popular Tags |