KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > refactoring > Utility


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 package org.netbeans.test.j2ee.refactoring;
20
21 import java.io.*;
22 import java.io.File JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.api.mdr.MDRepository;
26 import org.netbeans.jmi.javamodel.JavaClass;
27 import org.netbeans.jmi.javamodel.Type;
28 import org.netbeans.jmi.javamodel.TypeClass;
29 import org.netbeans.jmi.javamodel.TypeParameter;
30 import org.netbeans.jmi.javamodel.UnresolvedClass;
31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
32 import org.netbeans.modules.refactoring.classpath.RefactoringClassPathImplementation;
33 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject;
34 import org.openide.ErrorManager;
35 import org.openide.cookies.SaveCookie;
36
37 import javax.swing.*;
38
39
40 public class Utility {
41
42     public static void beginTrans(boolean writeAccess) {
43         getDefaultRepository().beginTrans(writeAccess);
44     }
45
46     public static void endTrans(boolean rollback) {
47         getDefaultRepository().endTrans(rollback);
48     }
49
50     public static void endTrans() {
51         getDefaultRepository().endTrans();
52     }
53
54     public static MDRepository getDefaultRepository() {
55         return JavaMetamodel.getDefaultRepository();
56     }
57
58     public static void prepareTest() {
59         beginTrans(true);
60         //class path
61
JavaMetamodel.getManager().setClassPath(RefactoringClassPathImplementation.getDefault());
62     }
63
64     public static void finishTest() {
65         endTrans(false);
66     }
67
68     public static File JavaDoc getFile(File JavaDoc classPathDataDir, String JavaDoc fileName) {
69         return new File JavaDoc(classPathDataDir, fileName);
70     }
71
72     /**
73      * must be called in a transaction
74      */

75     public static JavaClass findClass(String JavaDoc s) {
76         JavaClass result;
77         int i = 20;
78         do {
79             result = (JavaClass) JavaMetamodel.getManager().getDefaultExtent().getType().resolve(s);
80             if (result instanceof UnresolvedClass) {
81                 try {
82                     Thread.sleep(1000);
83                 } catch (InterruptedException JavaDoc e) {
84                     endTrans();
85                     e.printStackTrace();
86                     return null;
87                 }
88             }
89             i--;
90         } while ((result instanceof UnresolvedClass) && i > 0);
91         if (result instanceof UnresolvedClass) {
92             throw new IllegalStateException JavaDoc("Class " + s + " not found.");
93         }
94         return result;
95     }
96
97     /**
98      * must be called in a transaction
99      */

100     public static Type findType(String JavaDoc s, TypeClass typeProxy, JavaClass jc) {
101         Type result;
102         int i = 20;
103         do {
104             result = typeProxy.resolve(s);
105             if (result instanceof UnresolvedClass) {
106                 List JavaDoc l = jc.getTypeParameters();
107                 for (Iterator JavaDoc it = l.iterator(); it.hasNext();) {
108                     TypeParameter tp = (TypeParameter) (it.next());
109                     if (tp.getName().equals(s)) {
110                         return tp;
111                     }
112                 }
113                 try {
114                     Thread.sleep(1000);
115                 } catch (InterruptedException JavaDoc e) {
116                     endTrans();
117                     e.printStackTrace();
118                     return null;
119                 }
120             }
121             i--;
122         } while ((result instanceof UnresolvedClass) && i > 0);
123         if (result instanceof UnresolvedClass) {
124             throw new IllegalStateException JavaDoc("Type " + s + " not found.");
125         }
126         return result;
127     }
128
129     public static void copyFile(File JavaDoc src, File JavaDoc trg) throws Exception JavaDoc {
130         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
131         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(trg));
132         byte[] buffer = new byte[1024];
133         int length = 1;
134         while (length > 0) {
135             length = bis.read(buffer);
136             if (length <= 0) {
137                 break;
138             }
139             bos.write(buffer, 0, length);
140         }
141         bis.close();
142         bos.close();
143     }
144
145     public static void waitForAWTDispatchThread() {
146         final boolean[] finished = new boolean[]{false};
147         SwingUtilities.invokeLater(new Runnable JavaDoc() {
148             public void run() {
149                 finished[0] = true;
150             }
151         });
152         while (!finished[0]) {
153             try {
154                 Thread.sleep(200);
155             } catch (Exception JavaDoc e) {
156                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
157             }
158         }
159     }
160
161     public static boolean saveDD(EjbJarMultiViewDataObject ddo) {
162         for (int i = 0; i < 200; i++) {
163             waitForAWTDispatchThread();
164             SaveCookie saveCookie = (SaveCookie) ddo.getCookie(SaveCookie.class);
165             if (saveCookie != null) {
166                 try {
167                     saveCookie.save();
168                 } catch (IOException e) {
169                     ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
170                 }
171                 return true;
172             }
173             try {
174                 Thread.sleep(200);
175             } catch (Exception JavaDoc e) {
176                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
177             }
178         }
179         return false;
180     }
181
182 }
183
Popular Tags