Source Code Cross Referenced for FTFileUtil.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » functionTests » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derbyTesting.functionTests.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Derby - Class org.apache.derbyTesting.functionTests.util.FTFileUtil
004:
005:           Licensed to the Apache Software Foundation (ASF) under one or more
006:           contributor license agreements.  See the NOTICE file distributed with
007:           this work for additional information regarding copyright ownership.
008:           The ASF licenses this file to You under the Apache License, Version 2.0
009:           (the "License"); you may not use this file except in compliance with
010:           the License.  You may obtain a copy of the License at
011:
012:              http://www.apache.org/licenses/LICENSE-2.0
013:
014:           Unless required by applicable law or agreed to in writing, software
015:           distributed under the License is distributed on an "AS IS" BASIS,
016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:           See the License for the specific language governing permissions and
018:           limitations under the License.
019:
020:         */
021:
022:        package org.apache.derbyTesting.functionTests.util;
023:
024:        import java.io.FileWriter;
025:        import java.io.File;
026:        import java.security.AccessController;
027:        import java.security.PrivilegedAction;
028:        import java.security.PrivilegedActionException;
029:        import java.security.PrivilegedExceptionAction;
030:
031:        /**
032:         Convience functions for performing file manipulations
033:         in ij scripts.
034:         */
035:        public class FTFileUtil {
036:            /**
037:              Create a file.
038:
039:              @param name the file's name.
040:              @length the number of bytes of test data in the file.
041:              @exception Exception oops.
042:             */
043:            public static void mkFile(String fileName, int length)
044:                    throws Exception {
045:                FileWriter fw = new FileWriter(fileName);
046:                int offset = 0;
047:                String data = "Amber!";
048:                for (int ix = 0; ix < length; ix++) {
049:                    fw.write(data, offset, 1);
050:                    offset++;
051:                    if (offset >= data.length())
052:                        offset = 0;
053:                }
054:                fw.close();
055:            }
056:
057:            /**
058:             * rename a file. 
059:             * This method is  called by some tests through a SQL procedure:
060:             * RENAME_FILE(LOCATION VARCHAR(32000), NAME VARCHAR(32000), 
061:             *                                 NEW_NAME  VARCHAR(32000))
062:             * @param location location of the file
063:             * @param name the file's name
064:             * @param newName the file's new name
065:             */
066:            public static void renameFile(String location, String name,
067:                    String newName) throws Exception {
068:                final File src = new File(location, name);
069:                final File dst = new File(location, newName);
070:
071:                // needs to run in a privileged block as it will be
072:                // called through a SQL statement and thus a generated
073:                // class. The generated class on the stack has no permissions
074:                // granted to it.
075:                AccessController.doPrivileged(new PrivilegedExceptionAction() {
076:                    public Object run() throws Exception {
077:                        if (!src.renameTo(dst)) {
078:                            throw new Exception("unable to rename File: "
079:                                    + src.getAbsolutePath() + " To: "
080:                                    + dst.getAbsolutePath());
081:                        }
082:
083:                        return null; // nothing to return
084:                    }
085:                });
086:            }
087:
088:            /**
089:             * Check if a file exists ?
090:             *
091:             * This method is  called by some tests through a SQL function:
092:             * fileExists(fileName varchar(128))returns VARCHAR(100)
093:             *
094:             * @param name the file's name.
095:             * @return     <tt>"true"</tt> if the given file exists 
096:             *             <tt>"false"</tt> otherwise.
097:             * @exception Exception if any exception occurs 
098:             */
099:            public static String fileExists(String fileName)
100:                    throws PrivilegedActionException {
101:                final File fl = new File(fileName);
102:
103:                // needs to run in a privileged block as it will be
104:                // called through a SQL statement and thus a generated
105:                // class. The generated class on the stack has no permissions
106:                // granted to it.
107:
108:                return (String) AccessController
109:                        .doPrivileged(new PrivilegedExceptionAction() {
110:                            public Object run() {
111:                                if (fl.exists()) {
112:                                    return "true";
113:                                } else {
114:                                    return "false";
115:                                }
116:                            }
117:                        });
118:            }
119:
120:            /**
121:             *	Remove a directory and all of its contents.
122:             *   
123:             *  @param name the file's name.
124:             * @return     <tt>true</tt> if the omplete directory was removed
125:             *             <tt>false</tt> otherwise.f false is returned then some of 
126:             *              the files in the directory may have been removed.
127:             */
128:
129:            private static boolean removeDirectory(File directory) {
130:
131:                if (directory == null)
132:                    return false;
133:                if (!directory.exists())
134:                    return true;
135:                if (!directory.isDirectory())
136:                    return false;
137:
138:                String[] list = directory.list();
139:
140:                if (list != null) {
141:                    for (int i = 0; i < list.length; i++) {
142:                        File entry = new File(directory, list[i]);
143:
144:                        if (entry.isDirectory()) {
145:                            if (!removeDirectory(entry))
146:                                return false;
147:                        } else {
148:                            if (!entry.delete())
149:                                return false;
150:                        }
151:                    }
152:                }
153:
154:                return directory.delete();
155:            }
156:
157:            /**
158:             * Remove a directory and all of its contents.
159:             * This method is  called by some tests through a SQL function:
160:             * removeDirectory(fileName varchar(128)) returns VARCHAR(100)
161:             *   
162:             * @param name the file's name.
163:             * @return     <tt>"true"</tt> if the omplete directory was removed
164:             *             <tt>"false"</tt> otherwise.f false is returned then some of 
165:             *              the files in the directory may have been removed.
166:             */
167:
168:            public static String removeDirectory(final String directory)
169:                    throws PrivilegedActionException {
170:                // needs to run in a privileged block as it will be
171:                // called through a SQL statement and thus a generated
172:                // class. The generated class on the stack has no permissions
173:                // granted to it.
174:
175:                return (String) AccessController
176:                        .doPrivileged(new PrivilegedExceptionAction() {
177:                            public Object run() {
178:                                return (removeDirectory(new File(directory)) ? "true"
179:                                        : "false");
180:                            }
181:                        });
182:            }
183:
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.