/*
* Dog - Core
*
* Copyright (c) 2013 Dario Bonino and Luigi De Russis
*
* This software is based on a bundle of the Apache Felix project.
* See the NOTICE file distributed with this work for additional information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package it.polito.elite.dog.core.devicemanager.util;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.device.Device;
/**
* This class provides multiple utilities for the Device Manager bundle.
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
* (original version)
* @author <a href="mailto:luigi.derussis@polito.it">Luigi De Russis</a>
* (successive modifications)
* @see <a href="http://elite.polito.it">http://elite.polito.it</a>
*
*/
public class Util
{
/**
* Default (empty) constructor
*/
private Util()
{
}
public static String showDriver(ServiceReference<?> ref)
{
Object objectClass = ref.getProperty(Constants.OBJECTCLASS);
Object driverId = ref.getProperty(org.osgi.service.device.Constants.DRIVER_ID);
StringBuffer buffer = new StringBuffer();
buffer.append("Driver: ");
buffer.append(Constants.OBJECTCLASS).append("=");
if (String[].class.isInstance(objectClass))
{
buffer.append(enumerateStringArray(String[].class.cast(objectClass)));
}
else
{
buffer.append(objectClass);
}
buffer.append(" ");
buffer.append(org.osgi.service.device.Constants.DRIVER_ID).append("=");
buffer.append(driverId);
return buffer.toString();
}
public static boolean isDevice(ServiceReference<?> ref)
{
try
{
Filter device = createFilter("(|(%s=%s)(%s=%s))",
new Object[] { Constants.OBJECTCLASS, Device.class.getName(),
org.osgi.service.device.Constants.DEVICE_CATEGORY, "*" });
return device.match(ref);
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
public static boolean isDeviceInstance(ServiceReference<?> ref)
{
try
{
Filter device = createFilter("(%s=%s)", new Object[] { Constants.OBJECTCLASS, Device.class.getName() });
return device.match(ref);
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
public static String createFilterString(String input, Object[] data)
{
return String.format(input, data);
}
public static Filter createFilter(String input, Object[] data) throws InvalidSyntaxException
{
return FrameworkUtil.createFilter(String.format(input, data));
}
public static String showDevice(ServiceReference<?> ref)
{
Object objectClass = ref.getProperty(Constants.OBJECTCLASS);
Object category = ref.getProperty(org.osgi.service.device.Constants.DEVICE_CATEGORY);
StringBuffer buffer = new StringBuffer();
buffer.append("Device: ");
buffer.append(Constants.OBJECTCLASS).append("=");
appendObject(buffer, objectClass);
buffer.append(" ");
buffer.append(org.osgi.service.device.Constants.DEVICE_CATEGORY).append("=");
appendObject(buffer, category);
buffer.append("\n{ ");
String[] keys = ref.getPropertyKeys();
for (String key : keys)
{
if (key.equals(Constants.OBJECTCLASS))
{
continue;
}
if (key.equals(org.osgi.service.device.Constants.DEVICE_CATEGORY))
{
continue;
}
buffer.append(key).append("=");
appendObject(buffer, ref.getProperty(key));
buffer.append(" ");
}
buffer.append("}\n");
return buffer.toString();
}
private static void appendObject(StringBuffer buffer, Object obj)
{
if (String[].class.isInstance(obj))
{
buffer.append(enumerateStringArray(String[].class.cast(obj)));
}
else
{
buffer.append(obj);
}
}
private static String enumerateStringArray(String[] strings)
{
StringBuffer buffer = new StringBuffer();
buffer.append("[");
for (String str : strings)
{
buffer.append(str);
buffer.append(" ");
}
buffer.deleteCharAt(buffer.length() - 1);
buffer.append("]");
return buffer.toString();
}
}