root / trunk / build / distribution / IzPack / sample / src / myCompany / tools / install / listener / ChmodInstallerListener.java @ 21757
History | View | Annotate | Download (3.63 KB)
1 |
/*
|
---|---|
2 |
* $Id: ChmodInstallerListener.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2004 Klaus Bartz
|
5 |
*
|
6 |
* File : ChmodInstallerListener.java
|
7 |
* Description : Example for custom action for install time.
|
8 |
* Author's email : klaus.bartz@coi.de
|
9 |
* Author's Website : http://www.coi.de/
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
24 |
*/
|
25 |
|
26 |
package com.myCompany.tools.install.listener; |
27 |
|
28 |
import java.io.File; |
29 |
import java.io.IOException; |
30 |
|
31 |
import com.izforge.izpack.PackFile; |
32 |
import com.izforge.izpack.util.FileExecutor; |
33 |
import com.izforge.izpack.event.SimpleInstallerListener; |
34 |
import com.izforge.izpack.installer.InstallerException; |
35 |
|
36 |
/**
|
37 |
* <p>InstallerListener for file and directory permissions
|
38 |
* on Unix.</p>
|
39 |
*
|
40 |
* @author Klaus Bartz
|
41 |
*
|
42 |
*/
|
43 |
public class ChmodInstallerListener extends SimpleInstallerListener |
44 |
{ |
45 |
/* (non-Javadoc)
|
46 |
* @see com.izforge.izpack.installer.InstallerListener#isFileListener()
|
47 |
*/
|
48 |
public boolean isFileListener() |
49 |
{ |
50 |
// This is a file related listener.
|
51 |
return true; |
52 |
} |
53 |
/* (non-Javadoc)
|
54 |
* @see com.izforge.izpack.compiler.InstallerListener#handleFile(java.io.File, com.izforge.izpack.PackFile)
|
55 |
*/
|
56 |
public void afterFile(File filePath, PackFile pf) throws Exception |
57 |
{ |
58 |
if( pf.getAdditionals() == null ) |
59 |
return;
|
60 |
Object file = pf.getAdditionals().get("permission.file"); |
61 |
int fileVal = -1; |
62 |
if( file != null && file instanceof Integer ) |
63 |
fileVal = ((Integer) file).intValue();
|
64 |
if( fileVal != -1) |
65 |
chmod(filePath, fileVal); |
66 |
} |
67 |
|
68 |
/* (non-Javadoc)
|
69 |
* @see com.izforge.izpack.compiler.InstallerListener#handleDir(java.io.File, com.izforge.izpack.PackFile)
|
70 |
*/
|
71 |
public void afterDir(File dirPath, PackFile pf) throws Exception |
72 |
{ |
73 |
if( pf.getAdditionals() == null ) |
74 |
return;
|
75 |
if( dirPath == null ) |
76 |
return;
|
77 |
Object dir = pf.getAdditionals().get("permission.dir"); |
78 |
int dirVal = -1; |
79 |
if( dir != null &&dir instanceof Integer ) |
80 |
dirVal = ((Integer) dir).intValue();
|
81 |
if( dirVal != -1) |
82 |
{ |
83 |
if( (dirVal & 0x000001C0) < 0x000001C0 ) |
84 |
throw new InstallerException( "Bad owner permission for directory " |
85 |
+ dirPath.getAbsolutePath() +"; at installation time the owner needs full rights" );
|
86 |
chmod(dirPath, dirVal); |
87 |
} |
88 |
} |
89 |
|
90 |
private void chmod(File path, int permissions) throws IOException |
91 |
{ |
92 |
String pathSep = System.getProperty("path.separator"); |
93 |
if(OsVersion.IS_WINDOWS)
|
94 |
{ |
95 |
throw new IOException("Sorry, chmod not supported yet on windows; use this class OS dependant."); |
96 |
} |
97 |
if( path == null ) |
98 |
// Oops this is an error, but in this example we ignore it ...
|
99 |
return;
|
100 |
String permStr = Integer.toOctalString(permissions); |
101 |
String[] params = {"chmod", permStr, path.getAbsolutePath()}; |
102 |
String[] output = new String[2]; |
103 |
FileExecutor fe = new FileExecutor();
|
104 |
fe.executeCommand(params, output); |
105 |
} |
106 |
} |