Added
Link Here
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* The Original Software is NetBeans. The Initial Developer of the Original |
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
17 |
* Microsystems, Inc. All Rights Reserved. |
18 |
*/ |
19 |
|
20 |
package org.netbeans.modules.java.j2seproject.api; |
21 |
|
22 |
import java.io.IOException; |
23 |
import java.io.InputStream; |
24 |
import java.io.OutputStream; |
25 |
|
26 |
import java.util.Properties; |
27 |
|
28 |
import org.netbeans.api.project.Project; |
29 |
import org.netbeans.api.project.ProjectManager; |
30 |
|
31 |
import org.openide.filesystems.FileObject; |
32 |
import org.openide.filesystems.FileSystem; |
33 |
import org.openide.filesystems.FileUtil; |
34 |
|
35 |
import org.openide.util.Mutex; |
36 |
import org.openide.util.MutexException; |
37 |
|
38 |
/** |
39 |
* Utility class for creating project run configuration files |
40 |
* |
41 |
* @author Milan Kubec |
42 |
*/ |
43 |
public final class J2SEProjectConfigurations { |
44 |
|
45 |
J2SEProjectConfigurations() {} |
46 |
|
47 |
/** |
48 |
* Creates property files for run configuration and writes passed properties. |
49 |
* Shared properties are written to nbproject/configs folder and private properties |
50 |
* are written to nbproject/private/configs folder. The property file is not created |
51 |
* if null is passed for either shared or private properties. |
52 |
* |
53 |
* @param prj project under which property files will be created |
54 |
* @param configName name of the config file, '.properties' is apended |
55 |
* @param sharedProps properties to be written to shared file |
56 |
* @param privateProps properties to be written to private file |
57 |
*/ |
58 |
public static void createConfigurationFiles(Project prj, String configName, |
59 |
final Properties sharedProps, final Properties privateProps) throws IOException, IllegalArgumentException { |
60 |
|
61 |
if (prj == null || configName == null || "".equals(configName)) { |
62 |
throw new IllegalArgumentException(); |
63 |
} |
64 |
|
65 |
final String configFileName = configName + ".properties"; // NOI18N |
66 |
final FileObject prjDir = prj.getProjectDirectory(); |
67 |
|
68 |
try { |
69 |
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() { |
70 |
public Void run() throws IOException { |
71 |
prjDir.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { |
72 |
public void run() throws IOException { |
73 |
generateConfig(prjDir, "nbproject/configs/" + configFileName, sharedProps); // NOI18N |
74 |
generateConfig(prjDir, "nbproject/private/configs/" + configFileName, privateProps); // NOI18N |
75 |
} |
76 |
}); |
77 |
return null; |
78 |
} |
79 |
}); |
80 |
} catch (MutexException ex) { |
81 |
throw (IOException) ex.getException(); |
82 |
} |
83 |
|
84 |
} |
85 |
|
86 |
private static void generateConfig(FileObject prjDir, String cfgFilePath, Properties propsToWrite) throws IOException { |
87 |
|
88 |
if (propsToWrite == null) { |
89 |
// do not create anything if props is null |
90 |
return; |
91 |
} |
92 |
FileObject jwsConfigFO = FileUtil.createData(prjDir, cfgFilePath); |
93 |
if (jwsConfigFO != null) { |
94 |
Properties props = new Properties(); |
95 |
InputStream is = jwsConfigFO.getInputStream(); |
96 |
props.load(is); |
97 |
is.close(); |
98 |
if (props.equals(propsToWrite)) { |
99 |
// file already exists and props are the same |
100 |
return; |
101 |
} |
102 |
OutputStream os = jwsConfigFO.getOutputStream(); |
103 |
propsToWrite.store(os, null); |
104 |
os.close(); |
105 |
} |
106 |
|
107 |
} |
108 |
|
109 |
} |