KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > PropertyHelper


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbjarproject;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.api.project.ProjectManager;
25 import org.netbeans.spi.project.support.ant.EditableProperties;
26 import org.openide.ErrorManager;
27
28 /**
29  * A helper class to easily read and write properties.
30  * @author Andrei Badea
31  */

32 public final class PropertyHelper {
33     // TODO: AB: maybe in the future add methods to evaluate properties
34

35     private Project project;
36     private UpdateHelper updateHelper;
37
38     /** Creates a new instance of PropertyHelper */
39     public PropertyHelper(Project project, UpdateHelper updateHelper) {
40         this.project = project;
41         this.updateHelper = updateHelper;
42     }
43     
44     /**
45      * Returns the property as a string
46      * @param path the property path
47      * @param key the property name
48      * @returns the property value or null if it was not defined
49      */

50     public String JavaDoc getProperty(String JavaDoc path, String JavaDoc key) {
51         return getProperties(path).getProperty(key);
52     }
53     
54     /**
55      * Sets the property as a string. It puts the changed properties to
56      * the UpdateHelper. The caller should hold ProjectManager.mutex() write access.
57      * @param path the property path
58      * @param key the property name
59      * @param value the property value
60      */

61     public void setProperty(String JavaDoc path, String JavaDoc key, String JavaDoc value) {
62         assert ProjectManager.mutex().isWriteAccess() : "You must have write access to ProjectManager.mutex(). You can also use @see #saveProperty if you only need to save one property";
63         
64         EditableProperties ep = getProperties(path);
65         ep.setProperty(key, value);
66         putProperties(path, ep);
67     }
68     
69     /**
70      * Convenience method to set a single property and save the project.
71      * Acquires ProjectManager.mutex() write access.
72      * @param path the property path
73      * @param key the property name
74      * @param value the property value
75      */

76     public void saveProperty(final String JavaDoc path, final String JavaDoc key, final String JavaDoc value) {
77         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
78             public void run() {
79                 setProperty(path, key, value);
80                 save();
81             }
82         });
83     }
84     
85     /**
86      * Convenience method to save the project.
87      */

88     public void save() {
89         assert ProjectManager.mutex().isWriteAccess() : "You must have write access to ProjectManager.mutex().";
90         
91         try {
92             ProjectManager.getDefault().saveProject(project);
93         }
94         catch (IOException JavaDoc e) {
95             ErrorManager.getDefault().notify(e);
96         }
97     }
98     
99     private EditableProperties getProperties(String JavaDoc path) {
100         return updateHelper.getProperties(path);
101     }
102     
103     private void putProperties(String JavaDoc path, EditableProperties ep) {
104         updateHelper.putProperties(path, ep);
105     }
106 }
107
Popular Tags