Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | package org.apache.osgi.bundle.shell; |
| 18 | |
| 19 | import org.osgi.framework.Bundle; |
| 20 | import org.osgi.framework.Constants; |
| 21 | |
| 22 | public class Util |
| 23 | { |
| 24 | public static String getBundleName(Bundle bundle) |
| 25 | { |
| 26 | if (bundle != null) |
| 27 | { |
| 28 | String name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME); |
| 29 | return (name == null) |
| 30 | ? "Bundle " + Long.toString(bundle.getBundleId()) |
| 31 | : name + " (" + Long.toString(bundle.getBundleId()) + ")"; |
| 32 | } |
| 33 | return "[STALE BUNDLE]"; |
| 34 | } |
| 35 | |
| 36 | private static StringBuffer m_sb = new StringBuffer(); |
| 37 | |
| 38 | public static String getUnderlineString(String s) |
| 39 | { |
| 40 | synchronized (m_sb) |
| 41 | { |
| 42 | m_sb.delete(0, m_sb.length()); |
| 43 | for (int i = 0; i < s.length(); i++) |
| 44 | { |
| 45 | m_sb.append('-'); |
| 46 | } |
| 47 | return m_sb.toString(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public static String getValueString(Object obj) |
| 52 | { |
| 53 | synchronized (m_sb) |
| 54 | { |
| 55 | if (obj instanceof String) |
| 56 | { |
| 57 | return (String) obj; |
| 58 | } |
| 59 | else if (obj instanceof String[]) |
| 60 | { |
| 61 | String[] array = (String[]) obj; |
| 62 | m_sb.delete(0, m_sb.length()); |
| 63 | for (int i = 0; i < array.length; i++) |
| 64 | { |
| 65 | if (i != 0) |
| 66 | { |
| 67 | m_sb.append(", "); |
| 68 | } |
| 69 | m_sb.append(array[i].toString()); |
| 70 | } |
| 71 | return m_sb.toString(); |
| 72 | } |
| 73 | else if (obj instanceof Boolean) |
| 74 | { |
| 75 | return ((Boolean) obj).toString(); |
| 76 | } |
| 77 | else if (obj instanceof Long) |
| 78 | { |
| 79 | return ((Long) obj).toString(); |
| 80 | } |
| 81 | else if (obj instanceof Integer) |
| 82 | { |
| 83 | return ((Integer) obj).toString(); |
| 84 | } |
| 85 | else if (obj instanceof Short) |
| 86 | { |
| 87 | return ((Short) obj).toString(); |
| 88 | } |
| 89 | else if (obj instanceof Double) |
| 90 | { |
| 91 | return ((Double) obj).toString(); |
| 92 | } |
| 93 | else if (obj instanceof Float) |
| 94 | { |
| 95 | return ((Float) obj).toString(); |
| 96 | } |
| 97 | |
| 98 | return "<unknown value type>"; |
| 99 | } |
| 100 | } |
| 101 | } |