0001: /*
0002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
0003: *
0004: * http://www.izforge.com/izpack/
0005: * http://izpack.codehaus.org/
0006: *
0007: * Copyright 2003 Marc Eppelmann
0008: *
0009: * Licensed under the Apache License, Version 2.0 (the "License");
0010: * you may not use this file except in compliance with the License.
0011: * You may obtain a copy of the License at
0012: *
0013: * http://www.apache.org/licenses/LICENSE-2.0
0014: *
0015: * Unless required by applicable law or agreed to in writing, software
0016: * distributed under the License is distributed on an "AS IS" BASIS,
0017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018: * See the License for the specific language governing permissions and
0019: * limitations under the License.
0020: */
0021:
0022: /*
0023: * This represents a Implementation of the KDE/GNOME DesktopEntry.
0024: * which is standard from
0025: * "Desktop Entry Standard"
0026: * "The format of .desktop files, supported by KDE and GNOME."
0027: * http://www.freedesktop.org/standards/desktop-entry-spec/
0028: *
0029: * [Desktop Entry]
0030: // Comment=$Comment
0031: // Comment[de]=
0032: // Encoding=$UTF-8
0033: // Exec=$'/home/marc/CPS/tomcat/bin/catalina.sh' run
0034: // GenericName=$
0035: // GenericName[de]=$
0036: // Icon=$inetd
0037: // MimeType=$
0038: // Name=$Start Tomcat
0039: // Name[de]=$Start Tomcat
0040: // Path=$/home/marc/CPS/tomcat/bin/
0041: // ServiceTypes=$
0042: // SwallowExec=$
0043: // SwallowTitle=$
0044: // Terminal=$true
0045: // TerminalOptions=$
0046: // Type=$Application
0047: // X-KDE-SubstituteUID=$false
0048: // X-KDE-Username=$
0049: *
0050: */
0051: package com.izforge.izpack.util.os;
0052:
0053: import com.izforge.izpack.util.Debug;
0054: import com.izforge.izpack.util.FileExecutor;
0055: import com.izforge.izpack.util.OsVersion;
0056: import com.izforge.izpack.util.StringTool;
0057: import com.izforge.izpack.util.os.unix.ShellScript;
0058: import com.izforge.izpack.util.os.unix.UnixHelper;
0059: import com.izforge.izpack.util.os.unix.UnixUser;
0060: import com.izforge.izpack.util.os.unix.UnixUsers;
0061:
0062: import java.io.BufferedReader;
0063: import java.io.BufferedWriter;
0064: import java.io.File;
0065: import java.io.FileReader;
0066: import java.io.FileWriter;
0067: import java.io.IOException;
0068: import java.io.UnsupportedEncodingException;
0069:
0070: import java.util.ArrayList;
0071: import java.util.Enumeration;
0072: import java.util.Properties;
0073: import java.util.StringTokenizer;
0074: import java.util.Vector;
0075:
0076: /**
0077: * This is the Implementation of the RFC-Based Desktop-Link. Used in KDE and GNOME.
0078: *
0079: * @author marc.eppelmann@reddot.de
0080: */
0081: public class Unix_Shortcut extends Shortcut implements
0082: Unix_ShortcutConstants {
0083:
0084: // ~ Static fields/initializers
0085: // *******************************************************************************************************************************
0086: /** version = "$Id: Unix_Shortcut.java 2061 2008-02-25 20:05:31Z jponge $" */
0087: private static String version = "$Id: Unix_Shortcut.java 2061 2008-02-25 20:05:31Z jponge $";
0088:
0089: /** rev = "$Revision: 2061 $" */
0090: private static String rev = "$Revision: 2061 $";
0091:
0092: /** DESKTOP_EXT = ".desktop" */
0093: private static String DESKTOP_EXT = ".desktop";
0094:
0095: /** template = "" */
0096: private static String template = "";
0097:
0098: /** N = "\n" */
0099: private final static String N = "\n";
0100:
0101: /** H = "#" */
0102: private final static String H = "#";
0103:
0104: /** S = " " */
0105: private final static String S = " ";
0106:
0107: /** C = Comment = H+S = "# " */
0108: private final static String C = H + S;
0109:
0110: /** QM = "\"" : <b>Q</b>uotation<b>M</b>ark */
0111: private final static String QM = "\"";
0112:
0113: private int ShortcutType;
0114: private static ShellScript rootScript = null;
0115: private static ShellScript uninstallScript = null;
0116: private static ArrayList users = UnixUsers
0117: .getUsersWithValidShellsExistingHomesAndDesktops();
0118: //private static ArrayList tempfiles = new ArrayList();
0119:
0120: // ~ Instance fields
0121: // ******************************************************************************************************************************************
0122: /** internal String createdDirectory */
0123: private String createdDirectory;
0124:
0125: /** internal int itsUserType */
0126: private int itsUserType;
0127:
0128: /** internal String itsGroupName */
0129: private String itsGroupName;
0130:
0131: /** internal String itsName */
0132: private String itsName;
0133:
0134: /** internal String itsFileName */
0135: private String itsFileName;
0136:
0137: /** internal String itsApplnkFolder = "applnk" */
0138: private String itsApplnkFolder = "applnk";
0139:
0140: /** internal Properties Set */
0141: private Properties props;
0142:
0143: /**
0144: * forAll = new Boolean(false): A flag to indicate that this should created for all users.
0145: */
0146: private Boolean forAll = Boolean.FALSE;
0147:
0148: /** Internal Help Buffer */
0149: public StringBuffer hlp;
0150:
0151: // ~ Constructors ***********************************************************************
0152:
0153: // ~ Constructors
0154: // *********************************************************************************************************************************************
0155: /**
0156: * Creates a new Unix_Shortcut object.
0157: */
0158: public Unix_Shortcut() {
0159: hlp = new StringBuffer();
0160:
0161: String userLanguage = System.getProperty("user.language", "en");
0162:
0163: hlp.append("[Desktop Entry]" + N);
0164:
0165: // TODO implement Attribute: X-KDE-StartupNotify=true
0166:
0167: hlp.append("Categories=" + $Categories + N);
0168:
0169: hlp.append("Comment=" + $Comment + N);
0170: hlp.append("Comment[").append(userLanguage).append(
0171: "]=" + $Comment + N);
0172: hlp.append("Encoding=" + $Encoding + N);
0173:
0174: // this causes too many problems
0175: //hlp.append("TryExec=" + $E_QUOT + $Exec + $E_QUOT + S + $Arguments + N);
0176:
0177: hlp.append("Exec=" + $E_QUOT + $Exec + $E_QUOT + S + $Arguments
0178: + N);
0179: hlp.append("GenericName=" + $GenericName + N);
0180:
0181: hlp.append("GenericName[").append(userLanguage).append(
0182: "]=" + $GenericName + N);
0183: hlp.append("Icon=" + $Icon + N);
0184: hlp.append("MimeType=" + $MimeType + N);
0185: hlp.append("Name=" + $Name + N);
0186: hlp.append("Name[").append(userLanguage).append(
0187: "]=" + $Name + N);
0188:
0189: hlp.append("Path=" + $P_QUOT + $Path + $P_QUOT + N);
0190: hlp.append("ServiceTypes=" + $ServiceTypes + N);
0191: hlp.append("SwallowExec=" + $SwallowExec + N);
0192: hlp.append("SwallowTitle=" + $SwallowTitle + N);
0193: hlp.append("Terminal=" + $Terminal + N);
0194:
0195: hlp.append("TerminalOptions=" + $Options_For_Terminal + N);
0196: hlp.append("Type=" + $Type + N);
0197:
0198: hlp.append("URL=" + $URL + N);
0199: hlp.append("X-KDE-SubstituteUID=" + $X_KDE_SubstituteUID + N);
0200: hlp.append("X-KDE-Username=" + $X_KDE_Username + N);
0201: hlp.append(N);
0202: hlp.append(C + "created by" + S).append(getClass().getName())
0203: .append(S).append(rev).append(N);
0204: hlp.append(C).append(version);
0205:
0206: template = hlp.toString();
0207:
0208: props = new Properties();
0209:
0210: initProps();
0211:
0212: if (rootScript == null)
0213: rootScript = new ShellScript();
0214: if (uninstallScript == null)
0215: uninstallScript = new ShellScript();
0216: }
0217:
0218: // ~ Methods ****************************************************************************
0219:
0220: // ~ Methods
0221: // **************************************************************************************************************************************************
0222: /**
0223: * This initialisizes all Properties Values with "".
0224: */
0225: private void initProps() {
0226: String[] propsArray = { $Comment, $$LANG_Comment, $Encoding,
0227: $Exec, $Arguments, $GenericName, $$LANG_GenericName,
0228: $MimeType, $Name, $$LANG_Name, $Path, $ServiceTypes,
0229: $SwallowExec, $SwallowTitle, $Terminal,
0230: $Options_For_Terminal, $Type, $X_KDE_SubstituteUID,
0231: $X_KDE_Username, $Icon, $URL, $E_QUOT, $P_QUOT,
0232: $Categories, $TryExec };
0233:
0234: for (String aPropsArray : propsArray) {
0235: props.put(aPropsArray, "");
0236: }
0237: }
0238:
0239: /**
0240: * Overridden Method
0241: *
0242: * @see com.izforge.izpack.util.os.Shortcut#initialize(int, java.lang.String)
0243: */
0244: public void initialize(int aType, String aName) throws Exception {
0245: this .itsName = aName;
0246: props.put($Name, aName);
0247: }
0248:
0249: /**
0250: * This indicates that Unix will be supported.
0251: *
0252: * @see com.izforge.izpack.util.os.Shortcut#supported()
0253: */
0254: public boolean supported() {
0255: return true;
0256: }
0257:
0258: /**
0259: * Dummy
0260: *
0261: * @see com.izforge.izpack.util.os.Shortcut#getDirectoryCreated()
0262: */
0263: public String getDirectoryCreated() {
0264: return this .createdDirectory; // while not stored...
0265: }
0266:
0267: /**
0268: * Dummy
0269: *
0270: * @see com.izforge.izpack.util.os.Shortcut#getFileName()
0271: */
0272: public String getFileName() {
0273: return (this .itsFileName);
0274: }
0275:
0276: /**
0277: * Overridden compatibility method. Returns all directories in $USER/.kde/share/applink.
0278: *
0279: * @see com.izforge.izpack.util.os.Shortcut#getProgramGroups(int)
0280: */
0281: public Vector<String> getProgramGroups(int userType) {
0282: Vector<String> groups = new Vector<String>();
0283:
0284: File kdeShareApplnk = getKdeShareApplnkFolder(userType);
0285:
0286: try {
0287: File[] listing = kdeShareApplnk.listFiles();
0288:
0289: for (File aListing : listing) {
0290: if (aListing.isDirectory()) {
0291: groups.add(aListing.getName());
0292: }
0293: }
0294: } catch (Exception e) {
0295: // ignore and return an empty vector.
0296: }
0297:
0298: return (groups);
0299: }
0300:
0301: /**
0302: * Gets the Programsfolder for the given User (non-Javadoc).
0303: *
0304: * @see com.izforge.izpack.util.os.Shortcut#getProgramsFolder(int)
0305: */
0306: public String getProgramsFolder(int current_user) {
0307: String result = "";
0308:
0309: //
0310: result = getKdeShareApplnkFolder(current_user).toString();
0311:
0312: return result;
0313: }
0314:
0315: /**
0316: * Gets the XDG path to place the menu shortcuts
0317: *
0318: * @param userType to get for.
0319: *
0320: * @return handle to the directory
0321: */
0322: private File getKdeShareApplnkFolder(int userType) {
0323:
0324: if (userType == Shortcut.ALL_USERS) {
0325: return new File(File.separator + "usr" + File.separator
0326: + "share" + File.separator + "applications");
0327: } else {
0328: return new File(System.getProperty("user.home")
0329: + File.separator + ".local" + File.separator
0330: + "share" + File.separator + "applications");
0331: }
0332:
0333: }
0334:
0335: /**
0336: * Gets the name of the applink folder for the currently used distribution. Currently
0337: * "applnk-redhat for RedHat, "applnk-mdk" for Mandrake, and simply "applnk" for all others.
0338: *
0339: * @return result
0340: */
0341: private String getKdeApplinkFolderName() {
0342: String applinkFolderName = "applnk";
0343:
0344: if (OsVersion.IS_REDHAT_LINUX) {
0345: applinkFolderName = "applnk-redhat";
0346: }
0347:
0348: if (OsVersion.IS_MANDRAKE_LINUX) {
0349: applinkFolderName = "applnk-mdk";
0350: }
0351:
0352: return applinkFolderName;
0353: }
0354:
0355: /**
0356: * Gets the KDEBasedir for the given User.
0357: *
0358: * @param userType one of root or regular user
0359: *
0360: * @return the basedir
0361: */
0362: private File getKdeBase(int userType) {
0363: File result = null;
0364:
0365: if (userType == Shortcut.ALL_USERS) {
0366: FileExecutor fe = new FileExecutor();
0367:
0368: String[] execOut = new String[2];
0369:
0370: int execResult = fe
0371: .executeCommand(new String[] { "/usr/bin/env",
0372: "kde-config", "--prefix" }, execOut);
0373:
0374: result = new File(execOut[0].trim());
0375: } else {
0376: result = new File(System.getProperty("user.home")
0377: + File.separator + ".kde");
0378: }
0379:
0380: return result;
0381: }
0382:
0383: /**
0384: * overridden method
0385: *
0386: * @return true
0387: *
0388: * @see com.izforge.izpack.util.os.Shortcut#multipleUsers()
0389: */
0390: public boolean multipleUsers() {
0391: // EVER true for UNIXes ;-)
0392: return (true);
0393: }
0394:
0395: /**
0396: * Creates and stores the shortcut-files.
0397: *
0398: * @see com.izforge.izpack.util.os.Shortcut#save()
0399: */
0400: public void save() throws Exception {
0401: String FS = File.separator;
0402: String chmod = UnixHelper.getCustomCommand("chmod");
0403: String chown = UnixHelper.getCustomCommand("chown");
0404: String rm = UnixHelper.getRmCommand();
0405: String copy = UnixHelper.getCpCommand();
0406: String su = UnixHelper.getSuCommand();
0407:
0408: String myHome = System.getProperty("user.home");
0409:
0410: String target = null;
0411:
0412: String shortCutDef = this .replace();
0413:
0414: boolean rootUser4All = this .getUserType() == Shortcut.ALL_USERS;
0415: boolean create4All = this .getCreateForAll();
0416:
0417: // Create The Desktop Shortcuts
0418: if ("".equals(this .itsGroupName)
0419: && (this .getLinkType() == Shortcut.DESKTOP)) {
0420:
0421: this .itsFileName = target;
0422:
0423: // write my own ShortCut
0424: File writtenDesktopFile = writeSafeShortcut(myHome + FS
0425: + "Desktop" + FS, this .itsName, shortCutDef);
0426: uninstaller.addFile(writtenDesktopFile.toString(), true);
0427:
0428: // If I'm root and this Desktop.ShortCut should be for all other users
0429: if (rootUser4All && create4All) {
0430: File dest = null;
0431:
0432: // Create a tempFileName of this ShortCut
0433: File tempFile = File.createTempFile(this .getClass()
0434: .getName(), Long.toString(System
0435: .currentTimeMillis())
0436: + ".tmp");
0437:
0438: copyTo(writtenDesktopFile, tempFile);
0439:
0440: //Debug.log("Wrote Tempfile: " + tempFile.toString());
0441:
0442: FileExecutor.getExecOutput(new String[] { chmod,
0443: "uga+rwx", tempFile.toString() });
0444:
0445: // su marc.eppelmann -c "/bin/cp /home/marc.eppelmann/backup.job.out.txt
0446: // /home/marc.eppelmann/backup.job.out2.txt"
0447:
0448: //StringBuffer script = new StringBuffer();
0449: //
0450:
0451: for (Object user1 : users) {
0452: UnixUser user = ((UnixUser) user1);
0453:
0454: if (user.getHome().equals(myHome)) {
0455: Debug.log("need not to copy for itself: "
0456: + user.getHome() + "==" + myHome);
0457: continue;
0458: }
0459: try {
0460: // aHomePath = userHomesList[idx];
0461: dest = new File(user.getHome() + FS + "Desktop"
0462: + FS + writtenDesktopFile.getName());
0463: //
0464: // I'm root and cannot write into Users Home as root;
0465: // But I'm Root and I can slip in every users skin :-)
0466: //
0467: // by# su username
0468: //
0469: // This works as well
0470: // su $username -c "cp /tmp/desktopfile $HOME/Desktop/link.desktop"
0471: // chown $username $HOME/Desktop/link.desktop
0472:
0473: //Debug.log("Will Copy: " + tempFile.toString() + " to " + dest.toString());
0474:
0475: rootScript.append(su);
0476: rootScript.append(S);
0477: rootScript.append(user.getName());
0478: rootScript.append(S);
0479: rootScript.append("-c");
0480: rootScript.append(S);
0481: rootScript.append('"');
0482: rootScript.append(copy);
0483: rootScript.append(S);
0484: rootScript.append(tempFile.toString());
0485: rootScript.append(S);
0486: rootScript.append(StringTool.replace(dest
0487: .toString(), " ", "\\ "));
0488: rootScript.appendln('"');
0489:
0490: rootScript.append('\n');
0491:
0492: //Debug.log("Will exec: " + script.toString());
0493:
0494: rootScript.append(chown);
0495: rootScript.append(S);
0496: rootScript.append(user.getName());
0497: rootScript.append(S);
0498: rootScript.appendln(StringTool.replace(dest
0499: .toString(), " ", "\\ "));
0500: rootScript.append('\n');
0501: rootScript.append('\n');
0502:
0503: //Debug.log("Will exec: " + script.toString());
0504:
0505: uninstallScript.append(su);
0506: uninstallScript.append(S);
0507: uninstallScript.append(user.getName());
0508: uninstallScript.append(S);
0509: uninstallScript.append("-c");
0510: uninstallScript.append(S);
0511: uninstallScript.append('"');
0512: uninstallScript.append(rm);
0513: uninstallScript.append(S);
0514: uninstallScript.append(StringTool.replace(dest
0515: .toString(), " ", "\\ "));
0516: uninstallScript.appendln('"');
0517: uninstallScript.appendln();
0518: //Debug.log("Uninstall will exec: " + uninstallScript.toString());
0519: } catch (Exception rex) {
0520: System.out.println("Error while su Copy: "
0521: + rex.getLocalizedMessage() + "\n\n");
0522: rex.printStackTrace();
0523:
0524: /* ignore */
0525: // most distros does not allow root to access any user
0526: // home (ls -la /home/user drwx------)
0527: // But try it anyway...
0528: }
0529: }
0530:
0531: rootScript.append(rm);
0532: rootScript.append(S);
0533: rootScript.appendln(tempFile.toString());
0534: rootScript.appendln();
0535: }
0536: }
0537:
0538: // This is - or should be only a Link in the [K?]-Menu
0539: else {
0540: // the following is for backwards compatibility to older versions of KDE!
0541: // on newer versions of KDE the icons will appear duplicated unless you set
0542: // the category=""
0543:
0544: //removed because of compatibility issues
0545: /*
0546: Object categoryobject = props.getProperty($Categories);
0547: if(categoryobject != null && ((String)categoryobject).length()>0)
0548: {
0549: File kdeHomeShareApplnk = getKdeShareApplnkFolder(this.getUserType());
0550: target = kdeHomeShareApplnk.toString() + FS + this.itsGroupName + FS + this.itsName
0551: + DESKTOP_EXT;
0552: this.itsFileName = target;
0553: File kdemenufile = writeShortCut(target, shortCutDef);
0554:
0555: uninstaller.addFile(kdemenufile.toString(), true);
0556: }
0557: */
0558:
0559: if (rootUser4All && create4All) {
0560: {
0561: // write the icon pixmaps into /usr/share/pixmaps
0562:
0563: File theIcon = new File(this .getIconLocation());
0564: File commonIcon = new File("/usr/share/pixmaps/"
0565: + theIcon.getName());
0566:
0567: try {
0568: copyTo(theIcon, commonIcon);
0569: uninstaller
0570: .addFile(commonIcon.toString(), true);
0571: } catch (Exception cnc) {
0572: Debug.log("Could Not Copy: " + theIcon + " to "
0573: + commonIcon + "( " + cnc.getMessage()
0574: + " )");
0575: }
0576:
0577: // write *.desktop
0578:
0579: this .itsFileName = target;
0580: File writtenFile = writeSafeShortcut(
0581: "/usr/share/applications/", this .itsName,
0582: shortCutDef);
0583: setWrittenFileName(writtenFile.getName());
0584: uninstaller.addFile(writtenFile.toString(), true);
0585:
0586: }
0587: } else // create local XDG shortcuts
0588: {
0589: //System.out.println("Creating gnome shortcut");
0590: String localApps = myHome
0591: + "/.local/share/applications/";
0592: String localPixmaps = myHome + "/.local/share/pixmaps/";
0593: //System.out.println("Creating "+localApps);
0594: try {
0595: java.io.File f = new java.io.File(localApps);
0596: f.mkdirs();
0597:
0598: f = new java.io.File(localPixmaps);
0599: f.mkdirs();
0600: } catch (Exception ignore) {
0601: //System.out.println("Failed creating "+localApps + " or " + localPixmaps);
0602: Debug.log("Failed creating " + localApps + " or "
0603: + localPixmaps);
0604: }
0605:
0606: // write the icon pixmaps into ~/share/pixmaps
0607:
0608: File theIcon = new File(this .getIconLocation());
0609: File commonIcon = new File(localPixmaps
0610: + theIcon.getName());
0611:
0612: try {
0613: copyTo(theIcon, commonIcon);
0614: uninstaller.addFile(commonIcon.toString(), true);
0615: } catch (Exception cnc) {
0616: Debug.log("Could Not Copy: " + theIcon + " to "
0617: + commonIcon + "( " + cnc.getMessage()
0618: + " )");
0619: }
0620:
0621: // write *.desktop in the local folder
0622:
0623: this .itsFileName = target;
0624: File writtenFile = writeSafeShortcut(localApps,
0625: this .itsName, shortCutDef);
0626: setWrittenFileName(writtenFile.getName());
0627: uninstaller.addFile(writtenFile.toString(), true);
0628: }
0629:
0630: }
0631: }
0632:
0633: /**
0634: * Post Exec Action especially for the Unix Root User.
0635: * which executes the Root ShortCut Shellscript.
0636: * to copy all ShellScripts to the users Desktop.
0637: */
0638: public void execPostAction() {
0639: Debug.log("Call of Impl. execPostAction Method in "
0640: + this .getClass().getName());
0641:
0642: String pseudoUnique = this .getClass().getName()
0643: + Long.toString(System.currentTimeMillis());
0644:
0645: String scriptFilename = null;
0646:
0647: try {
0648: scriptFilename = File.createTempFile(pseudoUnique, ".sh")
0649: .toString();
0650: } catch (IOException e) {
0651: scriptFilename = System.getProperty("java.io.tmpdir",
0652: "/tmp")
0653: + "/" + pseudoUnique + ".sh";
0654: e.printStackTrace();
0655: }
0656:
0657: rootScript.write(scriptFilename);
0658: rootScript.exec();
0659:
0660: Debug.log(rootScript);
0661:
0662: Debug.log(uninstallScript);
0663:
0664: uninstaller.addRootUninstallScript(uninstallScript
0665: .getContentAsString());
0666: }
0667:
0668: /**
0669: * Copies the inFile file to outFile using cbuff as buffer.
0670: *
0671: * @param inFile The File to read from.
0672: * @param outFile The targetFile to write to.
0673: *
0674: * @throws IOException If an IO Error occurs
0675: */
0676: public static void copyTo(File inFile, File outFile)
0677: throws IOException {
0678: char[] cbuff = new char[32768];
0679: BufferedReader reader = new BufferedReader(new FileReader(
0680: inFile));
0681: BufferedWriter writer = new BufferedWriter(new FileWriter(
0682: outFile));
0683:
0684: int readedBytes = 0;
0685: long absWrittenBytes = 0;
0686:
0687: while ((readedBytes = reader.read(cbuff, 0, cbuff.length)) != -1) {
0688: writer.write(cbuff, 0, readedBytes);
0689: absWrittenBytes += readedBytes;
0690: }
0691:
0692: reader.close();
0693: writer.close();
0694: }
0695:
0696: private String writtenFileName;
0697:
0698: public String getWrittenFileName() {
0699: return writtenFileName;
0700: }
0701:
0702: protected void setWrittenFileName(String s) {
0703: writtenFileName = s;
0704: }
0705:
0706: private File writeSafeShortcut(String targetPath,
0707: String shortcutName, String shortcutDef) {
0708: if (!(targetPath.endsWith("/") || targetPath.endsWith("\\"))) {
0709: targetPath += File.separatorChar;
0710: }
0711:
0712: File shortcutFile;
0713:
0714: do {
0715: shortcutFile = new File(targetPath + shortcutName + "-"
0716: + System.currentTimeMillis() + DESKTOP_EXT);
0717: } while (shortcutFile.exists());
0718:
0719: FileWriter fileWriter = null;
0720:
0721: try {
0722: fileWriter = new FileWriter(shortcutFile);
0723: } catch (IOException e1) {
0724: System.out.println(e1.getMessage());
0725: }
0726:
0727: try {
0728: fileWriter.write(shortcutDef);
0729: } catch (IOException e) {
0730: e.printStackTrace();
0731: }
0732:
0733: try {
0734: fileWriter.close();
0735: } catch (IOException e2) {
0736: e2.printStackTrace();
0737: }
0738: return shortcutFile;
0739:
0740: }
0741:
0742: /**
0743: * Writes the given Shortcutdefinition to the given Target. Returns the written File.
0744: *
0745: * @param target
0746: * @param shortCutDef
0747: *
0748: * @return the File of the written shortcut.
0749: */
0750: private File writeShortCut(String target, String shortCutDef) {
0751: File targetPath = new File(target.substring(0, target
0752: .lastIndexOf(File.separatorChar)));
0753:
0754: if (!targetPath.exists()) {
0755: targetPath.mkdirs();
0756: this .createdDirectory = targetPath.toString();
0757: }
0758:
0759: File targetFileName = new File(target);
0760: File backupFile = new File(targetPath + File.separator + "."
0761: + targetFileName.getName() + System.currentTimeMillis());
0762:
0763: if (targetFileName.exists()) {
0764: try {
0765: // create a hidden backup.file of the existing shortcut with a timestamp name.
0766: copyTo(targetFileName, backupFile); // + System.e );
0767: targetFileName.delete();
0768: } catch (IOException e3) {
0769: System.out.println("cannot create backup file "
0770: + backupFile + " of " + targetFileName); // e3.printStackTrace();
0771: }
0772: }
0773:
0774: FileWriter fileWriter = null;
0775:
0776: try {
0777: fileWriter = new FileWriter(targetFileName);
0778: } catch (IOException e1) {
0779: System.out.println(e1.getMessage());
0780: }
0781:
0782: try {
0783: fileWriter.write(shortCutDef);
0784: } catch (IOException e) {
0785: e.printStackTrace();
0786: }
0787:
0788: try {
0789: fileWriter.close();
0790: } catch (IOException e2) {
0791: e2.printStackTrace();
0792: }
0793:
0794: return targetFileName;
0795: }
0796:
0797: /**
0798: * Set the Commandline Arguments
0799: *
0800: * @see com.izforge.izpack.util.os.Shortcut#setArguments(java.lang.String)
0801: */
0802: public void setArguments(String args) {
0803: props.put($Arguments, args);
0804: }
0805:
0806: /**
0807: * Sets the Description
0808: *
0809: * @see com.izforge.izpack.util.os.Shortcut#setDescription(java.lang.String)
0810: */
0811: public void setDescription(String description) {
0812: props.put($Comment, description);
0813: }
0814:
0815: /**
0816: * Sets The Icon Path
0817: *
0818: * @see com.izforge.izpack.util.os.Shortcut#setIconLocation(java.lang.String, int)
0819: */
0820: public void setIconLocation(String path, int index) {
0821: props.put($Icon, path);
0822: }
0823:
0824: /**
0825: * Sets the Name of this Shortcut
0826: *
0827: * @see com.izforge.izpack.util.os.Shortcut#setLinkName(java.lang.String)
0828: */
0829: public void setLinkName(String aName) {
0830: this .itsName = aName;
0831: props.put($Name, aName);
0832: }
0833:
0834: /**
0835: * Sets the type of this Shortcut
0836: *
0837: * @see com.izforge.izpack.util.os.Shortcut#setLinkType(int)
0838: */
0839: public void setLinkType(int aType) throws IllegalArgumentException,
0840: UnsupportedEncodingException {
0841: ShortcutType = aType;
0842: }
0843:
0844: /**
0845: * Sets the ProgramGroup
0846: *
0847: * @see com.izforge.izpack.util.os.Shortcut#setProgramGroup(java.lang.String)
0848: */
0849: public void setProgramGroup(String aGroupName) {
0850: this .itsGroupName = aGroupName;
0851: }
0852:
0853: /**
0854: * Sets the ShowMode
0855: *
0856: * @see com.izforge.izpack.util.os.Shortcut#setShowCommand(int)
0857: */
0858: public void setShowCommand(int show) {
0859: }
0860:
0861: /**
0862: * Sets The TargetPath
0863: *
0864: * @see com.izforge.izpack.util.os.Shortcut#setTargetPath(java.lang.String)
0865: */
0866: public void setTargetPath(String aPath) {
0867: StringTokenizer whiteSpaceTester = new StringTokenizer(aPath);
0868:
0869: if (whiteSpaceTester.countTokens() > 1) {
0870: props.put($E_QUOT, QM);
0871: }
0872:
0873: props.put($Exec, aPath);
0874: }
0875:
0876: /**
0877: * Sets the usertype.
0878: *
0879: * @see com.izforge.izpack.util.os.Shortcut#setUserType(int)
0880: */
0881: public void setUserType(int aUserType) {
0882: this .itsUserType = aUserType;
0883: }
0884:
0885: /**
0886: * Sets the working-directory
0887: *
0888: * @see com.izforge.izpack.util.os.Shortcut#setWorkingDirectory(java.lang.String)
0889: */
0890: public void setWorkingDirectory(String aDirectory) {
0891: StringTokenizer whiteSpaceTester = new StringTokenizer(
0892: aDirectory);
0893:
0894: if (whiteSpaceTester.countTokens() > 1) {
0895: props.put($P_QUOT, QM);
0896: }
0897:
0898: props.put($Path, aDirectory);
0899: }
0900:
0901: /**
0902: * Dumps the Name to console.
0903: *
0904: * @see java.lang.Object#toString()
0905: */
0906: public String toString() {
0907: return this .itsName + N + template;
0908: }
0909:
0910: /**
0911: * Creates the Shortcut String which will be stored as File.
0912: *
0913: * @return contents of the shortcut file
0914: */
0915: public String replace() {
0916: String result = template;
0917: Enumeration enumeration = props.keys();
0918:
0919: while (enumeration.hasMoreElements()) {
0920: String key = (String) enumeration.nextElement();
0921:
0922: result = StringTool.replace(result, key, props
0923: .getProperty(key));
0924: }
0925:
0926: return result;
0927: }
0928:
0929: /**
0930: * Test Method
0931: *
0932: * @param args
0933: */
0934: public static void main(String[] args) {
0935: Unix_Shortcut aSample = new Unix_Shortcut();
0936:
0937: try {
0938: aSample.initialize(APPLICATIONS, "Start Tomcat");
0939: } catch (Exception exc) {
0940: System.err.println("Could not init Unix_Shourtcut");
0941: }
0942:
0943: aSample.replace();
0944: System.out.println(aSample);
0945:
0946: File targetFileName = new File(System.getProperty("user.home")
0947: + File.separator + "Start Tomcat" + DESKTOP_EXT);
0948: FileWriter fileWriter = null;
0949:
0950: try {
0951: fileWriter = new FileWriter(targetFileName);
0952: } catch (IOException e1) {
0953: e1.printStackTrace();
0954: }
0955:
0956: try {
0957: fileWriter.write(template);
0958: } catch (IOException e) {
0959: e.printStackTrace();
0960: }
0961:
0962: try {
0963: fileWriter.close();
0964: } catch (IOException e2) {
0965: e2.printStackTrace();
0966: }
0967: }
0968:
0969: /**
0970: * Sets The Encoding
0971: *
0972: * @see com.izforge.izpack.util.os.Shortcut#setEncoding(java.lang.String)
0973: */
0974: public void setEncoding(String aEncoding) {
0975: props.put($Encoding, aEncoding);
0976: }
0977:
0978: /**
0979: * Sets The KDE Specific subst UID property
0980: *
0981: * @see com.izforge.izpack.util.os.Shortcut#setKdeSubstUID(java.lang.String)
0982: */
0983: public void setKdeSubstUID(String trueFalseOrNothing) {
0984: props.put($X_KDE_SubstituteUID, trueFalseOrNothing);
0985: }
0986:
0987: /**
0988: * Sets The KDE Specific subst UID property
0989: *
0990: * @see com.izforge.izpack.util.os.Shortcut#setKdeSubstUID(java.lang.String)
0991: */
0992: public void setKdeUserName(String aUserName) {
0993: props.put($X_KDE_Username, aUserName);
0994: }
0995:
0996: /**
0997: * Sets the MimeType
0998: *
0999: * @see com.izforge.izpack.util.os.Shortcut#setMimetype(java.lang.String)
1000: */
1001: public void setMimetype(String aMimetype) {
1002: props.put($MimeType, aMimetype);
1003: }
1004:
1005: /**
1006: * Sets the terminal
1007: *
1008: * @see com.izforge.izpack.util.os.Shortcut#setTerminal(java.lang.String)
1009: */
1010: public void setTerminal(String trueFalseOrNothing) {
1011: props.put($Terminal, trueFalseOrNothing);
1012: }
1013:
1014: /**
1015: * Sets the terminal options
1016: *
1017: * @see com.izforge.izpack.util.os.Shortcut#setTerminalOptions(java.lang.String)
1018: */
1019: public void setTerminalOptions(String someTerminalOptions) {
1020: props.put($Options_For_Terminal, someTerminalOptions);
1021: }
1022:
1023: /**
1024: * Sets the Shortcut type (one of Application, Link or Device)
1025: *
1026: * @see com.izforge.izpack.util.os.Shortcut#setType(java.lang.String)
1027: */
1028: public void setType(String aType) {
1029: props.put($Type, aType);
1030: }
1031:
1032: /**
1033: * Sets the Url for type Link. Can be also a apsolute file/path
1034: *
1035: * @see com.izforge.izpack.util.os.Shortcut#setURL(java.lang.String)
1036: */
1037: public void setURL(String anUrl) {
1038: props.put($URL, anUrl);
1039: }
1040:
1041: /**
1042: * Gets the Usertype of the Shortcut.
1043: *
1044: * @see com.izforge.izpack.util.os.Shortcut#getUserType()
1045: */
1046: public int getUserType() {
1047: return itsUserType;
1048: }
1049:
1050: /**
1051: * Sets the Categories Field
1052: *
1053: * @param theCategories the categories
1054: */
1055: public void setCategories(String theCategories) {
1056: props.put($Categories, theCategories);
1057: }
1058:
1059: /**
1060: * Sets the TryExecField.
1061: *
1062: * @param aTryExec the try exec command
1063: */
1064: public void setTryExec(String aTryExec) {
1065: props.put($TryExec, aTryExec);
1066: }
1067:
1068: public int getLinkType() {
1069: return ShortcutType;
1070: //return Shortcut.DESKTOP;
1071: }
1072: }
|