001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.tests.jfluid.utils;
042:
043: import org.netbeans.lib.profiler.ProfilingEventListener;
044: import org.netbeans.lib.profiler.tests.jfluid.CommonProfilerTestCase;
045: import java.io.BufferedInputStream;
046: import java.io.BufferedOutputStream;
047: import java.io.File;
048: import java.io.FileInputStream;
049: import java.io.FileNotFoundException;
050: import java.io.FileOutputStream;
051: import java.io.IOException;
052:
053: public class Utils {
054: //~ Constructors -------------------------------------------------------------------------------------------------------------
055:
056: public Utils() {
057: }
058:
059: //~ Methods ------------------------------------------------------------------------------------------------------------------
060:
061: public static void copyFile(File file, File target) {
062: try {
063: BufferedInputStream bis = new BufferedInputStream(
064: new FileInputStream(file));
065: BufferedOutputStream bos = new BufferedOutputStream(
066: new FileOutputStream(target));
067: byte[] buffer = new byte[10240];
068: int len = 0;
069:
070: while ((len = bis.read(buffer)) > 0) {
071: bos.write(buffer, 0, len);
072: }
073:
074: bis.close();
075: bos.close();
076: } catch (FileNotFoundException ex) {
077: ex.printStackTrace();
078: } catch (IOException ex) {
079: ex.printStackTrace();
080: }
081: }
082:
083: public static void copyFolder(File folder, File target) {
084: File[] lst = folder.listFiles();
085:
086: if (!target.exists()) {
087: target.mkdirs();
088: }
089:
090: for (int i = 0; i < lst.length; i++) {
091: File nw = new File(target, lst[i].getName());
092:
093: if (lst[i].isDirectory()) {
094: copyFolder(lst[i], nw);
095: } else {
096: copyFile(lst[i], nw);
097: }
098: }
099: }
100:
101: public static ProfilingEventListener createProfilingListener(
102: final CommonProfilerTestCase test) {
103: return new ProfilingEventListener() {
104: public void targetAppStarted() {
105: test.log("app started");
106: test.setStatus(CommonProfilerTestCase.STATUS_RUNNING);
107: }
108:
109: public void targetAppStopped() {
110: test.log("app stoped");
111: }
112:
113: public void targetAppSuspended() {
114: test.log("app suspended");
115: }
116:
117: public void targetAppResumed() {
118: test.log("app resumed");
119: }
120:
121: public void attachedToTarget() {
122: test.log("app attached to target");
123: }
124:
125: public void detachedFromTarget() {
126: test.log("app detached from target");
127: }
128:
129: public void targetVMTerminated() {
130: test.log("vm terminated");
131: test.setStatus(CommonProfilerTestCase.STATUS_FINISHED);
132: }
133: };
134: }
135:
136: public static void removeFolder(File folder) {
137: File[] lst = folder.listFiles();
138:
139: if (lst == null) {
140: System.err
141: .println("null files " + folder.getAbsolutePath());
142:
143: return;
144: }
145:
146: for (int i = 0; i < lst.length; i++) {
147: if (lst[i].isDirectory()) {
148: removeFolder(lst[i]);
149: } else {
150: lst[i].delete();
151: }
152: }
153:
154: folder.delete();
155: }
156: }
|