aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1221787 - in /aries/trunk/sandbox/jmx-next: jmx-api/src/main/java/org/osgi/jmx/framework/ jmx-core/src/main/java/org/apache/aries/jmx/codec/ jmx-core/src/main/java/org/apache/aries/jmx/framework/ jmx-itests/src/test/java/org/apache/aries/j...
Date Wed, 21 Dec 2011 16:25:54 GMT
Author: davidb
Date: Wed Dec 21 16:25:54 2011
New Revision: 1221787

URL: http://svn.apache.org/viewvc?rev=1221787&view=rev
Log:
Add support for FrameworkMBean.refreshBundlesAndWait()

Added:
    aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchRefreshResult.java
  (with props)
Modified:
    aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/FrameworkMBean.java
    aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
    aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/FrameworkMBeanTest.java

Modified: aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/FrameworkMBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/FrameworkMBean.java?rev=1221787&r1=1221786&r2=1221787&view=diff
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/FrameworkMBean.java
(original)
+++ aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/FrameworkMBean.java
Wed Dec 21 16:25:54 2011
@@ -151,7 +151,7 @@ public interface FrameworkMBean {
 																	COMPLETED_ITEM,
 																	ERROR_ITEM,
 																	REMAINING_ID_ITEM,
-																	SUCCESS_ITEM //
+																	SUCCESS_ITEM
 															);
 
 	/**
@@ -201,9 +201,26 @@ public interface FrameworkMBean {
 																	COMPLETED_ITEM,
 																	ERROR_ITEM,
 																	REMAINING_LOCATION_ITEM,
-																	SUCCESS_ITEM //
+																	SUCCESS_ITEM
 															);
 
+    /**
+     * The Composite Type which represents the result of a batch resolve
+     * operation. It is used in {@link #refreshBundlesAndWait(String[])}.
+     *
+     * This Composite Type consists of the following items:
+     * <ul>
+     * <li>{@link #COMPLETED_ITEM}</li>
+     * <li>{@link #SUCCESS_ITEM}</li>
+     * </ul>
+     */
+    CompositeType   BATCH_RESOLVE_RESULT_TYPE       = Item
+                                                            .compositeType(
+                                                                    "BATCH_RESOLVE_RESULT",
+                                                                    "This type encapsulates
a bundle batch resolve action result",
+                                                                    COMPLETED_ITEM,
+                                                                    SUCCESS_ITEM);
+
 	/**
 	 * Retrieve the framework start level
 	 *

Added: aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchRefreshResult.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchRefreshResult.java?rev=1221787&view=auto
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchRefreshResult.java
(added)
+++ aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchRefreshResult.java
Wed Dec 21 16:25:54 2011
@@ -0,0 +1,47 @@
+/**
+ *  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;
+
+public class BatchRefreshResult extends BatchResult {
+    private final boolean success;
+    private final Long[] successfulBundles;
+
+    public BatchRefreshResult(boolean success, Long[] successfulBundles) {
+        this.success = success;
+        this.successfulBundles = successfulBundles;
+    }
+
+    public CompositeData toCompositeData() {
+        try {
+            Map<String, Object> items = new HashMap<String, Object>();
+            items.put(FrameworkMBean.COMPLETED, successfulBundles);
+            items.put(FrameworkMBean.SUCCESS, success);
+            return new CompositeDataSupport(FrameworkMBean.BATCH_RESOLVE_RESULT_TYPE, items);
+        } catch (OpenDataException e) {
+            throw new IllegalStateException("Can't create CompositeData", e);
+        }
+    }
+}

Propchange: aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/BatchRefreshResult.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java?rev=1221787&r1=1221786&r2=1221787&view=diff
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
(original)
+++ aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/framework/Framework.java
Wed Dec 21 16:25:54 2011
@@ -19,6 +19,8 @@ package org.apache.aries.jmx.framework;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -26,6 +28,7 @@ import javax.management.openmbean.Compos
 
 import org.apache.aries.jmx.codec.BatchActionResult;
 import org.apache.aries.jmx.codec.BatchInstallResult;
+import org.apache.aries.jmx.codec.BatchRefreshResult;
 import org.apache.aries.jmx.util.FrameworkUtils;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -224,9 +227,45 @@ public class Framework implements Framew
        packageAdmin.refreshPackages(bundles);
     }
 
+    /**
+     * @see org.osgi.jmx.framework.FrameworkMBean#refreshBundlesAndWait(long[])
+     */
     public CompositeData refreshBundlesAndWait(long[] bundleIdentifiers) throws IOException
{
-        // TODO Auto-generated method stub
-        return null;
+        final CountDownLatch latch = new CountDownLatch(1);
+        FrameworkListener listener = new FrameworkListener() {
+            public void frameworkEvent(FrameworkEvent event) {
+                if (FrameworkEvent.PACKAGES_REFRESHED == event.getType()) {
+                    latch.countDown();
+                }
+            }
+        };
+        try {
+            context.addFrameworkListener(listener);
+            try {
+                Bundle [] bundles = new Bundle[bundleIdentifiers.length];
+                for (int i=0; i < bundleIdentifiers.length; i++) {
+                    bundles[i] = FrameworkUtils.resolveBundle(context, bundleIdentifiers[i]);
+                }
+                packageAdmin.refreshPackages(bundles);
+                boolean result = latch.await(30, TimeUnit.SECONDS);
+
+                List<Long> successList = new ArrayList<Long>();
+                for (Bundle bundle : bundles) {
+                    int state = bundle.getState();
+                    if ((state & (Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE))
> 0) {
+                        successList.add(bundle.getBundleId());
+                    }
+                }
+
+                return new BatchRefreshResult(result, successList.toArray(new Long[] {})).toCompositeData();
+            } catch (InterruptedException e) {
+                IOException ex = new IOException();
+                ex.initCause(e);
+                throw ex;
+            }
+        } finally {
+            context.removeFrameworkListener(listener);
+        }
     }
 
     /**

Modified: aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/FrameworkMBeanTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/FrameworkMBeanTest.java?rev=1221787&r1=1221786&r2=1221787&view=diff
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/FrameworkMBeanTest.java
(original)
+++ aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/FrameworkMBeanTest.java
Wed Dec 21 16:25:54 2011
@@ -69,8 +69,8 @@ public class FrameworkMBeanTest extends 
     @Configuration
     public static Option[] configuration() {
         return testOptions(
-//                 new VMOption( "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
),
-//                 new TimeoutOption( 0 ),
+            //  new VMOption( "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
),
+            //  new TimeoutOption( 0 ),
 
             PaxRunnerOptions.rawPaxRunnerOption("config", "classpath:ss-runner.properties"),
             CoreOptions.equinox().version("3.7.0.v20110613"),
@@ -103,11 +103,10 @@ public class FrameworkMBeanTest extends 
     }
 
     @Test
-    public void testRefresh() throws Exception {
+    public void testRefreshBundleAndWait() throws Exception {
         Bundle bundleA = context().getBundleByName("org.apache.aries.jmx.test.bundlea");
         Bundle bundleB = context().getBundleByName("org.apache.aries.jmx.test.bundleb");
 
-        BundleRevision br = bundleB.adapt(BundleRevision.class);
         BundleWiring bw = bundleB.adapt(BundleWiring.class);
 
         List<BundleWire> initialRequiredWires = bw.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
@@ -157,6 +156,62 @@ public class FrameworkMBeanTest extends 
         assertEquals(Arrays.asList("org.apache.aries.jmx.test.bundlea.api", "org.apache.aries.jmx.test.bundlea.impl"),
imported);
     }
 
+    @Test
+    public void testRefreshBundlesAndWait() throws Exception {
+        Bundle bundleA = context().getBundleByName("org.apache.aries.jmx.test.bundlea");
+        Bundle bundleB = context().getBundleByName("org.apache.aries.jmx.test.bundleb");
+
+        BundleWiring bw = bundleB.adapt(BundleWiring.class);
+
+        List<BundleWire> initialRequiredWires = bw.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
+        assertEquals(1, initialRequiredWires.size());
+        BundleWire wire = initialRequiredWires.get(0);
+        Map<String, Object> capabilityAttributes = wire.getCapability().getAttributes();
+        assertEquals("Precondition", bundleA.getSymbolicName(), capabilityAttributes.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
+        assertEquals("Precondition", new Version("1.0"), capabilityAttributes.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
+        assertEquals("Precondition", "org.apache.aries.jmx.test.bundlea.api", capabilityAttributes.get(BundleRevision.PACKAGE_NAMESPACE));
+
+        // Create an updated version of Bundle A, which an extra export and version 1.1
+        Manifest manifest = new Manifest();
+        manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
+        manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.aries.jmx.test.bundlea");
+        manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.1");
+        manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, "org.apache.aries.jmx.test.bundlea.api,org.apache.aries.jmx.test.bundlea.impl");
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        JarOutputStream jos = new JarOutputStream(baos, manifest);
+        addResourceToJar("org/apache/aries/jmx/test/bundlea/api/InterfaceA.class", jos, bundleA);
+        addResourceToJar("org/apache/aries/jmx/test/bundlea/impl/A2.class", jos, bundleA);
+        jos.close();
+
+        assertEquals("Precondition", 1, bundleA.adapt(BundleRevisions.class).getRevisions().size());
+        bundleA.update(new ByteArrayInputStream(baos.toByteArray()));
+        assertEquals("There should be 2 revisions now", 2, bundleA.adapt(BundleRevisions.class).getRevisions().size());
+        assertEquals("No refresh called, the bundle wiring for B should still be the old
one",
+                bw, bundleB.adapt(BundleWiring.class));
+
+        FrameworkMBean framework = getMBean(FrameworkMBean.OBJECTNAME, FrameworkMBean.class);
+        CompositeData result = framework.refreshBundlesAndWait(new long[] {bundleB.getBundleId()});
+        assertTrue((Boolean) result.get(FrameworkMBean.SUCCESS));
+        assertTrue(Arrays.equals(new Long[] {bundleB.getBundleId()}, (Long []) result.get(FrameworkMBean.COMPLETED)));
+
+        List<BundleWire> requiredWires = bundleB.adapt(BundleWiring.class).getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
+        assertEquals(2, requiredWires.size());
+        List<String> imported = new ArrayList<String>();
+        for (BundleWire w : requiredWires) {
+            Map<String, Object> ca = w.getCapability().getAttributes();
+            assertEquals(bundleA.getSymbolicName(), ca.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
+            imported.add(ca.get(BundleRevision.PACKAGE_NAMESPACE).toString());
+
+            if ("org.apache.aries.jmx.test.bundlea.impl".equals(ca.get(BundleRevision.PACKAGE_NAMESPACE)))
{
+                // Came across an issue where equinox was reporting the other package as
still coming from from the 1.0 bundle
+                // not sure if this is a bug or not...
+                assertEquals(new Version("1.1"), ca.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
+            }
+        }
+        assertEquals(Arrays.asList("org.apache.aries.jmx.test.bundlea.api", "org.apache.aries.jmx.test.bundlea.impl"),
imported);
+    }
+
     private void addResourceToJar(String resourceName, JarOutputStream jos, Bundle bundle)
throws IOException {
         InputStream intfIs = bundle.getResource("/" + resourceName).openStream();
         JarEntry entry = new JarEntry(resourceName);



Mime
View raw message