KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > customizer > WebProjectProperties


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

75 public class WebProjectProperties {
76     
77     public static final String JavaDoc J2EE_1_4 = "1.4"; // NOI18N
78
public static final String JavaDoc J2EE_1_3 = "1.3"; // NOI18N
79

80     // Special properties of the project
81
public static final String JavaDoc WEB_PROJECT_NAME = "web.project.name"; //NOI18N
82
public static final String JavaDoc JAVA_PLATFORM = "platform.active"; //NOI18N
83
public static final String JavaDoc J2EE_PLATFORM = "j2ee.platform"; //NOI18N
84

85     // Properties stored in the PROJECT.PROPERTIES
86
/** root of external web module sources (full path), ".." if the sources are within project folder */
87     public static final String JavaDoc SOURCE_ROOT = "source.root"; //NOI18N
88
public static final String JavaDoc BUILD_FILE = "buildfile"; //NOI18N
89
public static final String JavaDoc LIBRARIES_DIR = "lib.dir"; //NOI18N
90
public static final String JavaDoc DIST_DIR = "dist.dir"; //NOI18N
91
public static final String JavaDoc DIST_WAR = "dist.war"; //NOI18N
92
public static final String JavaDoc DIST_WAR_EAR = "dist.ear.war"; //NOI18N
93
public static final String JavaDoc JAVAC_CLASSPATH = "javac.classpath"; //NOI18N
94
public static final String JavaDoc DEBUG_CLASSPATH = "debug.classpath"; //NOI18N
95
public static final String JavaDoc JSPCOMPILATION_CLASSPATH = "jspcompilation.classpath"; //NOI18N
96

97     public static final String JavaDoc WAR_NAME = "war.name"; //NOI18N
98
public static final String JavaDoc WAR_EAR_NAME = "war.ear.name"; //NOI18N
99
public static final String JavaDoc WAR_COMPRESS = "jar.compress"; //NOI18N
100
public static final String JavaDoc WAR_PACKAGE = "war.package"; //NOI18N
101
public static final String JavaDoc WAR_CONTENT_ADDITIONAL = "war.content.additional"; //NOI18N
102

103     public static final String JavaDoc LAUNCH_URL_RELATIVE = "client.urlPart"; //NOI18N
104
public static final String JavaDoc DISPLAY_BROWSER = "display.browser"; //NOI18N
105
public static final String JavaDoc CONTEXT_PATH = "context.path"; //NOI18N
106
public static final String JavaDoc J2EE_SERVER_INSTANCE = "j2ee.server.instance"; //NOI18N
107
public static final String JavaDoc J2EE_SERVER_TYPE = "j2ee.server.type"; //NOI18N
108
public static final String JavaDoc J2EE_PLATFORM_CLASSPATH = "j2ee.platform.classpath"; //NOI18N
109
public static final String JavaDoc JAVAC_SOURCE = "javac.source"; //NOI18N
110
public static final String JavaDoc JAVAC_DEBUG = "javac.debug"; //NOI18N
111
public static final String JavaDoc JAVAC_DEPRECATION = "javac.deprecation"; //NOI18N
112
public static final String JavaDoc JAVAC_COMPILER_ARG = "javac.compilerargs"; //NOI18N
113
public static final String JavaDoc JAVAC_TARGET = "javac.target"; //NOI18N
114
public static final String JavaDoc SRC_DIR = "src.dir"; //NOI18N
115
public static final String JavaDoc TEST_SRC_DIR = "test.src.dir"; //NOI18N
116
public static final String JavaDoc CONF_DIR = "conf.dir"; //NOI18N
117
public static final String JavaDoc WEB_DOCBASE_DIR = "web.docbase.dir"; //NOI18N
118
public static final String JavaDoc RESOURCE_DIR = "resource.dir"; //NOI18N
119
public static final String JavaDoc BUILD_DIR = "build.dir"; //NOI18N
120
public static final String JavaDoc BUILD_WEB_DIR = "build.web.dir"; //NOI18N
121
public static final String JavaDoc BUILD_EAR_WEB_DIR = "build.ear.web.dir"; //NOI18N
122
public static final String JavaDoc BUILD_GENERATED_DIR = "build.generated.dir"; //NOI18N
123
public static final String JavaDoc BUILD_CLASSES_DIR = "build.classes.dir"; //NOI18N
124
public static final String JavaDoc BUILD_EAR_CLASSES_DIR = "build.ear.classes.dir"; //NOI18N
125
public static final String JavaDoc BUILD_CLASSES_EXCLUDES = "build.classes.excludes"; //NOI18N
126
public static final String JavaDoc BUILD_WEB_EXCLUDES = "build.web.excludes"; //NOI18N
127
public static final String JavaDoc DIST_JAVADOC_DIR = "dist.javadoc.dir"; //NOI18N
128
public static final String JavaDoc NO_DEPENDENCIES="no.dependencies"; //NOI18N
129

130     public static final String JavaDoc BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; // NOI18N
131
public static final String JavaDoc BUILD_TEST_RESULTS_DIR = "build.test.results.dir"; // NOI18N
132
public static final String JavaDoc JAVAC_TEST_CLASSPATH = "javac.test.classpath"; // NOI18N
133
public static final String JavaDoc RUN_TEST_CLASSPATH = "run.test.classpath"; // NOI18N
134
public static final String JavaDoc DEBUG_TEST_CLASSPATH = "debug.test.classpath"; // NOI18N
135

136     public static final String JavaDoc JAVADOC_PRIVATE="javadoc.private"; //NOI18N
137
public static final String JavaDoc JAVADOC_NO_TREE="javadoc.notree"; //NOI18N
138
public static final String JavaDoc JAVADOC_USE="javadoc.use"; //NOI18N
139
public static final String JavaDoc JAVADOC_NO_NAVBAR="javadoc.nonavbar"; //NOI18N
140
public static final String JavaDoc JAVADOC_NO_INDEX="javadoc.noindex"; //NOI18N
141
public static final String JavaDoc JAVADOC_SPLIT_INDEX="javadoc.splitindex"; //NOI18N
142
public static final String JavaDoc JAVADOC_AUTHOR="javadoc.author"; //NOI18N
143
public static final String JavaDoc JAVADOC_VERSION="javadoc.version"; //NOI18N
144
public static final String JavaDoc JAVADOC_WINDOW_TITLE="javadoc.windowtitle"; //NOI18N
145
public static final String JavaDoc JAVADOC_ENCODING="javadoc.encoding"; //NOI18N
146
public static final String JavaDoc JAVADOC_ADDITIONALPARAM="javadoc.additionalparam"; // NOI18N
147

148     public static final String JavaDoc COMPILE_JSPS = "compile.jsps"; //NOI18N
149

150     public static final String JavaDoc TAG_WEB_MODULE_LIBRARIES = "web-module-libraries"; // NOI18N
151
public static final String JavaDoc TAG_WEB_MODULE__ADDITIONAL_LIBRARIES = "web-module-additional-libraries"; //NOI18N
152

153     // Properties stored in the PRIVATE.PROPERTIES
154
public static final String JavaDoc APPLICATION_ARGS = "application.args"; // NOI18N
155
public static final String JavaDoc JAVADOC_PREVIEW="javadoc.preview"; // NOI18N
156

157     public static final String JavaDoc WS_DEBUG_CLASSPATHS = "ws.debug.classpaths"; //NOI18N
158
public static final String JavaDoc WS_WEB_DOCBASE_DIRS = "ws.web.docbase.dirs"; //NOI18N
159

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

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

164     // Well known paths
165
public static final String JavaDoc[] WELL_KNOWN_PATHS = new String JavaDoc[] {
166             "${" + JAVAC_CLASSPATH + "}", //NOI18N
167
"${" + JAVAC_TEST_CLASSPATH + "}", //NOI18N
168
"${" + RUN_TEST_CLASSPATH + "}", //NOI18N
169
"${" + BUILD_CLASSES_DIR + "}", //NOI18N
170
"${" + BUILD_TEST_CLASSES_DIR + "}", //NOI18N
171
};
172     
173     // Prefixes and suffixes of classpath
174
public static final String JavaDoc LIBRARY_PREFIX = "${libs."; // NOI18N
175
public static final String JavaDoc LIBRARY_SUFFIX = ".classpath}"; // NOI18N
176
public static final String JavaDoc ANT_ARTIFACT_PREFIX = "${reference."; // NOI18N
177

178     public ClassPathSupport cs;
179
180     //list of frameworks to add to the application
181
private List newFrameworks;
182     
183     // MODELS FOR VISUAL CONTROLS
184

185     // CustomizerSources
186
DefaultTableModel JavaDoc SOURCE_ROOTS_MODEL;
187     DefaultTableModel JavaDoc TEST_ROOTS_MODEL;
188     Document JavaDoc WEB_DOCBASE_DIR_MODEL;
189     ComboBoxModel JavaDoc JAVAC_SOURCE_MODEL;
190
191     // CustomizerLibraries
192
ClassPathUiSupport.ClassPathTableModel JAVAC_CLASSPATH_MODEL;
193     DefaultListModel JavaDoc JAVAC_TEST_CLASSPATH_MODEL;
194     DefaultListModel JavaDoc RUN_TEST_CLASSPATH_MODEL;
195     ComboBoxModel JavaDoc PLATFORM_MODEL;
196     ListCellRenderer JavaDoc CLASS_PATH_LIST_RENDERER;
197     ListCellRenderer JavaDoc PLATFORM_LIST_RENDERER;
198     WebClassPathUi.ClassPathTableCellItemRenderer CLASS_PATH_TABLE_ITEM_RENDERER;
199
200     // CustomizerCompile
201
ButtonModel JavaDoc JAVAC_DEPRECATION_MODEL;
202     ButtonModel JavaDoc JAVAC_DEBUG_MODEL;
203     ButtonModel JavaDoc NO_DEPENDENCIES_MODEL;
204     Document JavaDoc JAVAC_COMPILER_ARG_MODEL;
205     ButtonModel JavaDoc COMPILE_JSP_MODEL;
206     
207     // CustomizerWar
208
Document JavaDoc WAR_NAME_MODEL;
209     Document JavaDoc BUILD_CLASSES_EXCLUDES_MODEL;
210     ButtonModel JavaDoc WAR_COMPRESS_MODEL;
211     ButtonModel JavaDoc WAR_PACKAGE_MODEL;
212     WarIncludesUiSupport.ClasspathTableModel WAR_CONTENT_ADDITIONAL_MODEL;
213
214     // CustomizerJavadoc
215
ButtonModel JavaDoc JAVADOC_PRIVATE_MODEL;
216     ButtonModel JavaDoc JAVADOC_NO_TREE_MODEL;
217     ButtonModel JavaDoc JAVADOC_USE_MODEL;
218     ButtonModel JavaDoc JAVADOC_NO_NAVBAR_MODEL;
219     ButtonModel JavaDoc JAVADOC_NO_INDEX_MODEL;
220     ButtonModel JavaDoc JAVADOC_SPLIT_INDEX_MODEL;
221     ButtonModel JavaDoc JAVADOC_AUTHOR_MODEL;
222     ButtonModel JavaDoc JAVADOC_VERSION_MODEL;
223     Document JavaDoc JAVADOC_WINDOW_TITLE_MODEL;
224     ButtonModel JavaDoc JAVADOC_PREVIEW_MODEL;
225     Document JavaDoc JAVADOC_ADDITIONALPARAM_MODEL;
226
227     // CustomizerRun
228
Document JavaDoc J2EE_PLATFORM_MODEL;
229     Document JavaDoc CONTEXT_PATH_MODEL;
230     Document JavaDoc LAUNCH_URL_RELATIVE_MODEL;
231     ButtonModel JavaDoc DISPLAY_BROWSER_MODEL;
232     ComboBoxModel JavaDoc J2EE_SERVER_INSTANCE_MODEL;
233
234     // Private fields ----------------------------------------------------------
235
private WebProject project;
236     private ReferenceHelper refHelper;
237     private UpdateHelper updateHelper;
238     private PropertyEvaluator evaluator;
239
240     private StoreGroup privateGroup;
241     private StoreGroup projectGroup;
242     
243     private Properties additionalProperties;
244
245     private static boolean needsUpdate = false;
246     
247     private static String JavaDoc serverId;
248     private static String JavaDoc cp;
249
250     public static final String JavaDoc JAVA_SOURCE_BASED= "java.source.based";
251     
252     
253     public WebProjectProperties(WebProject project, UpdateHelper updateHelper, PropertyEvaluator evaluator, ReferenceHelper refHelper) {
254         this.project = project;
255         this.updateHelper = updateHelper;
256         
257         //this is called from updatehelper when user confirms the project update
258
updateHelper.setProjectUpdateListener(new UpdateHelper.ProjectUpdateListener() {
259             public void projectUpdated() {
260                 needsUpdate = true;
261             }
262         });
263         
264         this.evaluator = evaluator;
265         this.refHelper = refHelper;
266         
267         this.cs = new ClassPathSupport( evaluator, refHelper, updateHelper.getAntProjectHelper(), WELL_KNOWN_PATHS, LIBRARY_PREFIX, LIBRARY_SUFFIX, ANT_ARTIFACT_PREFIX);
268                 
269         privateGroup = new StoreGroup();
270         projectGroup = new StoreGroup();
271         
272         additionalProperties = new Properties();
273
274         init(); // Load known properties
275
}
276     
277     WebProject getProject() {
278         return project;
279     }
280
281     /** Initializes the visual models
282      */

283     private void init() {
284         
285         CLASS_PATH_LIST_RENDERER = new WebClassPathUi.ClassPathListCellRenderer( evaluator );
286         CLASS_PATH_TABLE_ITEM_RENDERER = new WebClassPathUi.ClassPathTableCellItemRenderer( evaluator );
287         
288         // CustomizerSources
289
SOURCE_ROOTS_MODEL = WebSourceRootsUi.createModel( project.getSourceRoots() );
290         TEST_ROOTS_MODEL = WebSourceRootsUi.createModel( project.getTestSourceRoots() );
291         WEB_DOCBASE_DIR_MODEL = projectGroup.createStringDocument( evaluator, WEB_DOCBASE_DIR );
292
293         // CustomizerLibraries
294
EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
295         EditableProperties privateProperties = updateHelper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
296
297         JAVAC_CLASSPATH_MODEL = ClassPathUiSupport.createTableModel( cs.itemsIterator( (String JavaDoc)projectProperties.get( JAVAC_CLASSPATH ), ClassPathSupport.TAG_WEB_MODULE_LIBRARIES) );
298         JAVAC_TEST_CLASSPATH_MODEL = ClassPathUiSupport.createListModel( cs.itemsIterator( (String JavaDoc)projectProperties.get( JAVAC_TEST_CLASSPATH ), null ) );
299         RUN_TEST_CLASSPATH_MODEL = ClassPathUiSupport.createListModel( cs.itemsIterator( (String JavaDoc)projectProperties.get( RUN_TEST_CLASSPATH ), null ) );
300         PLATFORM_MODEL = PlatformUiSupport.createPlatformComboBoxModel (evaluator.getProperty(JAVA_PLATFORM));
301         PLATFORM_LIST_RENDERER = PlatformUiSupport.createPlatformListCellRenderer();
302         JAVAC_SOURCE_MODEL = PlatformUiSupport.createSourceLevelComboBoxModel (PLATFORM_MODEL, evaluator.getProperty(JAVAC_SOURCE), evaluator.getProperty(J2EE_PLATFORM));
303         
304         // CustomizerCompile
305
JAVAC_DEPRECATION_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVAC_DEPRECATION );
306         JAVAC_DEBUG_MODEL = privateGroup.createToggleButtonModel( evaluator, JAVAC_DEBUG );
307         NO_DEPENDENCIES_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, NO_DEPENDENCIES );
308         JAVAC_COMPILER_ARG_MODEL = projectGroup.createStringDocument( evaluator, JAVAC_COMPILER_ARG );
309         COMPILE_JSP_MODEL = projectGroup.createToggleButtonModel( evaluator, COMPILE_JSPS );
310         
311         // CustomizerWar
312
WAR_NAME_MODEL = projectGroup.createStringDocument( evaluator, WAR_NAME );
313         BUILD_CLASSES_EXCLUDES_MODEL = projectGroup.createStringDocument( evaluator, BUILD_CLASSES_EXCLUDES );
314         WAR_COMPRESS_MODEL = projectGroup.createToggleButtonModel( evaluator, WAR_COMPRESS );
315         WAR_PACKAGE_MODEL = projectGroup.createToggleButtonModel( evaluator, WAR_PACKAGE );
316         WAR_CONTENT_ADDITIONAL_MODEL = WarIncludesUiSupport.createTableModel( cs.itemsList( (String JavaDoc)projectProperties.get( WAR_CONTENT_ADDITIONAL ), ClassPathSupport.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES));
317
318         // CustomizerJavadoc
319
JAVADOC_PRIVATE_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_PRIVATE );
320         JAVADOC_NO_TREE_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, JAVADOC_NO_TREE );
321         JAVADOC_USE_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_USE );
322         JAVADOC_NO_NAVBAR_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, JAVADOC_NO_NAVBAR );
323         JAVADOC_NO_INDEX_MODEL = projectGroup.createInverseToggleButtonModel( evaluator, JAVADOC_NO_INDEX );
324         JAVADOC_SPLIT_INDEX_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_SPLIT_INDEX );
325         JAVADOC_AUTHOR_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_AUTHOR );
326         JAVADOC_VERSION_MODEL = projectGroup.createToggleButtonModel( evaluator, JAVADOC_VERSION );
327         JAVADOC_WINDOW_TITLE_MODEL = projectGroup.createStringDocument( evaluator, JAVADOC_WINDOW_TITLE );
328         JAVADOC_PREVIEW_MODEL = privateGroup.createToggleButtonModel( evaluator, JAVADOC_PREVIEW );
329         JAVADOC_ADDITIONALPARAM_MODEL = projectGroup.createStringDocument( evaluator, JAVADOC_ADDITIONALPARAM );
330         
331         // CustomizerRun
332
J2EE_PLATFORM_MODEL = projectGroup.createStringDocument(evaluator, J2EE_PLATFORM);
333         LAUNCH_URL_RELATIVE_MODEL = projectGroup.createStringDocument(evaluator, LAUNCH_URL_RELATIVE);
334         DISPLAY_BROWSER_MODEL = projectGroup.createToggleButtonModel(evaluator, DISPLAY_BROWSER);
335         J2EE_SERVER_INSTANCE_MODEL = J2eePlatformUiSupport.createPlatformComboBoxModel(privateProperties.getProperty( J2EE_SERVER_INSTANCE ), projectProperties.getProperty(J2EE_PLATFORM));
336         try {
337             CONTEXT_PATH_MODEL = new PlainDocument JavaDoc();
338             CONTEXT_PATH_MODEL.remove(0, CONTEXT_PATH_MODEL.getLength());
339             ProjectWebModule wm = (ProjectWebModule) project.getLookup().lookup(ProjectWebModule.class);
340             String JavaDoc contextPath = wm.getContextPath();
341             if (contextPath != null) {
342                 CONTEXT_PATH_MODEL.insertString(0, contextPath, null);
343             }
344         } catch (BadLocationException JavaDoc exc) {
345             //ignore
346
}
347     }
348
349     public void save() {
350         try {
351             // Store properties
352
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
353                 public Object JavaDoc run() throws IOException JavaDoc {
354                     storeProperties();
355                     return null;
356                 }
357             });
358             // and save the project
359
ProjectManager.getDefault().saveProject(project);
360             
361             // extend project with selected frameworks
362
// It should be called outside mutex, which is used above. See issue#68118
363
if (newFrameworks != null) {
364                 for(int i = 0; i < newFrameworks.size(); i++)
365                     ((WebFrameworkProvider) newFrameworks.get(i)).extend(project.getAPIWebModule());
366
367                 newFrameworks.clear();
368             }
369             
370             //prevent deadlock reported in the issue #54643
371
//cp and serverId values are read in setNewContextPathValue() method which is called from storeProperties() before this code
372
//it is easier to preset them instead of reading them here again
373
if (cp != null) {
374                 ProjectWebModule wm = (ProjectWebModule) project.getLookup().lookup(ProjectWebModule.class);
375                 String JavaDoc oldCP = wm.getContextPath(serverId);
376                 if (!cp.equals(oldCP))
377                     wm.setContextPath(serverId, cp);
378             }
379         }
380         catch (MutexException e) {
381             ErrorManager.getDefault().notify((IOException JavaDoc)e.getException());
382         }
383         catch ( IOException JavaDoc ex ) {
384             ErrorManager.getDefault().notify( ex );
385         }
386     }
387
388     private void storeProperties() throws IOException JavaDoc {
389         // Store special properties
390

391         // Modify the project dependencies properly
392
resolveProjectDependencies();
393        
394         // Store source roots
395
storeRoots( project.getSourceRoots(), SOURCE_ROOTS_MODEL );
396         storeRoots( project.getTestSourceRoots(), TEST_ROOTS_MODEL );
397
398         //test whether user wants to update his project to newest version
399
if(needsUpdate) {
400             //remove servlet24 and jsp20 libraries (they are not used in 4.1)
401
ClassPathUiSupport.ClassPathTableModel cptm = getJavaClassPathModel();
402
403             ArrayList cpItemsToRemove = new ArrayList();
404             for(int i = 0; i < cptm.getRowCount(); i++) {
405                 Object JavaDoc item = cptm.getValueAt(i,0);
406                 if (item instanceof ClassPathSupport.Item) {
407                     ClassPathSupport.Item cpti = (ClassPathSupport.Item)item;
408                     String JavaDoc propertyName = cpti.getReference();
409                     if(propertyName != null) {
410                         String JavaDoc libname = propertyName.substring("${libs.".length());
411                         if(libname.indexOf(".classpath}") != -1) libname = libname.substring(0, libname.indexOf(".classpath}"));
412                                 
413                         if("servlet24".equals(libname) || "jsp20".equals(libname)) { //NOI18N
414
cpItemsToRemove.add(cpti);
415                         }
416                     }
417                 }
418             }
419             
420             //remove selected libraries
421
Iterator remove = cpItemsToRemove.iterator();
422             while(remove.hasNext()) {
423                 ClassPathSupport.Item cpti = (ClassPathSupport.Item)remove.next();
424                 cptm.getDefaultListModel().removeElement(cpti);
425             }
426             
427             //commented out, one more check follows
428
//needsUpdate = false;
429
}
430         
431         // Encode all paths (this may change the project properties)
432
String JavaDoc[] javac_cp = cs.encodeToStrings( ClassPathUiSupport.getIterator( JAVAC_CLASSPATH_MODEL.getDefaultListModel() ), ClassPathSupport.TAG_WEB_MODULE_LIBRARIES );
433         String JavaDoc[] javac_test_cp = cs.encodeToStrings( ClassPathUiSupport.getIterator( JAVAC_TEST_CLASSPATH_MODEL ), null );
434         String JavaDoc[] run_test_cp = cs.encodeToStrings( ClassPathUiSupport.getIterator( RUN_TEST_CLASSPATH_MODEL ), null );
435         String JavaDoc[] war_includes = cs.encodeToStrings( WarIncludesUiSupport.getIterator( WAR_CONTENT_ADDITIONAL_MODEL ), ClassPathSupport.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES );
436
437         // Store standard properties
438
EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
439         EditableProperties privateProperties = updateHelper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
440         
441         // Assure inegrity which can't shound not be assured in UI
442
if ( !JAVADOC_NO_INDEX_MODEL.isSelected() ) {
443             JAVADOC_SPLIT_INDEX_MODEL.setSelected( false ); // Can't split non existing index
444
}
445                                 
446         // Standard store of the properties
447
projectGroup.store( projectProperties );
448         privateGroup.store( privateProperties );
449
450         //test whether user wants to update his project to newest version
451
if(needsUpdate) {
452             //add items for test classpath (they are not used in 4.1)
453
javac_test_cp = new String JavaDoc[] {
454                 "${javac.classpath}:", // NOI18N
455
"${build.classes.dir}:", // NOI18N
456
"${libs.junit.classpath}", // NOI18N
457
};
458             run_test_cp = new String JavaDoc[] {
459                 "${javac.test.classpath}:", // NOI18N
460
"${build.test.classes.dir}", // NOI18N
461
};
462             projectProperties.setProperty(DEBUG_TEST_CLASSPATH, new String JavaDoc[] {
463                 "${run.test.classpath}", // NOI18N
464
});
465             
466             needsUpdate = false;
467         }
468         
469         // Save all paths
470
projectProperties.setProperty( JAVAC_CLASSPATH, javac_cp );
471         projectProperties.setProperty( JAVAC_TEST_CLASSPATH, javac_test_cp );
472         projectProperties.setProperty( RUN_TEST_CLASSPATH, run_test_cp );
473         
474         projectProperties.setProperty( WAR_CONTENT_ADDITIONAL, war_includes );
475         
476         //Handle platform selection and javac.source javac.target properties
477
SpecificationVersion sourceLevel = (SpecificationVersion) JAVAC_SOURCE_MODEL.getSelectedItem();
478         PlatformUiSupport.storePlatform (projectProperties, updateHelper, PLATFORM_MODEL.getSelectedItem(), sourceLevel);
479
480         // Handle other special cases
481
if ( NO_DEPENDENCIES_MODEL.isSelected() ) { // NOI18N
482
projectProperties.remove( NO_DEPENDENCIES ); // Remove the property completely if not set
483
}
484         
485         // Set new server instance ID
486
if (J2EE_SERVER_INSTANCE_MODEL.getSelectedItem() != null) {
487             setNewServerInstanceValue(J2eePlatformUiSupport.getServerInstanceID(J2EE_SERVER_INSTANCE_MODEL.getSelectedItem()), project, projectProperties, privateProperties);
488         }
489         
490         // Set new context path
491
try {
492         String JavaDoc cp = CONTEXT_PATH_MODEL.getText(0, CONTEXT_PATH_MODEL.getLength());
493         if (cp == null) {
494         cp = "/" + PropertyUtils.getUsablePropertyName(project.getName()); //NOI18N
495
} else if (!isCorrectCP(cp)) {
496         if (cp.startsWith("/")) //NOI18N
497
cp = cp.substring(1);
498         cp = "/" + PropertyUtils.getUsablePropertyName(cp); //NOI18N
499
}
500         
501             setNewContextPathValue(cp, project, projectProperties, privateProperties);
502         } catch (BadLocationException JavaDoc exc) {
503             //ignore
504
}
505
506         storeAdditionalProperties(projectProperties);
507         
508         ArrayList libs = new ArrayList ();
509         libs.addAll(ClassPathUiSupport.getList(JAVAC_CLASSPATH_MODEL.getDefaultListModel()));
510         libs.addAll(WarIncludesUiSupport.getList(WAR_CONTENT_ADDITIONAL_MODEL));
511         
512         storeLibrariesLocations (libs.iterator(), privateProperties);
513         
514         // Store the property changes into the project
515
updateHelper.putProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties );
516         updateHelper.putProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProperties );
517         
518     }
519
520     private static boolean isCorrectCP(String JavaDoc contextPath) {
521         if (contextPath.length() == 0) {
522             return true;
523         } else if (!contextPath.startsWith("/")) { //NOI18N
524
return false;
525         } else if (contextPath.endsWith("/")) { //NOI18N
526
return false;
527         } else if (contextPath.indexOf("//") >= 0) { //NOI18N
528
return false;
529         } else if (contextPath.indexOf(' ') >= 0) { //NOI18N
530
return false;
531         }
532     return true;
533     }
534     
535     private void storeAdditionalProperties(EditableProperties projectProperties) {
536         for (Iterator i = additionalProperties.keySet().iterator(); i.hasNext();) {
537             String JavaDoc key = i.next().toString();
538             projectProperties.put(key, additionalProperties.getProperty(key));
539         }
540     }
541     
542     /** XXX to be deleted when introduced in AntPropertyHeleper API
543      */

544     public static String JavaDoc getAntPropertyName( String JavaDoc property ) {
545         if ( property != null &&
546              property.startsWith( "${" ) && // NOI18N
547
property.endsWith( "}" ) ) { // NOI18N
548
return property.substring( 2, property.length() - 1 );
549         }
550         else {
551             return property;
552         }
553     }
554     
555     static boolean isAntProperty (String JavaDoc string) {
556         return string != null && string.startsWith( "${" ) && string.endsWith( "}" ); //NOI18N
557
}
558         
559     /** Finds out what are new and removed project dependencies and
560      * applyes the info to the project
561      */

562     private void resolveProjectDependencies() {
563             
564         // Create a set of old and new artifacts.
565
Set oldArtifacts = new HashSet();
566         EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
567         oldArtifacts.addAll( cs.itemsList( (String JavaDoc)projectProperties.get( JAVAC_CLASSPATH ), ClassPathSupport.Item.PATH_IN_WAR_LIB ) );
568         oldArtifacts.addAll( cs.itemsList( (String JavaDoc)projectProperties.get( JAVAC_TEST_CLASSPATH ), null ) );
569         oldArtifacts.addAll( cs.itemsList( (String JavaDoc)projectProperties.get( RUN_TEST_CLASSPATH ), null ) );
570
571         Set newArtifacts = new HashSet();
572         newArtifacts.addAll( ClassPathUiSupport.getList( JAVAC_CLASSPATH_MODEL.getDefaultListModel() ) );
573         newArtifacts.addAll( ClassPathUiSupport.getList( JAVAC_TEST_CLASSPATH_MODEL ) );
574         newArtifacts.addAll( ClassPathUiSupport.getList( RUN_TEST_CLASSPATH_MODEL ) );
575                 
576         // Create set of removed artifacts and remove them
577
Set removed = new HashSet( oldArtifacts );
578         removed.removeAll( newArtifacts );
579         Set added = new HashSet(newArtifacts);
580         added.removeAll(oldArtifacts);
581         
582         // 1. first remove all project references. The method will modify
583
// project property files, so it must be done separately
584
for( Iterator it = removed.iterator(); it.hasNext(); ) {
585             ClassPathSupport.Item item = (ClassPathSupport.Item)it.next();
586             if ( item.getType() == ClassPathSupport.Item.TYPE_ARTIFACT ||
587                     item.getType() == ClassPathSupport.Item.TYPE_JAR ) {
588                 refHelper.destroyReference(item.getReference());
589             }
590         }
591         
592         // 2. now read project.properties and modify rest
593
EditableProperties ep = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
594         boolean changed = false;
595         
596         for( Iterator it = removed.iterator(); it.hasNext(); ) {
597             ClassPathSupport.Item item = (ClassPathSupport.Item)it.next();
598             if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
599                 // remove helper property pointing to library jar if there is any
600
String JavaDoc prop = item.getReference();
601                 prop = prop.substring(2, prop.length()-1);
602                 ep.remove(prop);
603                 changed = true;
604             }
605         }
606         File JavaDoc projDir = FileUtil.toFile(updateHelper.getAntProjectHelper().getProjectDirectory());
607         for( Iterator it = added.iterator(); it.hasNext(); ) {
608             ClassPathSupport.Item item = (ClassPathSupport.Item)it.next();
609             if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
610                 // add property to project.properties pointing to relativized
611
// library jar(s) if possible
612
String JavaDoc prop = cs.getLibraryReference( item );
613                 prop = prop.substring(2, prop.length()-1); // XXX make a PropertyUtils method for this!
614
String JavaDoc value = relativizeLibraryClasspath(prop, projDir);
615                 if (value != null) {
616                     ep.setProperty(prop, value);
617                     ep.setComment(prop, new String JavaDoc[]{
618                         // XXX this should be I18N! Not least because the English is wrong...
619
"# Property "+prop+" is set here just to make sharing of project simpler.", //NOI18N
620
"# The library definition has always preference over this property."}, false); //NOI18N
621
changed = true;
622                 }
623             }
624         }
625         if (changed) {
626             updateHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
627         }
628     }
629         
630     /**
631      * Tokenize library classpath and try to relativize all the jars.
632      * @param property library property name ala "libs.someLib.classpath"
633      * @param projectDir project dir for relativization
634      * @return relativized library classpath or null if some jar is not collocated
635      */

636     private String JavaDoc relativizeLibraryClasspath(String JavaDoc property, File JavaDoc projectDir) {
637         String JavaDoc value = PropertyUtils.getGlobalProperties().getProperty(property);
638         // bugfix #42852, check if the classpath property is set, otherwise return null
639
if (value == null) {
640             return null;
641         }
642         String JavaDoc[] paths = PropertyUtils.tokenizePath(value);
643         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
644         for (int i=0; i<paths.length; i++) {
645             File JavaDoc f = updateHelper.getAntProjectHelper().resolveFile(paths[i]);
646             if (CollocationQuery.areCollocated(f, projectDir)) {
647                 sb.append(PropertyUtils.relativizeFile(projectDir, f));
648             } else {
649                 return null;
650             }
651             if (i+1<paths.length) {
652                 sb.append(File.pathSeparatorChar);
653             }
654         }
655         if (sb.length() == 0) {
656             return null;
657         } else {
658             return sb.toString();
659         }
660     }
661     
662     private void storeRoots( SourceRoots roots, DefaultTableModel JavaDoc tableModel ) throws MalformedURLException JavaDoc {
663         Vector data = tableModel.getDataVector();
664         URL JavaDoc[] rootURLs = new URL JavaDoc[data.size()];
665         String JavaDoc []rootLabels = new String JavaDoc[data.size()];
666         for (int i=0; i<data.size();i++) {
667             File JavaDoc f = ((File JavaDoc)((Vector)data.elementAt(i)).elementAt(0));
668             rootURLs[i] = WebProjectUtil.getRootURL(f,null);
669             rootLabels[i] = (String JavaDoc) ((Vector)data.elementAt(i)).elementAt(1);
670         }
671         roots.putRoots(rootURLs,rootLabels);
672     }
673
674     public Object JavaDoc get(String JavaDoc propertyName) {
675         EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
676         EditableProperties privateProperties = updateHelper.getProperties( AntProjectHelper.PRIVATE_PROPERTIES_PATH );
677
678         if (J2EE_SERVER_INSTANCE.equals(propertyName))
679             return privateProperties.getProperty(J2EE_SERVER_INSTANCE);
680         else
681             return projectProperties.getProperty(propertyName);
682         
683 // return evaluator.getProperty(propertyName);
684
}
685     
686     public void put( String JavaDoc propertyName, String JavaDoc value ) {
687         EditableProperties projectProperties = updateHelper.getProperties( AntProjectHelper.PROJECT_PROPERTIES_PATH );
688         projectProperties.put(propertyName, value);
689         if (J2EE_SERVER_INSTANCE.equals (propertyName)) {
690             projectProperties.put (J2EE_SERVER_TYPE, Deployment.getDefault ().getServerID ((String JavaDoc) value));
691         }
692     }
693
694     public void store() {
695         save();
696     }
697     
698     public static void setServerInstance(final Project project, final UpdateHelper helper, final String JavaDoc serverInstanceID) {
699         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
700             public void run() {
701                 try {
702                     EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
703                     EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
704                     setNewServerInstanceValue(serverInstanceID, project, projectProps, privateProps);
705                     helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
706                     helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
707                     ProjectManager.getDefault().saveProject(project);
708                 }
709                 catch (IOException JavaDoc e) {
710                     ErrorManager.getDefault().notify(e);
711                 }
712             }
713         });
714     }
715     
716     /* This is used by CustomizerWSServiceHost */
717     void putAdditionalProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
718         additionalProperties.setProperty(propertyName, propertyValue);
719     }
720     
721     private static void setNewServerInstanceValue(String JavaDoc newServInstID, Project project, EditableProperties projectProps, EditableProperties privateProps) {
722         // update j2ee.platform.classpath
723
String JavaDoc oldServInstID = privateProps.getProperty(J2EE_SERVER_INSTANCE);
724         if (oldServInstID != null) {
725             J2eePlatform oldJ2eePlatform = Deployment.getDefault().getJ2eePlatform(oldServInstID);
726             if (oldJ2eePlatform != null) {
727                 ((WebProject)project).unregisterJ2eePlatformListener(oldJ2eePlatform);
728             }
729         }
730         J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(newServInstID);
731         if (j2eePlatform == null) {
732             // probably missing server error
733
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "J2EE platform is null."); // NOI18N
734

735             // update j2ee.server.type (throws NPE)
736
//projectProps.setProperty(J2EE_SERVER_TYPE, Deployment.getDefault().getServerID(newServInstID));
737
// update j2ee.server.instance
738
privateProps.setProperty(J2EE_SERVER_INSTANCE, newServInstID);
739             
740             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH);
741             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH);
742             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH);
743             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH);
744             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_JSR109_SUPPORT);
745             
746             privateProps.remove(DEPLOY_ANT_PROPS_FILE);
747             return;
748         }
749         ((WebProject) project).registerJ2eePlatformListener(j2eePlatform);
750         String JavaDoc classpath = Utils.toClasspathString(j2eePlatform.getClasspathEntries());
751         privateProps.setProperty(J2EE_PLATFORM_CLASSPATH, classpath);
752
753         // update j2ee.platform.wscompile.classpath
754
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE)) {
755             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSCOMPILE);
756             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH,
757                     Utils.toClasspathString(wsClasspath));
758         } else {
759             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH);
760         }
761         
762         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSGEN)) {
763             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSGEN);
764             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH,
765                     Utils.toClasspathString(wsClasspath));
766         } else {
767             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH);
768         }
769
770         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIMPORT)) {
771             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSIMPORT);
772             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH,
773                     Utils.toClasspathString(wsClasspath));
774         } else {
775             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH);
776         }
777
778         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIT)) {
779             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSIT);
780             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_WSIT_CLASSPATH,
781                     Utils.toClasspathString(wsClasspath));
782         } else {
783             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_WSIT_CLASSPATH);
784         }
785
786         if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP)) {
787             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_JWSDP);
788             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH,
789                     Utils.toClasspathString(wsClasspath));
790         } else {
791             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH);
792         }
793         
794         // set j2ee.platform.jsr109 support
795
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109)) {
796             privateProps.setProperty(WebServicesConstants.J2EE_PLATFORM_JSR109_SUPPORT,
797                     "true"); //NOI18N
798
} else {
799             privateProps.remove(WebServicesConstants.J2EE_PLATFORM_JSR109_SUPPORT);
800         }
801         
802         // update j2ee.server.type
803
projectProps.setProperty(J2EE_SERVER_TYPE, Deployment.getDefault().getServerID(newServInstID));
804         
805         // update j2ee.server.instance
806
privateProps.setProperty(J2EE_SERVER_INSTANCE, newServInstID);
807         
808         // ant deployment support
809
File JavaDoc projectFolder = FileUtil.toFile(project.getProjectDirectory());
810         try {
811             AntDeploymentHelper.writeDeploymentScript(new File JavaDoc(projectFolder, ANT_DEPLOY_BUILD_SCRIPT), J2eeModule.WAR, newServInstID); // NOI18N
812
} catch (IOException JavaDoc ioe) {
813             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
814         }
815         File JavaDoc antDeployPropsFile = AntDeploymentHelper.getDeploymentPropertiesFile(newServInstID);
816         if (antDeployPropsFile == null) {
817             privateProps.remove(DEPLOY_ANT_PROPS_FILE);
818         } else {
819             privateProps.setProperty(DEPLOY_ANT_PROPS_FILE, antDeployPropsFile.getAbsolutePath());
820         }
821     }
822     
823     private static void setNewContextPathValue(String JavaDoc contextPath, Project project, EditableProperties projectProps, EditableProperties privateProps) {
824         if (contextPath == null)
825             return;
826
827         cp = contextPath;
828         serverId = privateProps.getProperty(J2EE_SERVER_INSTANCE);
829     }
830     
831     public ClassPathUiSupport.ClassPathTableModel getJavaClassPathModel() {
832         return JAVAC_CLASSPATH_MODEL;
833     }
834     
835     /** Store locations of libraries in the classpath param that have more the one
836      * file into the properties in the following format:
837      *
838      * <ul>
839      * <li>libs.foo.classpath.libdir.1=C:/foo
840      * <li>libs.foo.classpath.libdirs=1
841      * <li>libs.foo.classpath.libfile.1=C:/bar/a.jar
842      * <li>libs.foo.classpath.libfile.2=C:/bar/b.jar
843      * <li>libs.foo.classpath.libfiles=1
844      * </ul>
845      * This is needed for the Ant copy task as it cannot copy more the one file
846      * and it needs different handling for files and directories.
847      * <br>
848      * It removes all properties that match this format that were in the {@link #properties}
849      * but are not in the {@link #classpath}.
850      */

851     public static void storeLibrariesLocations (Iterator /*<Item>*/ classpath, EditableProperties privateProps) {
852         ArrayList exLibs = new ArrayList ();
853         Iterator propKeys = privateProps.keySet().iterator();
854         while (propKeys.hasNext()) {
855             String JavaDoc key = (String JavaDoc) propKeys.next();
856             if (key.endsWith(".libdirs") || key.endsWith(".libfiles") || //NOI18N
857
(key.indexOf(".libdir.") > 0) || (key.indexOf(".libfile.") > 0)) { //NOI18N
858
exLibs.add(key);
859             }
860         }
861         while (classpath.hasNext()) {
862             ClassPathSupport.Item item = (Item)classpath.next();
863             ArrayList /*File*/ files = new ArrayList ();
864             ArrayList /*File*/ dirs = new ArrayList ();
865             getFilesForItem (item, files, dirs);
866             String JavaDoc key;
867             if (files.size() > 1 || (files.size()>0 && dirs.size()>0)) {
868                 String JavaDoc ref = item.getType() == ClassPathSupport.Item.TYPE_LIBRARY ? item.getRaw() : item.getReference();
869                 for (int i = 0; i < files.size(); i++) {
870                     File JavaDoc f = (File JavaDoc) files.get(i);
871                     key = getAntPropertyName(ref)+".libfile." + (i+1); //NOI18N
872
privateProps.setProperty (key, "" + f.getAbsolutePath()); //NOI18N
873
exLibs.remove(key);
874                 }
875             }
876             if (dirs.size() > 1 || (files.size()>0 && dirs.size()>0)) {
877                 String JavaDoc ref = item.getType() == ClassPathSupport.Item.TYPE_LIBRARY ? item.getRaw() : item.getReference();
878                 for (int i = 0; i < dirs.size(); i++) {
879                     File JavaDoc f = (File JavaDoc) dirs.get(i);
880                     key = getAntPropertyName(ref)+".libdir." + (i+1); //NOI18N
881
privateProps.setProperty (key, "" + f.getAbsolutePath()); //NOI18N
882
exLibs.remove(key);
883                 }
884             }
885         }
886         Iterator unused = exLibs.iterator();
887         while (unused.hasNext()) {
888             privateProps.remove(unused.next());
889         }
890     }
891     
892     public static final void getFilesForItem (ClassPathSupport.Item item, List/*File*/ files, List/*File*/ dirs) {
893         if (item.isBroken()) {
894             return ;
895         }
896         if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
897             List/*<URL>*/ roots = item.getLibrary().getContent("classpath"); //NOI18N
898
for (Iterator it = roots.iterator(); it.hasNext();) {
899                 URL JavaDoc rootUrl = (URL JavaDoc) it.next();
900                 FileObject root = URLMapper.findFileObject (rootUrl);
901                 
902                 //file inside library is broken
903
if (root == null)
904                     continue;
905                 
906                 if ("jar".equals(rootUrl.getProtocol())) { //NOI18N
907
root = FileUtil.getArchiveFile (root);
908                 }
909                 File JavaDoc f = FileUtil.toFile(root);
910                 if (f != null) {
911                     if (f.isFile()) {
912                         files.add(f);
913                     } else {
914                         dirs.add(f);
915                     }
916                 }
917             }
918         }
919         if (item.getType() == ClassPathSupport.Item.TYPE_JAR) {
920             File JavaDoc root = item.getFile();
921             if (root != null) {
922                 if (root.isFile()) {
923                     files.add(root);
924                 } else {
925                     dirs.add(root);
926                 }
927             }
928         }
929         if (item.getType() == ClassPathSupport.Item.TYPE_ARTIFACT) {
930             String JavaDoc artifactFolder = item.getArtifact().getScriptLocation().getParent();
931             URI JavaDoc roots[] = item.getArtifact().getArtifactLocations();
932             for (int i = 0; i < roots.length; i++) {
933                 String JavaDoc root = artifactFolder + File.separator + roots [i];
934                 if (root.endsWith(File.separator)) {
935                     dirs.add(new File JavaDoc (root));
936                 } else {
937                     files.add(new File JavaDoc (root));
938                 }
939             }
940         }
941     }
942     
943     public void setNewFrameworks(List frameworks) {
944         newFrameworks = frameworks;
945     }
946 }
947
Popular Tags