KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > customizer > EjbJarProjectProperties


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.ui.customizer;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.math.BigDecimal JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.Vector JavaDoc;
35 import javax.swing.ButtonModel JavaDoc;
36 import javax.swing.ComboBoxModel JavaDoc;
37 import javax.swing.DefaultListModel JavaDoc;
38 import javax.swing.ListCellRenderer JavaDoc;
39 import javax.swing.table.DefaultTableModel JavaDoc;
40 import javax.swing.text.Document JavaDoc;
41 import org.netbeans.api.queries.CollocationQuery;
42 import org.netbeans.modules.j2ee.deployment.devmodules.api.AntDeploymentHelper;
43 import org.netbeans.modules.j2ee.ejbjarproject.SourceRoots;
44 import org.netbeans.modules.j2ee.ejbjarproject.UpdateHelper;
45 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
46 import org.openide.ErrorManager;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.modules.SpecificationVersion;
49 import org.openide.util.MutexException;
50 import org.openide.util.Mutex;
51 import org.netbeans.api.project.Project;
52 import org.netbeans.api.project.ProjectManager;
53 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
54 import org.netbeans.spi.project.support.ant.AntProjectHelper;
55 import org.netbeans.spi.project.support.ant.EditableProperties;
56 import org.netbeans.spi.project.support.ant.ReferenceHelper;
57 import org.netbeans.spi.project.support.ant.ui.StoreGroup;
58 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
59 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
60 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
61 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
62 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathSupport;
63 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
64 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProjectUtil;
65 import org.netbeans.modules.j2ee.ejbjarproject.Utils;
66 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
67 import org.netbeans.spi.project.support.ant.PropertyUtils;
68 import org.openide.filesystems.FileObject;
69 import org.openide.filesystems.URLMapper;
70
71
72 /** Helper class. Defines constants for properties. Knows the proper
73  * place where to store the properties.
74  *
75  * @author Petr Hrebejk
76  * @author Chris Webster
77  * @author Andrei Badea
78  */

79 public class EjbJarProjectProperties {
80     
81     public static final String JavaDoc JAVA_EE_5 = "1.5"; // NOI18N
82
public static final String JavaDoc J2EE_1_4 = "1.4"; // NOI18N
83
public static final String JavaDoc J2EE_1_3 = "1.3"; // NOI18N
84

85     // Special properties of the project
86
public static final String JavaDoc EJB_PROJECT_NAME = "j2ee.ejbjarproject.name"; // NOI18N
87
public static final String JavaDoc JAVA_PLATFORM = "platform.active"; // NOI18N
88
public static final String JavaDoc J2EE_PLATFORM = "j2ee.platform"; // NOI18N
89

90     // Properties stored in the PROJECT.PROPERTIES
91
/** root of external web module sources (full path), ".." if the sources are within project folder */
92     public static final String JavaDoc SOURCE_ROOT = "source.root"; // NOI18N
93
public static final String JavaDoc BUILD_FILE = "buildfile"; // NOI18N
94
public static final String JavaDoc LIBRARIES_DIR = "lib.dir"; //NOI18N
95
public static final String JavaDoc DIST_DIR = "dist.dir"; // NOI18N
96
public static final String JavaDoc DIST_JAR = "dist.jar"; // NOI18N
97
public static final String JavaDoc DIST_EAR_JAR = "dist.ear.jar"; //NOI18N
98
public static final String JavaDoc JAVAC_CLASSPATH = "javac.classpath"; // NOI18N
99
public static final String JavaDoc DEBUG_CLASSPATH = "debug.classpath"; // NOI18N
100

101     public static final String JavaDoc JAR_NAME = "jar.name"; // NOI18N
102
public static final String JavaDoc JAR_COMPRESS = "jar.compress"; // NOI18N
103

104     public static final String JavaDoc J2EE_SERVER_INSTANCE = "j2ee.server.instance"; // NOI18N
105
public static final String JavaDoc J2EE_SERVER_TYPE = "j2ee.server.type"; // NOI18N
106
public static final String JavaDoc J2EE_PLATFORM_CLASSPATH = "j2ee.platform.classpath"; //NOI18N
107
public static final String JavaDoc JAVAC_SOURCE = "javac.source"; // NOI18N
108
public static final String JavaDoc JAVAC_DEBUG = "javac.debug"; // NOI18N
109
public static final String JavaDoc JAVAC_DEPRECATION = "javac.deprecation"; // NOI18N
110
public static final String JavaDoc JAVAC_TARGET = "javac.target"; // NOI18N
111

112     public static final String JavaDoc JAVAC_COMPILER_ARG = "javac.compilerargs"; //NOI18N
113
public static final String JavaDoc SRC_DIR = "src.dir"; // NOI18N
114
public static final String JavaDoc TEST_SRC_DIR = "test.src.dir"; // NOI18N
115
public static final String JavaDoc META_INF = "meta.inf"; // NOI18N
116
public static final String JavaDoc RESOURCE_DIR = "resource.dir"; // NOI18N
117
public static final String JavaDoc BUILD_DIR = "build.dir"; // NOI18N
118
public static final String JavaDoc BUILD_GENERATED_DIR = "build.generated.dir"; // NOI18N
119
public static final String JavaDoc BUILD_CLASSES_DIR = "build.classes.dir"; // NOI18N
120
public static final String JavaDoc BUILD_EAR_CLASSES_DIR = "build.ear.classes.dir"; // NOI18N
121
public static final String JavaDoc BUILD_CLASSES_EXCLUDES = "build.classes.excludes"; // NOI18N
122

123     public static final String JavaDoc BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; // NOI18N
124
public static final String JavaDoc BUILD_TEST_RESULTS_DIR = "build.test.results.dir"; // NOI18N
125
public static final String JavaDoc JAVAC_TEST_CLASSPATH = "javac.test.classpath"; // NOI18N
126
public static final String JavaDoc RUN_TEST_CLASSPATH = "run.test.classpath"; // NOI18N
127
public static final String JavaDoc DEBUG_TEST_CLASSPATH = "debug.test.classpath"; // NOI18N
128

129     public static final String JavaDoc NO_DEPENDENCIES="no.dependencies"; //NOI18N
130
public static final String JavaDoc DIST_JAVADOC_DIR = "dist.javadoc.dir"; // NOI18N
131
public static final String JavaDoc JAVADOC_PRIVATE="javadoc.private"; // NOI18N
132
public static final String JavaDoc JAVADOC_NO_TREE="javadoc.notree"; // NOI18N
133
public static final String JavaDoc JAVADOC_USE="javadoc.use"; // NOI18N
134
public static final String JavaDoc JAVADOC_NO_NAVBAR="javadoc.nonavbar"; // NOI18N
135
public static final String JavaDoc JAVADOC_NO_INDEX="javadoc.noindex"; // NOI18N
136
public static final String JavaDoc JAVADOC_SPLIT_INDEX="javadoc.splitindex"; // NOI18N
137
public static final String JavaDoc JAVADOC_AUTHOR="javadoc.author"; // NOI18N
138
public static final String JavaDoc JAVADOC_VERSION="javadoc.version"; // NOI18N
139
public static final String JavaDoc JAVADOC_WINDOW_TITLE="javadoc.windowtitle"; // NOI18N
140
public static final String JavaDoc JAVADOC_ENCODING="javadoc.encoding"; // NOI18N
141
public static final String JavaDoc JAVADOC_ADDITIONALPARAM="javadoc.additionalparam"; // NOI18N
142

143     public static final String JavaDoc META_INF_EXCLUDES="meta.inf.excludes"; // NOI18N
144

145     // Properties stored in the PRIVATE.PROPERTIES
146
public static final String JavaDoc JAVADOC_PREVIEW="javadoc.preview"; // NOI18N
147

148     public static final String JavaDoc DEPLOY_ANT_PROPS_FILE = "deploy.ant.properties.file"; //NOI18N
149

150     public static final String JavaDoc ANT_DEPLOY_BUILD_SCRIPT = "nbproject/ant-deploy.xml"; // NOI18N
151

152     public static final String JavaDoc JAVA_SOURCE_BASED = "java.source.based";
153     
154     public static final String JavaDoc[] WELL_KNOWN_PATHS = new String JavaDoc[] {
155         "${" + JAVAC_CLASSPATH + "}", // NOI18N
156
"${" + JAVAC_TEST_CLASSPATH + "}", // NOI18N
157
"${" + RUN_TEST_CLASSPATH + "}", // NOI18N
158
"${" + BUILD_CLASSES_DIR + "}", // NOI18N
159
"${" + BUILD_TEST_CLASSES_DIR + "}" // NOI18N
160
};
161    
162     // Prefixes and suffixes of classpath
163
public static final String JavaDoc LIBRARY_PREFIX = "${libs."; // NOI18N
164
public static final String JavaDoc LIBRARY_SUFFIX = ".classpath}"; // NOI18N
165
public static final String JavaDoc ANT_ARTIFACT_PREFIX = "${reference."; // NOI18N
166

167     private ClassPathSupport cs;
168     
169     
170     // SOURCE ROOTS
171
// public static final String SOURCE_ROOTS = "__virtual_source_roots__"; //NOI18N
172
// public static final String TEST_ROOTS = "__virtual_test_roots__"; //NOI18N
173

174     // MODELS FOR VISUAL CONTROLS
175

176     // CustomizerSources
177
DefaultTableModel JavaDoc SOURCE_ROOTS_MODEL;
178     DefaultTableModel JavaDoc TEST_ROOTS_MODEL;
179     Document JavaDoc META_INF_MODEL;
180     ComboBoxModel JavaDoc JAVAC_SOURCE_MODEL;
181      
182     // CustomizerLibraries
183
ClassPathUiSupport.ClassPathTableModel JAVAC_CLASSPATH_MODEL;
184     DefaultListModel JavaDoc JAVAC_TEST_CLASSPATH_MODEL;
185     
186     //DefaultListModel RUN_CLASSPATH_MODEL;
187
DefaultListModel JavaDoc RUN_TEST_CLASSPATH_MODEL;
188     ComboBoxModel JavaDoc PLATFORM_MODEL;
189     ListCellRenderer JavaDoc CLASS_PATH_LIST_RENDERER;
190     ListCellRenderer JavaDoc PLATFORM_LIST_RENDERER;
191     EjbJarClassPathUi.ClassPathTableCellItemRenderer CLASS_PATH_TABLE_ITEM_RENDERER;
192     
193     // CustomizerCompile
194
ButtonModel JavaDoc JAVAC_DEPRECATION_MODEL;
195     ButtonModel JavaDoc JAVAC_DEBUG_MODEL;
196     ButtonModel JavaDoc NO_DEPENDENCIES_MODEL;
197     Document JavaDoc JAVAC_COMPILER_ARG_MODEL;
198     
199     // CustomizerCompileTest
200

201     // CustomizerJar
202
Document JavaDoc DIST_JAR_MODEL;
203     Document JavaDoc BUILD_CLASSES_EXCLUDES_MODEL;
204     ButtonModel JavaDoc JAR_COMPRESS_MODEL;
205                 
206     // CustomizerJavadoc
207
ButtonModel JavaDoc JAVADOC_PRIVATE_MODEL;
208     ButtonModel JavaDoc JAVADOC_NO_TREE_MODEL;
209     ButtonModel JavaDoc JAVADOC_USE_MODEL;
210     ButtonModel JavaDoc JAVADOC_NO_NAVBAR_MODEL;
211     ButtonModel JavaDoc JAVADOC_NO_INDEX_MODEL;
212     ButtonModel JavaDoc JAVADOC_SPLIT_INDEX_MODEL;
213     ButtonModel JavaDoc JAVADOC_AUTHOR_MODEL;
214     ButtonModel JavaDoc JAVADOC_VERSION_MODEL;
215     Document JavaDoc JAVADOC_WINDOW_TITLE_MODEL;
216     ButtonModel JavaDoc JAVADOC_PREVIEW_MODEL;
217     Document JavaDoc JAVADOC_ADDITIONALPARAM_MODEL;
218
219     // CustomizerRun
220
ComboBoxModel JavaDoc J2EE_SERVER_INSTANCE_MODEL;
221     ComboBoxModel JavaDoc J2EE_PLATFORM_MODEL;
222
223     // CustomizerRunTest
224

225     // Private fields ----------------------------------------------------------
226
private EjbJarProject project;
227     private PropertyEvaluator evaluator;
228     private ReferenceHelper refHelper;
229     private UpdateHelper updateHelper;
230     
231     private StoreGroup privateGroup;
232     private StoreGroup projectGroup;
233     
234     private Properties JavaDoc additionalProperties;
235     
236     Project getProject() {
237         return project;
238     }
239
240     /** Creates a new instance of EjbJarProjectProperties and initializes them */
241     public EjbJarProjectProperties(EjbJarProject project, UpdateHelper updateHelper, PropertyEvaluator evaluator, ReferenceHelper refHelper ) {
242         this.project = project;
243         this.updateHelper = updateHelper;
244         this.evaluator = evaluator;
245         this.refHelper = refHelper;
246         
247         cs = new ClassPathSupport( evaluator, refHelper, updateHelper.getAntProjectHelper(), WELL_KNOWN_PATHS, LIBRARY_PREFIX, LIBRARY_SUFFIX, ANT_ARTIFACT_PREFIX );
248         
249         privateGroup = new StoreGroup();
250         projectGroup = new StoreGroup();
251         
252         additionalProperties = new Properties JavaDoc();
253         
254         init(); // Load known properties
255
}
256     
257     /** Initializes the visual models
258      */

259     private void init() {
260         
261         CLASS_PATH_LIST_RENDERER = new EjbJarClassPathUi.ClassPathListCellRenderer( evaluator );
262         CLASS_PATH_TABLE_ITEM_RENDERER = new EjbJarClassPathUi.ClassPathTableCellItemRenderer( evaluator );
263         
264         // CustomizerSources
265
SOURCE_ROOTS_MODEL = EjbJarSourceRootsUi.createModel( project.getSourceRoots() );
266         TEST_ROOTS_MODEL = EjbJarSourceRootsUi.createModel( project.getTestSourceRoots() );
267         META_INF_MODEL = projectGroup.createStringDocument( evaluator, META_INF );
268                 
269         // CustomizerLibraries
270
EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
271         EditableProperties privateProperties = updateHelper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
272         
273         JAVAC_CLASSPATH_MODEL = ClassPathUiSupport.createTableModel( cs.itemsIterator( (String JavaDoc)projectProperties.get( JAVAC_CLASSPATH ), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES ) );
274         JAVAC_TEST_CLASSPATH_MODEL = ClassPathUiSupport.createListModel( cs.itemsIterator( (String JavaDoc)projectProperties.get( JAVAC_TEST_CLASSPATH ), null ) );
275         RUN_TEST_CLASSPATH_MODEL = ClassPathUiSupport.createListModel( cs.itemsIterator( (String JavaDoc)projectProperties.get( RUN_TEST_CLASSPATH ), null ) );
276         PLATFORM_MODEL = PlatformUiSupport.createPlatformComboBoxModel (evaluator.getProperty(JAVA_PLATFORM));
277         PLATFORM_LIST_RENDERER = PlatformUiSupport.createPlatformListCellRenderer();
278         JAVAC_SOURCE_MODEL = PlatformUiSupport.createSourceLevelComboBoxModel (PLATFORM_MODEL, evaluator.getProperty(JAVAC_SOURCE), evaluator.getProperty(J2EE_PLATFORM));
279                 
280         // CustomizerCompile
281
JAVAC_DEPRECATION_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVAC_DEPRECATION );
282         JAVAC_DEBUG_MODEL = privateGroup.createToggleButtonModel( evaluator, JAVAC_DEBUG );
283         NO_DEPENDENCIES_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, NO_DEPENDENCIES );
284         JAVAC_COMPILER_ARG_MODEL = projectGroup.createStringDocument( evaluator, JAVAC_COMPILER_ARG );
285         
286         // CustomizerJar
287
DIST_JAR_MODEL = projectGroup.createStringDocument( evaluator, DIST_JAR );
288         BUILD_CLASSES_EXCLUDES_MODEL = projectGroup.createStringDocument( evaluator, BUILD_CLASSES_EXCLUDES );
289         JAR_COMPRESS_MODEL = projectGroup.createToggleButtonModel( evaluator, JAR_COMPRESS );
290         
291         // CustomizerJavadoc
292
JAVADOC_PRIVATE_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_PRIVATE );
293         JAVADOC_NO_TREE_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, JAVADOC_NO_TREE );
294         JAVADOC_USE_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_USE );
295         JAVADOC_NO_NAVBAR_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, JAVADOC_NO_NAVBAR );
296         JAVADOC_NO_INDEX_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, JAVADOC_NO_INDEX );
297         JAVADOC_SPLIT_INDEX_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_SPLIT_INDEX );
298         JAVADOC_AUTHOR_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_AUTHOR );
299         JAVADOC_VERSION_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_VERSION );
300         JAVADOC_WINDOW_TITLE_MODEL = projectGroup.createStringDocument( evaluator, JAVADOC_WINDOW_TITLE );
301         JAVADOC_PREVIEW_MODEL = privateGroup.createToggleButtonModel( evaluator, JAVADOC_PREVIEW );
302         JAVADOC_ADDITIONALPARAM_MODEL = projectGroup.createStringDocument( evaluator, JAVADOC_ADDITIONALPARAM );
303         
304         // CustomizerRun
305
J2EE_SERVER_INSTANCE_MODEL = J2eePlatformUiSupport.createPlatformComboBoxModel(
306             privateProperties.getProperty( J2EE_SERVER_INSTANCE ));
307         J2EE_PLATFORM_MODEL = J2eePlatformUiSupport.createSpecVersionComboBoxModel(
308             projectProperties.getProperty( J2EE_PLATFORM ));
309     }
310     
311     public void save() {
312         try {
313             // Store properties
314
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
315                 public Object JavaDoc run() throws IOException JavaDoc {
316                     storeProperties();
317                     return null;
318                 }
319             });
320             // and save the project
321
ProjectManager.getDefault().saveProject(project);
322         }
323         catch (MutexException e) {
324             ErrorManager.getDefault().notify((IOException JavaDoc)e.getException());
325         }
326         catch ( IOException JavaDoc ex ) {
327             ErrorManager.getDefault().notify( ex );
328         }
329     }
330         
331     private void storeProperties() throws IOException JavaDoc {
332         // Store special properties
333

334         // Modify the project dependencies properly
335
resolveProjectDependenciesNew();
336         
337         // Encode all paths (this may change the project properties)
338
String JavaDoc[] javac_cp = cs.encodeToStrings( ClassPathUiSupport.getIterator( JAVAC_CLASSPATH_MODEL.getDefaultListModel() ), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES );
339         String JavaDoc[] javac_test_cp = cs.encodeToStrings( ClassPathUiSupport.getIterator( JAVAC_TEST_CLASSPATH_MODEL ), null );
340         String JavaDoc[] run_test_cp = cs.encodeToStrings( ClassPathUiSupport.getIterator( RUN_TEST_CLASSPATH_MODEL ), null );
341                 
342         // Store source roots
343
storeRoots( project.getSourceRoots(), SOURCE_ROOTS_MODEL );
344         storeRoots( project.getTestSourceRoots(), TEST_ROOTS_MODEL );
345                 
346         // Store standard properties
347
EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
348         EditableProperties privateProperties = updateHelper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
349         
350         // Assure inegrity which can't shound not be assured in UI
351
if ( !JAVADOC_NO_INDEX_MODEL.isSelected() ) {
352             JAVADOC_SPLIT_INDEX_MODEL.setSelected( false ); // Can't split non existing index
353
}
354                                 
355         // Standard store of the properties
356
projectGroup.store( projectProperties );
357         privateGroup.store( privateProperties );
358                 
359         // Save all paths
360
projectProperties.setProperty( JAVAC_CLASSPATH, javac_cp );
361         projectProperties.setProperty( JAVAC_TEST_CLASSPATH, javac_test_cp );
362         projectProperties.setProperty( RUN_TEST_CLASSPATH, run_test_cp );
363         
364         //Handle platform selection and javac.source javac.target properties
365
SpecificationVersion sourceLevel = (SpecificationVersion) JAVAC_SOURCE_MODEL.getSelectedItem();
366         PlatformUiSupport.storePlatform (projectProperties, updateHelper, PLATFORM_MODEL.getSelectedItem(), sourceLevel);
367                 
368         // Handle other special cases
369
if ( NO_DEPENDENCIES_MODEL.isSelected() ) { // NOI18N
370
projectProperties.remove( NO_DEPENDENCIES ); // Remove the property completely if not set
371
}
372         
373         // Set new server instance ID
374
if (J2EE_SERVER_INSTANCE_MODEL.getSelectedItem() != null) {
375             setNewServerInstanceValue(J2eePlatformUiSupport.getServerInstanceID(J2EE_SERVER_INSTANCE_MODEL.getSelectedItem()), project, projectProperties, privateProperties);
376         }
377         
378         // Update the deployment descriptor if upgrading from J2EE 1.3 to 1.4 and set the new J2EE spec version
379
String JavaDoc oldJ2eeVersion = projectProperties.getProperty(J2EE_PLATFORM);
380         String JavaDoc newJ2eeVersion = J2eePlatformUiSupport.getSpecVersion(J2EE_PLATFORM_MODEL.getSelectedItem());
381         if (oldJ2eeVersion != null && newJ2eeVersion != null) {
382             if (oldJ2eeVersion.equals(J2EE_1_3) && newJ2eeVersion.equals(J2EE_1_4)) {
383                 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJarModules[] = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project);
384                 if (ejbJarModules.length > 0) {
385                     FileObject ddFo = ejbJarModules[0].getDeploymentDescriptor();
386                     if (ddFo != null) {
387                         EjbJar ddRoot = DDProvider.getDefault().getMergedDDRoot(ejbJarModules[0].getMetadataUnit());
388                         if (ddRoot != null) {
389                             ddRoot.setVersion(new BigDecimal JavaDoc(EjbJar.VERSION_2_1));
390                             ddRoot.write(ddFo);
391                         }
392                     }
393                 }
394             }
395             
396             // Set the new J2EE spec version
397
projectProperties.setProperty(J2EE_PLATFORM, newJ2eeVersion);
398         }
399         
400         storeAdditionalProperties(projectProperties);
401
402         storeLibrariesLocations (ClassPathUiSupport.getList(JAVAC_CLASSPATH_MODEL.getDefaultListModel()).iterator(), privateProperties);
403         
404         // Store the property changes into the project
405
updateHelper.putProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties );
406         updateHelper.putProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProperties );
407         
408     }
409     
410     private void storeAdditionalProperties(EditableProperties projectProperties) {
411         for (Iterator JavaDoc i = additionalProperties.keySet().iterator(); i.hasNext();) {
412             String JavaDoc key = i.next().toString();
413             projectProperties.put(key, additionalProperties.getProperty(key));
414         }
415     }
416     
417     /** Finds out what are new and removed project dependencies and
418      * applyes the info to the project
419      */

420     private void resolveProjectDependenciesNew() {
421             
422         // Create a set of old and new artifacts.
423
Set JavaDoc oldArtifacts = new HashSet JavaDoc();
424         EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
425         oldArtifacts.addAll( cs.itemsList( (String JavaDoc)projectProperties.get( JAVAC_CLASSPATH ), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES ) );
426         oldArtifacts.addAll( cs.itemsList( (String JavaDoc)projectProperties.get( JAVAC_TEST_CLASSPATH ), null ) );
427         oldArtifacts.addAll( cs.itemsList( (String JavaDoc)projectProperties.get( RUN_TEST_CLASSPATH ), null ) );
428                    
429         Set JavaDoc newArtifacts = new HashSet JavaDoc();
430         newArtifacts.addAll( ClassPathUiSupport.getList( JAVAC_CLASSPATH_MODEL.getDefaultListModel() ) );
431         newArtifacts.addAll( ClassPathUiSupport.getList( JAVAC_TEST_CLASSPATH_MODEL ) );
432         newArtifacts.addAll( ClassPathUiSupport.getList( RUN_TEST_CLASSPATH_MODEL ) );
433                 
434         // Create set of removed artifacts and remove them
435
Set JavaDoc removed = new HashSet JavaDoc( oldArtifacts );
436         removed.removeAll( newArtifacts );
437         Set JavaDoc added = new HashSet JavaDoc(newArtifacts);
438         added.removeAll(oldArtifacts);
439         
440         // 1. first remove all project references. The method will modify
441
// project property files, so it must be done separately
442
for( Iterator JavaDoc it = removed.iterator(); it.hasNext(); ) {
443             ClassPathSupport.Item item = (ClassPathSupport.Item)it.next();
444             if ( item.getType() == ClassPathSupport.Item.TYPE_ARTIFACT ||
445                     item.getType() == ClassPathSupport.Item.TYPE_JAR ) {
446                 refHelper.destroyReference(item.getReference());
447             }
448         }
449         
450         // 2. now read project.properties and modify rest
451
EditableProperties ep = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
452         boolean changed = false;
453         
454         for( Iterator JavaDoc it = removed.iterator(); it.hasNext(); ) {
455             ClassPathSupport.Item item = (ClassPathSupport.Item)it.next();
456             if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
457                 // remove helper property pointing to library jar if there is any
458
String JavaDoc prop = item.getReference();
459                 prop = prop.substring(2, prop.length()-1);
460                 ep.remove(prop);
461                 changed = true;
462             }
463         }
464         File JavaDoc projDir = FileUtil.toFile(updateHelper.getAntProjectHelper().getProjectDirectory());
465         for( Iterator JavaDoc it = added.iterator(); it.hasNext(); ) {
466             ClassPathSupport.Item item = (ClassPathSupport.Item)it.next();
467             if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
468                 // add property to project.properties pointing to relativized
469
// library jar(s) if possible
470
String JavaDoc prop = cs.getLibraryReference( item );
471                 prop = prop.substring(2, prop.length()-1); // XXX make a PropertyUtils method for this!
472
String JavaDoc value = relativizeLibraryClasspath(prop, projDir);
473                 if (value != null) {
474                     ep.setProperty(prop, value);
475                     ep.setComment(prop, new String JavaDoc[]{
476                         // XXX this should be I18N! Not least because the English is wrong...
477
"# Property "+prop+" is set here just to make sharing of project simpler.", // NOI18N
478
"# The library definition has always preference over this property."}, false); // NOI18N
479
changed = true;
480                 }
481             }
482         }
483         if (changed) {
484             updateHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
485         }
486     }
487     
488     /**
489      * Tokenize library classpath and try to relativize all the jars.
490      * @param property library property name ala "libs.someLib.classpath"
491      * @param projectDir project dir for relativization
492      * @return relativized library classpath or null if some jar is not collocated
493      */

494     private String JavaDoc relativizeLibraryClasspath(String JavaDoc property, File JavaDoc projectDir) {
495         String JavaDoc value = PropertyUtils.getGlobalProperties().getProperty(property);
496         // bugfix #42852, check if the classpath property is set, otherwise return null
497
if (value == null) {
498             return null;
499         }
500         String JavaDoc[] paths = PropertyUtils.tokenizePath(value);
501         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
502         for (int i=0; i<paths.length; i++) {
503             File JavaDoc f = updateHelper.getAntProjectHelper().resolveFile(paths[i]);
504             if (CollocationQuery.areCollocated(f, projectDir)) {
505                 sb.append(PropertyUtils.relativizeFile(projectDir, f));
506             } else {
507                 return null;
508             }
509             if (i+1<paths.length) {
510                 sb.append(File.pathSeparatorChar);
511             }
512         }
513         if (sb.length() == 0) {
514             return null;
515         } else {
516             return sb.toString();
517         }
518     }
519     
520     private void storeRoots( SourceRoots roots, DefaultTableModel JavaDoc tableModel ) throws MalformedURLException JavaDoc {
521         Vector JavaDoc data = tableModel.getDataVector();
522         URL JavaDoc[] rootURLs = new URL JavaDoc[data.size()];
523         String JavaDoc []rootLabels = new String JavaDoc[data.size()];
524         for (int i=0; i<data.size();i++) {
525             File JavaDoc f = ((File JavaDoc)((Vector JavaDoc)data.elementAt(i)).elementAt(0));
526             rootURLs[i] = EjbJarProjectUtil.getRootURL(f,null);
527             rootLabels[i] = (String JavaDoc) ((Vector JavaDoc)data.elementAt(i)).elementAt(1);
528         }
529         roots.putRoots(rootURLs,rootLabels);
530     }
531     
532     public static void setServerInstance(final Project project, final AntProjectHelper helper, final String JavaDoc serverInstanceID) {
533         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
534             public void run() {
535                 try {
536                     EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
537                     EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
538                     setNewServerInstanceValue(serverInstanceID, project, projectProps, privateProps);
539                     helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
540                     helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
541                     ProjectManager.getDefault().saveProject(project);
542                 }
543                 catch (IOException JavaDoc e) {
544                     ErrorManager.getDefault().notify();
545                 }
546             }
547         });
548     }
549     
550     /* This is used by CustomizerWSServiceHost */
551     void putAdditionalProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
552         additionalProperties.setProperty(propertyName, propertyValue);
553     }
554     
555     private static void setNewServerInstanceValue(String JavaDoc newServInstID, Project project, EditableProperties projectProps, EditableProperties privateProps) {
556         // update j2ee.platform.classpath
557
String JavaDoc oldServInstID = privateProps.getProperty(J2EE_SERVER_INSTANCE);
558         if (oldServInstID != null) {
559             J2eePlatform oldJ2eePlatform = Deployment.getDefault().getJ2eePlatform(oldServInstID);
560             if (oldJ2eePlatform != null) {
561                 ((EjbJarProject)project).unregisterJ2eePlatformListener(oldJ2eePlatform);
562             }
563         }
564         J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(newServInstID);
565         if (j2eePlatform == null) {
566             // probably missing server error
567
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "J2EE platform is null."); // NOI18N
568

569             // update j2ee.server.type (throws NPE)
570
//projectProps.setProperty(J2EE_SERVER_TYPE, Deployment.getDefault().getServerID(newServInstID));
571
// update j2ee.server.instance
572
privateProps.setProperty(J2EE_SERVER_INSTANCE, newServInstID);
573             
574             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH);
575             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH);
576             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH);
577             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSIT_CLASSPATH);
578             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH);
579             
580             privateProps.remove(DEPLOY_ANT_PROPS_FILE);
581             return;
582         }
583         ((EjbJarProject)project).registerJ2eePlatformListener(j2eePlatform);
584         String JavaDoc classpath = Utils.toClasspathString(j2eePlatform.getClasspathEntries());
585         privateProps.setProperty(J2EE_PLATFORM_CLASSPATH, classpath);
586
587         // update j2ee.platform.wscompile.classpath
588
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE)) {
589             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSCOMPILE);
590             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH,
591                     Utils.toClasspathString(wsClasspath));
592         } else {
593             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH);
594         }
595         
596         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSGEN)) {
597             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSGEN);
598             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH,
599                     Utils.toClasspathString(wsClasspath));
600         } else {
601             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH);
602         }
603
604         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIMPORT)) {
605             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSIMPORT);
606             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH,
607                     Utils.toClasspathString(wsClasspath));
608         } else {
609             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH);
610         }
611
612         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIT)) {
613             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSIT);
614             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSIT_CLASSPATH,
615                     Utils.toClasspathString(wsClasspath));
616         } else {
617             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSIT_CLASSPATH);
618         }
619
620         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP)) {
621             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_JWSDP);
622             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH,
623                     Utils.toClasspathString(wsClasspath));
624         } else {
625             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH);
626         }
627         
628         // update j2ee.server.type
629
projectProps.setProperty(J2EE_SERVER_TYPE, Deployment.getDefault().getServerID(newServInstID));
630         
631         // update j2ee.server.instance
632
privateProps.setProperty(J2EE_SERVER_INSTANCE, newServInstID);
633         
634         // ant deployment support
635
File JavaDoc projectFolder = FileUtil.toFile(project.getProjectDirectory());
636         try {
637             AntDeploymentHelper.writeDeploymentScript(new File JavaDoc(projectFolder, ANT_DEPLOY_BUILD_SCRIPT), J2eeModule.EJB, newServInstID); // NOI18N
638
} catch (IOException JavaDoc ioe) {
639             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
640         }
641         File JavaDoc deployAntPropsFile = AntDeploymentHelper.getDeploymentPropertiesFile(newServInstID);
642         if (deployAntPropsFile == null) {
643             privateProps.remove(DEPLOY_ANT_PROPS_FILE);
644         } else {
645             privateProps.setProperty(DEPLOY_ANT_PROPS_FILE, deployAntPropsFile.getAbsolutePath());
646         }
647     }
648     
649     public static String JavaDoc getProperty(final String JavaDoc property, final AntProjectHelper helper, final String JavaDoc path) {
650         EditableProperties props = helper.getProperties(path);
651         return props.getProperty(property);
652     }
653     
654     public static String JavaDoc getAntPropertyName( String JavaDoc property ) {
655         if ( property != null &&
656              property.startsWith( "${" ) && // NOI18N
657
property.endsWith( "}" ) ) { // NOI18N
658
return property.substring( 2, property.length() - 1 );
659         }
660         else {
661             return property;
662         }
663     }
664     
665      /** Store locations of libraries in the classpath param that have more the one
666      * file into the properties in the following format:
667      *
668      * <ul>
669      * <li>libs.foo.classpath.libdir.1=C:/foo
670      * <li>libs.foo.classpath.libdirs=1
671      * <li>libs.foo.classpath.libfile.1=C:/bar/a.jar
672      * <li>libs.foo.classpath.libfile.2=C:/bar/b.jar
673      * <li>libs.foo.classpath.libfiles=1
674      * </ul>
675      * This is needed for the Ant copy task as it cannot copy more the one file
676      * and it needs different handling for files and directories.
677      * <br>
678      * It removes all properties that match this format that were in the {@link #properties}
679      * but are not in the {@link #classpath}.
680      */

681     public static void storeLibrariesLocations (Iterator JavaDoc /*<Item>*/ classpath, EditableProperties privateProps) {
682         ArrayList JavaDoc exLibs = new ArrayList JavaDoc ();
683         Iterator JavaDoc propKeys = privateProps.keySet().iterator();
684         while (propKeys.hasNext()) {
685             String JavaDoc key = (String JavaDoc) propKeys.next();
686             if (key.endsWith(".libdirs") || key.endsWith(".libfiles") || //NOI18N
687
(key.indexOf(".libdir.") > 0) || (key.indexOf(".libfile.") > 0)) { //NOI18N
688
exLibs.add(key);
689             }
690         }
691         while (classpath.hasNext()) {
692             ClassPathSupport.Item item = (ClassPathSupport.Item)classpath.next();
693             ArrayList JavaDoc /*File*/ files = new ArrayList JavaDoc ();
694             ArrayList JavaDoc /*File*/ dirs = new ArrayList JavaDoc ();
695             getFilesForItem (item, files, dirs);
696             String JavaDoc key;
697             if (files.size() > 1 || (files.size()>0 && dirs.size()>0)) {
698                 String JavaDoc ref = item.getReference() == null ? item.getRaw() : item.getReference();
699                 for (int i = 0; i < files.size(); i++) {
700                     File JavaDoc f = (File JavaDoc) files.get(i);
701                     key = getAntPropertyName(ref)+".libfile." + (i+1); //NOI18N
702
privateProps.setProperty (key, "" + f.getAbsolutePath()); //NOI18N
703
exLibs.remove(key);
704                 }
705             }
706             if (dirs.size() > 1 || (files.size()>0 && dirs.size()>0)) {
707                 String JavaDoc ref = item.getReference() == null ? item.getRaw() : item.getReference();
708                 for (int i = 0; i < dirs.size(); i++) {
709                     File JavaDoc f = (File JavaDoc) dirs.get(i);
710                     key = getAntPropertyName(ref)+".libdir." + (i+1); //NOI18N
711
privateProps.setProperty (key, "" + f.getAbsolutePath()); //NOI18N
712
exLibs.remove(key);
713                 }
714             }
715         }
716         Iterator JavaDoc unused = exLibs.iterator();
717         while (unused.hasNext()) {
718             privateProps.remove(unused.next());
719         }
720     }
721     
722     public static final void getFilesForItem (ClassPathSupport.Item item, List JavaDoc/*File*/ files, List JavaDoc/*File*/ dirs) {
723         if (item.isBroken()) {
724             return ;
725         }
726         if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
727             List JavaDoc/*<URL>*/ roots = item.getLibrary().getContent("classpath"); //NOI18N
728
for (Iterator JavaDoc it = roots.iterator(); it.hasNext();) {
729                 URL JavaDoc rootUrl = (URL JavaDoc) it.next();
730                 FileObject root = URLMapper.findFileObject (rootUrl);
731                 if ("jar".equals(rootUrl.getProtocol())) { //NOI18N
732
root = FileUtil.getArchiveFile (root);
733                 }
734                 File JavaDoc f = FileUtil.toFile(root);
735                 if (f != null) {
736                     if (f.isFile()) {
737                         files.add(f);
738                     } else {
739                         dirs.add(f);
740                     }
741                 }
742             }
743         }
744         if (item.getType() == ClassPathSupport.Item.TYPE_JAR) {
745             File JavaDoc root = item.getFile();
746             if (root != null) {
747                 if (root.isFile()) {
748                     files.add(root);
749                 } else {
750                     dirs.add(root);
751                 }
752             }
753         }
754         if (item.getType() == ClassPathSupport.Item.TYPE_ARTIFACT) {
755             String JavaDoc artifactFolder = item.getArtifact().getScriptLocation().getParent();
756             URI JavaDoc roots[] = item.getArtifact().getArtifactLocations();
757             for (int i = 0; i < roots.length; i++) {
758                 String JavaDoc root = artifactFolder + File.separator + roots [i];
759                 if (root.endsWith(File.separator)) {
760                     dirs.add(new File JavaDoc (root));
761                 } else {
762                     files.add(new File JavaDoc (root));
763                 }
764             }
765         }
766     }
767 }
768
Popular Tags