Source Code Cross Referenced for Util.java in  » Web-Services » crispy » net » sf » crispy » 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 » Web Services » crispy » net.sf.crispy.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on 08.03.2005
003:         *
004:         */
005:        package net.sf.crispy.util;
006:
007:        import java.io.FileInputStream;
008:        import java.io.FileNotFoundException;
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.net.InetAddress;
012:        import java.net.Socket;
013:        import java.util.Collection;
014:        import java.util.Date;
015:        import java.util.Enumeration;
016:        import java.util.Iterator;
017:        import java.util.Map;
018:        import java.util.Properties;
019:        import java.util.TreeMap;
020:        import java.util.Map.Entry;
021:        import java.util.logging.LogManager;
022:        import java.util.logging.Logger;
023:
024:        import javax.swing.JFrame;
025:        import javax.swing.JScrollPane;
026:        import javax.swing.JTree;
027:        import javax.swing.tree.DefaultMutableTreeNode;
028:        import javax.xml.namespace.QName;
029:
030:        import net.sf.crispy.InvocationException;
031:
032:        /**
033:         * Utility (helper) class.
034:         * 
035:         * @author Linke
036:         *
037:         */
038:        public class Util {
039:
040:            /**
041:             * Find the deepest Exception by nested Exception.
042:             * Example:
043:             * 
044:             * <pre>
045:             * Exception e0 = new Exception ();
046:             * Exception e1 = new Exception (e0);
047:             * 
048:             * Exception eFind = findDeepestThrowable(e1);
049:             * The result: eFind == e0
050:             * </pre>
051:             * 
052:             * @param pvThrowable Root-Exception.
053:             * @return The deepest Exception.
054:             */
055:            public static Throwable findDeepestThrowable(Throwable pvThrowable) {
056:                if (pvThrowable.getCause() != null) {
057:                    Throwable lvThrowable = pvThrowable.getCause();
058:                    return findDeepestThrowable(lvThrowable);
059:                } else {
060:                    return pvThrowable;
061:                }
062:            }
063:
064:            /**
065:             * Test to free port.
066:             * 
067:             * @param pvPort tested port
068:             * @return <code>true</code>, if the port is free, else <code>false</code>.
069:             */
070:            public static boolean isPortFree(int pvPort) {
071:                try {
072:                    InetAddress lvAddress = InetAddress.getLocalHost();
073:                    Socket lvSocket = new Socket(lvAddress, pvPort);
074:                    lvSocket.isBound();
075:                    return false;
076:                } catch (Exception e) {
077:                    return true;
078:                }
079:            }
080:
081:            /**
082:             * Separate from url the url and the port: <code>localhost:8080 --> http://localhost and 8080</code>
083:             * @param pvUrlAndPort
084:             * @return String-Array with pos 0 = URL and pos 1 = the Port
085:             */
086:            public static String[] separateHostAndPort(String pvUrlAndPort) {
087:                if (pvUrlAndPort == null) {
088:                    return null;
089:                } else {
090:                    String s[] = pvUrlAndPort.split(":");
091:                    if (s.length != 2) {
092:                        throw new IllegalArgumentException(
093:                                pvUrlAndPort
094:                                        + " is a not separatable URL. Expected a String with two ':', how by http://localhost:8080.");
095:                    }
096:                    String lvUrl = s[0];
097:                    String lvPort = s[1];
098:                    // test whether port is a integer
099:                    Integer.parseInt(lvPort);
100:                    s = new String[] { lvUrl, lvPort };
101:                    return s;
102:                }
103:            }
104:
105:            /**
106:             * Cut a String on the end of a input-String.
107:             * Example: Input-String <code>rmi://localhost:1099/</code>, Cutting-String <code>/</code>, New-String <code>rmi://localhost:1099</code>.
108:             * 
109:             * @param pvString Input-String
110:             * @param pvCuttingString Cutting-String
111:             * @return New String, without Cutting-String.
112:             */
113:            public static String cutString(final String pvString,
114:                    final String pvCuttingString) {
115:                if ((pvString != null) && (pvCuttingString != null)
116:                        && (pvString.endsWith(pvCuttingString))) {
117:                    return pvString.substring(0,
118:                            (pvString.length() - pvCuttingString.length()));
119:                } else {
120:                    return pvString;
121:                }
122:
123:            }
124:
125:            /**
126:             * Create a object with the reflection API.
127:             * 
128:             * @param pvObjectString String from the object.
129:             * @return Created object.
130:             * @throws Exception
131:             */
132:            public static Object createObject(String pvObjectString)
133:                    throws Exception {
134:                if (pvObjectString == null) {
135:                    return null;
136:                } else {
137:                    Class clazz = Class.forName(pvObjectString);
138:                    return clazz.newInstance();
139:                }
140:            }
141:
142:            /**
143:             * Test as search class is exist in the class array.
144:             * 
145:             * @param pvClassArray class array
146:             * @param pvSearchClass search class 
147:             * @return if the class is finding than <code>true</code>, else <code>false</code>
148:             */
149:            public static boolean isClassInArray(Class[] pvClassArray,
150:                    Class pvSearchClass) {
151:                if ((pvClassArray == null) || (pvSearchClass == null)) {
152:                    return false;
153:                }
154:                for (int i = 0; i < pvClassArray.length; i++) {
155:                    boolean equals = pvClassArray[i].equals(pvSearchClass);
156:                    if (equals == true) {
157:                        return true;
158:                    }
159:                }
160:                return false;
161:            }
162:
163:            public static String printArray(Object[] pvArray) {
164:                StringBuffer sb = new StringBuffer();
165:                if (pvArray == null) {
166:                    return "Array is NULL";
167:                } else {
168:                    for (int i = 0; i < pvArray.length; i++) {
169:                        sb.append(pvArray[i]).append(" -> ").append(
170:                                pvArray[i].getClass().getName()).append("\n");
171:                    }
172:                    return sb.toString();
173:                }
174:            }
175:
176:            public static String array2String(Object pvStrArray[]) {
177:                if (pvStrArray == null) {
178:                    return null;
179:                }
180:                StringBuffer ret = new StringBuffer();
181:                for (int i = 0; i < pvStrArray.length; i++) {
182:                    ret.append(pvStrArray[i]).append(" ; ");
183:                }
184:                return ret.toString();
185:            }
186:
187:            public static void printMapInfo(Map pvMap) {
188:                if (pvMap == null) {
189:                    System.out.println("Util.printMapInfo: Map ist null");
190:                } else {
191:                    Iterator it = pvMap.entrySet().iterator();
192:                    while (it.hasNext()) {
193:                        Map.Entry lvMapEntry = (Entry) it.next();
194:                        System.out.println(lvMapEntry.getKey() + " = "
195:                                + lvMapEntry.getValue() + " ("
196:                                + lvMapEntry.getValue().getClass().getName()
197:                                + ")");
198:                    }
199:                }
200:            }
201:
202:            public static String getClassNameWithOutPackage(final Class pvClass) {
203:                if (pvClass == null) {
204:                    return null;
205:                } else {
206:                    return getClassNameWithOutPackage(pvClass.getName());
207:                }
208:            }
209:
210:            public static String getClassNameWithOutPackage(
211:                    final String pvClassName) {
212:                if (pvClassName == null) {
213:                    return null;
214:                } else {
215:                    String lvNames[] = pvClassName.split("\\.");
216:                    return lvNames[(lvNames.length - 1)];
217:                }
218:            }
219:
220:            /**
221:             * Reverse the package fro class. For example:
222:             * <code>test.crispy.example.model.Kunde</code> -> <code>model.example.crispy.test</code>.
223:             * By a class without a package, is the return value a empty String.
224:             *   
225:             * @param pvClass
226:             * @return reverse string.
227:             */
228:            public static String reverse(Class pvClass) {
229:                if (pvClass == null) {
230:                    return null;
231:                }
232:                String lvClassName = pvClass.getName();
233:                String lvParts[] = lvClassName.split("\\.");
234:                String lvRet = "";
235:                int l = lvParts.length - 1;
236:                for (int i = l; i > 0; i--) {
237:                    lvRet = lvRet + lvParts[i - 1] + ".";
238:                }
239:                lvRet = lvRet.substring(0, (lvRet.length() - 1));
240:                return lvRet;
241:            }
242:
243:            /**
244:             * Create a <code>javax.xml.namespace.QName</code> by the class. For example
245:             * class: <code>test.crispy.example.model.Kunde</code> ->
246:             * LocalPart: <code>test.crispy.example.model.Kunde</code> and 
247:             * NamespaceURI: <code>model.example.crispy.test</code>.
248:             * 
249:             * @param pvClass The class for create a QName.
250:             * @return The created QName object.
251:             */
252:            public static QName createQNameByClass(Class pvClass) {
253:                if (pvClass == null) {
254:                    return null;
255:                }
256:                return new QName(Util.reverse(pvClass), pvClass.getName());
257:            }
258:
259:            /**
260:             * Get from the properites all properties, that begins with the prefix and sorted this
261:             * properties to the property keys.
262:             * 
263:             * @param pvProperties all properties
264:             * @param pvPrefix filter = prefix from property key
265:             * @return sorted Map from all properties with correct prefix.
266:             */
267:            public static Map getAllPropertiesByPrefixAndSort(
268:                    Properties pvProperties, String pvPrefix) {
269:                Enumeration enumeration = pvProperties.keys();
270:                Map lvSorterMap = new TreeMap();
271:                while (enumeration.hasMoreElements()) {
272:                    Object lvPropertyObject = enumeration.nextElement();
273:                    if (!(lvPropertyObject.getClass().getName()
274:                            .equals(String.class.getName()))) {
275:                        throw new InvocationException(
276:                                "The property must be a String, but is: "
277:                                        + lvPropertyObject.getClass().getName()
278:                                        + " --> with value (key): "
279:                                        + lvPropertyObject);
280:                    }
281:                    String lvKey = (String) lvPropertyObject;
282:
283:                    if (lvKey.startsWith(pvPrefix)) {
284:                        String lvProxyInterceptorStr = pvProperties
285:                                .getProperty(lvKey);
286:                        lvSorterMap.put(lvKey, lvProxyInterceptorStr);
287:                    }
288:                }
289:                return lvSorterMap;
290:            }
291:
292:            public static byte[] inputStream2ByteArray(InputStream is)
293:                    throws IOException {
294:                int BUFFER_SIZE = 8192;
295:                byte[] buffer = new byte[BUFFER_SIZE];
296:                byte[] content = new byte[0];
297:                int length;
298:                while ((length = is.read(buffer)) > 0) {
299:                    byte[] tmp = content;
300:                    content = new byte[tmp.length + length];
301:                    System.arraycopy(tmp, 0, content, 0, tmp.length);
302:                    System.arraycopy(buffer, 0, content, tmp.length, length);
303:                }
304:                return content;
305:            }
306:
307:            public static void visualisationTree(Object pvObject) {
308:                DefaultMutableTreeNode lvRoot = new DefaultMutableTreeNode(
309:                        "ROOT");
310:                createTree(pvObject, lvRoot);
311:                JFrame lvFrame = new JFrame("Crispy tree visualisation ...");
312:                JTree lvTree = new JTree(lvRoot);
313:                lvFrame.getContentPane().add(new JScrollPane(lvTree));
314:                lvFrame.setVisible(true);
315:                lvFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
316:                lvFrame.show();
317:                lvFrame.pack();
318:
319:            }
320:
321:            public static void createTree(Object pvObject,
322:                    DefaultMutableTreeNode pvRoot) {
323:                if (pvObject != null) {
324:                    Class clazz = pvObject.getClass();
325:                    if (pvObject instanceof  Collection) {
326:                        createTreeByCollection((Collection) pvObject, pvRoot,
327:                                null);
328:                    } else if (pvObject instanceof  Map) {
329:                        createTreeByMap((Map) pvObject, pvRoot, null);
330:                    } else if ((clazz.equals(Integer.class))
331:                            || (clazz.equals(Double.class))
332:                            || (clazz.equals(Boolean.class))
333:                            || (clazz.equals(String.class))
334:                            || (clazz.equals(Date.class))) {
335:                        pvRoot.add(new DefaultMutableTreeNode(pvObject));
336:                    } else {
337:                        pvRoot.add(new DefaultMutableTreeNode(
338:                                "-- Ungueltiger Typ: " + clazz + ": "
339:                                        + pvObject));
340:                    }
341:                }
342:            }
343:
344:            public static String map2String(Map pvMap, String pvPropertyName) {
345:                if (pvMap == null) {
346:                    return null;
347:                }
348:                StringBuffer sb = null;
349:                if (pvPropertyName != null) {
350:                    sb = new StringBuffer(pvPropertyName + ": ");
351:                } else {
352:                    sb = new StringBuffer();
353:                }
354:                StringBuffer sbClazz = new StringBuffer();
355:
356:                Iterator it = pvMap.entrySet().iterator();
357:                while (it.hasNext()) {
358:                    Map.Entry lvMapEntry = (Entry) it.next();
359:                    Object key = lvMapEntry.getKey();
360:                    Object value = lvMapEntry.getValue();
361:                    //            if ((value instanceof Collection) || (value instanceof Map)) {
362:
363:                    // mache nichts
364:                    //            } else {
365:                    // ???????????
366:                    if (!(value instanceof  Collection)
367:                            && !(value instanceof  Map)) {
368:                        if (key.toString().compareTo("class") == 0) {
369:                            sbClazz.append(" -> (").append(key).append(" = ")
370:                                    .append(value).append(")");
371:                        } else {
372:                            sb.append("[").append(key).append(" = ").append(
373:                                    value).append("] ");
374:                        }
375:                    }
376:                }
377:                return sb.append(sbClazz).toString();
378:            }
379:
380:            public static void createTreeByMap(Map pvMap,
381:                    DefaultMutableTreeNode pvRoot, String pvPropertyName) {
382:                if (pvMap != null) {
383:                    DefaultMutableTreeNode child = new DefaultMutableTreeNode(
384:                            map2String(pvMap, pvPropertyName));
385:                    pvRoot.add(child);
386:
387:                    Iterator it = pvMap.entrySet().iterator();
388:                    while (it.hasNext()) {
389:                        Map.Entry lvMapEntry = (Entry) it.next();
390:                        if (lvMapEntry.getValue() instanceof  Collection) {
391:                            createTreeByCollection((Collection) lvMapEntry
392:                                    .getValue(), child, lvMapEntry.getKey()
393:                                    .toString());
394:                        } else if (lvMapEntry.getValue() instanceof  Map) {
395:                            createTreeByMap((Map) lvMapEntry.getValue(), child,
396:                                    lvMapEntry.getKey().toString());
397:                        }
398:                    }
399:                }
400:            }
401:
402:            public static void createTreeByCollection(Collection pvCollection,
403:                    DefaultMutableTreeNode pvRoot, String pvPropertyName) {
404:                if ((pvCollection != null) && (pvCollection.size() > 0)) {
405:                    Iterator it = pvCollection.iterator();
406:                    while (it.hasNext()) {
407:                        Object value = it.next();
408:                        if (value instanceof  Collection) {
409:                            createTreeByCollection((Collection) value, pvRoot,
410:                                    pvPropertyName);
411:                        } else if (value instanceof  Map) {
412:                            createTreeByMap((Map) value, pvRoot, pvPropertyName);
413:                        }
414:                    }
415:                }
416:            }
417:
418:            public static boolean initJdkLogger() {
419:                return initJdkLogger2(Util.class
420:                        .getResourceAsStream("jdk14-logging.properties"));
421:            }
422:
423:            public static boolean initJdkLogger(String pvFileName) {
424:                try {
425:                    return initJdkLogger2(new FileInputStream(pvFileName));
426:                } catch (FileNotFoundException e) {
427:                    e.printStackTrace();
428:                }
429:                return false;
430:            }
431:
432:            public static boolean initJdkLogger2(InputStream pvInputStream) {
433:                try {
434:                    LogManager.getLogManager().readConfiguration(pvInputStream);
435:                    Logger logger = Logger.global;
436:                    LogManager.getLogManager().addLogger(logger);
437:                    logger.finest("Jdk14Logger initialisiert ...");
438:                    return true;
439:                } catch (Exception e) {
440:                    e.printStackTrace();
441:                }
442:                return false;
443:            }
444:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.