KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > api > J2SEProjectConfigurations


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 JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import java.util.Properties JavaDoc;
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  * @since 1.10
43  */

44 public final class J2SEProjectConfigurations {
45     
46     J2SEProjectConfigurations() {}
47     
48     /**
49      * Creates property files for run configuration and writes passed properties.
50      * Shared properties are written to nbproject/configs folder and private properties
51      * are written to nbproject/private/configs folder. The property file is not created
52      * if null is passed for either shared or private properties.
53      *
54      * @param prj project under which property files will be created
55      * @param configName name of the config file, '.properties' is apended
56      * @param sharedProps properties to be written to shared file; is allowed to
57      * contain special purpose properties starting with $ (e.g. $label)
58      * @param privateProps properties to be written to private file
59      */

60     public static void createConfigurationFiles(Project prj, String JavaDoc configName,
61             final Properties JavaDoc sharedProps, final Properties JavaDoc privateProps) throws IOException JavaDoc, IllegalArgumentException JavaDoc {
62         
63         if (prj == null || configName == null || "".equals(configName)) {
64             throw new IllegalArgumentException JavaDoc();
65         }
66         
67         final String JavaDoc configFileName = configName + ".properties"; // NOI18N
68
final FileObject prjDir = prj.getProjectDirectory();
69         
70         try {
71             ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
72                 public Void JavaDoc run() throws IOException JavaDoc {
73                     prjDir.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
74                         public void run() throws IOException JavaDoc {
75                             generateConfig(prjDir, "nbproject/configs/" + configFileName, sharedProps); // NOI18N
76
generateConfig(prjDir, "nbproject/private/configs/" + configFileName, privateProps); // NOI18N
77
}
78                     });
79                     return null;
80                 }
81             });
82         } catch (MutexException ex) {
83             throw (IOException JavaDoc) ex.getException();
84         }
85         
86     }
87     
88     private static void generateConfig(FileObject prjDir, String JavaDoc cfgFilePath, Properties JavaDoc propsToWrite) throws IOException JavaDoc {
89         
90         if (propsToWrite == null) {
91             // do not create anything if props is null
92
return;
93         }
94         FileObject jwsConfigFO = FileUtil.createData(prjDir, cfgFilePath);
95         Properties JavaDoc props = new Properties JavaDoc();
96         InputStream JavaDoc is = jwsConfigFO.getInputStream();
97         props.load(is);
98         is.close();
99         if (props.equals(propsToWrite)) {
100             // file already exists and props are the same
101
return;
102         }
103         OutputStream JavaDoc os = jwsConfigFO.getOutputStream();
104         propsToWrite.store(os, null);
105         os.close();
106         
107     }
108     
109 }
110
Popular Tags