KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > project > ArchiveProjectProperties


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.archive.project;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.api.project.Project;
25 import org.netbeans.api.project.ProjectManager;
26 import org.netbeans.modules.j2ee.deployment.devmodules.api.AntDeploymentHelper;
27 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
28 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
29 import org.netbeans.spi.project.support.ant.AntProjectHelper;
30 import org.netbeans.spi.project.support.ant.EditableProperties;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.util.Mutex;
34 import org.openide.util.MutexException;
35 import org.w3c.dom.Element JavaDoc;
36
37 public class ArchiveProjectProperties {
38     
39     private Project project;
40     private AntProjectHelper helper;
41     private EditableProperties projectProps, privateProps;
42     
43     final public static String JavaDoc J2EE_SERVER_INSTANCE = "j2ee.server.instance"; //NOI18N
44

45     final public static String JavaDoc J2EE_SERVER_TYPE = "j2ee.server.type"; //NOI18N
46

47     final public static String JavaDoc SOURCE_ARCHIVE = "source.archive"; //NOI18N
48

49     final public static String JavaDoc DIST_DIR = "dist.dir"; //NOI18N
50

51     // TODO - better name (also fix in build-impl.xsl)
52
final public static String JavaDoc WAR_NAME = "war.name"; //NOI18N
53

54     final public static String JavaDoc PROXY_PROJECT_DIR = "proxy.project.dir"; //NOI18N
55

56     final public static String JavaDoc TMP_PROJ_DIR_VALUE = "tmpproj"; //NOI18N
57

58     final public static String JavaDoc DIST_DIR_VALUE = "dist"; //NOI18N
59

60     final public static String JavaDoc DIST_ARCHIVE ="dist.archive"; //NOI18N
61

62     final public static String JavaDoc CONTENT_DIR = "content.dir"; //NOI18N
63

64     final public static String JavaDoc CONF_DIR = "conf.dir"; //NOI18N
65

66     public static final String JavaDoc SETUP_DIR_VALUE = "setup"; //NOI18N
67

68     public static final String JavaDoc ARCHIVE_TYPE = "javaee.archive.type"; //NOI18N
69

70     public static final String JavaDoc PROJECT_TYPE_VALUE_UNKNOWN = "unknown"; //NOI18N
71

72     public static final String JavaDoc PROJECT_TYPE_VALUE_WAR = "war"; //NOI18N
73

74     public static final String JavaDoc PROJECT_TYPE_VALUE_EAR = "ear"; //NOI18N
75

76     public static final String JavaDoc PROJECT_TYPE_VALUE_CAR = "car"; //NOI18N
77

78     public static final String JavaDoc PROJECT_TYPE_VALUE_RAR = "rar"; //NOI18N
79

80     public static final String JavaDoc PROJECT_TYPE_VALUE_JAR = "jar"; //NOI18N
81

82     public static final String JavaDoc WAR_ARCHIVES = "javaee.archive.war.archives";
83     public static final String JavaDoc JAR_ARCHIVES = "javaee.archive.jar.archives";
84     public static final String JavaDoc RAR_ARCHIVES = "javaee.archive.rar.archives";
85     
86     public static final String JavaDoc DEPLOY_ANT_PROPS_FILE = "deploy.ant.properties.file"; //NOI18N
87
public static final String JavaDoc ANT_DEPLOY_BUILD_SCRIPT = "nbproject/ant-deploy.xml"; // NOI18N
88
public static final String JavaDoc CAR_ARCHIVES = "javaee.archive.car.archives";
89     
90     public ArchiveProjectProperties(Project project, AntProjectHelper helper
91             /*PropertyEvaluator evaluator, ReferenceHelper refHelper*/) {
92         this.project = project;
93         this.helper = helper;
94         
95         privateProps = helper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
96         projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
97         
98 // privateGroup = new StoreGroup();
99
// projectGroup = new StoreGroup();
100
}
101     
102     public void save() {
103         try {
104             ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
105                 public Object JavaDoc run() throws IOException JavaDoc {
106                     // and save the project
107
try {
108 ////
109
//// EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
110
//// setNewServerInstanceValue(serverInstanceID, projectProps, privateProps);
111
helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
112                         helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
113                         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
114                         helper.putPrimaryConfigurationData(data, true);
115                         ProjectManager.getDefault().saveProject(project);
116                     } catch (IOException JavaDoc e) {
117                         ErrorManager.getDefault().notify(e);
118                     }
119                     return null;
120                 }
121             });
122         } catch (MutexException e) {
123             ErrorManager.getDefault().notify(e.getException());
124         }
125     }
126     
127     public Object JavaDoc get(String JavaDoc propertyName) {
128 // EditableProperties projectProperties = helper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
129
// EditableProperties privateProperties = helper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
130
Object JavaDoc retVal;
131         if (J2EE_SERVER_INSTANCE.equals(propertyName))
132             retVal = privateProps.getProperty(J2EE_SERVER_INSTANCE);
133         else
134             retVal = projectProps.getProperty(propertyName);
135         return retVal;
136     }
137     
138     public void put( String JavaDoc propertyName, String JavaDoc value ) {
139 // EditableProperties projectProperties = helper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
140
if (J2EE_SERVER_INSTANCE.equals(propertyName)) {
141             projectProps.put(J2EE_SERVER_TYPE, Deployment.getDefault().getServerID((String JavaDoc) value));
142 // EditableProperties privateProperties = helper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
143
privateProps.put(J2EE_SERVER_INSTANCE,value);
144             
145         } else {
146             projectProps.put(propertyName, value);
147         }
148     }
149     
150     public static void setServerInstance(final Project project, final AntProjectHelper helper, final String JavaDoc serverInstanceID) {
151         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
152             public void run() {
153                 try {
154                     EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
155                     EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
156                     setNewServerInstanceValue(serverInstanceID, projectProps, privateProps);
157                     // ant deployment support
158
File JavaDoc projectFolder = FileUtil.toFile(project.getProjectDirectory());
159                     try {
160                         AntDeploymentHelper.writeDeploymentScript(new File JavaDoc(projectFolder, ANT_DEPLOY_BUILD_SCRIPT),
161                                 mapType(projectProps.getProperty(ARCHIVE_TYPE)), serverInstanceID); // NOI18N
162
} catch (IOException JavaDoc ioe) {
163                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
164                     }
165                     File JavaDoc antDeployPropsFile = AntDeploymentHelper.getDeploymentPropertiesFile(serverInstanceID);
166                     if (antDeployPropsFile == null) {
167                         privateProps.remove(DEPLOY_ANT_PROPS_FILE);
168                     } else {
169                         privateProps.setProperty(DEPLOY_ANT_PROPS_FILE, antDeployPropsFile.getAbsolutePath());
170                     }
171                     helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
172                     helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
173                     ProjectManager.getDefault().saveProject(project);
174                 } catch (IOException JavaDoc e) {
175                     ErrorManager.getDefault().notify(e);
176                 }
177             }
178         });
179     }
180     
181     private static void setNewServerInstanceValue(String JavaDoc newServInstID, /* Project project,*/ EditableProperties projectProps, EditableProperties privateProps) {
182         // update j2ee.server.type
183
projectProps.setProperty(J2EE_SERVER_TYPE, Deployment.getDefault().getServerID(newServInstID));
184         
185         // update j2ee.server.instance
186
privateProps.setProperty(J2EE_SERVER_INSTANCE, newServInstID);
187     }
188     public static Object JavaDoc mapType(String JavaDoc string) {
189         Object JavaDoc retVal = J2eeModule.EJB;
190         if (string.equals(ArchiveProjectProperties.PROJECT_TYPE_VALUE_CAR)) {
191             retVal = J2eeModule.CLIENT;
192         }
193         if (string.equals(ArchiveProjectProperties.PROJECT_TYPE_VALUE_WAR)) {
194             retVal = J2eeModule.WAR;
195         }
196         if (string.equals(ArchiveProjectProperties.PROJECT_TYPE_VALUE_JAR)) {
197             retVal = J2eeModule.EJB;
198         }
199         if (string.equals(ArchiveProjectProperties.PROJECT_TYPE_VALUE_EAR)) {
200             retVal = J2eeModule.EAR;
201         }
202         if (string.equals(ArchiveProjectProperties.PROJECT_TYPE_VALUE_RAR)) {
203             retVal = J2eeModule.CONN;
204         }
205         return retVal;
206     }
207     
208 }
209
Popular Tags