Author: atk
Date: Thu Dec 3 08:45:49 2009
New Revision: 886705
URL: http://svn.apache.org/viewvc?rev=886705&view=rev
Log:
ARIES-31 ARIES-35 Implement BundleState and ServiceState MBeans
Added:
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/FrameworkUtils.java
(with props)
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java
(with props)
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/FrameworkUtilsTest.java
(with props)
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/TypeUtilsTest.java
(with props)
Added:
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/FrameworkUtils.java
URL:
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/FrameworkUtils.java?rev=886705&view=auto
==============================================================================
---
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/FrameworkUtils.java
(added)
+++
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/FrameworkUtils.java
Thu Dec 3 08:45:49 2009
@@ -0,0 +1,480 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.aries.jmx.util;
+
+import static org.osgi.jmx.framework.BundleStateMBean.ACTIVE;
+import static org.osgi.jmx.framework.BundleStateMBean.INSTALLED;
+import static org.osgi.jmx.framework.BundleStateMBean.RESOLVED;
+import static org.osgi.jmx.framework.BundleStateMBean.STARTING;
+import static org.osgi.jmx.framework.BundleStateMBean.STOPPING;
+import static org.osgi.jmx.framework.BundleStateMBean.UNINSTALLED;
+import static org.osgi.jmx.framework.BundleStateMBean.UNKNOWN;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.packageadmin.RequiredBundle;
+
+/**
+ * This class contains common utilities related to Framework operations for
the MBean implementations
+ *
+ * @version $Rev$ $Date$
+ */
+public class FrameworkUtils {
+
+ private FrameworkUtils() {
+ super();
+ }
+
+ /**
+ *
+ * Returns the Bundle object for a given id
+ *
+ * @param bundleContext
+ * @param bundleId
+ * @return
+ * @throws IllegalArgumentException
+ * if no Bundle is found with matching bundleId
+ */
+ public static Bundle resolveBundle(BundleContext bundleContext, long
bundleId) throws IllegalArgumentException {
+ if (bundleContext == null) {
+ throw new IllegalArgumentException("Argument bundleContext cannot
be null");
+ }
+ Bundle bundle = bundleContext.getBundle(bundleId);
+ if (bundle == null) {
+ throw new IllegalArgumentException("Bundle with id [" + bundleId +
"] Not Found");
+ }
+ return bundle;
+ }
+
+ /**
+ * Returns an array of bundleIds
+ *
+ * @param bundles
+ * array of <code>Bundle</code> objects
+ * @return bundleIds in sequence
+ */
+ public static long[] getBundleIds(Bundle[] bundles) {
+ long[] result = (bundles == null) ? new long[0] : new
long[bundles.length];
+ for (int i = 0; i < result.length; i++) {
+ result[i] = bundles[i].getBundleId();
+ }
+ return result;
+ }
+
+ /**
+ *
+ * Returns the ServiceReference object with matching service.id
+ *
+ * @param bundleContext
+ * @param serviceId
+ * @return ServiceReference with matching service.id property
+ * @throws IllegalArgumentException
+ * if bundleContext is null or no service is found with the
given id
+ */
+ public static ServiceReference resolveService(BundleContext bundleContext,
long serviceId) {
+ if (bundleContext == null) {
+ throw new IllegalArgumentException("Argument bundleContext cannot
be null");
+ }
+ ServiceReference result = null;
+ try {
+ ServiceReference[] references =
bundleContext.getAllServiceReferences(null, "(" + Constants.SERVICE_ID + "=" +
serviceId + ")");
+ if (references == null || references.length < 1) {
+ throw new IllegalArgumentException("Service with id [" +
serviceId + "] Not Found");
+ } else {
+ result = references[0];
+ }
+ } catch (InvalidSyntaxException e) {
+ throw new IllegalStateException("Failure when resolving service ",
e);
+ }
+ return result;
+ }
+
+ /**
+ * Returns an array of service.id values
+ *
+ * @param serviceReferences
+ * array of <code>ServiceReference</code> objects
+ * @return service.id values in sequence
+ */
+ public static long[] getServiceIds(ServiceReference[] serviceReferences) {
+ long result[] = (serviceReferences == null) ? new long[0] : new
long[serviceReferences.length];
+ for (int i = 0; i < result.length; i++) {
+ result[i] = (Long)
serviceReferences[i].getProperty(Constants.SERVICE_ID);
+ }
+ return result;
+ }
+
+ /**
+ * Returns the packages exported by the specified bundle
+ *
+ * @param bundle
+ * @param packageAdmin
+ * @return
+ * @throws IllegalArgumentException
+ * if bundle or packageAdmin are null
+ */
+ public static String[] getBundleExportedPackages(Bundle bundle,
PackageAdmin packageAdmin)
+ throws IllegalArgumentException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ String[] exportedPackages;
+ ExportedPackage[] exported = packageAdmin.getExportedPackages(bundle);
+ if (exported != null) {
+ exportedPackages = new String[exported.length];
+ for (int i = 0; i < exported.length; i++) {
+ exportedPackages[i] = exported[i].getName() + ";" +
exported[i].getVersion().toString();
+ }
+ } else {
+ exportedPackages = new String[0];
+ }
+ return exportedPackages;
+ }
+
+ /**
+ * Returns the bundle ids of any resolved fragments
+ *
+ * @param bundle
+ * @param packageAdmin
+ * @return
+ * @throws IllegalArgumentException
+ * if bundle or packageAdmin are null
+ */
+ public static long[] getFragmentIds(Bundle bundle, PackageAdmin
packageAdmin) throws IllegalArgumentException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ long[] fragmentIds;
+ Bundle[] fragments = packageAdmin.getFragments(bundle);
+ if (fragments != null) {
+ fragmentIds = getBundleIds(fragments);
+ } else {
+ fragmentIds = new long[0];
+ }
+ return fragmentIds;
+ }
+
+ /**
+ * Returns the bundle ids of any resolved hosts
+ *
+ * @param fragment
+ * @param packageAdmin
+ * @return
+ * @throws IllegalArgumentException
+ * if fragment or packageAdmin are null
+ */
+ public static long[] getHostIds(Bundle fragment, PackageAdmin
packageAdmin) throws IllegalArgumentException {
+ if (fragment == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ long[] hostIds;
+ Bundle[] hosts = packageAdmin.getHosts(fragment);
+ if (hosts != null) {
+ hostIds = getBundleIds(hosts);
+ } else {
+ hostIds = new long[0];
+ }
+ return hostIds;
+ }
+
+ /**
+ * Returns the resolved package imports for the given bundle
+ *
+ * @param bundle
+ * @param packageAdmin
+ * @return
+ * @throws IllegalArgumentException
+ * if fragment or packageAdmin are null
+ */
+ public static String[] getBundleImportedPackages(Bundle bundle,
PackageAdmin packageAdmin)
+ throws IllegalArgumentException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ List<String> result = new ArrayList<String>();
+ // TODO - Is there an easier way to achieve this? Unable to find a
direct way through Framework
+ // API to find the actual package wiring
+ Bundle[] bundles = bundle.getBundleContext().getBundles();
+ for (Bundle candidate : bundles) {
+ if (candidate.equals(bundle)) {
+ continue;
+ }
+ ExportedPackage[] candidateExports =
packageAdmin.getExportedPackages(candidate);
+ if (candidateExports != null) {
+ for (ExportedPackage exportedPackage : candidateExports) {
+ Bundle[] userBundles =
exportedPackage.getImportingBundles();
+ if (userBundles != null && arrayContains(userBundles,
bundle)) {
+ result.add(exportedPackage.getName() + ";" +
exportedPackage.getVersion().toString());
+ }
+ }// end for candidateExports
+ }
+ }// end for bundles
+ return result.toArray(new String[result.size()]);
+ }
+
+ /**
+ * Returns the service.id values for services registered by the given
bundle
+ *
+ * @param bundle
+ * @return
+ * @throws IllegalArgumentException
+ * if bundle is null
+ * @throws IlleglStateException
+ * if bundle has been uninstalled
+ */
+ public static long[] getRegisteredServiceIds(Bundle bundle) throws
IllegalArgumentException, IllegalStateException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ long[] serviceIds;
+ ServiceReference[] serviceReferences = bundle.getRegisteredServices();
+ if (serviceReferences != null) {
+ serviceIds = new long[serviceReferences.length];
+ for (int i = 0; i < serviceReferences.length; i++) {
+ serviceIds[i] = (Long)
serviceReferences[i].getProperty(Constants.SERVICE_ID);
+ }
+ } else {
+ serviceIds = new long[0];
+ }
+ return serviceIds;
+ }
+
+ /**
+ * Returns the service.id values of services being used by the given bundle
+ *
+ * @param bundle
+ * @return
+ * @throws IllegalArgumentException
+ * if bundle is null
+ * @throws IlleglStateException
+ * if bundle has been uninstalled
+ */
+ public static long[] getServicesInUseByBundle(Bundle bundle) throws
IllegalArgumentException, IllegalStateException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ long[] serviceIds;
+ ServiceReference[] serviceReferences = bundle.getServicesInUse();
+ if (serviceReferences != null) {
+ serviceIds = new long[serviceReferences.length];
+ for (int i = 0; i < serviceReferences.length; i++) {
+ serviceIds[i] = (Long)
serviceReferences[i].getProperty(Constants.SERVICE_ID);
+ }
+ } else {
+ serviceIds = new long[0];
+ }
+ return serviceIds;
+ }
+
+ /**
+ * Returns the status of pending removal
+ *
+ * @param bundle
+ * @return true if the bundle is pending removal
+ * @throws IllegalArgumentException
+ * if bundle or packageAdmin are null
+ */
+ public static boolean isBundlePendingRemoval(Bundle bundle, PackageAdmin
packageAdmin)
+ throws IllegalArgumentException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ boolean result = false;
+ RequiredBundle[] requiredBundles =
packageAdmin.getRequiredBundles(bundle.getSymbolicName());
+ if (requiredBundles != null) {
+ for (RequiredBundle requiredBundle : requiredBundles) {
+ Bundle required = requiredBundle.getBundle();
+ if (required != null && required.equals(bundle)) {
+ result = requiredBundle.isRemovalPending();
+ break;
+ }
+ }// end for requiredBundles
+ }
+ return result;
+ }
+
+ /**
+ * Checks if the given bundle is currently required by other bundles
+ *
+ * @param bundle
+ * @param packageAdmin
+ * @return
+ * @throws IllegalArgumentException
+ * if bundle or packageAdmin are null
+ */
+ public static boolean isBundleRequiredByOthers(Bundle bundle, PackageAdmin
packageAdmin)
+ throws IllegalArgumentException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ boolean result = false;
+ RequiredBundle[] requiredBundles =
packageAdmin.getRequiredBundles(bundle.getSymbolicName());
+ if (requiredBundles != null) {
+ for (RequiredBundle requiredBundle : requiredBundles) {
+ Bundle required = requiredBundle.getBundle();
+ if (required != null && required.equals(bundle)) {
+ Bundle[] requiring = requiredBundle.getRequiringBundles();
+ if (requiring != null && requiring.length > 0) {
+ result = true;
+ break;
+ }
+ }
+ }// end for requiredBundles
+ }
+ return result;
+ }
+
+ /**
+ * Returns an array of ids of bundles the given bundle depends on
+ *
+ * @param bundle
+ * @param packageAdmin
+ * @return
+ * @throws IllegalArgumentException
+ * if bundle or packageAdmin are null
+ */
+ public static long[] getBundleDependencies(Bundle bundle, PackageAdmin
packageAdmin)
+ throws IllegalArgumentException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ List<Bundle> dependencies = new ArrayList<Bundle>();
+ // TODO - Is there an easier way to achieve this? Unable to find a
direct way through Framework
+ // API to resolve the current dependencies
+ for (Bundle candidate : bundle.getBundleContext().getBundles()) {
+ if (candidate.equals(bundle)) {
+ continue;
+ }
+ RequiredBundle[] candidateRequiredBundles =
packageAdmin.getRequiredBundles(candidate.getSymbolicName());
+ if (candidateRequiredBundles == null) {
+ continue;
+ } else {
+ for (RequiredBundle candidateRequiredBundle :
candidateRequiredBundles) {
+ Bundle[] bundlesRequiring =
candidateRequiredBundle.getRequiringBundles();
+ if (bundlesRequiring != null &&
arrayContains(bundlesRequiring, bundle)) {
+ dependencies.add(candidateRequiredBundle.getBundle());
+ }
+ }
+ }
+ }
+ return getBundleIds(dependencies.toArray(new
Bundle[dependencies.size()]));
+ }
+
+ /**
+ * Returns an array of ids of bundles that depend on the given bundle
+ *
+ * @param bundle
+ * @param packageAdmin
+ * @return
+ * @throws IllegalArgumentException
+ * if bundle or packageAdmin are null
+ */
+ public static long[] getDependentBundles(Bundle bundle, PackageAdmin
packageAdmin) throws IllegalArgumentException {
+ if (bundle == null) {
+ throw new IllegalArgumentException("Argument bundle cannot be
null");
+ }
+ if (packageAdmin == null) {
+ throw new IllegalArgumentException("Argument packageAdmin cannot
be null");
+ }
+ long[] bundleIds = new long[0];
+ RequiredBundle[] requiredBundles =
packageAdmin.getRequiredBundles(bundle.getSymbolicName());
+ if (requiredBundles != null) {
+ for (RequiredBundle requiredBundle : requiredBundles) {
+ Bundle required = requiredBundle.getBundle();
+ if (required != null && required.equals(bundle)) {
+ bundleIds =
getBundleIds(requiredBundle.getRequiringBundles());
+ }
+ }
+ }
+ return bundleIds;
+ }
+
+ /**
+ * Returns a String representation of the bundles state
+ *
+ * @param bundle
+ * @return
+ */
+ public static String getBundleState(Bundle bundle) {
+ String state = UNKNOWN;
+ switch (bundle.getState()) {
+ case Bundle.INSTALLED:
+ state = INSTALLED;
+ break;
+ case Bundle.RESOLVED:
+ state = RESOLVED;
+ break;
+ case Bundle.STARTING:
+ state = STARTING;
+ break;
+ case Bundle.ACTIVE:
+ state = ACTIVE;
+ break;
+ case Bundle.STOPPING:
+ state = STOPPING;
+ break;
+ case Bundle.UNINSTALLED:
+ state = UNINSTALLED;
+ }
+ return state;
+ }
+
+ /*
+ * Checks if an object exists in the given array (based on object equality)
+ */
+ public static boolean arrayContains(Object[] array, Object value) {
+ boolean result = false;
+ if (array != null && value != null) {
+ for (Object element : array) {
+ if (value.equals(element)) {
+ result = true;
+ break;
+ }
+ }
+ }
+ return result;
+ }
+}
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/FrameworkUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/FrameworkUtils.java
------------------------------------------------------------------------------
svn:keywords = Revision Date
Added:
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java
URL:
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java?rev=886705&view=auto
==============================================================================
---
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java
(added)
+++
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java
Thu Dec 3 08:45:49 2009
@@ -0,0 +1,194 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.aries.jmx.util;
+
+import static org.osgi.jmx.JmxConstants.BIGDECIMAL;
+import static org.osgi.jmx.JmxConstants.BIGINTEGER;
+import static org.osgi.jmx.JmxConstants.BOOLEAN;
+import static org.osgi.jmx.JmxConstants.BYTE;
+import static org.osgi.jmx.JmxConstants.CHARACTER;
+import static org.osgi.jmx.JmxConstants.DOUBLE;
+import static org.osgi.jmx.JmxConstants.FLOAT;
+import static org.osgi.jmx.JmxConstants.INTEGER;
+import static org.osgi.jmx.JmxConstants.LONG;
+import static org.osgi.jmx.JmxConstants.P_BOOLEAN;
+import static org.osgi.jmx.JmxConstants.P_BYTE;
+import static org.osgi.jmx.JmxConstants.P_CHAR;
+import static org.osgi.jmx.JmxConstants.P_DOUBLE;
+import static org.osgi.jmx.JmxConstants.P_FLOAT;
+import static org.osgi.jmx.JmxConstants.P_INT;
+import static org.osgi.jmx.JmxConstants.P_LONG;
+import static org.osgi.jmx.JmxConstants.P_SHORT;
+import static org.osgi.jmx.JmxConstants.SHORT;
+import static org.osgi.jmx.JmxConstants.STRING;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class provides common utilities related to type conversions for the
MBean implementations
+ *
+ * @version $Rev$ $Date$
+ */
+public class TypeUtils {
+
+ private TypeUtils() {
+ super();
+ }
+
+ public static Map<String, Class<? extends Object>> primitiveTypes = new
HashMap<String, Class<? extends Object>>();
+ public static Map<String, Class<? extends Object>> wrapperTypes = new
HashMap<String, Class<? extends Object>>();
+ public static Map<String, Class<? extends Object>> mathTypes = new
HashMap<String, Class<? extends Object>>();
+ public static Map<Class<? extends Object>, Class<? extends Object>>
primitiveToWrapper = new HashMap<Class<? extends Object>, Class<? extends
Object>>();
+ public static Map<String, Class<? extends Object>> types = new
HashMap<String, Class<? extends Object>>();
+
+ static {
+ primitiveTypes.put(P_FLOAT, Float.TYPE);
+ primitiveTypes.put(P_INT, Integer.TYPE);
+ primitiveTypes.put(P_LONG, Long.TYPE);
+ primitiveTypes.put(P_DOUBLE, Double.TYPE);
+ primitiveTypes.put(P_BYTE, Byte.TYPE);
+ primitiveTypes.put(P_SHORT, Short.TYPE);
+ primitiveTypes.put(P_CHAR, Character.TYPE);
+ primitiveTypes.put(P_BOOLEAN, Boolean.TYPE);
+ primitiveToWrapper.put(Float.TYPE, Float.class);
+ primitiveToWrapper.put(Integer.TYPE, Integer.class);
+ primitiveToWrapper.put(Long.TYPE, Long.class);
+ primitiveToWrapper.put(Double.TYPE, Double.class);
+ primitiveToWrapper.put(Byte.TYPE, Byte.class);
+ primitiveToWrapper.put(Short.TYPE, Short.class);
+ primitiveToWrapper.put(Boolean.TYPE, Boolean.class);
+ wrapperTypes.put(INTEGER, Integer.class);
+ wrapperTypes.put(FLOAT, Float.class);
+ wrapperTypes.put(LONG, Long.class);
+ wrapperTypes.put(DOUBLE, Double.class);
+ wrapperTypes.put(BYTE, Byte.class);
+ wrapperTypes.put(SHORT, Short.class);
+ wrapperTypes.put(BOOLEAN, Boolean.class);
+ wrapperTypes.put(CHARACTER, Character.class);
+ mathTypes.put(BIGDECIMAL, BigDecimal.class);
+ mathTypes.put(BIGINTEGER, BigInteger.class);
+ types.put(STRING, String.class);
+ types.putAll(primitiveTypes);
+ types.putAll(wrapperTypes);
+ types.putAll(mathTypes);
+ }
+
+ /**
+ * Converts a <code>Dictionary</code> object to a <code>Map</code>
+ *
+ * @param dictionary
+ * @return
+ */
+ public static Map<String, String> fromDictionary(Dictionary<String,
String> dictionary) {
+ Map<String, String> result = new HashMap<String, String>();
+ Enumeration<String> keys = dictionary.keys();
+ while (keys.hasMoreElements()) {
+ String key = keys.nextElement();
+ result.put(key, dictionary.get(key));
+ }
+ return result;
+ }
+
+ /**
+ * Converts primitive long[] array to Long[]
+ *
+ * @param array
+ * @return
+ */
+ public static Long[] toLong(long[] array) {
+ Long[] toArray = (array == null) ? new Long[0] : new
Long[array.length];
+ for (int i = 0; i < toArray.length; i++) {
+ toArray[i] = array[i];
+ }
+ return toArray;
+ }
+
+ /**
+ * Converts Long[] array to primitive
+ *
+ * @param array
+ * @return
+ */
+ public static long[] toPrimitive(Long[] array) {
+ long[] toArray = (array == null) ? new long[0] : new
long[array.length];
+ for (int i = 0; i < toArray.length; i++) {
+ toArray[i] = array[i];
+ }
+ return toArray;
+ }
+
+ /**
+ * Converts a String value to an Object of the specified type
+ *
+ * @param type
+ * one of types listed in {...@link #types}
+ * @param value
+ * @return instance of class <code>type</code>
+ * @throws IllegalArgumentException
+ * if type or value are null or if the Class type does not
support a valueOf() or cannot be converted to
+ * a wrapper type
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T fromString(Class<T> type, String value) {
+ if (type == null || !types.containsValue(type)) {
+ throw new IllegalArgumentException("Cannot convert to type
argument : " + type);
+ }
+ if (value == null || value.length() < 1) {
+ throw new IllegalArgumentException("Argument value cannot be null
or empty");
+ }
+ T result = null;
+ try {
+ if (type.equals(String.class)) {
+ result = (T) value;
+ } else if (type.equals(Character.class) ||
type.equals(Character.TYPE)) {
+ result = (T) Character.valueOf(value.charAt(0));
+ } else if (wrapperTypes.containsValue(type) ||
mathTypes.containsValue(type)) {
+ Constructor<? extends Object> constructor =
type.getConstructor(String.class);
+ result = (T) constructor.newInstance(value);
+ } else if (primitiveToWrapper.containsKey(type)) { // attempt to
promote to wrapper and resolve to the base
+ // type
+ Class<? extends Object> promotedType =
primitiveToWrapper.get(type);
+ char[] simpleTypeName = type.getName().toCharArray();
+ simpleTypeName[0] = Character.toUpperCase(simpleTypeName[0]);
+ String parseMethodName = "parse" + new String(simpleTypeName);
+ Method parseMethod =
promotedType.getDeclaredMethod(parseMethodName, String.class);
+ result = (T) parseMethod.invoke(null, value);
+ }
+ } catch (SecurityException e) {
+ throw new IllegalArgumentException("Cannot convert value [" +
value + "] to type [" + type + "]", e);
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException("Cannot convert value [" +
value + "] to type [" + type + "]", e);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Cannot convert value [" +
value + "] to type [" + type + "]", e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException("Cannot convert value [" +
value + "] to type [" + type + "]", e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException("Cannot convert value [" +
value + "] to type [" + type + "]", e);
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException("Cannot convert value [" +
value + "] to type [" + type + "]", e);
+ }
+ return result;
+ }
+}
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/util/TypeUtils.java
------------------------------------------------------------------------------
svn:keywords = Revision Date
Added:
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/FrameworkUtilsTest.java
URL:
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/FrameworkUtilsTest.java?rev=886705&view=auto
==============================================================================
---
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/FrameworkUtilsTest.java
(added)
+++
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/FrameworkUtilsTest.java
Thu Dec 3 08:45:49 2009
@@ -0,0 +1,265 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.aries.jmx.util;
+
+import static org.apache.aries.jmx.util.FrameworkUtils.getBundleDependencies;
+import static
org.apache.aries.jmx.util.FrameworkUtils.getBundleExportedPackages;
+import static org.apache.aries.jmx.util.FrameworkUtils.getBundleIds;
+import static
org.apache.aries.jmx.util.FrameworkUtils.getBundleImportedPackages;
+import static org.apache.aries.jmx.util.FrameworkUtils.getRegisteredServiceIds;
+import static org.apache.aries.jmx.util.FrameworkUtils.getServiceIds;
+import static
org.apache.aries.jmx.util.FrameworkUtils.getServicesInUseByBundle;
+import static org.apache.aries.jmx.util.FrameworkUtils.isBundlePendingRemoval;
+import static
org.apache.aries.jmx.util.FrameworkUtils.isBundleRequiredByOthers;
+import static org.apache.aries.jmx.util.FrameworkUtils.resolveService;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.packageadmin.RequiredBundle;
+
+/**
+ *
+ *
+ *
+ * @version $Rev$ $Date$
+ */
+public class FrameworkUtilsTest {
+
+
+ @Test
+ public void testGetBundleIds() throws Exception {
+
+ assertEquals(0, getBundleIds(null).length);
+ assertEquals(0, getBundleIds(new Bundle[0]).length);
+
+ Bundle b1 = mock(Bundle.class);
+ when(b1.getBundleId()).thenReturn(new Long(47));
+ Bundle b2 = mock(Bundle.class);
+ when(b2.getBundleId()).thenReturn(new Long(23));
+
+ assertArrayEquals(new long[] { 47 , 23 }, getBundleIds(new Bundle[] {
b1, b2 }));
+
+ }
+
+ @Test
+ public void testResolveService() throws Exception {
+
+ BundleContext context = mock(BundleContext.class);
+ ServiceReference reference = mock(ServiceReference.class);
+ when(context.getAllServiceReferences(anyString(),
anyString())).thenReturn(new ServiceReference[] { reference });
+ ServiceReference result = resolveService(context, 998);
+ assertNotNull(result);
+
+ }
+
+ @Test
+ public void testGetServiceIds() throws Exception {
+
+ assertEquals(0, getServiceIds(null).length);
+ assertEquals(0, getServiceIds(new ServiceReference[0]).length);
+
+ ServiceReference s1 = mock(ServiceReference.class);
+ when(s1.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(15));
+ ServiceReference s2 = mock(ServiceReference.class);
+ when(s2.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(5));
+ ServiceReference s3 = mock(ServiceReference.class);
+ when(s3.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(25));
+
+ assertArrayEquals(new long[] { 15, 5, 25 },
+ getServiceIds(new ServiceReference[] {s1, s2, s3} ) );
+ }
+
+ @Test
+ public void testGetBundleExportedPackages() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+ PackageAdmin admin = mock(PackageAdmin.class);
+
+ assertEquals(0, getBundleExportedPackages(bundle, admin).length);
+
+ ExportedPackage exported = mock(ExportedPackage.class);
+ when(exported.getName()).thenReturn("org.apache.aries.jmx");
+ when(exported.getVersion()).thenReturn(new Version("1.0.0"));
+ when(admin.getExportedPackages(bundle)).thenReturn(new
ExportedPackage[] { exported });
+
+ assertArrayEquals(new String[] { "org.apache.aries.jmx;1.0.0"} ,
getBundleExportedPackages(bundle, admin));
+
+ }
+
+
+ @Test
+ public void testGetBundleImportedPackages() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+ BundleContext context = mock(BundleContext.class);
+ when(bundle.getBundleContext()).thenReturn(context);
+
+ Bundle b1 = mock(Bundle.class);
+ Bundle b2 = mock(Bundle.class);
+ Bundle b3 = mock(Bundle.class);
+ when(context.getBundles()).thenReturn(new Bundle[] { bundle, b1, b2,
b3 });
+
+ ExportedPackage ep1 = mock(ExportedPackage.class);
+ when(ep1.getImportingBundles()).thenReturn(new Bundle[] { bundle, b2,
b3 });
+ when(ep1.getName()).thenReturn("org.apache.aries.jmx.b1");
+ when(ep1.getVersion()).thenReturn(Version.emptyVersion);
+ ExportedPackage ep2 = mock(ExportedPackage.class);
+ when(ep2.getImportingBundles()).thenReturn(new Bundle[] { bundle, b3
});
+ when(ep2.getName()).thenReturn("org.apache.aries.jmx.b2");
+ when(ep2.getVersion()).thenReturn(Version.parseVersion("2.0.1"));
+
+
+ PackageAdmin admin = mock(PackageAdmin.class);
+ when(admin.getExportedPackages(b1)).thenReturn(new ExportedPackage[] {
ep1 });
+ when(admin.getExportedPackages(b2)).thenReturn(new ExportedPackage[] {
ep2 });
+ when(admin.getExportedPackages(b3)).thenReturn(null);
+
+ assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0" ,
"org.apache.aries.jmx.b2;2.0.1"}
+ , getBundleImportedPackages(bundle, admin));
+
+ }
+
+ @Test
+ public void testGetRegisteredServiceIds() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+
+ ServiceReference s1 = mock(ServiceReference.class);
+ when(s1.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(56));
+ ServiceReference s2 = mock(ServiceReference.class);
+ when(s2.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(5));
+ ServiceReference s3 = mock(ServiceReference.class);
+ when(s3.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(34));
+
+ when(bundle.getRegisteredServices()).thenReturn(new ServiceReference[]
{ s1, s2, s3 });
+
+ assertArrayEquals(new long[] { 56, 5, 34},
getRegisteredServiceIds(bundle));
+
+ }
+
+ @Test
+ public void testGetServicesInUseByBundle() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+
+ ServiceReference s1 = mock(ServiceReference.class);
+ when(s1.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(15));
+ ServiceReference s2 = mock(ServiceReference.class);
+ when(s2.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(16));
+ ServiceReference s3 = mock(ServiceReference.class);
+ when(s3.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(17));
+
+ when(bundle.getServicesInUse()).thenReturn(new ServiceReference[] {
s1, s2, s3 });
+
+ assertArrayEquals(new long[] { 15, 16, 17 },
getServicesInUseByBundle(bundle));
+
+ }
+
+ @Test
+ public void testIsBundlePendingRemoval() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+ when(bundle.getSymbolicName()).thenReturn("org.apache.testb");
+
+ RequiredBundle reqBundle = mock(RequiredBundle.class);
+ when(reqBundle.getBundle()).thenReturn(bundle);
+ when(reqBundle.isRemovalPending()).thenReturn(true);
+
+ PackageAdmin admin = mock(PackageAdmin.class);
+ when(admin.getRequiredBundles("org.apache.testb")).thenReturn(new
RequiredBundle[] { reqBundle });
+
+ assertTrue(isBundlePendingRemoval(bundle, admin));
+
+ }
+
+ @Test
+ public void testIsBundleRequiredByOthers() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+ when(bundle.getSymbolicName()).thenReturn("org.apache.testb");
+
+ RequiredBundle reqBundle = mock(RequiredBundle.class);
+ when(reqBundle.getBundle()).thenReturn(bundle);
+ when(reqBundle.getRequiringBundles()).thenReturn(new Bundle[0]);
+
+ PackageAdmin admin = mock(PackageAdmin.class);
+ when(admin.getRequiredBundles("org.apache.testb")).thenReturn(new
RequiredBundle[] { reqBundle });
+
+ assertFalse(isBundleRequiredByOthers(bundle, admin));
+
+ Bundle user = mock(Bundle.class);
+ when(reqBundle.getRequiringBundles()).thenReturn(new Bundle[] { user
});
+
+ assertTrue(isBundleRequiredByOthers(bundle, admin));
+ }
+
+
+ @Test
+ public void testGetBundleDependencies() throws Exception {
+
+ Bundle bundle = mock(Bundle.class);
+ BundleContext context = mock(BundleContext.class);
+ when(bundle.getBundleContext()).thenReturn(context);
+
+ Bundle b1 = mock(Bundle.class);
+ when(b1.getSymbolicName()).thenReturn("b1");
+ when(b1.getBundleId()).thenReturn(new Long(44));
+ Bundle b2 = mock(Bundle.class);
+ when(b2.getSymbolicName()).thenReturn("b2");
+ when(b2.getBundleId()).thenReturn(new Long(55));
+ Bundle b3 = mock(Bundle.class);
+ when(b3.getSymbolicName()).thenReturn("b3");
+ when(b3.getBundleId()).thenReturn(new Long(66));
+
+ when(context.getBundles()).thenReturn(new Bundle[] { bundle, b1, b2,
b3 });
+
+ PackageAdmin admin = mock(PackageAdmin.class);
+ assertEquals(0, getBundleDependencies(bundle, admin).length);
+
+ RequiredBundle rb1 = mock(RequiredBundle.class);
+ when(rb1.getBundle()).thenReturn(b1);
+ when(rb1.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b2
});
+ RequiredBundle rb2 = mock(RequiredBundle.class);
+ when(rb2.getBundle()).thenReturn(b2);
+ when(rb2.getRequiringBundles()).thenReturn(new Bundle[] { b1 });
+ RequiredBundle rb3 = mock(RequiredBundle.class);
+ when(rb3.getBundle()).thenReturn(b3);
+ when(rb3.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b1,
b2 });
+
+ when(admin.getRequiredBundles("b1")).thenReturn(new RequiredBundle[] {
rb1 });
+ when(admin.getRequiredBundles("b2")).thenReturn(new RequiredBundle[] {
rb2 });
+ when(admin.getRequiredBundles("b3")).thenReturn(new RequiredBundle[] {
rb3 });
+
+ assertArrayEquals(new long[] { 44, 66 }, getBundleDependencies(bundle,
admin));
+
+
+ }
+}
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/FrameworkUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/FrameworkUtilsTest.java
------------------------------------------------------------------------------
svn:keywords = Revision Date
Added:
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/TypeUtilsTest.java
URL:
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/TypeUtilsTest.java?rev=886705&view=auto
==============================================================================
---
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/TypeUtilsTest.java
(added)
+++
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/TypeUtilsTest.java
Thu Dec 3 08:45:49 2009
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.aries.jmx.util;
+
+import static org.apache.aries.jmx.util.TypeUtils.fromDictionary;
+import static org.apache.aries.jmx.util.TypeUtils.fromString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.junit.Test;
+
+public class TypeUtilsTest {
+
+
+ @Test
+ public void testMapFromDictionary() throws Exception{
+
+ Dictionary<String, String> dictionary = new Hashtable<String,
String>();
+ dictionary.put("one", "1");
+ dictionary.put("two", "2");
+
+ Map<String,String> map = fromDictionary(dictionary);
+ assertEquals(2, map.size());
+ assertEquals("1", map.get("one"));
+ assertEquals("2", map.get("two"));
+
+ }
+
+ @Test
+ public void testFromString() throws Exception {
+
+ String value;
+
+ value = "1";
+ Integer integerValue = fromString(Integer.class, value);
+ assertEquals(new Integer(1), integerValue);
+
+ int intValue = fromString(Integer.TYPE, value);
+ assertEquals(1, intValue);
+
+ Long wrappedLongValue = fromString(Long.class, value);
+ assertEquals(Long.valueOf(1), wrappedLongValue);
+
+ long longValue = fromString(Long.TYPE, value);
+ assertEquals(1, longValue);
+
+ Double wrappedDoubleValue = fromString(Double.class, value);
+ assertEquals(Double.valueOf(1), wrappedDoubleValue);
+
+ double doubleValue = fromString(Double.TYPE, value);
+ assertEquals(1, doubleValue, 0);
+
+ Float wrappedFloatValue = fromString(Float.class, value);
+ assertEquals(Float.valueOf(1), wrappedFloatValue);
+
+ float floatValue = fromString(Float.TYPE, value);
+ assertEquals(1, floatValue, 0);
+
+ Short shortValue = fromString(Short.class, value);
+ assertEquals(Short.valueOf(value), shortValue);
+
+ Byte byteValue = fromString(Byte.class, value);
+ assertEquals(Byte.valueOf(value), byteValue);
+
+ value = "true";
+ assertTrue(fromString(Boolean.class, value));
+ assertTrue(fromString(Boolean.TYPE, value));
+
+ char charValue = fromString(Character.TYPE, "a");
+ assertEquals('a', charValue);
+ Character characterValue = fromString(Character.class, "a");
+ assertEquals(Character.valueOf('a'), characterValue);
+
+ BigDecimal bigDecimal = fromString(BigDecimal.class, "2");
+ assertEquals(new BigDecimal("2"), bigDecimal);
+
+ BigInteger bigInteger = fromString(BigInteger.class, "2");
+ assertEquals(new BigInteger("2"), bigInteger);
+
+ String stringValue = fromString(String.class, value);
+ assertEquals(stringValue, value);
+
+ }
+}
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/TypeUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/util/TypeUtilsTest.java
------------------------------------------------------------------------------
svn:keywords = Revision Date