KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > copylibstask > CopyLibs


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.java.j2seproject.copylibstask;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.Task;
31 import org.apache.tools.ant.taskdefs.Jar;
32 import org.apache.tools.ant.types.Path;
33 import org.apache.tools.ant.util.FileUtils;
34
35 /**
36  *
37  * @author Tomas Zezula
38  */

39 public class CopyLibs extends Jar {
40
41     private static final String JavaDoc LIB = "lib"; //NOI18N
42

43     Path runtimePath;
44     
45     /** Creates a new instance of CopyLibs */
46     public CopyLibs () {
47     }
48     
49     public void setRuntimeClassPath (final Path path) {
50         assert path != null;
51         this.runtimePath = path;
52     }
53     
54     public Path getRuntimeClassPath () {
55         return this.runtimePath;
56     }
57     
58     public void execute() throws BuildException {
59         if (this.runtimePath == null) {
60             throw new BuildException ("RuntimeClassPath must be set.");
61         }
62         final String JavaDoc[] pathElements = this.runtimePath.list();
63         File JavaDoc[] filesToCopy = new File JavaDoc[pathElements.length];
64         for (int i=0; i< pathElements.length; i++) {
65             File JavaDoc f = new File JavaDoc (pathElements[i]);
66             if (f.isDirectory() || !f.canRead()) {
67                 filesToCopy = null;
68                 break;
69             }
70             else {
71                 filesToCopy[i] = f;
72             }
73         }
74         super.execute();
75         
76         final File JavaDoc destFile = this.getDestFile();
77         final File JavaDoc destFolder = destFile.getParentFile();
78         assert destFolder != null && destFolder.canWrite();
79         try {
80             ResourceBundle JavaDoc bundle = ResourceBundle.getBundle("org.netbeans.modules.java.j2seproject.copylibstask.Bundle"); //NOI18N
81
assert bundle != null;
82             final File JavaDoc readme = new File JavaDoc (destFolder,bundle.getString("TXT_README_FILE_NAME"));
83             if (!readme.exists()) {
84                 readme.createNewFile();
85             }
86             final PrintWriter JavaDoc out = new PrintWriter JavaDoc (new FileWriter JavaDoc (readme));
87             try {
88                 final String JavaDoc content = bundle.getString("TXT_README_FILE_CONTENT");
89                 out.println (MessageFormat.format(content,new Object JavaDoc[] {destFile.getName()}));
90             } finally {
91                 out.close ();
92             }
93         } catch (IOException JavaDoc ioe) {
94             this.log("Cannot generate readme file.",Project.MSG_VERBOSE);
95         }
96         
97         if (filesToCopy != null && filesToCopy.length>0) {
98             final File JavaDoc libFolder = new File JavaDoc (destFolder,LIB);
99             if (!libFolder.exists()) {
100                 libFolder.mkdir ();
101             }
102             assert libFolder.canWrite();
103             FileUtils utils = FileUtils.newFileUtils();
104             for (int i=0; i<filesToCopy.length; i++) {
105                 try {
106                     File JavaDoc libFile = new File JavaDoc (libFolder,filesToCopy[i].getName());
107                     utils.copyFile(filesToCopy[i],libFile);
108                 } catch (IOException JavaDoc ioe) {
109                     throw new BuildException (ioe);
110                 }
111             }
112         }
113         
114     }
115     
116 }
117
Popular Tags