KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > os > Unix_Shortcut


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2003 Marc Eppelmann
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 /*
23  * This represents a Implementation of the KDE/GNOME DesktopEntry.
24  * which is standard from
25  * "Desktop Entry Standard"
26  * "The format of .desktop files, supported by KDE and GNOME."
27  * http://www.freedesktop.org/standards/desktop-entry-spec/
28  *
29  * [Desktop Entry]
30  // Comment=$Comment
31  // Comment[de]=
32  // Encoding=$UTF-8
33  // Exec=$'/home/marc/CPS/tomcat/bin/catalina.sh' run
34  // GenericName=$
35  // GenericName[de]=$
36  // Icon=$inetd
37  // MimeType=$
38  // Name=$Start Tomcat
39  // Name[de]=$Start Tomcat
40  // Path=$/home/marc/CPS/tomcat/bin/
41  // ServiceTypes=$
42  // SwallowExec=$
43  // SwallowTitle=$
44  // Terminal=$true
45  // TerminalOptions=$
46  // Type=$Application
47  // X-KDE-SubstituteUID=$false
48  // X-KDE-Username=$
49  *
50  */

51 package com.izforge.izpack.util.os;
52
53 import com.izforge.izpack.util.Debug;
54 import com.izforge.izpack.util.FileExecutor;
55 import com.izforge.izpack.util.OsVersion;
56 import com.izforge.izpack.util.StringTool;
57 import com.izforge.izpack.util.os.unix.ShellScript;
58 import com.izforge.izpack.util.os.unix.UnixHelper;
59 import com.izforge.izpack.util.os.unix.UnixUser;
60 import com.izforge.izpack.util.os.unix.UnixUsers;
61
62 import java.io.BufferedReader JavaDoc;
63 import java.io.BufferedWriter JavaDoc;
64 import java.io.File JavaDoc;
65 import java.io.FileReader JavaDoc;
66 import java.io.FileWriter JavaDoc;
67 import java.io.IOException JavaDoc;
68
69 import java.util.ArrayList JavaDoc;
70 import java.util.Enumeration JavaDoc;
71 import java.util.Properties JavaDoc;
72 import java.util.StringTokenizer JavaDoc;
73 import java.util.Vector JavaDoc;
74
75 /**
76  * This is the Implementation of the RFC-Based Desktop-Link. Used in KDE and GNOME.
77  *
78  * @author marc.eppelmann@reddot.de
79  */

80 public class Unix_Shortcut extends Shortcut implements Unix_ShortcutConstants
81 {
82
83     // ~ Static fields/initializers
84
// *******************************************************************************************************************************
85
/** version = "$Id: Unix_Shortcut.java 1708 2007-01-13 18:31:26Z jponge $" */
86     private static String JavaDoc version = "$Id: Unix_Shortcut.java 1708 2007-01-13 18:31:26Z jponge $";
87
88     /** rev = "$Revision: 1708 $" */
89     private static String JavaDoc rev = "$Revision: 1708 $";
90
91     /** DESKTOP_EXT = ".desktop" */
92     private static String JavaDoc DESKTOP_EXT = ".desktop";
93
94     /** template = "" */
95     private static String JavaDoc template = "";
96
97     /** N = "\n" */
98     private final static String JavaDoc N = "\n";
99
100     /** H = "#" */
101     private final static String JavaDoc H = "#";
102
103     /** S = " " */
104     private final static String JavaDoc S = " ";
105
106     /** C = Comment = H+S = "# " */
107     private final static String JavaDoc C = H + S;
108
109     /** QM = "\"" : <b>Q</b>uotation<b>M</b>ark */
110     private final static String JavaDoc QM = "\"";
111     
112     private static ShellScript rootScript = null;
113     private static ShellScript uninstallScript = null;
114     private static ArrayList JavaDoc users = UnixUsers.getUsersWithValidShellsExistingHomesAndDesktops();
115     //private static ArrayList tempfiles = new ArrayList();
116

117     // ~ Instance fields
118
// ******************************************************************************************************************************************
119
/** internal String createdDirectory */
120     private String JavaDoc createdDirectory;
121
122     /** internal int itsUserType */
123     private int itsUserType;
124
125     /** internal String itsGroupName */
126     private String JavaDoc itsGroupName;
127
128     /** internal String itsName */
129     private String JavaDoc itsName;
130
131     /** internal String itsFileName */
132     private String JavaDoc itsFileName;
133
134     /** internal String itsApplnkFolder = "applnk" */
135     private String JavaDoc itsApplnkFolder = "applnk";
136
137     /** internal Properties Set */
138     private Properties JavaDoc props;
139
140     /**
141      * forAll = new Boolean(false): A flag to indicate that this should created for all users.
142      */

143     private Boolean JavaDoc forAll = Boolean.FALSE;
144
145     /** Internal Help Buffer */
146     public StringBuffer JavaDoc hlp;
147
148     // ~ Constructors ***********************************************************************
149

150     // ~ Constructors
151
// *********************************************************************************************************************************************
152
/**
153      * Creates a new Unix_Shortcut object.
154      */

155     public Unix_Shortcut()
156     {
157         hlp = new StringBuffer JavaDoc();
158
159         String JavaDoc userLanguage = System.getProperty("user.language", "en");
160
161         hlp.append("[Desktop Entry]" + N);
162
163         // TODO implement Attribute: X-KDE-StartupNotify=true
164

165         hlp.append("Categories=" + $Categories + N);
166
167         hlp.append("Comment=" + $Comment + N);
168         hlp.append("Comment[").append(userLanguage).append("]=" + $Comment + N);
169         hlp.append("Encoding=" + $Encoding + N);
170
171         hlp.append("TryExec=" + $TryExec + N);
172
173         hlp.append("Exec=" + $E_QUOT + $Exec + $E_QUOT + S + $Arguments + N);
174         hlp.append("GenericName=" + $GenericName + N);
175
176         hlp.append("GenericName[").append(userLanguage).append("]=" + $GenericName + N);
177         hlp.append("Icon=" + $Icon + N);
178         hlp.append("MimeType=" + $MimeType + N);
179         hlp.append("Name=" + $Name + N);
180         hlp.append("Name[").append(userLanguage).append("]=" + $Name + N);
181
182         hlp.append("Path=" + $P_QUOT + $Path + $P_QUOT + N);
183         hlp.append("ServiceTypes=" + $ServiceTypes + N);
184         hlp.append("SwallowExec=" + $SwallowExec + N);
185         hlp.append("SwallowTitle=" + $SwallowTitle + N);
186         hlp.append("Terminal=" + $Terminal + N);
187
188         hlp.append("TerminalOptions=" + $Options_For_Terminal + N);
189         hlp.append("Type=" + $Type + N);
190         hlp.append("URL=" + $URL + N);
191         hlp.append("X-KDE-SubstituteUID=" + $X_KDE_SubstituteUID + N);
192         hlp.append("X-KDE-Username=" + $X_KDE_Username + N);
193         hlp.append(N);
194         hlp.append(C + "created by" + S).append(getClass().getName()).append(S).append(rev).append(
195                 N);
196         hlp.append(C).append(version);
197
198         template = hlp.toString();
199
200         props = new Properties JavaDoc();
201
202         initProps();
203         
204         if( rootScript==null )
205             rootScript = new ShellScript();
206         if( uninstallScript == null)
207             uninstallScript = new ShellScript();
208     }
209
210     // ~ Methods ****************************************************************************
211

212     // ~ Methods
213
// **************************************************************************************************************************************************
214
/**
215      * This initialisizes all Properties Values with &quot;&quot;.
216      */

217     private void initProps()
218     {
219         String JavaDoc[] propsArray = { $Comment, $$LANG_Comment, $Encoding, $Exec, $Arguments,
220                 $GenericName, $$LANG_GenericName, $MimeType, $Name, $$LANG_Name, $Path,
221                 $ServiceTypes, $SwallowExec, $SwallowTitle, $Terminal, $Options_For_Terminal,
222                 $Type, $X_KDE_SubstituteUID, $X_KDE_Username, $Icon, $URL, $E_QUOT, $P_QUOT,
223                 $Categories, $TryExec};
224
225         for (int i = 0; i < propsArray.length; i++)
226         {
227             props.put(propsArray[i], "");
228         }
229     }
230
231     /**
232      * Overridden Method
233      *
234      * @see com.izforge.izpack.util.os.Shortcut#initialize(int, java.lang.String)
235      */

236     public void initialize(int aType, String JavaDoc aName) throws Exception JavaDoc
237     {
238         this.itsName = aName;
239         props.put($Name, aName);
240     }
241
242     /**
243      * This indicates that Unix will be supported.
244      *
245      * @see com.izforge.izpack.util.os.Shortcut#supported()
246      */

247     public boolean supported()
248     {
249         return true;
250     }
251
252     /**
253      * Dummy
254      *
255      * @see com.izforge.izpack.util.os.Shortcut#getDirectoryCreated()
256      */

257     public String JavaDoc getDirectoryCreated()
258     {
259         return this.createdDirectory; // while not stored...
260
}
261
262     /**
263      * Dummy
264      *
265      * @see com.izforge.izpack.util.os.Shortcut#getFileName()
266      */

267     public String JavaDoc getFileName()
268     {
269         return (this.itsFileName);
270     }
271
272     /**
273      * Overridden compatibility method. Returns all directories in $USER/.kde/share/applink.
274      *
275      * @see com.izforge.izpack.util.os.Shortcut#getProgramGroups(int)
276      */

277     public Vector JavaDoc getProgramGroups(int userType)
278     {
279         Vector JavaDoc groups = new Vector JavaDoc();
280
281         File JavaDoc kdeShareApplnk = getKdeShareApplnkFolder(userType);
282
283         try
284         {
285             File JavaDoc[] listing = kdeShareApplnk.listFiles();
286
287             for (int i = 0; i < listing.length; i++)
288             {
289                 if (listing[i].isDirectory())
290                 {
291                     groups.add(listing[i].getName());
292                 }
293             }
294         }
295         catch (Exception JavaDoc e)
296         {
297             // ignore and return an empty vector.
298
}
299
300         return (groups);
301     }
302
303     /**
304      * Gets the Programsfolder for the given User (non-Javadoc).
305      *
306      * @see com.izforge.izpack.util.os.Shortcut#getProgramsFolder(int)
307      */

308     public String JavaDoc getProgramsFolder(int current_user)
309     {
310         String JavaDoc result = "";
311
312         //
313
result = getKdeShareApplnkFolder(current_user).toString();
314
315         return result;
316     }
317
318     /**
319      * Gets the kde/share/applink - Folder for the given user and for the currently known and
320      * supported distribution.
321      *
322      * @param userType to get for.
323      *
324      * @return the users or the systems kde share/applink(-redhat/-mdk)
325      */

326     private File JavaDoc getKdeShareApplnkFolder(int userType)
327     {
328         File JavaDoc kdeBase = getKdeBase(userType);
329
330         File JavaDoc result = new File JavaDoc(kdeBase + File.separator + "share" + File.separator
331                 + getKdeApplinkFolderName());
332
333         return result;
334     }
335
336     /**
337      * Gets the name of the applink folder for the currently used distribution. Currently
338      * "applnk-redhat for RedHat, "applnk-mdk" for Mandrake, and simply "applnk" for all others.
339      *
340      * @return result
341      */

342     private String JavaDoc getKdeApplinkFolderName()
343     {
344         String JavaDoc applinkFolderName = "applnk";
345
346         if (OsVersion.IS_REDHAT_LINUX)
347         {
348             applinkFolderName = "applnk-redhat";
349         }
350
351         if (OsVersion.IS_MANDRAKE_LINUX)
352         {
353             applinkFolderName = "applnk-mdk";
354         }
355
356         return applinkFolderName;
357     }
358
359     /**
360      * Gets the KDEBasedir for the given User.
361      *
362      * @param userType one of root or regular user
363      *
364      * @return the basedir
365      */

366     private File JavaDoc getKdeBase(int userType)
367     {
368         File JavaDoc result = null;
369
370         if (userType == Shortcut.ALL_USERS)
371         {
372             FileExecutor fe = new FileExecutor();
373
374             String JavaDoc[] execOut = new String JavaDoc[2];
375
376             int execResult = fe.executeCommand(new String JavaDoc[] { "/usr/bin/env", "kde-config",
377                     "--prefix"}, execOut);
378
379             result = new File JavaDoc(execOut[0].trim());
380         }
381         else
382         {
383             result = new File JavaDoc(System.getProperty("user.home") + File.separator + ".kde");
384         }
385
386         return result;
387     }
388
389     /**
390      * overridden method
391      *
392      * @return true
393      *
394      * @see com.izforge.izpack.util.os.Shortcut#multipleUsers()
395      */

396     public boolean multipleUsers()
397     {
398         // EVER true for UNIXes ;-)
399
return (true);
400     }
401
402     /**
403      * Creates and stores the shortcut-files.
404      *
405      * @see com.izforge.izpack.util.os.Shortcut#save()
406      */

407     public void save() throws Exception JavaDoc
408     {
409         String JavaDoc FS = File.separator;
410         String JavaDoc chmod = UnixHelper.getCustomCommand("chmod");
411         String JavaDoc chown = UnixHelper.getCustomCommand("chown");
412         String JavaDoc rm = UnixHelper.getRmCommand();
413         String JavaDoc copy = UnixHelper.getCpCommand();
414         String JavaDoc su = UnixHelper.getSuCommand();
415         
416         String JavaDoc myHome=System.getProperty("user.home");
417         
418         String JavaDoc target = null;
419
420         String JavaDoc shortCutDef = this.replace();
421
422         boolean rootUser4All = this.getUserType() == Shortcut.ALL_USERS;
423         boolean create4All = this.getCreateForAll().booleanValue();
424
425         // Create The Desktop Shortcuts
426
if ("".equals(this.itsGroupName) && (this.getLinkType() == Shortcut.DESKTOP))
427         {
428             
429             target = myHome + FS + "Desktop" + FS + this.itsName
430                     + DESKTOP_EXT;
431             this.itsFileName = target;
432
433             // write my own ShortCut
434
File JavaDoc writtenDesktopFile = writeShortCut(target, shortCutDef);
435
436             // If I'm root and this Desktop.ShortCut should be for all other users
437
if (rootUser4All && create4All)
438             {
439                 File JavaDoc dest = null;
440
441                 // Create a tempFileName of this ShortCut
442
File JavaDoc tempFile = File.createTempFile(this.getClass().getName(), Long.toString(System
443                         .currentTimeMillis())
444                         + ".tmp");
445
446                 copyTo(writtenDesktopFile, tempFile);
447
448                 //Debug.log("Wrote Tempfile: " + tempFile.toString());
449

450                 FileExecutor.getExecOutput(new String JavaDoc[] { chmod, "uga+rwx", tempFile.toString()});
451
452                 // su marc.eppelmann -c "/bin/cp /home/marc.eppelmann/backup.job.out.txt
453
// /home/marc.eppelmann/backup.job.out2.txt"
454

455                 //StringBuffer script = new StringBuffer();
456
//
457

458                 for (int idx = 0; idx < users.size(); idx++)
459                 {
460                     UnixUser user = ((UnixUser) users.get(idx));
461                     
462                     if( user.getHome().equals(myHome) )
463                     {
464                        Debug.log( "need not to copy for itself: " + user.getHome() + "==" + myHome );
465                        continue;
466                     }
467                     try
468                     {
469                         // aHomePath = userHomesList[idx];
470
dest = new File JavaDoc(user.getHome() + FS + "Desktop" + FS
471                                 + writtenDesktopFile.getName());
472                         //
473
// I'm root and cannot write into Users Home as root;
474
// But I'm Root and I can slip in every users skin :-)
475
//
476
// by# su username
477
//
478
// This works as well
479
// su $username -c "cp /tmp/desktopfile $HOME/Desktop/link.desktop"
480
// chown $username $HOME/Desktop/link.desktop
481

482                         //Debug.log("Will Copy: " + tempFile.toString() + " to " + dest.toString());
483

484                         rootScript.append(su);
485                         rootScript.append(S);
486                         rootScript.append(user.getName());
487                         rootScript.append(S);
488                         rootScript.append("-c");
489                         rootScript.append(S);
490                         rootScript.append('"');
491                         rootScript.append(copy);
492                         rootScript.append(S);
493                         rootScript.append(tempFile.toString());
494                         rootScript.append(S);
495                         rootScript.append(StringTool.replace(dest.toString(), " ", "\\ "));
496                         rootScript.appendln('"');
497                         
498                         rootScript.append('\n');
499                         
500                         //Debug.log("Will exec: " + script.toString());
501

502                         rootScript.append( chown );
503                         rootScript.append( S );
504                         rootScript.append( user.getName() );
505                         rootScript.append( S );
506                         rootScript.appendln( StringTool.replace(dest.toString(), " ", "\\ ") );
507                         rootScript.append('\n');
508                         rootScript.append('\n');
509
510                         //Debug.log("Will exec: " + script.toString());
511

512                         uninstallScript.append(su);
513                         uninstallScript.append(S);
514                         uninstallScript.append(user.getName());
515                         uninstallScript.append(S);
516                         uninstallScript.append("-c");
517                         uninstallScript.append(S);
518                         uninstallScript.append('"');
519                         uninstallScript.append(rm);
520                         uninstallScript.append(S);
521                         uninstallScript.append(StringTool.replace(dest.toString(), " ", "\\ "));
522                         uninstallScript.appendln('"');
523                         uninstallScript.appendln();
524                         //Debug.log("Uninstall will exec: " + uninstallScript.toString());
525
}
526                     catch (Exception JavaDoc rex)
527                     {
528                         System.out.println("Error while su Copy: " + rex.getLocalizedMessage()+"\n\n");
529                         rex.printStackTrace();
530
531                         /* ignore */
532                         // most distros does not allow root to access any user
533
// home (ls -la /home/user drwx------)
534
// But try it anyway...
535
}
536                 }
537                 
538
539                 rootScript.append( rm );
540                 rootScript.append( S );
541                 rootScript.appendln( tempFile.toString());
542                 rootScript.appendln();
543             }
544         }
545
546         // This is - or should be only a Link in the [K?]-Menu
547
else
548         {
549             File JavaDoc kdeHomeShareApplnk = getKdeShareApplnkFolder(this.getUserType());
550             target = kdeHomeShareApplnk.toString() + FS + this.itsGroupName + FS + this.itsName
551                     + DESKTOP_EXT;
552             this.itsFileName = target;
553             writeShortCut(target, shortCutDef);
554
555             if (rootUser4All)
556             {
557                 if (create4All)
558                 {
559                     // write the icon pixmaps into /usr/share/pixmaps
560

561                     File JavaDoc theIcon = new File JavaDoc(this.getIconLocation());
562                     File JavaDoc commonIcon = new File JavaDoc("/usr/share/pixmaps/" + theIcon.getName());
563
564                     try
565                     {
566                         copyTo(theIcon, commonIcon);
567                         uninstaller.addFile(commonIcon.toString());
568                     }
569                     catch (Exception JavaDoc cnc)
570                     {
571                         Debug.log("Could Not Copy: " + theIcon + " to " + commonIcon + "( "
572                                 + cnc.getMessage() + " )");
573                     }
574
575                     // write *.desktop.file into /usr/share/applications
576

577                     String JavaDoc commonTarget = "/usr/share/applications/" + this.itsName + DESKTOP_EXT;
578                     this.itsFileName = target;
579                     File JavaDoc writtenFile = writeShortCut(commonTarget, shortCutDef);
580
581                     uninstaller.addFile(writtenFile.toString());
582
583                 }
584                 else
585                 {
586                     // do nothing
587
}
588             }
589
590         }
591     }
592     
593     
594     /**
595      * Post Exec Action especially for the Unix Root User.
596      * which executes the Root ShortCut Shellscript.
597      * to copy all ShellScripts to the users Desktop.
598      */

599     public void execPostAction()
600     {
601         Debug.log("Call of Impl. execPostAction Method in " + this.getClass().getName() );
602         
603         String JavaDoc pseudoUnique = this.getClass().getName()+ Long.toString(System.currentTimeMillis());
604         
605         String JavaDoc scriptFilename = null;
606         
607         try
608         {
609             scriptFilename = File.createTempFile( pseudoUnique, ".sh" ).toString();
610         }
611         catch (IOException JavaDoc e)
612         {
613             scriptFilename = System.getProperty("java.io.tmpdir", "/tmp") + "/" + pseudoUnique + ".sh";
614             e.printStackTrace();
615         }
616         
617         rootScript.write(scriptFilename);
618         rootScript.exec();
619         
620         Debug.log(rootScript);
621         
622         
623         Debug.log(uninstallScript);
624         
625         uninstaller.addRootUninstallScript( uninstallScript.getContentAsString() );
626     }
627
628     /**
629      * Copies the inFile file to outFile using cbuff as buffer.
630      *
631      * @param inFile The File to read from.
632      * @param outFile The targetFile to write to.
633      *
634      * @throws IOException If an IO Error occurs
635      */

636     public static void copyTo(File JavaDoc inFile, File JavaDoc outFile) throws IOException JavaDoc
637     {
638         char[] cbuff = new char[32768];
639         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
640         BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(outFile));
641
642         int readedBytes = 0;
643         long absWrittenBytes = 0;
644
645         while ((readedBytes = reader.read(cbuff, 0, cbuff.length)) != -1)
646         {
647             writer.write(cbuff, 0, readedBytes);
648             absWrittenBytes += readedBytes;
649         }
650
651         reader.close();
652         writer.close();
653     }
654
655     /**
656      * Writes the given Shortcutdefinition to the given Target. Returns the written File.
657      *
658      * @param target
659      * @param shortCutDef
660      *
661      * @return the File of the written shortcut.
662      */

663     private File JavaDoc writeShortCut(String JavaDoc target, String JavaDoc shortCutDef)
664     {
665         File JavaDoc targetPath = new File JavaDoc(target.substring(0, target.lastIndexOf(File.separatorChar)));
666
667         if (!targetPath.exists())
668         {
669             targetPath.mkdirs();
670             this.createdDirectory = targetPath.toString();
671         }
672
673         File JavaDoc targetFileName = new File JavaDoc(target);
674         File JavaDoc backupFile = new File JavaDoc(targetPath + File.separator + "." + targetFileName.getName()
675                 + System.currentTimeMillis());
676
677         if (targetFileName.exists())
678         {
679             try
680             {
681                 // create a hidden backup.file of the existing shortcut with a timestamp name.
682
copyTo(targetFileName, backupFile); // + System.e );
683
targetFileName.delete();
684             }
685             catch (IOException JavaDoc e3)
686             {
687                 System.out.println("cannot create backup file " + backupFile + " of "
688                         + targetFileName); // e3.printStackTrace();
689
}
690         }
691
692         FileWriter JavaDoc fileWriter = null;
693
694         try
695         {
696             fileWriter = new FileWriter JavaDoc(targetFileName);
697         }
698         catch (IOException JavaDoc e1)
699         {
700             System.out.println(e1.getMessage());
701         }
702
703         try
704         {
705             fileWriter.write(shortCutDef);
706         }
707         catch (IOException JavaDoc e)
708         {
709             e.printStackTrace();
710         }
711
712         try
713         {
714             fileWriter.close();
715         }
716         catch (IOException JavaDoc e2)
717         {
718             e2.printStackTrace();
719         }
720
721         return targetFileName;
722     }
723
724     /**
725      * Set the Commandline Arguments
726      *
727      * @see com.izforge.izpack.util.os.Shortcut#setArguments(java.lang.String)
728      */

729     public void setArguments(String JavaDoc args)
730     {
731         props.put($Arguments, args);
732     }
733
734     /**
735      * Sets the Description
736      *
737      * @see com.izforge.izpack.util.os.Shortcut#setDescription(java.lang.String)
738      */

739     public void setDescription(String JavaDoc description)
740     {
741         props.put($Comment, description);
742     }
743
744     /**
745      * Sets The Icon Path
746      *
747      * @see com.izforge.izpack.util.os.Shortcut#setIconLocation(java.lang.String, int)
748      */

749     public void setIconLocation(String JavaDoc path, int index)
750     {
751         props.put($Icon, path);
752     }
753
754     /**
755      * Sets the Name of this Shortcut
756      *
757      * @see com.izforge.izpack.util.os.Shortcut#setLinkName(java.lang.String)
758      */

759     public void setLinkName(String JavaDoc aName)
760     {
761         this.itsName = aName;
762         props.put($Name, aName);
763     }
764
765     /**
766      * Sets the type of this Shortcut
767      *
768      * @see com.izforge.izpack.util.os.Shortcut#setLinkType(int)
769      */

770     public void setLinkType(int aType) throws IllegalArgumentException JavaDoc
771     {
772     }
773
774     /**
775      * Sets the ProgramGroup
776      *
777      * @see com.izforge.izpack.util.os.Shortcut#setProgramGroup(java.lang.String)
778      */

779     public void setProgramGroup(String JavaDoc aGroupName)
780     {
781         this.itsGroupName = aGroupName;
782     }
783
784     /**
785      * Sets the ShowMode
786      *
787      * @see com.izforge.izpack.util.os.Shortcut#setShowCommand(int)
788      */

789     public void setShowCommand(int show)
790     {
791     }
792
793     /**
794      * Sets The TargetPath
795      *
796      * @see com.izforge.izpack.util.os.Shortcut#setTargetPath(java.lang.String)
797      */

798     public void setTargetPath(String JavaDoc aPath)
799     {
800         StringTokenizer JavaDoc whiteSpaceTester = new StringTokenizer JavaDoc(aPath);
801
802         if (whiteSpaceTester.countTokens() > 1)
803         {
804             props.put($E_QUOT, QM);
805         }
806
807         props.put($Exec, aPath);
808     }
809
810     /**
811      * Sets the usertype.
812      *
813      * @see com.izforge.izpack.util.os.Shortcut#setUserType(int)
814      */

815     public void setUserType(int aUserType)
816     {
817         this.itsUserType = aUserType;
818     }
819
820     /**
821      * Sets the working-directory
822      *
823      * @see com.izforge.izpack.util.os.Shortcut#setWorkingDirectory(java.lang.String)
824      */

825     public void setWorkingDirectory(String JavaDoc aDirectory)
826     {
827         StringTokenizer JavaDoc whiteSpaceTester = new StringTokenizer JavaDoc(aDirectory);
828
829         if (whiteSpaceTester.countTokens() > 1)
830         {
831             props.put($P_QUOT, QM);
832         }
833
834         props.put($Path, aDirectory);
835     }
836
837     /**
838      * Dumps the Name to console.
839      *
840      * @see java.lang.Object#toString()
841      */

842     public String JavaDoc toString()
843     {
844         return this.itsName + N + template;
845     }
846
847     /**
848      * Creates the Shortcut String which will be stored as File.
849      *
850      * @return contents of the shortcut file
851      */

852     public String JavaDoc replace()
853     {
854         String JavaDoc result = template;
855         Enumeration JavaDoc enumeration = props.keys();
856
857         while (enumeration.hasMoreElements())
858         {
859             String JavaDoc key = (String JavaDoc) enumeration.nextElement();
860
861             result = StringTool.replace(result, key, props.getProperty(key));
862         }
863
864         return result;
865     }
866
867     /**
868      * Test Method
869      *
870      * @param args
871      */

872     public static void main(String JavaDoc[] args)
873     {
874         Unix_Shortcut aSample = new Unix_Shortcut();
875
876         try
877         {
878             aSample.initialize(APPLICATIONS, "Start Tomcat");
879         }
880         catch (Exception JavaDoc exc)
881         {
882             System.err.println("Could not init Unix_Shourtcut");
883         }
884
885         aSample.replace();
886         System.out.println(aSample);
887
888         File JavaDoc targetFileName = new File JavaDoc(System.getProperty("user.home") + File.separator
889                 + "Start Tomcat" + DESKTOP_EXT);
890         FileWriter JavaDoc fileWriter = null;
891
892         try
893         {
894             fileWriter = new FileWriter JavaDoc(targetFileName);
895         }
896         catch (IOException JavaDoc e1)
897         {
898             e1.printStackTrace();
899         }
900
901         try
902         {
903             fileWriter.write(template);
904         }
905         catch (IOException JavaDoc e)
906         {
907             e.printStackTrace();
908         }
909
910         try
911         {
912             fileWriter.close();
913         }
914         catch (IOException JavaDoc e2)
915         {
916             e2.printStackTrace();
917         }
918     }
919
920     /**
921      * Sets The Encoding
922      *
923      * @see com.izforge.izpack.util.os.Shortcut#setEncoding(java.lang.String)
924      */

925     public void setEncoding(String JavaDoc aEncoding)
926     {
927         props.put($Encoding, aEncoding);
928     }
929
930     /**
931      * Sets The KDE Specific subst UID property
932      *
933      * @see com.izforge.izpack.util.os.Shortcut#setKdeSubstUID(java.lang.String)
934      */

935     public void setKdeSubstUID(String JavaDoc trueFalseOrNothing)
936     {
937         props.put($X_KDE_SubstituteUID, trueFalseOrNothing);
938     }
939
940     /**
941      * Sets The KDE Specific subst UID property
942      *
943      * @see com.izforge.izpack.util.os.Shortcut#setKdeSubstUID(java.lang.String)
944      */

945     public void setKdeUserName(String JavaDoc aUserName)
946     {
947         props.put($X_KDE_Username, aUserName);
948     }
949
950     /**
951      * Sets the MimeType
952      *
953      * @see com.izforge.izpack.util.os.Shortcut#setMimetype(java.lang.String)
954      */

955     public void setMimetype(String JavaDoc aMimetype)
956     {
957         props.put($MimeType, aMimetype);
958     }
959
960     /**
961      * Sets the terminal
962      *
963      * @see com.izforge.izpack.util.os.Shortcut#setTerminal(java.lang.String)
964      */

965     public void setTerminal(String JavaDoc trueFalseOrNothing)
966     {
967         props.put($Terminal, trueFalseOrNothing);
968     }
969
970     /**
971      * Sets the terminal options
972      *
973      * @see com.izforge.izpack.util.os.Shortcut#setTerminalOptions(java.lang.String)
974      */

975     public void setTerminalOptions(String JavaDoc someTerminalOptions)
976     {
977         props.put($Options_For_Terminal, someTerminalOptions);
978     }
979
980     /**
981      * Sets the Shortcut type (one of Application, Link or Device)
982      *
983      * @see com.izforge.izpack.util.os.Shortcut#setType(java.lang.String)
984      */

985     public void setType(String JavaDoc aType)
986     {
987         props.put($Type, aType);
988     }
989
990     /**
991      * Sets the Url for type Link. Can be also a apsolute file/path
992      *
993      * @see com.izforge.izpack.util.os.Shortcut#setURL(java.lang.String)
994      */

995     public void setURL(String JavaDoc anUrl)
996     {
997         props.put($URL, anUrl);
998     }
999
1000    /**
1001     * Gets the Usertype of the Shortcut.
1002     *
1003     * @see com.izforge.izpack.util.os.Shortcut#getUserType()
1004     */

1005    public int getUserType()
1006    {
1007        return itsUserType;
1008    }
1009
1010    /**
1011     * Sets the Categories Field
1012     *
1013     * @param theCategories the categories
1014     */

1015    public void setCategories(String JavaDoc theCategories)
1016    {
1017        props.put($Categories, theCategories);
1018    }
1019
1020    /**
1021     * Sets the TryExecField.
1022     *
1023     * @param aTryExec the try exec command
1024     */

1025    public void setTryExec(String JavaDoc aTryExec)
1026    {
1027        props.put($TryExec, aTryExec);
1028    }
1029
1030}
1031
Popular Tags