Source Code Cross Referenced for MetadataUtils.java in  » Testing » Ejb3Unit » com » bm » ejb3metadata » utils » 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 » Testing » Ejb3Unit » com.bm.ejb3metadata.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.bm.ejb3metadata.utils;
002:
003:        import java.io.File;
004:        import java.io.FileOutputStream;
005:        import java.io.IOException;
006:        import java.io.InputStream;
007:        import java.io.OutputStream;
008:        import java.lang.reflect.Field;
009:        import java.lang.reflect.Method;
010:        import java.net.URL;
011:        import java.util.ArrayList;
012:        import java.util.List;
013:        import java.util.StringTokenizer;
014:        import java.util.jar.JarInputStream;
015:        import java.util.zip.ZipEntry;
016:
017:        import javax.ejb.Local;
018:        import javax.ejb.Remote;
019:
020:        import com.bm.utils.Ejb3Utils;
021:
022:        /**
023:         * Util class for ejb3unit.
024:         * 
025:         * @author Daniel Wiese
026:         */
027:        public final class MetadataUtils {
028:
029:            /**
030:             * This field is used to OSTYPE_WINDOWS.
031:             */
032:            public static final int OSTYPE_WINDOWS = 1;
033:
034:            /**
035:             * This field is used to OSTYPE_WINNT.
036:             */
037:            public static final int OSTYPE_WINNT = 2;
038:
039:            /**
040:             * This field is used to OSTYPE_WINCE.
041:             */
042:            public static final int OSTYPE_WINCE = 3;
043:
044:            /**
045:             * This field is used to OSTYPE_LINUX.
046:             */
047:            public static final int OSTYPE_LINUX = 4;
048:
049:            /**
050:             * This field is used to OSTYPE_MAC.
051:             */
052:            public static final int OSTYPE_MAC = 5;
053:
054:            /**
055:             * This field is used to OSTYPE_SOLARIS.
056:             */
057:            public static final int OSTYPE_SOLARIS = 6;
058:
059:            /**
060:             * This field is used to OSTYPE_NETWARE.
061:             */
062:            public static final int OSTYPE_NETWARE = 7;
063:
064:            /**
065:             * This field is used to OSTYPE_OS2.
066:             */
067:            public static final int OSTYPE_OS2 = 8;
068:
069:            /**
070:             * This field is used to OSTYPE_UNKNOWN.
071:             */
072:            public static final int OSTYPE_UNKNOWN = 9;
073:
074:            /** default type * */
075:            private static int type = OSTYPE_UNKNOWN;
076:
077:            private MetadataUtils() {
078:                // intentinally left blank
079:            }
080:
081:            /**
082:             * Isolates a jar file when a file was found inside a jar.
083:             * 
084:             * @param fileInJar -
085:             *            the path to the file inside the jar file
086:             * @return - the name of the jar file
087:             */
088:            public static String isolateJarName(URL fileInJar) {
089:                return Ejb3Utils.isolateJarName(fileInJar);
090:            }
091:
092:            /**
093:             * Determines the OS.
094:             * 
095:             * @return an integer identifying the OS (one of the OSTYPE constants)
096:             */
097:            public static int getOs() {
098:                if (type == OSTYPE_UNKNOWN) {
099:
100:                    final String osname = System.getProperty("os.name")
101:                            .toLowerCase();
102:
103:                    if (osname.indexOf("windows") != -1) {
104:                        if (osname.indexOf("nt") != -1
105:                                || osname.indexOf("2000") != -1
106:                                || osname.indexOf("xp") != -1) {
107:                            type = OSTYPE_WINNT;
108:                        } else if (osname.indexOf("ce") != -1) {
109:                            type = OSTYPE_WINCE;
110:                        } else {
111:                            type = OSTYPE_WINDOWS;
112:                        }
113:                    } else if (osname.indexOf("linux") != -1
114:                            || osname.indexOf("bsd") != -1) {
115:                        type = OSTYPE_LINUX;
116:                    } else if (osname.indexOf("mac os") != -1
117:                            || osname.indexOf("macos") != -1) {
118:                        type = OSTYPE_MAC;
119:                    } else if (osname.indexOf("solaris") != -1) {
120:                        type = OSTYPE_SOLARIS; // could also be old freebsd version
121:                    } else if (osname.indexOf("netware") != -1) {
122:                        type = OSTYPE_NETWARE;
123:                    } else if (osname.indexOf("os/2") != -1) {
124:                        type = OSTYPE_OS2;
125:                    } else {
126:                        type = OSTYPE_UNKNOWN;
127:                    }
128:                }
129:
130:                return type;
131:            }
132:
133:            /**
134:             * Dump the contents of a JarArchive to the dpecified destination.
135:             * 
136:             * @param in -
137:             *            the jar archive as input stream
138:             * @param dest -
139:             *            the destination (to extract the content)
140:             * @return - a list with all extracted files
141:             * @throws IOException -
142:             *             in an error case
143:             */
144:            public static List<File> unjar(InputStream in, File dest)
145:                    throws IOException {
146:
147:                final List<File> back = new ArrayList<File>();
148:                if (!dest.exists()) {
149:                    dest.mkdirs();
150:                }
151:
152:                if (!dest.isDirectory()) {
153:                    throw new IOException("Destination must be a directory.");
154:                }
155:
156:                JarInputStream jin = new JarInputStream(in);
157:                final byte[] buffer = new byte[1024];
158:                ZipEntry entry = jin.getNextEntry();
159:                while (entry != null) {
160:
161:                    String fileName = entry.getName();
162:                    if (fileName.charAt(fileName.length() - 1) == '/') {
163:                        fileName = fileName.substring(0, fileName.length() - 1);
164:                    }
165:
166:                    if (fileName.charAt(0) == '/') {
167:                        fileName = fileName.substring(1);
168:                    }
169:
170:                    if (File.separatorChar != '/') {
171:                        fileName = fileName.replace('/', File.separatorChar);
172:                    }
173:
174:                    final File file = new File(dest, fileName);
175:                    if (entry.isDirectory()) {
176:                        // make sure the directory exists
177:                        file.mkdirs();
178:                        jin.closeEntry();
179:                    } else {
180:                        // make sure the directory exists
181:                        final File parent = file.getParentFile();
182:                        if (parent != null && !parent.exists()) {
183:                            parent.mkdirs();
184:                        }
185:
186:                        // dump the file
187:
188:                        final OutputStream out = new FileOutputStream(file);
189:                        int len = 0;
190:                        while ((len = jin.read(buffer, 0, buffer.length)) != -1) {
191:                            out.write(buffer, 0, len);
192:                        }
193:
194:                        out.flush();
195:                        out.close();
196:                        jin.closeEntry();
197:                        back.add(file);
198:
199:                    }
200:
201:                    entry = jin.getNextEntry();
202:                }
203:                jin.close();
204:                return back;
205:            }
206:
207:            /**
208:             * Scan for files in jar file.
209:             * 
210:             * @param in -
211:             *            the jar archive as input stream
212:             * 
213:             * @return - a list with all extracted files
214:             * @throws IOException -
215:             *             in an error case
216:             */
217:            public static List<String> scanFileNamesInArchive(InputStream in)
218:                    throws IOException {
219:
220:                final List<String> back = new ArrayList<String>();
221:
222:                JarInputStream jin = new JarInputStream(in);
223:                ZipEntry entry = jin.getNextEntry();
224:                while (entry != null) {
225:
226:                    String fileName = entry.getName();
227:                    if (fileName.charAt(fileName.length() - 1) == '/') {
228:                        fileName = fileName.substring(0, fileName.length() - 1);
229:                    }
230:
231:                    if (fileName.charAt(0) == '/') {
232:                        fileName = fileName.substring(1);
233:                    }
234:
235:                    if (File.separatorChar != '/') {
236:                        fileName = fileName.replace('/', File.separatorChar);
237:                    }
238:
239:                    if (!entry.isDirectory()) {
240:                        back.add(fileName);
241:                    }
242:
243:                    entry = jin.getNextEntry();
244:                }
245:                jin.close();
246:                return back;
247:            }
248:
249:            /**
250:             * Returns all business (local, remote) interfaces of the class.
251:             * 
252:             * @author Daniel Wiese
253:             * @since 05.02.2006
254:             * @param toAnalyse -
255:             *            the session bean /service to analyse
256:             * @return - the interfaces
257:             */
258:            public static List<Class> getLocalRemoteInterfaces(Class toAnalyse) {
259:                final List<Class> back = new ArrayList<Class>();
260:                if (toAnalyse != null) {
261:                    Class[] interfaces = toAnalyse.getInterfaces();
262:                    if (interfaces != null) {
263:                        for (Class<Object> interf : interfaces) {
264:                            if (interf.getAnnotation(Local.class) != null
265:                                    || interf.getAnnotation(Remote.class) != null) {
266:                                back.add(interf);
267:                            }
268:                        }
269:                    }
270:                }
271:
272:                return back;
273:            }
274:
275:            /**
276:             * This method will do the transformation of primitive types if neccessary.
277:             * 
278:             * @param aktField -
279:             *            the field to inspect
280:             * @return the declaring type (or primitive representant)
281:             */
282:            public static Class getNonPrimitiveType(Class aktField) {
283:                if (aktField == double.class) {
284:                    return Double.class;
285:                } else if (aktField == float.class) {
286:                    return Float.class;
287:                } else if (aktField == int.class) {
288:                    return Integer.class;
289:                } else if (aktField == boolean.class) {
290:                    return Boolean.class;
291:                } else if (aktField == char.class) {
292:                    return Character.class;
293:                } else if (aktField == byte[].class) {
294:                    return Byte.class;
295:                } else if (aktField == long.class) {
296:                    return Long.class;
297:                } else if (aktField == short.class) {
298:                    return Short.class;
299:                } else {
300:                    return aktField;
301:                }
302:
303:            }
304:
305:            /**
306:             * Returns all fields (including fields from all superclasses) of a class.
307:             * 
308:             * @param forClass -
309:             *            for which class
310:             * @return - all fields
311:             */
312:            public static Field[] getAllFields(Class forClass) {
313:                final List<Field> fields = new ArrayList<Field>();
314:                Class aktClass = forClass;
315:                while (!aktClass.equals(Object.class)) {
316:                    Field[] tmp = aktClass.getDeclaredFields();
317:                    for (Field akt : tmp) {
318:                        fields.add(akt);
319:                    }
320:                    aktClass = aktClass.getSuperclass();
321:                }
322:                return fields.toArray(new Field[fields.size()]);
323:            }
324:
325:            /**
326:             * Returns all fields (including fields from all superclasses) of a class.
327:             * 
328:             * @param forClass -
329:             *            for which class
330:             * @return - all fields
331:             */
332:            public static Method[] getAllMethods(Class forClass) {
333:                final List<Method> methods = new ArrayList<Method>();
334:                Class aktClass = forClass;
335:                while (!aktClass.equals(Object.class)) {
336:                    Method[] tmp = aktClass.getDeclaredMethods();
337:                    for (Method akt : tmp) {
338:                        methods.add(akt);
339:                    }
340:                    aktClass = aktClass.getSuperclass();
341:                }
342:                return methods.toArray(new Method[methods.size()]);
343:            }
344:
345:            /**
346:             * Retrun a short class name. E.g. java.util.StringTokenizer will be
347:             * StringTokenizer
348:             * 
349:             * @param longClassName -
350:             *            the long fully qualified calss name
351:             * @return - short class name
352:             */
353:            public static String getShortClassName(String longClassName) {
354:                final StringTokenizer tk = new StringTokenizer(longClassName,
355:                        ".");
356:                String last = longClassName;
357:                while (tk.hasMoreTokens()) {
358:                    last = tk.nextToken();
359:                }
360:
361:                return last;
362:            }
363:
364:            /**
365:             * Retrun a short class name. E.g. java.util.StringTokenizer will be
366:             * StringTokenizer
367:             * 
368:             * @param longClassName -
369:             *            the long fully qualified calss name
370:             * @return - short class name
371:             */
372:            public static String getPackageName(String longClassName) {
373:                final StringTokenizer tk = new StringTokenizer(longClassName,
374:                        ".");
375:                final StringBuilder sb = new StringBuilder();
376:                String last = longClassName;
377:                while (tk.hasMoreTokens()) {
378:                    last = tk.nextToken();
379:                    if (tk.hasMoreTokens()) {
380:                        sb.append(last);
381:                        sb.append(".");
382:                    }
383:                }
384:
385:                return sb.toString().substring(0, sb.toString().length() - 1);
386:            }
387:
388:            /**
389:             * Retrurns the root package directory e.g com.ejb3unit.eg --> returns com.
390:             * 
391:             * @param location -
392:             *            the location of th epackage
393:             * 
394:             * @param longPackageName -
395:             *            the long fully qualified class name
396:             * @return - root file name
397:             */
398:            public static File getRootPackageDir(File location,
399:                    String longPackageName) {
400:                final StringTokenizer tk = new StringTokenizer(longPackageName,
401:                        ".");
402:                File back = location;
403:                while (tk.hasMoreTokens()) {
404:                    tk.nextToken();
405:                    back = back.getParentFile();
406:                }
407:                return back;
408:            }
409:
410:            /**
411:             * Retrun a short class name. E.g. java.util.StringTokenizer will be
412:             * StringTokenizer
413:             * 
414:             * @param clazz -
415:             *            for class
416:             * @return - short class name
417:             */
418:            public static String getShortClassName(Class clazz) {
419:                return getShortClassName(clazz.getName());
420:            }
421:
422:            /**
423:             * Returns a path to an temp directory.
424:             * 
425:             * @author Daniel Wiese
426:             * @since 29.06.2006
427:             * @return - a path to a temp directory.
428:             */
429:            public static File getTempDirectory() {
430:                File tempdir = new File(System.getProperty("java.io.tmpdir"));
431:                return tempdir;
432:            }
433:
434:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.