/* * Copyright (c) 2004, KNOPFLERFISH project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * - Neither the name of the KNOPFLERFISH project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.knopflerfish.osgi.bundle.bundlerepository.desktop; import org.osgi.framework.*; import java.util.*; import org.ungoverned.osgi.service.bundlerepository.*; import java.net.*; import java.io.*; import java.lang.reflect.Array; /** * Misc static utility methods */ public class Util { /** * Check if a bundle is installed from repo * */ public static boolean isInRepo(Bundle b, String updateURL) { String bundleUpLoc = (String)b.getHeaders().get(BundleRecord.BUNDLE_UPDATELOCATION); boolean bIsRepoBundle = false; if(bundleUpLoc != null) { bIsRepoBundle = updateURL.equals(bundleUpLoc); } else { bIsRepoBundle = updateURL.equals(b.getLocation()); } return bIsRepoBundle; } /** * Check if an installed bundle corresponds to an OBR BubdleRecord. * *
* Equality is tested on either equal bundle locations, or * if bundle name and bundle version are equal. *
* */ static boolean bundleEqual(Bundle b, BundleRecord br) { String loc = (String)br.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); if(loc.equals(b.getLocation())) { return true; } // Use UUID if present String uuid_br = (String)br.getAttribute("Bundle-UUID"); String uuid_b = (String)b.getHeaders().get("Bundle-UUID"); if(uuid_b != null && uuid_b.equals(uuid_br)) { return true; } if(bundleAttrEqual(b, br, "Bundle-Name") && bundleAttrEqual(b, br, "Bundle-Version")) { return true; } return false; } /** * Compare attribute values in a bundle and a bundle record. * * An attribute value is equal either if its reference equal, or * if equals on the bundle's attribute value is true */ static boolean bundleAttrEqual(Bundle b, BundleRecord br, String attr) { String val_br = (String)br.getAttribute(attr); String val_b = (String)b.getHeaders().get(attr); return (val_br == val_b) || (val_b != null && val_b.equals(val_br)); } /** * Get a string value from a BundleRecord, with default value * * @param name attribute to get * @param def value to return if attribute value is null or * the empty string. * @return value if the specified attribute or def if * the value is null or the empty string. */ public static String getAttribute(BundleRecord br, String name, String def) { Object obj = br.getAttribute(name); String s = def; if(obj instanceof String) { s = (String)obj; if(s == null || "".equals(s)) { s = def; } } else if(obj instanceof java.util.List) { java.util.List list = (java.util.List)obj; if(list.size() == 1) { return list.get(0).toString(); } else if(list.size() > 1) { System.out.println(name + " has more than one entry " + list + ", using the first"); return list.get(0).toString(); } else { return def; } } else { System.out.println(name + ", type=" + obj.getClass().getName() + " " + obj); } return s; } /** * Generic Object to HTML string conversion method. */ public static String toHTML(Object obj) { if(obj == null) { return "null"; } if(obj instanceof String) { String s = (String)obj; try { URL url = new URL(s); return "" + s + ""; } catch (Exception e) { } return s; } else if(obj.getClass().isArray()) { StringBuffer sb = new StringBuffer(); int len = Array.getLength(obj); for(int i = 0; i < len; i++) { sb.append(toHTML(Array.get(obj, i))); if(i < len - 1) { sb.append("