aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From z..@apache.org
Subject svn commit: r1075096 [6/13] - in /aries/tags/jmx-0.1-incubating: ./ jmx-api/ jmx-api/src/ jmx-api/src/main/ jmx-api/src/main/appended-resources/ jmx-api/src/main/appended-resources/META-INF/ jmx-api/src/main/java/ jmx-api/src/main/java/org/ jmx-api/src...
Date Sun, 27 Feb 2011 17:58:23 GMT
Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,280 @@
+/**
+ *  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.agent;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectName;
+import javax.management.StandardMBean;
+
+import org.apache.aries.jmx.JMXThreadFactory;
+import org.apache.aries.jmx.Logger;
+import org.apache.aries.jmx.MBeanHandler;
+import org.apache.aries.jmx.MBeanServiceTracker;
+import org.apache.aries.jmx.cm.ConfigurationAdminMBeanHandler;
+import org.apache.aries.jmx.framework.BundleStateMBeanHandler;
+import org.apache.aries.jmx.framework.FrameworkMBeanHandler;
+import org.apache.aries.jmx.framework.PackageStateMBeanHandler;
+import org.apache.aries.jmx.framework.ServiceStateMBeanHandler;
+import org.apache.aries.jmx.permissionadmin.PermissionAdminMBeanHandler;
+import org.apache.aries.jmx.provisioning.ProvisioningServiceMBeanHandler;
+import org.apache.aries.jmx.useradmin.UserAdminMBeanHandler;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * <p>
+ * Represent agent for MBeanServers registered in ServiceRegistry. Providing registration and unregistration methods.
+ * </p>
+ * 
+ * @see JMXAgent
+ * 
+ * @version $Rev: 931474 $ $Date: 2010-04-07 10:20:32 +0100 (Wed, 07 Apr 2010) $
+ */
+public class JMXAgentImpl implements JMXAgent {
+
+    private ServiceTracker mbeanServiceTracker;
+    /**
+     * {@link MBeanHandler} store.
+     */
+    private Set<MBeanHandler> mbeansHandlers;
+    private JMXAgentContext agentContext;
+    private Logger logger;
+
+    /**
+     * Registration {@link ExecutorService}.
+     */
+    private ExecutorService registrationExecutor;
+
+    /**
+     * Constructs new JMXAgent.
+     * 
+     * @param logger @see org.apache.aries.jmx.Logger
+     */
+    public JMXAgentImpl(Logger logger) {
+        this.logger = logger;
+        this.mbeansHandlers = new HashSet<MBeanHandler>();
+        this.registrationExecutor = Executors.newSingleThreadExecutor(new JMXThreadFactory("JMX OSGi Agent"));
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#start()
+     */
+    public void start() {
+        logger.log(LogService.LOG_INFO, "Starting JMX OSGi agent");
+        BundleContext bc = agentContext.getBundleContext();
+        MBeanHandler frameworkHandler = new FrameworkMBeanHandler(bc, logger);
+        frameworkHandler.open();
+        mbeansHandlers.add(frameworkHandler);
+        MBeanHandler bundleStateHandler = new BundleStateMBeanHandler(bc, logger);
+        bundleStateHandler.open();
+        mbeansHandlers.add(bundleStateHandler);
+        MBeanHandler serviceStateHandler = new ServiceStateMBeanHandler(bc, logger);
+        serviceStateHandler.open();
+        mbeansHandlers.add(serviceStateHandler);
+        MBeanHandler packageStateHandler = new PackageStateMBeanHandler(bc, logger);
+        packageStateHandler.open();
+        mbeansHandlers.add(packageStateHandler);
+        MBeanHandler permissionAdminHandler = new PermissionAdminMBeanHandler(agentContext);
+        permissionAdminHandler.open();
+        mbeansHandlers.add(permissionAdminHandler);
+        MBeanHandler userAdminHandler = new UserAdminMBeanHandler(agentContext);
+        userAdminHandler.open();
+        mbeansHandlers.add(userAdminHandler);
+        MBeanHandler configAdminHandler = new ConfigurationAdminMBeanHandler(agentContext);
+        configAdminHandler.open();
+        mbeansHandlers.add(configAdminHandler);
+        MBeanHandler provServiceHandler = new ProvisioningServiceMBeanHandler(agentContext);
+        provServiceHandler.open();
+        mbeansHandlers.add(provServiceHandler);
+        mbeanServiceTracker = new MBeanServiceTracker(agentContext);
+        mbeanServiceTracker.open();
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#registerMBeans(javax.management.MBeanServer)
+     */
+    public void registerMBeans(final MBeanServer server) {
+        for (MBeanHandler mbeanHandler : mbeansHandlers) {
+            String name = mbeanHandler.getName();
+            StandardMBean mbean = mbeanHandler.getMbean();
+            if (mbean != null) {
+                try {
+                    logger.log(LogService.LOG_INFO, "Registering " + mbean.getMBeanInterface().getName()
+                            + " to MBeanServer " + server + " with name " + name);
+                    server.registerMBean(mbean, new ObjectName(name));
+                } catch (InstanceAlreadyExistsException e) {
+                    logger.log(LogService.LOG_ERROR, "MBean is already registered", e);
+                } catch (MBeanRegistrationException e) {
+                    logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
+                } catch (NotCompliantMBeanException e) {
+                    logger.log(LogService.LOG_ERROR, "MBean is not compliant MBean", e);
+                } catch (MalformedObjectNameException e) {
+                    logger.log(LogService.LOG_ERROR, "Try to register with no valid objectname", e);
+                } catch (NullPointerException e) {
+                    logger.log(LogService.LOG_ERROR, "Name of objectname can't be null", e);
+                }
+            }
+        }
+
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBeans(javax.management.MBeanServer)
+     */
+    public void unregisterMBeans(final MBeanServer server) {
+        for (MBeanHandler mBeanHandler : mbeansHandlers) {
+            try
+            {
+               String name = mBeanHandler.getName();
+               StandardMBean mbean = mBeanHandler.getMbean();
+               if (mbean != null) {
+                   logger.log(LogService.LOG_INFO, "Unregistering " + mbean.getMBeanInterface().getName()
+                         + " to MBeanServer " + server + " with name " + name);
+                   server.unregisterMBean(new ObjectName(name));
+               }
+            } catch (MBeanRegistrationException e) {
+               logger.log(LogService.LOG_ERROR, "Can't unregister MBean", e);
+            } catch (InstanceNotFoundException e) {
+               logger.log(LogService.LOG_ERROR, "Mbena doesn't exist in the repository", e);
+            } catch (MalformedObjectNameException e) {
+               logger.log(LogService.LOG_ERROR, "Try to unregister with no valid objectname", e);
+            } catch (NullPointerException e) {
+               logger.log(LogService.LOG_ERROR, "Name of objectname can't be null ", e);
+            } catch (Exception e) {
+               logger.log(LogService.LOG_ERROR, "Cannot unregister MBean: " + mBeanHandler, e);
+            } 
+        }
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#registerMBean(org.apache.aries.jmx.MBeanHandler)
+     */
+    public void registerMBean(final MBeanHandler mBeanHandler) {
+        Object[] servers = getMBeanServers();
+        if (servers == null) {
+            logger.log(LogService.LOG_WARNING, "There are no MBean servers registred, can't register MBeans");
+            return;
+        }
+
+        for (Object server : servers) {
+            String name = mBeanHandler.getName();
+            StandardMBean mbean = mBeanHandler.getMbean();
+            try {
+                logger.log(LogService.LOG_INFO, "Registering " + mbean.getMBeanInterface().getName()
+                        + " to MBeanServer " + server + " with name " + name);
+                ((MBeanServer) server).registerMBean(mbean, new ObjectName(name));
+
+            } catch (InstanceAlreadyExistsException e) {
+                logger.log(LogService.LOG_ERROR, "MBean is already registered", e);
+            } catch (MBeanRegistrationException e) {
+                logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
+            } catch (NotCompliantMBeanException e) {
+                logger.log(LogService.LOG_ERROR, "MBean is not compliant MBean, Stopping registration", e);
+                return;
+            } catch (MalformedObjectNameException e) {
+                logger.log(LogService.LOG_ERROR, "Try to register with no valid objectname, Stopping registration", e);
+                return;
+            } catch (NullPointerException e) {
+                logger.log(LogService.LOG_ERROR, "Name of objectname can't be null, Stopping registration", e);
+                return;
+            }
+        }
+
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBean(java.lang.String)
+     */
+    public void unregisterMBean(final String name) {
+        Object[] servers = getMBeanServers();
+        for (Object server : servers) {
+
+            try {
+                logger.log(LogService.LOG_INFO, "Unregistering mbean " + " to MBeanServer " + server + " with name "
+                        + name);
+                ((MBeanServer) server).unregisterMBean(new ObjectName(name));
+            } catch (MBeanRegistrationException e) {
+                logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
+            } catch (InstanceNotFoundException e) {
+                logger.log(LogService.LOG_ERROR, "Mbena doesn't exist in the repository", e);
+            } catch (MalformedObjectNameException e) {
+                logger.log(LogService.LOG_ERROR, "Try to register with no valid objectname, Stopping registration", e);
+                return;
+            } catch (NullPointerException e) {
+                logger.log(LogService.LOG_ERROR, "Name of objectname can't be null, Stopping registration", e);
+                return;
+            }
+
+        }
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#stop()
+     */
+    public void stop() {
+        logger.log(LogService.LOG_INFO, "Stopping JMX OSGi agent");
+        mbeanServiceTracker.close();
+        for (MBeanHandler mBeanHandler : mbeansHandlers) {
+            mBeanHandler.close();
+        }
+        if (registrationExecutor != null && !registrationExecutor.isShutdown()) {
+            registrationExecutor.shutdown();
+        }
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#getAgentContext()
+     */
+    public JMXAgentContext getAgentContext() {
+        return agentContext;
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#setAgentContext(org.apache.aries.jmx.agent.JMXAgentContext)
+     */
+    public void setAgentContext(JMXAgentContext agentContext) {
+        this.agentContext = agentContext;
+    }
+
+    /**
+     * Gets all MBeanServers from MBeanServiceTracker.
+     * 
+     * @return array of MBean servers.
+     */
+    private Object[] getMBeanServers() {
+        return mbeanServiceTracker.getServices();
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#getRegistrationExecutor()
+     */
+    public ExecutorService getRegistrationExecutor() {
+        return registrationExecutor;
+    }
+
+}

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdmin.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdmin.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdmin.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdmin.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,236 @@
+/**
+ *  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.cm;
+
+import static org.osgi.jmx.JmxConstants.*;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+
+import org.apache.aries.jmx.codec.PropertyData;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.jmx.service.cm.ConfigurationAdminMBean;
+import org.osgi.service.cm.Configuration;
+
+/**
+ * Implementation of <code>ConfigurationAdminMBean</code> 
+ *
+ * @version $Rev: 922888 $ $Date: 2010-03-14 16:31:18 +0000 (Sun, 14 Mar 2010) $
+ */
+public class ConfigurationAdmin implements ConfigurationAdminMBean {
+
+    private org.osgi.service.cm.ConfigurationAdmin configurationAdmin;
+    
+    /**
+     * Constructs a ConfigurationAdmin implementation
+     * @param configurationAdmin instance of org.osgi.service.cm.ConfigurationAdmin service
+     */
+    public ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin configurationAdmin) {
+        this.configurationAdmin = configurationAdmin;
+    }
+    
+    
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#createFactoryConfiguration(java.lang.String)
+     */
+    public String createFactoryConfiguration(String factoryPid) throws IOException {
+        return createFactoryConfigurationForLocation(factoryPid, null); 
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#createFactoryConfigurationForLocation(java.lang.String, java.lang.String)
+     */
+    public String createFactoryConfigurationForLocation(String factoryPid, String location) throws IOException {
+        if (factoryPid == null || factoryPid.length() < 1) {
+            throw new IllegalArgumentException("Argument factoryPid cannot be null or empty");
+        }
+        Configuration config = configurationAdmin.createFactoryConfiguration(factoryPid, location);
+        return config.getPid();
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#delete(java.lang.String)
+     */
+    public void delete(String pid) throws IOException {
+       deleteForLocation(pid, null);
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#deleteForLocation(java.lang.String, java.lang.String)
+     */
+    public void deleteForLocation(String pid, String location) throws IOException {
+        if (pid == null || pid.length() < 1) {
+            throw new IllegalArgumentException("Argument pid cannot be null or empty");
+        }
+        Configuration config = configurationAdmin.getConfiguration(pid, location);
+        config.delete();
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#deleteConfigurations(java.lang.String)
+     */
+    public void deleteConfigurations(String filter) throws IOException {
+        if (filter == null || filter.length() < 1) {
+            throw new IllegalArgumentException("Argument filter cannot be null or empty");
+        }
+        Configuration[] configuations = null;
+        try {
+            configuations = configurationAdmin.listConfigurations(filter);
+        }
+        catch (InvalidSyntaxException e) {
+            throw new IllegalArgumentException("Invalid filter [" + filter + "] : " + e);
+        }
+        if (configuations != null) {
+            for (Configuration config : configuations) {
+                config.delete();
+            }
+        }
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#getBundleLocation(java.lang.String)
+     */
+    public String getBundleLocation(String pid) throws IOException {
+        if (pid == null || pid.length() < 1) {
+            throw new IllegalArgumentException("Argument pid cannot be null or empty");
+        }
+        Configuration config = configurationAdmin.getConfiguration(pid, null);
+        String bundleLocation = (config.getBundleLocation() == null) ? "Configuration is not yet bound to a bundle location" : config.getBundleLocation();
+        return bundleLocation;
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#getConfigurations(java.lang.String)
+     */
+    public String[][] getConfigurations(String filter) throws IOException {
+        if (filter == null || filter.length() < 1) {
+            throw new IllegalArgumentException("Argument filter cannot be null or empty");
+        }
+        List<String[]> result = new ArrayList<String[]>();
+        Configuration[] configurations = null;
+        try {
+            configurations = configurationAdmin.listConfigurations(filter);
+        }
+        catch (InvalidSyntaxException e) {
+            throw new IllegalArgumentException("Invalid filter [" + filter + "] : " + e);
+        }
+        if (configurations != null) {
+            for (Configuration config : configurations) {
+                result.add(new String[] { config.getPid(), config.getBundleLocation() });
+            }
+        }
+        return result.toArray(new String[result.size()][]);
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#getFactoryPid(java.lang.String)
+     */
+    public String getFactoryPid(String pid) throws IOException {
+       return getFactoryPidForLocation(pid, null);
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#getFactoryPidForLocation(java.lang.String, java.lang.String)
+     */
+    public String getFactoryPidForLocation(String pid, String location) throws IOException {
+        if (pid == null || pid.length() < 1) {
+            throw new IllegalArgumentException("Argument pid cannot be null or empty");
+        }
+        Configuration config = configurationAdmin.getConfiguration(pid, location);
+        return config.getFactoryPid();
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#getProperties(java.lang.String)
+     */
+    public TabularData getProperties(String pid) throws IOException {
+       return getPropertiesForLocation(pid, null);
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#getPropertiesForLocation(java.lang.String, java.lang.String)
+     */
+    @SuppressWarnings("unchecked")
+    public TabularData getPropertiesForLocation(String pid, String location) throws IOException {
+        if (pid == null || pid.length() < 1) {
+            throw new IllegalArgumentException("Argument pid cannot be null or empty");
+        }
+        TabularData propertiesTable = new TabularDataSupport(PROPERTIES_TYPE);
+        Configuration config = configurationAdmin.getConfiguration(pid, location);
+        Dictionary<String, Object> properties = config.getProperties();
+        if (properties != null) {
+            Enumeration<String> keys = properties.keys();
+            while (keys.hasMoreElements()) {
+                String key = keys.nextElement();
+                propertiesTable.put(PropertyData.newInstance(key, properties.get(key)).toCompositeData());
+            }
+        }
+        return propertiesTable;
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#setBundleLocation(java.lang.String, java.lang.String)
+     */
+    public void setBundleLocation(String pid, String location) throws IOException {
+        if (pid == null || pid.length() < 1) {
+            throw new IllegalArgumentException("Argument factoryPid cannot be null or empty");
+        }
+        Configuration config = configurationAdmin.getConfiguration(pid, null);
+        config.setBundleLocation(location);
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#update(java.lang.String, javax.management.openmbean.TabularData)
+     */
+    public void update(String pid, TabularData configurationTable) throws IOException {
+        updateForLocation(pid, null, configurationTable);
+    }
+
+    /**
+     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#updateForLocation(java.lang.String, java.lang.String, javax.management.openmbean.TabularData)
+     */
+    @SuppressWarnings("unchecked")
+    public void updateForLocation(String pid, String location, TabularData configurationTable) throws IOException {
+        if (pid == null || pid.length() < 1) {
+            throw new IllegalArgumentException("Argument pid cannot be null or empty");
+        }
+        if (configurationTable == null) {
+            throw new IllegalArgumentException("Argument properties cannot be null");
+        }
+        if (!PROPERTIES_TYPE.equals(configurationTable.getTabularType())) {
+            throw new IllegalArgumentException("Invalid TabularType ["  + configurationTable.getTabularType() + "]");
+        }
+        Dictionary<String, Object> configurationProperties = new Hashtable<String, Object>();
+        Collection<CompositeData> compositeData = (Collection<CompositeData>) configurationTable.values();
+        for (CompositeData row: compositeData) {
+            PropertyData<? extends Class> propertyData = PropertyData.from(row);
+            configurationProperties.put(propertyData.getKey(), propertyData.getValue());
+        }
+        Configuration config = configurationAdmin.getConfiguration(pid, location);
+        config.update(configurationProperties);
+    }
+
+}

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandler.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandler.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandler.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/cm/ConfigurationAdminMBeanHandler.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,74 @@
+/**
+ *  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.cm;
+
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+
+import org.apache.aries.jmx.AbstractCompendiumHandler;
+import org.apache.aries.jmx.Logger;
+import org.apache.aries.jmx.MBeanHandler;
+import org.apache.aries.jmx.agent.JMXAgentContext;
+import org.osgi.jmx.service.cm.ConfigurationAdminMBean;
+import org.osgi.service.log.LogService;
+
+/**
+ * <p>
+ * Implementation of <code>MBeanHandler</code> which manages the <code>ConfigurationAdminMBean</code> implementation
+ * 
+ * @see MBeanHandler </p>
+ * 
+ * @version $Rev: 897317 $ $Date: 2010-01-08 20:16:28 +0000 (Fri, 08 Jan 2010) $
+ */
+public class ConfigurationAdminMBeanHandler extends AbstractCompendiumHandler {
+
+    /**
+     * Constructs new ConfigurationAdminMBeanHandler instance
+     * 
+     * @param agentContext
+     *            JMXAgentContext instance
+     */
+    public ConfigurationAdminMBeanHandler(JMXAgentContext agentContext) {
+        super(agentContext, "org.osgi.service.cm.ConfigurationAdmin");
+    }
+
+    /**
+     * @see org.apache.aries.jmx.AbstractCompendiumHandler#constructInjectMBean(java.lang.Object)
+     */
+    @Override
+    protected StandardMBean constructInjectMBean(Object targetService) {
+        ConfigurationAdminMBean caMBean = new org.apache.aries.jmx.cm.ConfigurationAdmin(
+                (org.osgi.service.cm.ConfigurationAdmin) targetService);
+        StandardMBean mbean = null;
+        try {
+            mbean = new StandardMBean(caMBean, ConfigurationAdminMBean.class);
+        } catch (NotCompliantMBeanException e) {
+            Logger logger = agentContext.getLogger();
+            logger.log(LogService.LOG_ERROR, "Failed to instantiate MBean for "
+                    + ConfigurationAdminMBean.class.getName(), e);
+        }
+        return mbean;
+    }
+
+    /**
+     * @see org.apache.aries.jmx.MBeanHandler#getName()
+     */
+    public String getName() {
+        return ConfigurationAdminMBean.OBJECTNAME;
+    }
+
+}

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/AuthorizationData.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/AuthorizationData.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/AuthorizationData.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/AuthorizationData.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,114 @@
+/**
+ *  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.codec;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+
+import org.osgi.jmx.service.useradmin.UserAdminMBean;
+import org.osgi.service.useradmin.Authorization;
+
+/**
+ * <p>
+ * <tt>AuthorizationData</tt> represents Authorization Type @see {@link UserAdminMBean#AUTORIZATION_TYPE}.It is a codec
+ * for the <code>CompositeData</code> representing an Authorization .
+ * </p>
+ * 
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class AuthorizationData {
+    
+    /**
+     * authorization context name.
+     */
+    private String name;
+    /**
+     * roles implied by authorization context.
+     */
+    private String[] roles;
+    
+    /**
+     * Constructs new AuthorizationData from Authorization. 
+     * @param auth {@link Authorization} instance.
+     */
+    public AuthorizationData(Authorization auth){
+        this.name = auth.getName();
+        this.roles = auth.getRoles();
+    }
+    
+    /**
+     * Constructs new AuthorizationData.
+     * 
+     * @param name of authorization context.
+     * @param roles implied by authorization context.
+     */
+    public AuthorizationData(String name, String[] roles){
+        this.name = name;
+        this.roles = roles;
+    }
+    /**
+     * Translates AuthorizationData to CompositeData represented by
+     * compositeType {@link UserAdminMBean#AUTORIZATION_TYPE}.
+     * 
+     * @return translated AuthorizationData to compositeData.
+     */
+    public CompositeData toCompositeData() {
+        try {
+            Map<String, Object> items = new HashMap<String, Object>();
+            items.put(UserAdminMBean.NAME, name);
+            items.put(UserAdminMBean.ROLES, roles);
+            return new CompositeDataSupport(UserAdminMBean.AUTORIZATION_TYPE, items);
+        } catch (OpenDataException e) {
+            throw new IllegalStateException("Can't create CompositeData" + e);
+        }
+    }
+
+    /**
+     * Static factory method to create AuthorizationData from CompositeData object.
+     * 
+     * @param data {@link CompositeData} instance.
+     * @return AuthorizationData instance.
+     */
+    public static AuthorizationData from(CompositeData data) {
+        if(data == null){
+            return null;
+        }
+        String name = (String) data.get(UserAdminMBean.NAME);
+        String[] roles = (String[]) data.get(UserAdminMBean.ROLES);
+        return new AuthorizationData(name, roles);
+    }
+
+    /**
+     * @return the name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * @return the roles
+     */
+    public String[] getRoles() {
+        return roles;
+    }
+
+}
\ No newline at end of file

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchActionResult.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchActionResult.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchActionResult.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchActionResult.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,164 @@
+/**
+ *  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.codec;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+
+import org.osgi.jmx.framework.FrameworkMBean;
+
+/**
+ * <p>
+ * <tt>BatchInstallResult</tt> represents codec for resulting CompositeData of batch operations
+ * made on bundle via FrameworkMBean.
+ * It's converting batch install results to CompositeData {@link #toCompositeData()}
+ * and from CompositeData to this BatchActionResult {@link #from(CompositeData)}.
+ * It provides also constructors to build BatchActionResult.  
+ * Structure of compositeData is as defined in compositeType @see {@link FrameworkMBean#BATCH_ACTION_RESULT_TYPE}.
+ * </p>
+ * @see BatchResult
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class BatchActionResult extends BatchResult{
+
+    /**
+     * @see FrameworkMBean#REMAINING_ID_ITEM
+     * @see FrameworkMBean#REMAINING_LOCATION_ITEM
+     */
+	private long[] remainingItems;
+	/**
+	 * @see FrameworkMBean#BUNDLE_IN_ERROR_ID_ITEM
+	 * @see FrameworkMBean#BUNDLE_IN_ERROR
+	 */
+	private long bundleInError;
+
+	/**
+	 * Constructs new BatchActionResult with completedItems array.
+     * Newly created object represents successful batch result.
+     * @param completedItems containing the list of bundles completing the batch operation.
+	 */
+	public BatchActionResult(long[] completedItems){
+		this.completed = completedItems;
+		success = true;
+	}
+	
+	 /**
+     * Constructs new BatchActionResult with error message.
+     * Newly created object represents failed batch result.
+     * @param error containing the error message of the batch operation.
+     */
+    public BatchActionResult(String error){
+        this.error = error;
+        success = false;
+    }
+	
+	/**
+	 * Constructs new BatchActionResult.
+     * Newly created object represents failed batch result.
+     * 
+     * @param completedItems containing the list of bundles completing the batch operation.
+     * @param error containing the error message of the batch operation.
+     * @param remainingItems remaining bundles unprocessed by the
+     * failing batch operation.
+     * @param bundleInError containing the bundle which caused the error during the batch
+     * operation.
+	 */
+	public BatchActionResult(long[] completedItems, String error, long[] remainingItems, long bundleInError){
+		this(completedItems,error,remainingItems,false,bundleInError);
+	}
+	
+	/**
+	 * Constructs new BatchActionResult.
+     * 
+     * @param completedItems containing the list of bundles completing the batch operation.
+     * @param error containing the error message of the batch operation.
+     * @param remainingItems remaining bundles unprocessed by the
+     * failing batch operation.
+     * @param success indicates if this operation was successful.
+     * @param bundleInError containing the bundle which caused the error during the batch
+     * operation.
+	 */
+	public BatchActionResult(long[] completedItems, String error, long[] remainingItems, boolean success, long bundleInError){
+		this.bundleInError = bundleInError;
+		this.completed = completedItems;
+		this.error = error;
+		this.remainingItems = remainingItems;
+		this.success = success;
+	}
+	
+	/**
+	 * Translates BatchActionResult to CompositeData represented by
+     * compositeType {@link FrameworkMBean#BATCH_ACTION_RESULT_TYPE}.
+     * 
+	 * @return translated BatchActionResult  to compositeData.
+	 */
+	public CompositeData toCompositeData(){
+		try {
+			Map<String, Object> items = new HashMap<String, Object>();
+			items.put(FrameworkMBean.BUNDLE_IN_ERROR, bundleInError);
+			items.put(FrameworkMBean.COMPLETED, toLongArray(completed));
+			items.put(FrameworkMBean.ERROR, error);
+			items.put(FrameworkMBean.REMAINING, toLongArray(remainingItems));
+			items.put(FrameworkMBean.SUCCESS, success);
+			return new CompositeDataSupport(FrameworkMBean.BATCH_ACTION_RESULT_TYPE, items);
+		} catch (OpenDataException e) {
+			throw new IllegalStateException("Can't create CompositeData" + e);
+		}
+	}
+	
+	/**
+	 * Static factory method to create BatchActionResult from CompositeData object.
+	 * 
+	 * @param data {@link CompositeData} instance.
+	 * @return BatchActionResult instance.
+	 */
+	public static BatchActionResult from(CompositeData data){
+	    if(data == null){
+	        return null;
+	    }
+		long bundleInError = (Long) data.get(FrameworkMBean.BUNDLE_IN_ERROR);
+		// need to convert primitive array to wrapper type array
+		// compositeData accept only wrapper type array
+		long[] completedItems = toLongPrimitiveArray((Long[])data.get(FrameworkMBean.COMPLETED));
+		long[] remainingItems = toLongPrimitiveArray((Long[]) data.get(FrameworkMBean.REMAINING));
+		String error = (String) data.get(FrameworkMBean.ERROR);
+		Boolean success = (Boolean) data.get(FrameworkMBean.SUCCESS);
+		return new BatchActionResult(completedItems, error, remainingItems, success, bundleInError);
+	}
+
+	/**
+	 * Gets remaining items id's.
+	 * @return the remainingItems.
+	 */
+	public long[] getRemainingItems() {
+		return remainingItems;
+	}
+
+	/**
+	 * Gets bundle in error id.
+	 * @return the bundleInError.
+	 */
+	public long getBundleInError() {
+		return bundleInError;
+	}
+	
+}

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchInstallResult.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchInstallResult.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchInstallResult.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchInstallResult.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,163 @@
+/**
+ *  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.codec;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+
+import org.osgi.jmx.framework.FrameworkMBean;
+
+/**
+ * <p>
+ * <tt>BatchInstallResult</tt> represents codec for resulting CompositeData of
+ * FrameworkMBean installBundles methods.
+ * It converting batch install results to CompositeData {@link #toCompositeData()}
+ * and from CompositeData to this BatchInstallResult {@link #from(CompositeData)}.
+ * It provides also constructors to build BatchInstallResult.  
+ * Structure of compositeData as defined in compositeType @see {@link FrameworkMBean#BATCH_INSTALL_RESULT_TYPE}.
+ * </p>
+ * @see BatchResult
+ *
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class BatchInstallResult extends BatchResult {
+
+    /**
+     * @see FrameworkMBean#REMAINING_LOCATION_ITEM
+     * @see FrameworkMBean#REMAINING
+     */
+    private String[] remainingLocationItems;
+    
+    /**
+     * @see FrameworkMBean#BUNDLE_IN_ERROR_LOCATION_ITEM
+     * @see FrameworkMBean#BUNDLE_IN_ERROR
+     */
+    private String bundleInError;
+
+    /**
+     * Constructs new BatchInstallResult with completedItems array.
+     * Newly created object represents successful batch result.
+     * @param completedItems containing the list of bundles completing the batch operation.
+     */
+    public BatchInstallResult(long[] completedItems) {
+        this.completed = completedItems;
+        success = true;
+    }
+
+    /**
+     * Constructs new BatchInstallResult with error message.
+     * Newly created object represents failed batch result.
+     * @param error containing the error message of the batch operation.
+     */
+    public BatchInstallResult(String error){
+        this.error = error;
+        success = false;
+    }
+    /**
+     * Constructs new BatchInstallResult.
+     * Newly created object represents failed batch result.
+     * 
+     * @param completedItems containing the list of bundles completing the batch operation.
+     * @param error containing the error message of the batch operation.
+     * @param remainingLocationItems remaining bundles unprocessed by the
+     * failing batch operation.
+     * @param bundleInError containing the bundle which caused the error during the batch
+     * operation.
+     */
+    public BatchInstallResult(long[] completedItems, String error, String[] remainingLocationItems, String bundleInError) {
+        this(completedItems, error, remainingLocationItems, false, bundleInError);
+    }
+
+    /**
+     * Constructs new BatchInstallResult.
+     * 
+     * @param completedItems containing the list of bundles completing the batch operation.
+     * @param error containing the error message of the batch operation.
+     * @param remainingLocationItems remaining bundles unprocessed by the
+     * failing batch operation.
+     * @param success indicates if this operation was successful.
+     * @param bundleInError containing the bundle which caused the error during the batch
+     * operation.
+     */
+    public BatchInstallResult(long[] completedItems, String error, String[] remainingLocationItems, boolean success,
+            String bundleInError) {
+        this.bundleInError = bundleInError;
+        this.completed = completedItems;
+        this.error = error;
+        this.remainingLocationItems = remainingLocationItems;
+        this.success = success;
+    }
+
+    /**
+     * Translates BatchInstallResult to CompositeData represented by
+     * compositeType {@link FrameworkMBean#BATCH_INSTALL_RESULT_TYPE}.
+     * 
+     * @return translated BatchInstallResult to compositeData.
+     */
+    public CompositeData toCompositeData() {
+        try {
+            Map<String, Object> items = new HashMap<String, Object>();
+            items.put(FrameworkMBean.BUNDLE_IN_ERROR, bundleInError);
+            items.put(FrameworkMBean.COMPLETED, toLongArray(completed));
+            items.put(FrameworkMBean.ERROR, error);
+            items.put(FrameworkMBean.REMAINING, remainingLocationItems);
+            items.put(FrameworkMBean.SUCCESS, success);
+            return new CompositeDataSupport(FrameworkMBean.BATCH_INSTALL_RESULT_TYPE, items);
+        } catch (OpenDataException e) {
+            throw new IllegalStateException("Can't create CompositeData" + e);
+        }
+    }
+
+    /**
+     * Static factory method to create BatchInstallResult from CompositeData object.
+     * 
+     * @param data {@link CompositeData} instance.
+     * @return BatchInstallResult instance.
+     */
+    public static BatchInstallResult from(CompositeData data) {
+        if(data == null){
+            return null;
+        }
+        String bundleInError = (String) data.get(FrameworkMBean.BUNDLE_IN_ERROR);
+        long[] completedItems = toLongPrimitiveArray((Long[]) data.get(FrameworkMBean.COMPLETED));
+        String[] remainingLocationItems = (String[]) data.get(FrameworkMBean.REMAINING);
+        String error = (String) data.get(FrameworkMBean.ERROR);
+        boolean success = (Boolean) data.get(FrameworkMBean.SUCCESS);
+        return new BatchInstallResult(completedItems, error, remainingLocationItems, success, bundleInError);
+    }
+
+    /**
+     * Gets remaining location items.
+     * @return array of String with locations.
+     */
+    public String[] getRemainingLocationItems() {
+        return remainingLocationItems;
+    }
+
+    /**
+     * Gets bundle in error location.
+     * @return the bundleInError.
+     */
+    public String getBundleInError() {
+        return bundleInError;
+    }
+
+}

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchResult.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchResult.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchResult.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchResult.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,113 @@
+/**
+ *  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.codec;
+
+import org.osgi.jmx.framework.FrameworkMBean;
+
+/**
+ * <p>
+ * <tt>BatchResult</tt> represents abstract class for BatchResults.
+ * It contains common data structure of batch result:
+ * <ul>
+ * <li>completed containing the list of bundles completing the batch operation.</li>
+ * <li>error containing the error message of the batch operation.</li>
+ * <li>success indicates if this operation was successful.</li>
+ * </ul>
+ * </p>
+ * 
+ * 
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public abstract class BatchResult {
+
+    /**
+     * @see FrameworkMBean#COMPLETED_ITEM
+     * @see FrameworkMBean#COMPLETED
+     */
+    protected long[] completed;
+    /**
+     * @see FrameworkMBean#ERROR_ITEM
+     * @see FrameworkMBean#ERROR
+     */
+    protected String error;
+    /**
+     * @see FrameworkMBean#SUCCESS_ITEM
+     * @see FrameworkMBean#SUCCESS
+     */
+    protected boolean success;
+
+    /**
+     * Gets completed item id's.
+     * @return completed items id's.
+     */
+    public long[] getCompleted() {
+        return completed;
+    }
+
+    /**
+     * Gets error message.
+     * @return error message.
+     */
+    public String getError() {
+        return error;
+    }
+
+    /**
+     * Gets success value.
+     * @return true if success false if not.
+     */
+    public boolean isSuccess() {
+        return success;
+    }
+    
+    /**
+     * Converts primitive array of strings to Long array.
+     * 
+     * @param primitiveArray primitive long array.
+     * @return Long array.
+     */
+    protected Long[] toLongArray(long[] primitiveArray) {
+        if (primitiveArray == null) {
+            return null;
+        }
+        Long[] converted = new Long[primitiveArray.length];
+        for (int i = 0; i < primitiveArray.length; i++) {
+            converted[i] = primitiveArray[i];
+        }
+
+        return converted;
+    }
+
+    /**
+     * Converts Long array to primitive array of long.
+     * 
+     * @param wrapperArray Long array.
+     * @return primitive long array.
+     */
+    protected static long[] toLongPrimitiveArray(Long[] wrapperArray) {
+        if (wrapperArray == null) {
+            return null;
+        }
+        long[] converted = new long[wrapperArray.length];
+        for (int i = 0; i < wrapperArray.length; i++) {
+            converted[i] = wrapperArray[i];
+        }
+
+        return converted;
+    }
+
+}
\ No newline at end of file

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleData.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleData.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleData.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleData.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,445 @@
+/**
+ *  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.codec;
+
+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.getBundleImportedPackages;
+import static org.apache.aries.jmx.util.FrameworkUtils.getBundleState;
+import static org.apache.aries.jmx.util.FrameworkUtils.getDependentBundles;
+import static org.apache.aries.jmx.util.FrameworkUtils.getFragmentIds;
+import static org.apache.aries.jmx.util.FrameworkUtils.getHostIds;
+import static org.apache.aries.jmx.util.FrameworkUtils.getRegisteredServiceIds;
+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.TypeUtils.toLong;
+import static org.apache.aries.jmx.util.TypeUtils.toPrimitive;
+import static org.osgi.jmx.framework.BundleStateMBean.BUNDLE_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.EXPORTED_PACKAGES;
+import static org.osgi.jmx.framework.BundleStateMBean.FRAGMENT;
+import static org.osgi.jmx.framework.BundleStateMBean.FRAGMENTS;
+import static org.osgi.jmx.framework.BundleStateMBean.HEADERS;
+import static org.osgi.jmx.framework.BundleStateMBean.HEADERS_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.HEADER_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.HOSTS;
+import static org.osgi.jmx.framework.BundleStateMBean.IDENTIFIER;
+import static org.osgi.jmx.framework.BundleStateMBean.IMPORTED_PACKAGES;
+import static org.osgi.jmx.framework.BundleStateMBean.KEY;
+import static org.osgi.jmx.framework.BundleStateMBean.LAST_MODIFIED;
+import static org.osgi.jmx.framework.BundleStateMBean.LOCATION;
+import static org.osgi.jmx.framework.BundleStateMBean.PERSISTENTLY_STARTED;
+import static org.osgi.jmx.framework.BundleStateMBean.REGISTERED_SERVICES;
+import static org.osgi.jmx.framework.BundleStateMBean.REMOVAL_PENDING;
+import static org.osgi.jmx.framework.BundleStateMBean.REQUIRED;
+import static org.osgi.jmx.framework.BundleStateMBean.REQUIRED_BUNDLES;
+import static org.osgi.jmx.framework.BundleStateMBean.REQUIRING_BUNDLES;
+import static org.osgi.jmx.framework.BundleStateMBean.SERVICES_IN_USE;
+import static org.osgi.jmx.framework.BundleStateMBean.START_LEVEL;
+import static org.osgi.jmx.framework.BundleStateMBean.STATE;
+import static org.osgi.jmx.framework.BundleStateMBean.SYMBOLIC_NAME;
+import static org.osgi.jmx.framework.BundleStateMBean.VALUE;
+import static org.osgi.jmx.framework.BundleStateMBean.VERSION;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.management.JMRuntimeException;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.jmx.framework.BundleStateMBean;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.startlevel.StartLevel;
+
+/**
+ * <p>
+ * <tt>BundleData</tt> represents BundleData Type @see {@link BundleStateMBean#BUNDLE_TYPE}. It is a codec for the
+ * <code>CompositeData</code> representing an OSGi BundleData.
+ * </p>
+ * 
+ * @version $Rev: 897313 $ $Date: 2010-01-08 20:07:12 +0000 (Fri, 08 Jan 2010) $
+ */
+public class BundleData {
+
+    /**
+     * @see BundleStateMBean#EXPORTED_PACKAGES_ITEM
+     */
+    private String[] exportedPackages;
+
+    /**
+     * @see BundleStateMBean#FRAGMENT_ITEM
+     */
+    private boolean fragment;
+
+    /**
+     * @see BundleStateMBean#FRAGMENTS_ITEM
+     */
+    private long[] fragments;
+
+    /**
+     * @see BundleStateMBean#HEADER_TYPE
+     */
+    private List<Header> headers = new ArrayList<Header>();
+
+    /**
+     * @see BundleStateMBean#HOSTS_ITEM
+     */
+    private long[] hosts;
+
+    /**
+     * @see BundleStateMBean#IDENTIFIER_ITEM
+     */
+    private long identifier;
+
+    /**
+     * @see BundleStateMBean#IMPORTED_PACKAGES_ITEM
+     */
+    private String[] importedPackages;
+
+    /**
+     * @see BundleStateMBean#LAST_MODIFIED_ITEM
+     */
+    private long lastModified;
+
+    /**
+     * @see BundleStateMBean#LOCATION_ITEM
+     */
+    private String location;
+
+    /**
+     * @see BundleStateMBean#PERSISTENTLY_STARTED_ITEM
+     */
+    private boolean persistentlyStarted;
+
+    /**
+     * @see BundleStateMBean#REGISTERED_SERVICES_ITEM
+     */
+    private long[] registeredServices;
+
+    /**
+     * @see BundleStateMBean#REMOVAL_PENDING_ITEM
+     */
+    private boolean removalPending;
+
+    /**
+     * @see BundleStateMBean#REQUIRED_ITEM
+     */
+    private boolean required;
+
+    /**
+     * @see BundleStateMBean#REQUIRED_BUNDLES_ITEM
+     */
+    private long[] requiredBundles;
+
+    /**
+     * @see BundleStateMBean#REQUIRING_BUNDLES_ITEM
+     */
+    private long[] requiringBundles;
+
+    /**
+     * @see BundleStateMBean#SERVICES_IN_USE_ITEM
+     */
+    private long[] servicesInUse;
+
+    /**
+     * @see BundleStateMBean#START_LEVEL_ITEM
+     */
+    private int bundleStartLevel;
+
+    /**
+     * @see BundleStateMBean#STATE_ITEM
+     */
+    private String state;
+
+    /**
+     * @see BundleStateMBean#SYMBOLIC_NAME_ITEM
+     */
+    private String symbolicName;
+
+    /**
+     * @see BundleStateMBean#VERSION_ITEM
+     */
+    private String version;
+
+    private BundleData() {
+        super();
+    }
+
+    @SuppressWarnings("unchecked")
+    public BundleData(BundleContext localBundleContext, Bundle bundle, PackageAdmin packageAdmin, StartLevel startLevel) {
+        if (bundle == null) {
+            throw new IllegalArgumentException("Argument bundle cannot be null");
+        }
+        if (packageAdmin == null || startLevel == null) {
+            throw new IllegalArgumentException("Arguments PackageAdmin / startLevel cannot be null");
+        }
+        this.exportedPackages = getBundleExportedPackages(bundle, packageAdmin);
+        this.fragment = (PackageAdmin.BUNDLE_TYPE_FRAGMENT == packageAdmin.getBundleType(bundle));
+        this.fragments = getFragmentIds(bundle, packageAdmin);
+        Dictionary<String, String> bundleHeaders = bundle.getHeaders();
+        Enumeration<String> keys = bundleHeaders.keys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            headers.add(new Header(key, bundleHeaders.get(key)));
+        }
+        this.hosts = getHostIds(bundle, packageAdmin);
+        this.identifier = bundle.getBundleId();
+        this.importedPackages = getBundleImportedPackages(localBundleContext, bundle, packageAdmin);
+        this.lastModified = bundle.getLastModified();
+        this.location = bundle.getLocation();
+        this.persistentlyStarted = startLevel.isBundlePersistentlyStarted(bundle);
+        this.registeredServices = getRegisteredServiceIds(bundle);
+        this.removalPending = isBundlePendingRemoval(bundle, packageAdmin);
+        this.required = isBundleRequiredByOthers(bundle, packageAdmin);
+        this.requiredBundles = getBundleDependencies(localBundleContext, bundle, packageAdmin);
+        this.requiringBundles = getDependentBundles(bundle, packageAdmin);
+        this.servicesInUse = getServicesInUseByBundle(bundle);
+        this.bundleStartLevel = startLevel.getBundleStartLevel(bundle);
+        this.state = getBundleState(bundle);
+        this.symbolicName = bundle.getSymbolicName();
+        this.version = bundle.getVersion().toString();
+    }
+
+    /**
+     * Returns CompositeData representing a BundleData complete state typed by {@link BundleStateMBean#BUNDLE_TYPE}
+     * 
+     * @return
+     */
+    public CompositeData toCompositeData() {
+        CompositeData result = null;
+        Map<String, Object> items = new HashMap<String, Object>();
+        items.put(EXPORTED_PACKAGES, this.exportedPackages);
+        items.put(FRAGMENT, this.fragment);
+        items.put(FRAGMENTS, toLong(this.fragments));
+        items.put(HOSTS, toLong(this.hosts));
+        items.put(IDENTIFIER, this.identifier);
+        items.put(IMPORTED_PACKAGES, this.importedPackages);
+        items.put(LAST_MODIFIED, this.lastModified);
+        items.put(LOCATION, this.location);
+        items.put(PERSISTENTLY_STARTED, this.persistentlyStarted);
+        items.put(REGISTERED_SERVICES, toLong(this.registeredServices));
+        items.put(REMOVAL_PENDING, this.removalPending);
+        items.put(REQUIRED, this.required);
+        items.put(REQUIRED_BUNDLES, toLong(this.requiredBundles));
+        items.put(REQUIRING_BUNDLES, toLong(this.requiringBundles));
+        items.put(SERVICES_IN_USE, toLong(this.servicesInUse));
+        items.put(START_LEVEL, this.bundleStartLevel);
+        items.put(STATE, this.state);
+        items.put(SYMBOLIC_NAME, this.symbolicName);
+        items.put(VERSION, this.version);
+        TabularData headerTable = new TabularDataSupport(HEADERS_TYPE);
+        for (Header header : this.headers) {
+            headerTable.put(header.toCompositeData());
+        }
+        items.put(HEADERS, headerTable);
+        try {
+            result = new CompositeDataSupport(BUNDLE_TYPE, items);
+        } catch (OpenDataException e) {
+            throw new IllegalStateException("Failed to create CompositeData for BundleData [" + this.identifier
+                    + "]", e);
+        }
+        return result;
+    }
+
+    /**
+     * Constructs a <code>BundleData</code> object from the given <code>CompositeData</code>
+     * 
+     * @param compositeData
+     * @return
+     * @throws IlleglArgumentException
+     *             if compositeData is null or not of type {@link BundleStateMBean#BUNDLE_TYPE}
+     */
+    @SuppressWarnings("unchecked")
+    public static BundleData from(CompositeData compositeData) throws IllegalArgumentException {
+        if (compositeData == null) {
+            throw new IllegalArgumentException("Argument compositeData cannot be null");
+        }
+        if (!compositeData.getCompositeType().equals(BUNDLE_TYPE)) {
+            throw new IllegalArgumentException("Invalid CompositeType [" + compositeData.getCompositeType() + "]");
+        }
+        BundleData bundleData = new BundleData();
+        bundleData.exportedPackages = (String[]) compositeData.get(EXPORTED_PACKAGES);
+        bundleData.fragment = (Boolean) compositeData.get(FRAGMENT);
+        bundleData.fragments = toPrimitive((Long[]) compositeData.get(FRAGMENTS));
+        bundleData.hosts = toPrimitive((Long[]) compositeData.get(HOSTS));
+        bundleData.identifier = (Long) compositeData.get(IDENTIFIER);
+        bundleData.importedPackages = (String[]) compositeData.get(IMPORTED_PACKAGES);
+        bundleData.lastModified = (Long) compositeData.get(LAST_MODIFIED);
+        bundleData.location = (String) compositeData.get(LOCATION);
+        bundleData.persistentlyStarted = (Boolean) compositeData.get(PERSISTENTLY_STARTED);
+        bundleData.registeredServices = toPrimitive((Long[]) compositeData.get(REGISTERED_SERVICES));
+        bundleData.removalPending = (Boolean) compositeData.get(REMOVAL_PENDING);
+        bundleData.required = (Boolean) compositeData.get(REQUIRED);
+        bundleData.requiredBundles = toPrimitive((Long[]) compositeData.get(REQUIRED_BUNDLES));
+        bundleData.requiringBundles = toPrimitive((Long[]) compositeData.get(REQUIRING_BUNDLES));
+        bundleData.servicesInUse = toPrimitive((Long[]) compositeData.get(SERVICES_IN_USE));
+        bundleData.bundleStartLevel = (Integer) compositeData.get(START_LEVEL);
+        bundleData.state = (String) compositeData.get(STATE);
+        bundleData.symbolicName = (String) compositeData.get(SYMBOLIC_NAME);
+        bundleData.version = (String) compositeData.get(VERSION);
+        TabularData headerTable = (TabularData) compositeData.get(HEADERS);
+        Collection<CompositeData> headerData = (Collection<CompositeData>) headerTable.values();
+        for (CompositeData headerRow : headerData) {
+            bundleData.headers.add(Header.from(headerRow));
+        }
+        return bundleData;
+    }
+
+    public String[] getExportedPackages() {
+        return exportedPackages;
+    }
+
+    public boolean isFragment() {
+        return fragment;
+    }
+
+    public long[] getFragments() {
+        return fragments;
+    }
+
+    public List<Header> getHeaders() {
+        return headers;
+    }
+
+    public long[] getHosts() {
+        return hosts;
+    }
+
+    public long getIdentifier() {
+        return identifier;
+    }
+
+    public String[] getImportedPackages() {
+        return importedPackages;
+    }
+
+    public long getLastModified() {
+        return lastModified;
+    }
+
+    public String getLocation() {
+        return location;
+    }
+
+    public boolean isPersistentlyStarted() {
+        return persistentlyStarted;
+    }
+
+    public long[] getRegisteredServices() {
+        return registeredServices;
+    }
+
+    public boolean isRemovalPending() {
+        return removalPending;
+    }
+
+    public boolean isRequired() {
+        return required;
+    }
+
+    public long[] getRequiredBundles() {
+        return requiredBundles;
+    }
+
+    public long[] getRequiringBundles() {
+        return requiringBundles;
+    }
+
+    public long[] getServicesInUse() {
+        return servicesInUse;
+    }
+
+    public int getBundleStartLevel() {
+        return bundleStartLevel;
+    }
+
+    public String getState() {
+        return state;
+    }
+
+    public String getSymbolicName() {
+        return symbolicName;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    /*
+     * Represents key/value pair in BundleData headers
+     */
+    public static class Header {
+
+        private String key;
+        private String value;
+
+        public String getKey() {
+            return key;
+        }
+
+        public String getValue() {
+            return value;
+        }
+
+        private Header() {
+            super();
+        }
+
+        public Header(String key, String value) {
+            this.key = key;
+            this.value = value;
+        }
+
+        public CompositeData toCompositeData() throws JMRuntimeException {
+            CompositeData result = null;
+            Map<String, Object> items = new HashMap<String, Object>();
+            items.put(KEY, key);
+            items.put(VALUE, value);
+            try {
+                result = new CompositeDataSupport(HEADER_TYPE, items);
+            } catch (OpenDataException e) {
+                throw new JMRuntimeException("Failed to create CompositeData for header [" + key + ":" + value + "] - "
+                        + e.getMessage());
+            }
+            return result;
+        }
+
+        public static Header from(CompositeData compositeData) {
+            if (compositeData == null) {
+                throw new IllegalArgumentException("Argument compositeData cannot be null");
+            }
+            if (!compositeData.getCompositeType().equals(HEADER_TYPE)) {
+                throw new IllegalArgumentException("Invalid CompositeType [" + compositeData.getCompositeType() + "]");
+            }
+            Header header = new Header();
+            header.key = (String) compositeData.get(KEY);
+            header.value = (String) compositeData.get(VALUE);
+            return header;
+        }
+    }
+}

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleEventData.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleEventData.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleEventData.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/BundleEventData.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,137 @@
+/**
+ *  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.codec;
+
+import static org.osgi.jmx.framework.BundleStateMBean.BUNDLE_EVENT_TYPE;
+import static org.osgi.jmx.framework.BundleStateMBean.EVENT;
+import static org.osgi.jmx.framework.BundleStateMBean.IDENTIFIER;
+import static org.osgi.jmx.framework.BundleStateMBean.LOCATION;
+import static org.osgi.jmx.framework.BundleStateMBean.SYMBOLIC_NAME;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.jmx.framework.BundleStateMBean;
+
+/**
+ * <p>
+ * <tt>BundleEventData</tt> represents BundleEvent Type @see {@link BundleStateMBean#BUNDLE_EVENT_TYPE}. It is a codec
+ * for the <code>CompositeData</code> representing an OSGi BundleEvent.
+ * </p>
+ * 
+ * @version $Rev: 896239 $ $Date: 2010-01-05 22:02:23 +0000 (Tue, 05 Jan 2010) $
+ */
+public class BundleEventData {
+
+    /**
+     * @see BundleStateMBean#IDENTIFIER_ITEM
+     */
+    private long bundleId;
+
+    /**
+     * @see BundleStateMBean#LOCATION_ITEM
+     */
+    private String location;
+
+    /**
+     * @see BundleStateMBean#SYMBOLIC_NAME_ITEM
+     */
+    private String bundleSymbolicName;
+
+    /**
+     * @see BundleStateMBean#EVENT_ITEM
+     */
+    private int eventType;
+
+    private BundleEventData() {
+        super();
+    }
+
+    public BundleEventData(BundleEvent bundleEvent) {
+        this.eventType = bundleEvent.getType();
+        Bundle bundle = bundleEvent.getBundle();
+        this.bundleId = bundle.getBundleId();
+        this.location = bundle.getLocation();
+        this.bundleSymbolicName = bundle.getSymbolicName();
+    }
+
+    /**
+     * Returns CompositeData representing a BundleEvent typed by {@link BundleStateMBean#BUNDLE_EVENT_TYPE}
+     * 
+     * @return
+     */
+    public CompositeData toCompositeData() {
+        CompositeData result = null;
+        Map<String, Object> items = new HashMap<String, Object>();
+        items.put(IDENTIFIER, this.bundleId);
+        items.put(SYMBOLIC_NAME, this.bundleSymbolicName);
+        items.put(LOCATION, this.location);
+        items.put(EVENT, this.eventType);
+        try {
+            result = new CompositeDataSupport(BUNDLE_EVENT_TYPE, items);
+        } catch (OpenDataException e) {
+            throw new IllegalStateException("Failed to create CompositeData for BundleEvent for Bundle ["
+                    + this.bundleId + "]", e);
+        }
+        return result;
+    }
+
+    /**
+     * Returns a <code>BundleEventData</code> representation of the given compositeData
+     * 
+     * @param compositeData
+     * @return
+     * @throws IllegalArgumentException
+     *             if the compositeData is null or incorrect type
+     */
+    public static BundleEventData from(CompositeData compositeData) throws IllegalArgumentException {
+        BundleEventData eventData = new BundleEventData();
+        if (compositeData == null) {
+            throw new IllegalArgumentException("Argument compositeData cannot be null");
+        }
+        if (!compositeData.getCompositeType().equals(BUNDLE_EVENT_TYPE)) {
+            throw new IllegalArgumentException("Invalid CompositeType [" + compositeData.getCompositeType() + "]");
+        }
+        eventData.bundleId = (Long) compositeData.get(IDENTIFIER);
+        eventData.bundleSymbolicName = (String) compositeData.get(SYMBOLIC_NAME);
+        eventData.eventType = (Integer) compositeData.get(EVENT);
+        eventData.location = (String) compositeData.get(LOCATION);
+        return eventData;
+    }
+
+    public long getBundleId() {
+        return bundleId;
+    }
+
+    public String getLocation() {
+        return location;
+    }
+
+    public String getBundleSymbolicName() {
+        return bundleSymbolicName;
+    }
+
+    public int getEventType() {
+        return eventType;
+    }
+}

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/GroupData.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/GroupData.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/GroupData.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/GroupData.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,156 @@
+/**
+ *  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.codec;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+
+import org.osgi.jmx.service.useradmin.UserAdminMBean;
+import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
+
+/**
+ * <p>
+ * <tt>GroupData</tt> represents Group Type @see {@link UserAdminMBean#GROUP_TYPE}.It is a codec
+ * for the <code>CompositeData</code> representing a Group.
+ * </p>
+ * </p>
+ * 
+ * @version $Rev: 920467 $ $Date: 2010-03-08 19:22:22 +0000 (Mon, 08 Mar 2010) $
+ */
+public class GroupData extends UserData {
+
+    /**
+     * @see UserAdminMBean#MEMBERS_ITEM
+     * @see UserAdminMBean#MEMBERS
+     */
+    private String[] members;
+    /**
+     * @see UserAdminMBean#REQUIRED_MEMBERS
+     * @see UserAdminMBean#REQUIRED_MEMBERS_ITEM
+     */
+    private String[] requiredMembers;
+
+    /**
+     * Constructs new GroupData from Group object.
+     * @param group {@link Group} instance.
+     */
+    public GroupData(Group group) {
+        super(group.getName(), Role.GROUP);
+        setRequiredMembers(group);
+        setMembers(group);
+    }
+
+    /**
+     * Constructs new GroupData.
+     * 
+     * @param name group name.
+     * @param members basic members.
+     * @param requiredMembers required members.
+     */
+    public GroupData(String name, String[] members, String[] requiredMembers) {
+        super(name, Role.GROUP);
+        this.members = members;
+        this.requiredMembers = requiredMembers;
+    }
+
+    /**
+     * Translates GroupData to CompositeData represented by compositeType {@link UserAdminMBean#GROUP_TYPE}.
+     * 
+     * @return translated GroupData to compositeData.
+     */
+    public CompositeData toCompositeData() {
+        try {
+            Map<String, Object> items = new HashMap<String, Object>();
+            items.put(UserAdminMBean.NAME, name);
+            items.put(UserAdminMBean.TYPE, type);
+            items.put(UserAdminMBean.MEMBERS, members);
+            items.put(UserAdminMBean.REQUIRED_MEMBERS, requiredMembers);
+            return new CompositeDataSupport(UserAdminMBean.GROUP_TYPE, items);
+        } catch (OpenDataException e) {
+            throw new IllegalStateException("Can't create CompositeData" + e);
+        }
+    }
+
+    /**
+     * Static factory method to create GroupData from CompositeData object.
+     * 
+     * @param data
+     *            {@link CompositeData} instance.
+     * @return GroupData instance.
+     */
+    public static GroupData from(CompositeData data) {
+        if (data == null) {
+            return null;
+        }
+        String name = (String) data.get(UserAdminMBean.NAME);
+        String[] members = (String[]) data.get(UserAdminMBean.MEMBERS);
+        String[] requiredMembers = (String[]) data.get(UserAdminMBean.REQUIRED_MEMBERS);
+        return new GroupData(name, members, requiredMembers);
+    }
+
+    /**
+     * @return the members
+     */
+    public String[] getMembers() {
+        return members;
+    }
+
+    /**
+     * @return the requiredMembers
+     */
+    public String[] getRequiredMembers() {
+        return requiredMembers;
+    }
+
+    /**
+     * Sets group basic members by getting them from Group object.
+     * @param group {@link Group} instance.
+     */
+    private void setMembers(org.osgi.service.useradmin.Group group) {
+        Role[] roles = group.getMembers();
+        if (roles != null) {
+            List<String> members = new ArrayList<String>();
+            for (Role role : roles) {
+                members.add(role.getName());
+            }
+            this.members = members.toArray(new String[roles.length]);
+        }
+    }
+
+    /**
+     * Sets group required members by getting them from Group object.
+     * @param group {@link Group} instance.
+     */
+    private void setRequiredMembers(org.osgi.service.useradmin.Group group) {
+        Role[] requiredRoles = group.getRequiredMembers();
+        if (requiredRoles != null) {
+            List<String> reqMembers = new ArrayList<String>();
+            for (Role role : requiredRoles) {
+                reqMembers.add(role.getName());
+            }
+            this.requiredMembers = reqMembers.toArray(new String[requiredRoles.length]);
+        }
+    }
+
+}
\ No newline at end of file

Added: aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/PackageData.java
URL: http://svn.apache.org/viewvc/aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/PackageData.java?rev=1075096&view=auto
==============================================================================
--- aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/PackageData.java (added)
+++ aries/tags/jmx-0.1-incubating/jmx-core/src/main/java/org/apache/aries/jmx/codec/PackageData.java Sun Feb 27 17:58:16 2011
@@ -0,0 +1,258 @@
+/**
+ *  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.codec;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+
+import org.osgi.framework.Bundle;
+import org.osgi.jmx.framework.PackageStateMBean;
+import org.osgi.service.packageadmin.ExportedPackage;
+
+/**
+ * <p>
+ * <tt>PackageData</tt>represents PackageType @see {@link PackageStateMBean#PACKAGE_TYPE}.
+ * It is a codec for the composite data representing an OSGi ExportedPackage.
+ * </p>
+ * 
+ * @version $Rev: 919575 $ $Date: 2010-03-05 20:00:30 +0000 (Fri, 05 Mar 2010) $
+ */
+public class PackageData {
+
+    /**
+     * {@link PackageStateMBean#EXPORTING_BUNDLES}
+     */
+    long[] exportingBundles;
+
+    /**
+     * {@link PackageStateMBean#IMPORTING_BUNDLES}
+     */
+    long[] importingBundles;
+
+    /**
+     * {@link PackageStateMBean#NAME}
+     */
+    String name;
+
+    /**
+     * {@link PackageStateMBean#REMOVAL_PENDING}
+     */
+    boolean removalPending;
+
+    /**
+     * {@link PackageStateMBean#VERSION}
+     */
+    String version;
+
+    /**
+     * Constructs new PackageData with provided ExportedPackage.
+     * @param exportedPackage @see {@link ExportedPackage}.
+     */
+    public PackageData(ExportedPackage exportedPackage) {
+        this(new long[]{exportedPackage.getExportingBundle().getBundleId()}, toBundleIds(exportedPackage.getImportingBundles()),
+                exportedPackage.getName(), exportedPackage.isRemovalPending(), exportedPackage.getVersion().toString());
+
+    }
+
+    /**
+     * Constructs new PackageData.
+     * 
+     * @param exportingBundles the bundle the package belongs to.
+     * @param importingBundles the importing bundles of the package.
+     * @param name the package name.
+     * @param removalPending whether the package is pending removal.
+     * @param version package version.
+     */
+    public PackageData(long[] exportingBundles, long[] importingBundles, String name, boolean removalPending, String version) {
+        this.exportingBundles = exportingBundles;
+        this.importingBundles = importingBundles;
+        this.name = name;
+        this.removalPending = removalPending;
+        this.version = version;
+    }
+    
+    /**
+     * Translates PackageData to CompositeData represented by
+     * compositeType {@link PackageStateMBean#PACKAGE_TYPE}.
+     * 
+     * @return translated PackageData to compositeData.
+     */
+    public CompositeData toCompositeData() {
+        try {
+            Map<String, Object> items = new HashMap<String, Object>();
+            items.put(PackageStateMBean.EXPORTING_BUNDLES, toLongArray(exportingBundles));
+            items.put(PackageStateMBean.IMPORTING_BUNDLES, toLongArray(importingBundles));
+            items.put(PackageStateMBean.NAME, name);
+            items.put(PackageStateMBean.REMOVAL_PENDING, removalPending);
+            items.put(PackageStateMBean.VERSION, version);
+            return new CompositeDataSupport(PackageStateMBean.PACKAGE_TYPE, items);
+        } catch (OpenDataException e) {
+            throw new IllegalStateException("Can't create CompositeData" + e);
+        }
+    }
+
+    /**
+     * Static factory method to create PackageData from CompositeData object.
+     * 
+     * @param data {@link CompositeData} instance.
+     * @return PackageData instance.
+     */
+    public static PackageData from(CompositeData data) {
+        if(data == null){
+            return null;
+        }
+        long[] exportingBundle = toLongPrimitiveArray((Long[])data.get(PackageStateMBean.EXPORTING_BUNDLES));
+        long[] importingBundles = toLongPrimitiveArray((Long[]) data.get(PackageStateMBean.IMPORTING_BUNDLES));
+        String name = (String) data.get(PackageStateMBean.NAME);
+        boolean removalPending = (Boolean) data.get(PackageStateMBean.REMOVAL_PENDING);
+        String version = (String) data.get(PackageStateMBean.VERSION);
+        return new PackageData(exportingBundle,importingBundles,name, removalPending,version);
+    }
+
+    /**
+     * Creates {@link TabularData} for set of PackageData's.
+     * 
+     * @param packages set of PackageData's
+     * @return {@link TabularData} instance.
+     */
+    public static TabularData tableFrom(Set<PackageData> packages){
+        TabularData table = new TabularDataSupport(PackageStateMBean.PACKAGES_TYPE);
+        for(PackageData pkg : packages){
+            table.put(pkg.toCompositeData());
+        }
+        return table;
+    }
+
+    /**
+     * Converts array of bundles to array of bundle id's.
+     * 
+     * @param bundles array of Bundle's.
+     * @return array of bundle id's.
+     */
+    public static long[] toBundleIds(Bundle[] bundles) {
+        if (bundles != null) {
+            long[] importingBundles = new long[bundles.length];
+            for (int i = 0; i < bundles.length; i++) {
+                importingBundles[i] = bundles[i].getBundleId();
+            }
+            return importingBundles;
+        }
+        return null;
+    }
+    
+    /**
+     * Converts primitive array of strings to Long array.
+     * 
+     * @param primitiveArray primitive long array.
+     * @return Long array.
+     */
+    protected Long[] toLongArray(long[] primitiveArray) {
+        if (primitiveArray == null) {
+            return null;
+        }
+        Long[] converted = new Long[primitiveArray.length];
+        for (int i = 0; i < primitiveArray.length; i++) {
+            converted[i] = primitiveArray[i];
+        }
+
+        return converted;
+    }
+
+    /**
+     * Converts Long array to primitive array of long.
+     * 
+     * @param wrapperArray Long array.
+     * @return primitive long array.
+     */
+    protected static long[] toLongPrimitiveArray(Long[] wrapperArray) {
+        if (wrapperArray == null) {
+            return null;
+        }
+        long[] converted = new long[wrapperArray.length];
+        for (int i = 0; i < wrapperArray.length; i++) {
+            converted[i] = wrapperArray[i];
+        }
+
+        return converted;
+    }
+
+    /**
+     * @return the exportingBundles
+     */
+    public long[] getExportingBundles() {
+        return exportingBundles;
+    }
+
+    /**
+     * @return the importingBundles
+     */
+    public long[] getImportingBundles() {
+        return importingBundles;
+    }
+
+    /**
+     * @return the name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * @return the removalPending
+     */
+    public boolean isRemovalPending() {
+        return removalPending;
+    }
+
+    /**
+     * @return the version
+     */
+    public String getVersion() {
+        return version;
+    }
+    
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        PackageData that = (PackageData) o;
+
+        // exportingBundle must be always there
+        if (exportingBundles[0] != that.exportingBundles[0]) return false;
+        if (!name.equals(that.name)) return false;
+        if (!version.equals(that.version)) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = (int) (exportingBundles[0] ^ (exportingBundles[0] >>> 32));
+        result = 31 * result + name.hashCode();
+        result = 31 * result + version.hashCode();
+        return result;
+    }
+
+}



Mime
View raw message