Author: thomas.diesler(a)jboss.com
Date: 2010-03-16 19:44:55 -0400 (Tue, 16 Mar 2010)
New Revision: 102491
Added:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeTest.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java
Removed:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java
Modified:
projects/jboss-osgi/projects/spi/trunk/pom.xml
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java
Log:
Prepare for SPI/Testing integration
Modified: projects/jboss-osgi/projects/spi/trunk/pom.xml
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/pom.xml 2010-03-16 23:12:02 UTC (rev 102490)
+++ projects/jboss-osgi/projects/spi/trunk/pom.xml 2010-03-16 23:44:55 UTC (rev 102491)
@@ -29,17 +29,12 @@
<version>1.0.5-SNAPSHOT</version>
</parent>
- <!-- Subversion -->
- <scm>
-
<
connection>scm:svn:http://anonsvn.jboss.org/repos/jbossas/projects/jbo...
-
<
developerConnection>scm:svn:https://svn.jboss.org/repos/jbossas/projec...
- <
url>http://fisheye.jboss.com/qsearch/JBossOSGi</url>
- </scm>
-
<!-- Properties -->
<properties>
<version.args4j>2.0.12</version.args4j>
+ <version.jboss.osgi.jmx>1.0.4-SNAPSHOT</version.jboss.osgi.jmx>
<version.jboss.osgi.vfs>1.0.0-SNAPSHOT</version.jboss.osgi.vfs>
+ <version.jboss.shrinkwrap>1.0.0-SNAPSHOT</version.jboss.shrinkwrap>
<version.osgi>4.2.0</version.osgi>
</properties>
@@ -57,10 +52,24 @@
<artifactId>jboss-logging-spi</artifactId>
</dependency>
<dependency>
+ <groupId>org.jboss.osgi.jmx</groupId>
+ <artifactId>jboss-osgi-jmx-api</artifactId>
+ <version>${version.jboss.osgi.jmx}</version>
+ </dependency>
+ <dependency>
<groupId>org.jboss.osgi.vfs</groupId>
<artifactId>jboss-osgi-vfs</artifactId>
<version>${version.jboss.osgi.vfs}</version>
</dependency>
+ <dependency>
+ <groupId>org.jboss.shrinkwrap</groupId>
+ <artifactId>shrinkwrap-impl-base</artifactId>
+ <version>${version.jboss.shrinkwrap}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
<!-- Provided Dependencies -->
<dependency>
@@ -128,4 +137,12 @@
</snapshots>
</repository>
</repositories>
+
+ <!-- Subversion -->
+ <scm>
+
<
connection>scm:svn:http://anonsvn.jboss.org/repos/jbossas/projects/jbo...
+
<
developerConnection>scm:svn:https://svn.jboss.org/repos/jbossas/projec...
+ <
url>http://fisheye.jboss.com/qsearch/JBossOSGi</url>
+ </scm>
+
</project>
Copied: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing (from
rev 102159,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing)
Modified:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java
===================================================================
---
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java 2010-03-09
19:47:32 UTC (rev 102159)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -99,7 +99,7 @@
/**
* Returns a URL to the entry at the specified path in this bundle.
*
- * @param name The path name of the entry
+ * @param path The path name of the entry
* @return A URL to the entry, or null if no entry could be found
*/
public abstract URL getEntry(String path);
Copied:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java
(from rev 102342,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java)
===================================================================
---
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java
(rev 0)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeHelper.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.osgi.testing;
+
+import org.jboss.osgi.spi.framework.OSGiBootstrap;
+import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
+import org.jboss.osgi.testing.internal.EmbeddedRuntime;
+import org.jboss.osgi.testing.internal.RemoteRuntime;
+
+/**
+ * A helper for the OSGi runtime abstraction.
+ *
+ * @author Thomas.Diesler(a)jboss.org
+ * @since 25-Sep-2008
+ */
+public class OSGiRuntimeHelper extends OSGiTestHelper
+{
+ // The OSGiBootstrapProvider is a lazy property of the helper
+ private OSGiBootstrapProvider bootProvider;
+ private boolean skipBootstrap;
+
+ public OSGiBootstrapProvider getBootstrapProvider()
+ {
+ if (bootProvider == null && skipBootstrap == false)
+ {
+ try
+ {
+ bootProvider = OSGiBootstrap.getBootstrapProvider();
+ }
+ catch (RuntimeException rte)
+ {
+ skipBootstrap = true;
+ throw rte;
+ }
+ }
+ return bootProvider;
+ }
+
+ public void ungetBootstrapProvider()
+ {
+ bootProvider = null;
+ }
+
+ public OSGiRuntime getDefaultRuntime()
+ {
+ OSGiRuntime runtime;
+
+ String target = System.getProperty("target.container");
+ if (target == null)
+ {
+ runtime = getEmbeddedRuntime();
+ }
+ else
+ {
+ runtime = getRemoteRuntime();
+ }
+ return runtime;
+ }
+
+ public OSGiRuntime getEmbeddedRuntime()
+ {
+ return new EmbeddedRuntime(this);
+ }
+
+ public OSGiRuntime getRemoteRuntime()
+ {
+ return new RemoteRuntime(this);
+ }
+}
Copied:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeTest.java
(from rev 102342,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeTest.java)
===================================================================
---
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeTest.java
(rev 0)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiRuntimeTest.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -0,0 +1,73 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.osgi.testing;
+
+/**
+ * An abstract OSGi runtime test.
+ *
+ * A convenience wrapper for the functionality provided by {@link OSGiRuntimeHelper}.
+ *
+ * @author Thomas.Diesler(a)jboss.org
+ * @since 25-Sep-2008
+ */
+public abstract class OSGiRuntimeTest extends OSGiTest
+{
+ private OSGiRuntimeHelper helper;
+
+ /**
+ * Get the test helper used by this test
+ *
+ * Overwrite if you need to supply another helper
+ * i.e. one that you have statically setup
+ */
+ protected OSGiRuntimeHelper getRuntimeHelper()
+ {
+ if (helper == null)
+ helper = new OSGiRuntimeHelper();
+
+ return helper;
+ }
+
+ /**
+ * Delegates to {@link OSGiRuntimeHelper#getDefaultRuntime()}
+ */
+ protected OSGiRuntime getDefaultRuntime()
+ {
+ return getRuntimeHelper().getDefaultRuntime();
+ }
+
+ /**
+ * Delegates to {@link OSGiRuntimeHelper#getEmbeddedRuntime()}
+ */
+ protected OSGiRuntime getEmbeddedRuntime()
+ {
+ return getRuntimeHelper().getEmbeddedRuntime();
+ }
+
+ /**
+ * Delegates to {@link OSGiRuntimeHelper#getRemoteRuntime()}
+ */
+ public OSGiRuntime getRemoteRuntime()
+ {
+ return getRuntimeHelper().getRemoteRuntime();
+ }
+}
Deleted:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java
===================================================================
---
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java 2010-03-09
19:47:32 UTC (rev 102159)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -1,221 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.osgi.testing;
-
-import java.io.File;
-import java.net.URL;
-
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.jboss.logging.Logger;
-import org.jboss.osgi.vfs.VirtualFile;
-import org.junit.After;
-import org.junit.Before;
-import org.osgi.framework.Bundle;
-
-/**
- * An abstract OSGi Test.
- *
- * {@link OSGiTest} is a convenience wrapper for the functionality provided
- * by {@link OSGiTestHelper}.
- *
- * @author Thomas.Diesler(a)jboss.org
- * @since 25-Sep-2008
- */
-public abstract class OSGiTest
-{
- // Provide logging
- private static final Logger log = Logger.getLogger(OSGiTest.class);
-
- private OSGiTestHelper helper;
-
- protected OSGiTest()
- {
- }
-
- /**
- * Get the test helper used by this test
- *
- * Overwrite if you need to supply another helper
- * i.e. one that you have statically setup
- */
- protected OSGiTestHelper getTestHelper()
- {
- if (helper == null)
- helper = new OSGiTestHelper();
-
- return helper;
- }
-
- /**
- * Writes a a debug start messge
- */
- @Before
- public void setUp() throws Exception
- {
- log.debug("### START " + getLongName());
- }
-
- /**
- * Writes a a debug stop messge
- */
- @After
- public void tearDown() throws Exception
- {
- log.debug("### END " + getLongName());
- }
-
- /**
- * Get the last token in the FQN of this test class.
- */
- protected String getShortName()
- {
- String shortName = getClass().getName();
- shortName = shortName.substring(shortName.lastIndexOf(".") + 1);
- return shortName;
- }
-
- /**
- * Get the the FQN of this test class.
- */
- protected String getLongName()
- {
- return getClass().getName();
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getDefaultRuntime()}
- */
- protected OSGiRuntime getDefaultRuntime()
- {
- return getTestHelper().getDefaultRuntime();
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getEmbeddedRuntime()}
- */
- protected OSGiRuntime getEmbeddedRuntime()
- {
- return getTestHelper().getEmbeddedRuntime();
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getRemoteRuntime()}
- */
- public OSGiRuntime getRemoteRuntime()
- {
- return getTestHelper().getRemoteRuntime();
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getResourceURL(String)}
- */
- protected URL getResourceURL(String resource)
- {
- return getTestHelper().getResourceURL(resource);
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getResourceFile(String)}
- */
- protected File getResourceFile(String resource)
- {
- return getTestHelper().getResourceFile(resource);
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getTestArchiveURL(String)}
- */
- protected URL getTestArchiveURL(String archive)
- {
- return getTestHelper().getTestArchiveURL(archive);
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getTestArchivePath(String)}
- */
- protected String getTestArchivePath(String archive)
- {
- return getTestHelper().getTestArchivePath(archive);
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getTestArchiveFile(String)}
- */
- protected File getTestArchiveFile(String archive)
- {
- return getTestHelper().getTestArchiveFile(archive);
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getInitialContext()}
- */
- public InitialContext getInitialContext() throws NamingException
- {
- return getTestHelper().getInitialContext();
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getServerHost()}
- */
- public String getServerHost()
- {
- return getTestHelper().getServerHost();
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getTargetContainer()}
- */
- public String getTargetContainer()
- {
- return getTestHelper().getTargetContainer();
- }
-
- /**
- * Delegates to {@link OSGiTestHelper#getFrameworkName()}
- */
- public String getFrameworkName()
- {
- return getTestHelper().getFrameworkName();
- }
-
- public VirtualFile assembleBundle(String name, String resource, Class<?>...
packages) throws Exception
- {
- return getTestHelper().assembleBundle(name, resource, packages);
- }
-
- public VirtualFile assembleBundle(String name, String[] resources, Class<?>...
packages) throws Exception
- {
- return getTestHelper().assembleBundle(name, resources, packages);
- }
-
- public void assertBundleState(int expState, int wasState)
- {
- getTestHelper().assertBundleState(expState, wasState);
- }
-
- public void assertBundleLoadClass(Bundle bundle, String className, boolean success)
- {
- getTestHelper().assertBundleLoadClass(bundle, className, success);
- }
-}
Copied:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java
(from rev 102342,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java)
===================================================================
---
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java
(rev 0)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTest.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -0,0 +1,212 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.osgi.testing;
+
+import java.io.File;
+import java.net.URL;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.logging.Logger;
+import org.jboss.osgi.vfs.VirtualFile;
+import org.junit.After;
+import org.junit.Before;
+import org.osgi.framework.Bundle;
+
+/**
+ * An abstract OSGi Test.
+ *
+ * A convenience wrapper for the functionality provided by {@link OSGiTestHelper}.
+ *
+ * @author Thomas.Diesler(a)jboss.org
+ * @since 25-Sep-2008
+ */
+public abstract class OSGiTest
+{
+ // Provide logging
+ private static final Logger log = Logger.getLogger(OSGiTest.class);
+
+ private OSGiTestHelper helper;
+
+ /**
+ * Get the test helper used by this test
+ *
+ * Overwrite if you need to supply another helper
+ * i.e. one that you have statically setup
+ */
+ protected OSGiTestHelper getTestHelper()
+ {
+ if (helper == null)
+ helper = new OSGiTestHelper();
+
+ return helper;
+ }
+
+ /**
+ * Writes a a debug start messge
+ */
+ @Before
+ public void setUp() throws Exception
+ {
+ log.debug("### START " + getLongName());
+ }
+
+ /**
+ * Writes a a debug stop messge
+ */
+ @After
+ public void tearDown() throws Exception
+ {
+ log.debug("### END " + getLongName());
+ }
+
+ /**
+ * Get the last token in the FQN of this test class.
+ */
+ protected String getShortName()
+ {
+ String shortName = getClass().getName();
+ shortName = shortName.substring(shortName.lastIndexOf(".") + 1);
+ return shortName;
+ }
+
+ /**
+ * Get the the FQN of this test class.
+ */
+ protected String getLongName()
+ {
+ return getClass().getName();
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getResourceURL(String)}
+ */
+ protected URL getResourceURL(String resource)
+ {
+ return getTestHelper().getResourceURL(resource);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getResourceFile(String)}
+ */
+ protected File getResourceFile(String resource)
+ {
+ return getTestHelper().getResourceFile(resource);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getTestArchiveURL(String)}
+ */
+ protected URL getTestArchiveURL(String archive)
+ {
+ return getTestHelper().getTestArchiveURL(archive);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getTestArchivePath(String)}
+ */
+ protected String getTestArchivePath(String archive)
+ {
+ return getTestHelper().getTestArchivePath(archive);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getTestArchiveFile(String)}
+ */
+ protected File getTestArchiveFile(String archive)
+ {
+ return getTestHelper().getTestArchiveFile(archive);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getInitialContext()}
+ */
+ public InitialContext getInitialContext() throws NamingException
+ {
+ return getTestHelper().getInitialContext();
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getServerHost()}
+ */
+ public String getServerHost()
+ {
+ return getTestHelper().getServerHost();
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getTargetContainer()}
+ */
+ public String getTargetContainer()
+ {
+ return getTestHelper().getTargetContainer();
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#getFrameworkName()}
+ */
+ public String getFrameworkName()
+ {
+ return getTestHelper().getFrameworkName();
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#assembleArchive(String, String, Class...)}
+ */
+ public VirtualFile assembleArchive(String name, String resource, Class<?>...
packages) throws Exception
+ {
+ return getTestHelper().assembleArchive(name, resource, packages);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#assembleArchive(String, String[], Class...)}
+ */
+ public VirtualFile assembleArchive(String name, String[] resources, Class<?>...
packages) throws Exception
+ {
+ return getTestHelper().assembleArchive(name, resources, packages);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#assertBundleState(int, int)}
+ */
+ public void assertBundleState(int expState, int wasState)
+ {
+ getTestHelper().assertBundleState(expState, wasState);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#assertLoadClass(Bundle, String)}
+ */
+ public Class<?> assertLoadClass(Bundle bundle, String className)
+ {
+ return getTestHelper().assertLoadClass(bundle, className);
+ }
+
+ /**
+ * Delegates to {@link OSGiTestHelper#assertLoadClassFail(Bundle, String)}
+ */
+ public void assertLoadClassFail(Bundle bundle, String className)
+ {
+ getTestHelper().assertLoadClassFail(bundle, className);
+ }
+}
Deleted:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java
===================================================================
---
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java 2010-03-09
19:47:32 UTC (rev 102159)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -1,334 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.osgi.testing;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Hashtable;
-
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.jboss.osgi.spi.framework.OSGiBootstrap;
-import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
-import org.jboss.osgi.spi.util.ConstantsHelper;
-import org.jboss.osgi.testing.internal.EmbeddedRuntime;
-import org.jboss.osgi.testing.internal.RemoteRuntime;
-import org.jboss.osgi.vfs.AbstractVFS;
-import org.jboss.osgi.vfs.VirtualFile;
-import org.jboss.shrinkwrap.api.Archives;
-import org.jboss.shrinkwrap.api.Asset;
-import org.jboss.shrinkwrap.api.exporter.ZipExporter;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.osgi.framework.Bundle;
-
-/**
- * An OSGi Test Helper
- *
- * @author Thomas.Diesler(a)jboss.org
- * @since 25-Sep-2008
- */
-public class OSGiTestHelper
-{
- private static final String SYSPROP_TEST_RESOURCES_DIRECTORY =
"test.resources.directory";
- private static final String SYSPROP_TEST_ARCHIVE_DIRECTORY =
"test.archive.directory";
-
- // The OSGiBootstrapProvider is a lazy property of the helper
- private OSGiBootstrapProvider bootProvider;
- private boolean skipCreateBootstrapProvider;
-
- private static String testResourcesDir;
- private static String testArchiveDir;
-
- public OSGiTestHelper()
- {
- testResourcesDir = System.getProperty(SYSPROP_TEST_RESOURCES_DIRECTORY,
"target/test-classes");
- testArchiveDir = System.getProperty(SYSPROP_TEST_ARCHIVE_DIRECTORY,
"target/test-libs");
- }
-
- public OSGiBootstrapProvider getBootstrapProvider()
- {
- if (bootProvider == null && skipCreateBootstrapProvider == false)
- {
- try
- {
- bootProvider = OSGiBootstrap.getBootstrapProvider();
- }
- catch (RuntimeException rte)
- {
- skipCreateBootstrapProvider = true;
- throw rte;
- }
- }
- return bootProvider;
- }
-
- public void ungetBootstrapProvider()
- {
- bootProvider = null;
- }
-
- public OSGiRuntime getDefaultRuntime()
- {
- OSGiRuntime runtime;
-
- String target = System.getProperty("target.container");
- if (target == null)
- {
- runtime = getEmbeddedRuntime();
- }
- else
- {
- runtime = getRemoteRuntime();
- }
- return runtime;
- }
-
- public OSGiRuntime getEmbeddedRuntime()
- {
- return new EmbeddedRuntime(this);
- }
-
- public OSGiRuntime getRemoteRuntime()
- {
- return new RemoteRuntime(this);
- }
-
- /** Try to discover the URL for the test resource */
- public URL getResourceURL(String resource)
- {
- URL resURL = null;
- try
- {
- File resourceFile = getResourceFile(resource);
- resURL = resourceFile.toURI().toURL();
- }
- catch (MalformedURLException e)
- {
- // ignore
- }
- return resURL;
- }
-
- /** Try to discover the File for the test resource */
- public File getResourceFile(String resource)
- {
- File file = new File(resource);
- if (file.exists())
- return file;
-
- file = new File(testResourcesDir + "/" + resource);
- if (file.exists())
- return file;
-
- throw new IllegalArgumentException("Cannot obtain '" +
testResourcesDir + "/" + resource + "'");
- }
-
- /** Try to discover the URL for the deployment archive */
- public URL getTestArchiveURL(String archive)
- {
- try
- {
- return getTestArchiveFile(archive).toURI().toURL();
- }
- catch (MalformedURLException ex)
- {
- throw new IllegalStateException(ex);
- }
- }
-
- /** Try to discover the absolute path for the deployment archive */
- public String getTestArchivePath(String archive)
- {
- return getTestArchiveFile(archive).getAbsolutePath();
- }
-
- /** Try to discover the File for the deployment archive */
- public File getTestArchiveFile(String archive)
- {
- File file = new File(archive);
- if (file.exists())
- return file;
-
- file = new File(testArchiveDir + "/" + archive);
- if (file.exists())
- return file;
-
- throw new IllegalArgumentException("Cannot obtain '" + testArchiveDir
+ "/" + archive + "'.");
- }
-
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public InitialContext getInitialContext() throws NamingException
- {
- String port = System.getProperty("jndi.server.port", "1099");
- Integer jndiPort = new Integer(port);
-
- Hashtable env = new Hashtable();
- env.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
- env.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
- env.put("java.naming.provider.url", "jnp://" + getServerHost()
+ ":" + jndiPort);
- return new InitialContext(env);
- }
-
- public String getServerHost()
- {
- String bindAddress = System.getProperty("jboss.bind.address",
"localhost");
- return bindAddress;
- }
-
- public String getTargetContainer()
- {
- String targetContainer = System.getProperty("target.container");
- return targetContainer;
- }
-
- public String getFrameworkName()
- {
- String framework = System.getProperty("framework");
- if (framework == null || framework.length() == 0 ||
framework.equals("${framework}"))
- framework = "jbossmc";
-
- return framework;
- }
-
- public VirtualFile assembleBundle(String name, String resource, Class<?>...
packages) throws Exception
- {
- return assembleBundle(name, new String[] { resource }, packages);
- }
-
- public VirtualFile assembleBundle(String name, String[] resources, Class<?>...
packages) throws IOException
- {
- JavaArchive archive = Archives.create(name + ".jar", JavaArchive.class);
- if (resources != null)
- {
- for (String res : resources)
- {
- URL url = getClass().getResource(res);
- if (url == null)
- throw new IllegalArgumentException("Cannot load resource: " +
res);
-
- final VirtualFile file = AbstractVFS.getChild(url);
- if (file.isDirectory())
- {
- addResources(archive, file, file);
- }
- else
- {
- addResource(archive, res, file);
- }
- }
- }
- if (packages != null)
- {
- for(Class<?> clazz : packages)
- {
- URL url = clazz.getResource("/");
- VirtualFile base = AbstractVFS.getChild(url);
-
- String path = clazz.getName().replace('.', '/');
- path = path.substring(0, path.lastIndexOf("/"));
-
- VirtualFile classes = base.getChild(path);
- addResources(archive, base, classes);
- }
- }
-
- // Convert archive to file URL
- ZipExporter exporter = archive.as(ZipExporter.class);
- File target = File.createTempFile("archive_", ".jar");
- exporter.exportZip(target, true);
- target.deleteOnExit();
-
- return AbstractVFS.getChild(target.toURI().toURL());
- }
-
- private void addResources(JavaArchive archive, VirtualFile basedir, VirtualFile
resdir) throws IOException
- {
- String basepath = basedir.getPathName();
- for (final VirtualFile child : resdir.getChildrenRecursively())
- {
- if (child.isDirectory())
- continue;
-
- String path = child.getPathName();
- path = path.substring(basepath.length());
-
- addResource(archive, path, child);
- }
- }
-
- private void addResource(JavaArchive archive, String path, final VirtualFile file)
- {
- Asset asset = new Asset()
- {
- public InputStream openStream()
- {
- try
- {
- return file.openStream();
- }
- catch (IOException ex)
- {
- throw new IllegalStateException("Cannot open stream for: " +
file, ex);
- }
- }
- };
- archive.add(asset, path);
- }
-
- public void assertBundleState(int expState, int wasState)
- {
- String expstr = ConstantsHelper.bundleState(expState);
- String wasstr = ConstantsHelper.bundleState(wasState);
- assertEquals("Bundle " + expstr, expstr, wasstr);
- }
-
- public void assertBundleLoadClass(Bundle bundle, String className, boolean success)
- {
- String message = bundle.getSymbolicName() + " loads " + className;
-
- Class<?> clazz;
- try
- {
- clazz = bundle.loadClass(className);
- if (success)
- {
- assertEquals(message, className, clazz.getName());
- }
- else
- {
- fail("ClassNotFoundException expected for: " + message +
"\nLoaded from " + clazz.getClassLoader());
- }
- }
- catch (ClassNotFoundException ex)
- {
- if (success)
- fail("Unexpected ClassNotFoundException for: " + message);
- }
- }
-}
Copied:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java
(from rev 102342,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java)
===================================================================
---
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java
(rev 0)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/OSGiTestHelper.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -0,0 +1,282 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.osgi.testing;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Hashtable;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.osgi.spi.util.ConstantsHelper;
+import org.jboss.osgi.vfs.AbstractVFS;
+import org.jboss.osgi.vfs.VirtualFile;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.api.exporter.ZipExporter;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.osgi.framework.Bundle;
+
+/**
+ * An OSGi Test Helper
+ *
+ * @author Thomas.Diesler(a)jboss.org
+ * @since 25-Sep-2008
+ */
+public class OSGiTestHelper
+{
+ private static final String SYSPROP_TEST_RESOURCES_DIRECTORY =
"test.resources.directory";
+ private static final String SYSPROP_TEST_ARCHIVE_DIRECTORY =
"test.archive.directory";
+
+ private static String testResourcesDir;
+ private static String testArchiveDir;
+
+ public OSGiTestHelper()
+ {
+ testResourcesDir = System.getProperty(SYSPROP_TEST_RESOURCES_DIRECTORY,
"target/test-classes");
+ testArchiveDir = System.getProperty(SYSPROP_TEST_ARCHIVE_DIRECTORY,
"target/test-libs");
+ }
+
+ /** Try to discover the URL for the test resource */
+ public URL getResourceURL(String resource)
+ {
+ URL resURL = null;
+ try
+ {
+ File resourceFile = getResourceFile(resource);
+ resURL = resourceFile.toURI().toURL();
+ }
+ catch (MalformedURLException e)
+ {
+ // ignore
+ }
+ return resURL;
+ }
+
+ /** Try to discover the File for the test resource */
+ public File getResourceFile(String resource)
+ {
+ File file = new File(resource);
+ if (file.exists())
+ return file;
+
+ file = new File(testResourcesDir + "/" + resource);
+ if (file.exists())
+ return file;
+
+ throw new IllegalArgumentException("Cannot obtain '" +
testResourcesDir + "/" + resource + "'");
+ }
+
+ /** Try to discover the URL for the deployment archive */
+ public URL getTestArchiveURL(String archive)
+ {
+ try
+ {
+ return getTestArchiveFile(archive).toURI().toURL();
+ }
+ catch (MalformedURLException ex)
+ {
+ throw new IllegalStateException(ex);
+ }
+ }
+
+ /** Try to discover the absolute path for the deployment archive */
+ public String getTestArchivePath(String archive)
+ {
+ return getTestArchiveFile(archive).getAbsolutePath();
+ }
+
+ /** Try to discover the File for the deployment archive */
+ public File getTestArchiveFile(String archive)
+ {
+ File file = new File(archive);
+ if (file.exists())
+ return file;
+
+ file = new File(testArchiveDir + "/" + archive);
+ if (file.exists())
+ return file;
+
+ throw new IllegalArgumentException("Cannot obtain '" + testArchiveDir
+ "/" + archive + "'.");
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ public InitialContext getInitialContext() throws NamingException
+ {
+ String port = System.getProperty("jndi.server.port", "1099");
+ Integer jndiPort = new Integer(port);
+
+ Hashtable env = new Hashtable();
+ env.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
+ env.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
+ env.put("java.naming.provider.url", "jnp://" + getServerHost()
+ ":" + jndiPort);
+ return new InitialContext(env);
+ }
+
+ public String getServerHost()
+ {
+ String bindAddress = System.getProperty("jboss.bind.address",
"localhost");
+ return bindAddress;
+ }
+
+ public String getTargetContainer()
+ {
+ String targetContainer = System.getProperty("target.container");
+ return targetContainer;
+ }
+
+ public String getFrameworkName()
+ {
+ String framework = System.getProperty("framework");
+ if (framework == null || framework.length() == 0 ||
framework.equals("${framework}"))
+ framework = "jbossmc";
+
+ return framework;
+ }
+
+ public VirtualFile assembleArchive(String name, String resource, Class<?>...
packages) throws Exception
+ {
+ return assembleArchive(name, new String[] { resource }, packages);
+ }
+
+ public VirtualFile assembleArchive(String name, String[] resources, Class<?>...
packages) throws IOException
+ {
+ JavaArchive archive = Archives.create(name + ".jar", JavaArchive.class);
+ if (resources != null)
+ {
+ for (String res : resources)
+ {
+ URL url = getClass().getResource(res);
+ if (url == null)
+ throw new IllegalArgumentException("Cannot load resource: " +
res);
+
+ final VirtualFile file = AbstractVFS.getRoot(url);
+ if (file.isDirectory())
+ {
+ addResources(archive, file, file);
+ }
+ else
+ {
+ addResource(archive, res, file);
+ }
+ }
+ }
+ if (packages != null)
+ {
+ for (Class<?> clazz : packages)
+ {
+ URL url = clazz.getResource("/");
+ VirtualFile base = AbstractVFS.getRoot(url);
+
+ String path = clazz.getName().replace('.', '/');
+ path = path.substring(0, path.lastIndexOf("/"));
+
+ VirtualFile classes = base.getChild(path);
+ addResources(archive, base, classes);
+ }
+ }
+
+ // Convert archive to file URL
+ ZipExporter exporter = archive.as(ZipExporter.class);
+ File target = File.createTempFile("archive_", ".jar");
+ exporter.exportZip(target, true);
+ target.deleteOnExit();
+
+ return AbstractVFS.getRoot(target.toURI().toURL());
+ }
+
+ private void addResources(JavaArchive archive, VirtualFile basedir, VirtualFile
resdir) throws IOException
+ {
+ String basepath = basedir.getPathName();
+ for (final VirtualFile child : resdir.getChildrenRecursively())
+ {
+ if (child.isDirectory())
+ continue;
+
+ String path = child.getPathName();
+ path = path.substring(basepath.length());
+
+ addResource(archive, path, child);
+ }
+ }
+
+ private void addResource(JavaArchive archive, String path, final VirtualFile file)
+ {
+ Asset asset = new Asset()
+ {
+ public InputStream openStream()
+ {
+ try
+ {
+ return file.openStream();
+ }
+ catch (IOException ex)
+ {
+ throw new IllegalStateException("Cannot open stream for: " +
file, ex);
+ }
+ }
+ };
+ archive.add(asset, path);
+ }
+
+ public void assertBundleState(int expState, int wasState)
+ {
+ String expstr = ConstantsHelper.bundleState(expState);
+ String wasstr = ConstantsHelper.bundleState(wasState);
+ assertEquals("Bundle " + expstr, expstr, wasstr);
+ }
+
+ public Class<?> assertLoadClass(Bundle bundle, String className)
+ {
+ try
+ {
+ return bundle.loadClass(className);
+ }
+ catch (ClassNotFoundException ex)
+ {
+ String message = bundle.getSymbolicName() + " loads " + className;
+ fail("Unexpected ClassNotFoundException for: " + message);
+ return null;
+ }
+ }
+
+ public void assertLoadClassFail(Bundle bundle, String className)
+ {
+ try
+ {
+ Class<?> clazz = bundle.loadClass(className);
+ String message = bundle.getSymbolicName() + " loads " + className;
+ fail("ClassNotFoundException expected for: " + message +
"\nLoaded from " + clazz.getClassLoader());
+ }
+ catch (ClassNotFoundException ex)
+ {
+ // expected
+ }
+ }
+}
Deleted:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java
===================================================================
---
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java 2010-03-09
19:47:32 UTC (rev 102159)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -1,204 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.osgi.testing.internal;
-
-// $Id$
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import javax.management.MBeanServer;
-import javax.management.MBeanServerConnection;
-import javax.management.MBeanServerFactory;
-
-import org.jboss.logging.Logger;
-import org.jboss.osgi.spi.capability.Capability;
-import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
-import org.jboss.osgi.spi.util.BundleInfo;
-import org.jboss.osgi.testing.OSGiBundle;
-import org.jboss.osgi.testing.OSGiRuntime;
-import org.jboss.osgi.testing.OSGiServiceReference;
-import org.jboss.osgi.testing.OSGiTestHelper;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.launch.Framework;
-
-/**
- * An embedded implementation of the {@link OSGiRuntime}
- *
- * @author Thomas.Diesler(a)jboss.org
- * @since 25-Sep-2008
- */
-public class EmbeddedRuntime extends OSGiRuntimeImpl
-{
- // Provide logging
- private static final Logger log = Logger.getLogger(EmbeddedRuntime.class);
-
- private MBeanServer server;
-
- public EmbeddedRuntime(OSGiTestHelper helper)
- {
- super(helper);
- }
-
- OSGiBundle installBundle(BundleInfo info) throws BundleException
- {
- BundleContext context = getSystemContext();
- String location = fixVirtualFileURL(info.getRootURL()).toExternalForm();
- Bundle auxBundle = context.installBundle(location);
- OSGiBundle bundle = new EmbeddedBundle(this, auxBundle);
- return registerBundle(bundle.getLocation(), bundle);
- }
-
- public OSGiBundle[] getBundles()
- {
- List<OSGiBundle> absBundles = new ArrayList<OSGiBundle>();
- for (Bundle bundle : getSystemContext().getBundles())
- {
- absBundles.add(new EmbeddedBundle(this, bundle));
- }
- OSGiBundle[] bundleArr = new OSGiBundle[absBundles.size()];
- absBundles.toArray(bundleArr);
- return bundleArr;
- }
-
- public OSGiBundle getBundle(long bundleId)
- {
- Bundle bundle = getSystemContext().getBundle(bundleId);
- return bundle != null ? new EmbeddedBundle(this, bundle) : null;
- }
-
- public OSGiServiceReference getServiceReference(String clazz)
- {
- ServiceReference sref = getSystemContext().getServiceReference(clazz);
- return (sref != null ? new EmbeddedServiceReference(sref) : null);
- }
-
- public OSGiServiceReference[] getServiceReferences(String clazz, String filter)
- {
- OSGiServiceReference[] retRefs = null;
-
- ServiceReference[] srefs;
- try
- {
- srefs = getSystemContext().getServiceReferences(clazz, filter);
- }
- catch (InvalidSyntaxException e)
- {
- throw new IllegalArgumentException("Invalid filter syntax: " +
filter);
- }
-
- if (srefs != null)
- {
- retRefs = new OSGiServiceReference[srefs.length];
- for (int i = 0; i < srefs.length; i++)
- retRefs[i] = new EmbeddedServiceReference(srefs[i]);
- }
- return retRefs;
- }
-
- @Override
- public void addCapability(Capability capability) throws BundleException
- {
- // Copy the properties to the System props
- Map<String, String> props = capability.getSystemProperties();
- for (Entry<String, String> entry : props.entrySet())
- {
- String value = System.getProperty(entry.getKey());
- if (value == null)
- System.setProperty(entry.getKey(), entry.getValue());
- }
-
- super.addCapability(capability);
- }
-
- @Override
- public void shutdown()
- {
- OSGiBootstrapProvider bootProvider = getTestHelper().getBootstrapProvider();
- if (bootProvider != null)
- {
- super.shutdown();
- try
- {
- Framework framework = bootProvider.getFramework();
- log.debug("Framework stop: " + framework);
- framework.stop();
- framework.waitForStop(5000);
- }
- catch (Exception ex)
- {
- log.error("Cannot stop the framework", ex);
- }
- finally
- {
- getTestHelper().ungetBootstrapProvider();
- }
- }
- }
-
- public MBeanServerConnection getMBeanServer()
- {
- if (server == null)
- {
- ArrayList<MBeanServer> serverArr =
MBeanServerFactory.findMBeanServer(null);
- if (serverArr.size() > 1)
- throw new IllegalStateException("Multiple MBeanServer instances not
supported");
-
- if (serverArr.size() == 1)
- server = serverArr.get(0);
-
- if (server == null)
- server = MBeanServerFactory.createMBeanServer();
- }
- return server;
- }
-
- public boolean isRemoteRuntime()
- {
- return false;
- }
-
- BundleContext getSystemContext()
- {
- OSGiBootstrapProvider bootProvider = getTestHelper().getBootstrapProvider();
- Framework framework = bootProvider.getFramework();
- if (framework.getState() != Bundle.ACTIVE)
- {
- try
- {
- log.debug("Framework start: " + framework);
- framework.start();
- }
- catch (BundleException ex)
- {
- throw new IllegalStateException("Cannot start framework", ex);
- }
- }
- return framework.getBundleContext();
- }
-}
Copied:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java
(from rev 102342,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java)
===================================================================
---
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java
(rev 0)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedRuntime.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -0,0 +1,213 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.osgi.testing.internal;
+
+// $Id$
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerConnection;
+import javax.management.MBeanServerFactory;
+
+import org.jboss.logging.Logger;
+import org.jboss.osgi.spi.capability.Capability;
+import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
+import org.jboss.osgi.spi.util.BundleInfo;
+import org.jboss.osgi.testing.OSGiBundle;
+import org.jboss.osgi.testing.OSGiRuntime;
+import org.jboss.osgi.testing.OSGiRuntimeHelper;
+import org.jboss.osgi.testing.OSGiServiceReference;
+import org.jboss.osgi.vfs.VirtualFile;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.launch.Framework;
+
+/**
+ * An embedded implementation of the {@link OSGiRuntime}
+ *
+ * @author Thomas.Diesler(a)jboss.org
+ * @since 25-Sep-2008
+ */
+public class EmbeddedRuntime extends OSGiRuntimeImpl
+{
+ // Provide logging
+ private static final Logger log = Logger.getLogger(EmbeddedRuntime.class);
+
+ private MBeanServer server;
+
+ public EmbeddedRuntime(OSGiRuntimeHelper helper)
+ {
+ super(helper);
+ }
+
+ OSGiBundle installBundle(BundleInfo info) throws BundleException
+ {
+ try
+ {
+ VirtualFile rootFile = info.getRoot();
+ BundleContext context = getSystemContext();
+ Bundle auxBundle = context.installBundle(info.getLocation(),
rootFile.openStream());
+ OSGiBundle bundle = new EmbeddedBundle(this, auxBundle);
+ return registerBundle(bundle.getLocation(), bundle);
+ }
+ catch (IOException ex)
+ {
+ throw new BundleException("Cannot install bundle: " + info, ex);
+ }
+ }
+
+ public OSGiBundle[] getBundles()
+ {
+ List<OSGiBundle> absBundles = new ArrayList<OSGiBundle>();
+ for (Bundle bundle : getSystemContext().getBundles())
+ {
+ absBundles.add(new EmbeddedBundle(this, bundle));
+ }
+ OSGiBundle[] bundleArr = new OSGiBundle[absBundles.size()];
+ absBundles.toArray(bundleArr);
+ return bundleArr;
+ }
+
+ public OSGiBundle getBundle(long bundleId)
+ {
+ Bundle bundle = getSystemContext().getBundle(bundleId);
+ return bundle != null ? new EmbeddedBundle(this, bundle) : null;
+ }
+
+ public OSGiServiceReference getServiceReference(String clazz)
+ {
+ ServiceReference sref = getSystemContext().getServiceReference(clazz);
+ return (sref != null ? new EmbeddedServiceReference(sref) : null);
+ }
+
+ public OSGiServiceReference[] getServiceReferences(String clazz, String filter)
+ {
+ OSGiServiceReference[] retRefs = null;
+
+ ServiceReference[] srefs;
+ try
+ {
+ srefs = getSystemContext().getServiceReferences(clazz, filter);
+ }
+ catch (InvalidSyntaxException e)
+ {
+ throw new IllegalArgumentException("Invalid filter syntax: " +
filter);
+ }
+
+ if (srefs != null)
+ {
+ retRefs = new OSGiServiceReference[srefs.length];
+ for (int i = 0; i < srefs.length; i++)
+ retRefs[i] = new EmbeddedServiceReference(srefs[i]);
+ }
+ return retRefs;
+ }
+
+ @Override
+ public void addCapability(Capability capability) throws BundleException
+ {
+ // Copy the properties to the System props
+ Map<String, String> props = capability.getSystemProperties();
+ for (Entry<String, String> entry : props.entrySet())
+ {
+ String value = System.getProperty(entry.getKey());
+ if (value == null)
+ System.setProperty(entry.getKey(), entry.getValue());
+ }
+
+ super.addCapability(capability);
+ }
+
+ @Override
+ public void shutdown()
+ {
+ OSGiBootstrapProvider bootProvider = getTestHelper().getBootstrapProvider();
+ if (bootProvider != null)
+ {
+ super.shutdown();
+ try
+ {
+ Framework framework = bootProvider.getFramework();
+ log.debug("Framework stop: " + framework);
+ framework.stop();
+ framework.waitForStop(5000);
+ }
+ catch (Exception ex)
+ {
+ log.error("Cannot stop the framework", ex);
+ }
+ finally
+ {
+ getTestHelper().ungetBootstrapProvider();
+ }
+ }
+ }
+
+ public MBeanServerConnection getMBeanServer()
+ {
+ if (server == null)
+ {
+ ArrayList<MBeanServer> serverArr =
MBeanServerFactory.findMBeanServer(null);
+ if (serverArr.size() > 1)
+ throw new IllegalStateException("Multiple MBeanServer instances not
supported");
+
+ if (serverArr.size() == 1)
+ server = serverArr.get(0);
+
+ if (server == null)
+ server = MBeanServerFactory.createMBeanServer();
+ }
+ return server;
+ }
+
+ public boolean isRemoteRuntime()
+ {
+ return false;
+ }
+
+ BundleContext getSystemContext()
+ {
+ OSGiBootstrapProvider bootProvider = getTestHelper().getBootstrapProvider();
+ Framework framework = bootProvider.getFramework();
+ if (framework.getState() != Bundle.ACTIVE)
+ {
+ try
+ {
+ log.debug("Framework start: " + framework);
+ framework.start();
+ }
+ catch (BundleException ex)
+ {
+ throw new IllegalStateException("Cannot start framework", ex);
+ }
+ }
+ return framework.getBundleContext();
+ }
+}
Deleted:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java
===================================================================
---
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java 2010-03-09
19:47:32 UTC (rev 102159)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -1,449 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.osgi.testing.internal;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Map.Entry;
-import java.util.jar.Attributes;
-import java.util.jar.JarFile;
-import java.util.jar.Manifest;
-
-import javax.management.MBeanServerConnection;
-import javax.management.ObjectName;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.jboss.logging.Logger;
-import org.jboss.osgi.jmx.BundleStateMBeanExt;
-import org.jboss.osgi.jmx.FrameworkMBeanExt;
-import org.jboss.osgi.jmx.MBeanProxy;
-import org.jboss.osgi.jmx.ObjectNameFactory;
-import org.jboss.osgi.jmx.PackageStateMBeanExt;
-import org.jboss.osgi.jmx.ServiceStateMBeanExt;
-import org.jboss.osgi.spi.capability.Capability;
-import org.jboss.osgi.spi.util.BundleInfo;
-import org.jboss.osgi.testing.OSGiBundle;
-import org.jboss.osgi.testing.OSGiRuntime;
-import org.jboss.osgi.testing.OSGiServiceReference;
-import org.jboss.osgi.testing.OSGiTestHelper;
-import org.jboss.osgi.vfs.AbstractVFS;
-import org.jboss.osgi.vfs.VirtualFile;
-import org.jboss.shrinkwrap.api.Archive;
-import org.jboss.shrinkwrap.api.exporter.ZipExporter;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.Version;
-import org.osgi.jmx.framework.BundleStateMBean;
-import org.osgi.jmx.framework.FrameworkMBean;
-import org.osgi.jmx.framework.PackageStateMBean;
-import org.osgi.jmx.framework.ServiceStateMBean;
-
-/**
- * An abstract implementation of the {@link OSGiRuntime}
- *
- * @author Thomas.Diesler(a)jboss.org
- * @since 25-Sep-2008
- */
-public abstract class OSGiRuntimeImpl implements OSGiRuntime
-{
- // Provide logging
- private static final Logger log = Logger.getLogger(OSGiRuntimeImpl.class);
-
- private OSGiTestHelper helper;
- private Map<String, OSGiBundle> bundles = new LinkedHashMap<String,
OSGiBundle>();
- private List<Capability> capabilities = new ArrayList<Capability>();
-
- private FrameworkMBean frameworkState;
- private BundleStateMBean bundleState;
- private ServiceStateMBean serviceState;
- private PackageStateMBean packageState;
-
- public OSGiRuntimeImpl(OSGiTestHelper helper)
- {
- this.helper = helper;
- }
-
- public OSGiTestHelper getTestHelper()
- {
- return helper;
- }
-
- public void addCapability(Capability capability) throws BundleException
- {
- // Add dependent capabilies
- for (Capability dependency : capability.getDependencies())
- addCapability(dependency);
-
- OSGiServiceReference[] srefs = null;
-
- // Check if the service provided by the capability exists already
- String serviceName = capability.getServiceName();
- if (serviceName != null)
- srefs = getServiceReferences(serviceName, capability.getFilter());
-
- if (srefs == null || srefs.length == 0)
- {
- log.debug("Add capability: " + capability);
-
- // Install the capability bundles
- List<OSGiBundle> installed = new ArrayList<OSGiBundle>();
- for (BundleInfo info : capability.getBundles())
- {
- String location = info.getLocation();
- String symName = info.getSymbolicName();
- Version version = info.getVersion();
- if (bundles.get(location) == null && getBundle(symName, version) ==
null)
- {
- OSGiBundle bundle = installBundle(location);
- installed.add(bundle);
- }
- else
- {
- log.debug("Skip bundle: " + location);
- }
- }
-
- // Start the capability bundles
- for (OSGiBundle bundle : installed)
- {
- bundle.start();
- }
- capabilities.add(capability);
- }
- else
- {
- log.debug("Skip capability: " + capability);
- }
- }
-
- public void removeCapability(Capability capability)
- {
- if (capabilities.remove(capability))
- {
- log.debug("Remove capability : " + capability);
-
- List<BundleInfo> bundleInfos = new
ArrayList<BundleInfo>(capability.getBundles());
- Collections.reverse(bundleInfos);
-
- for (BundleInfo info : bundleInfos)
- failsafeUninstall(bundles.remove(info.getLocation()));
- }
-
- List<Capability> dependencies = new
ArrayList<Capability>(capability.getDependencies());
- Collections.reverse(dependencies);
-
- // Remove dependent capabilities
- for (Capability dependency : dependencies)
- removeCapability(dependency);
- }
-
- public OSGiBundle installBundle(String location) throws BundleException
- {
- BundleInfo info = BundleInfo.createBundleInfo(location);
- return installBundle(info);
- }
-
- public OSGiBundle installBundle(Archive<?> archive) throws BundleException,
IOException
- {
- VirtualFile file = toVirtualFile(archive);
- return installBundle(file);
- }
-
- public OSGiBundle installBundle(VirtualFile virtualFile) throws BundleException
- {
- BundleInfo info = BundleInfo.createBundleInfo(virtualFile);
- return installBundle(info);
- }
-
- abstract OSGiBundle installBundle(BundleInfo info) throws BundleException;
-
- // [JBVFS-147] Cannot read from vfs: protocol URL
- URL fixVirtualFileURL(URL url)
- {
- if (url.getProtocol().equals("vfs"))
- {
- File file = new File(url.getPath());
- if (file.exists())
- {
- try
- {
- url = file.toURI().toURL();
- }
- catch (MalformedURLException e)
- {
- // ignore
- }
- }
- }
- return url;
- }
-
- public void shutdown()
- {
- log.debug("Start Shutdown");
-
- // Uninstall the registered bundles
- ArrayList<String> locations = new ArrayList<String>(bundles.keySet());
- Collections.reverse(locations);
-
- while (locations.size() > 0)
- {
- String location = locations.remove(0);
- failsafeUninstall(bundles.remove(location));
- }
-
- // Uninstall the capabilities
- Collections.reverse(capabilities);
- while (capabilities.size() > 0)
- {
- Capability capability = capabilities.get(0);
- removeCapability(capability);
- }
-
- log.debug("End Shutdown");
- }
-
- public FrameworkMBean getFrameworkMBean() throws IOException
- {
- if (frameworkState == null)
- {
- ObjectName objectName = ObjectNameFactory.create(FrameworkMBeanExt.OBJECTNAME);
- MBeanServerConnection server = getMBeanServer();
- if (server.isRegistered(objectName))
- {
- frameworkState = MBeanProxy.get(server, objectName,
FrameworkMBeanExt.class);
- }
- else
- {
- objectName = ObjectNameFactory.create(FrameworkMBean.OBJECTNAME);
- frameworkState = MBeanProxy.get(server, objectName, FrameworkMBean.class);
- }
- }
- return frameworkState;
- }
-
- public BundleStateMBean getBundleStateMBean() throws IOException
- {
- if (bundleState == null)
- {
- MBeanServerConnection server = getMBeanServer();
- ObjectName objectName =
ObjectNameFactory.create(BundleStateMBeanExt.OBJECTNAME);
- if (server.isRegistered(objectName))
- {
- bundleState = MBeanProxy.get(server, objectName, BundleStateMBeanExt.class);
- }
- else
- {
- objectName = ObjectNameFactory.create(BundleStateMBean.OBJECTNAME);
- bundleState = MBeanProxy.get(server, objectName, BundleStateMBean.class);
- }
- }
- return bundleState;
- }
-
- public PackageStateMBean getPackageStateMBean() throws IOException
- {
- if (packageState == null)
- {
- MBeanServerConnection server = getMBeanServer();
- ObjectName objectName =
ObjectNameFactory.create(PackageStateMBeanExt.OBJECTNAME);
- if (server.isRegistered(objectName))
- {
- packageState = MBeanProxy.get(server, objectName,
PackageStateMBeanExt.class);
- }
- else
- {
- objectName = ObjectNameFactory.create(PackageStateMBean.OBJECTNAME);
- packageState = MBeanProxy.get(server, objectName, PackageStateMBean.class);
- }
- }
- return packageState;
- }
-
- public ServiceStateMBean getServiceStateMBean() throws IOException
- {
- if (serviceState == null)
- {
- MBeanServerConnection server = getMBeanServer();
- ObjectName objectName =
ObjectNameFactory.create(ServiceStateMBeanExt.OBJECTNAME);
- if (server.isRegistered(objectName))
- {
- serviceState = MBeanProxy.get(server, objectName,
ServiceStateMBeanExt.class);
- }
- else
- {
- objectName = ObjectNameFactory.create(ServiceStateMBean.OBJECTNAME);
- serviceState = MBeanProxy.get(server, objectName, ServiceStateMBean.class);
- }
- }
- return serviceState;
- }
-
- public InitialContext getInitialContext() throws NamingException
- {
- return helper.getInitialContext();
- }
-
- public String getServerHost()
- {
- return helper.getServerHost();
- }
-
- public OSGiBundle getBundle(String symbolicName, Version version)
- {
- OSGiBundle bundle = getBundle(symbolicName, version, false);
- return bundle;
- }
-
- public OSGiServiceReference getServiceReference(String clazz, long timeout)
- {
- int fraktion = 200;
- timeout = timeout / fraktion;
- OSGiServiceReference sref = getServiceReference(clazz);
- while (sref == null && 0 < timeout--)
- {
- try
- {
- Thread.sleep(fraktion);
- }
- catch (InterruptedException e)
- {
- // ignore
- }
- sref = getServiceReference(clazz);
- }
- return sref;
- }
-
- OSGiBundle getBundle(String symbolicName, Version version, boolean mustExist)
- {
- OSGiBundle bundle = null;
- List<OSGiBundle> bundles = Arrays.asList(getBundles());
- for (OSGiBundle aux : bundles)
- {
- if (aux.getSymbolicName().equals(symbolicName))
- {
- if (version == null || version.equals(aux.getVersion()))
- {
- bundle = aux;
- break;
- }
- }
- }
-
- if (bundle == null && mustExist == true)
- throw new IllegalStateException("Cannot obtain bundle: " +
symbolicName + "-" + version + ". We have " + bundles);
-
- return bundle;
- }
-
- String getManifestEntry(String location, String key)
- {
- Manifest manifest = getManifest(location);
- Attributes attribs = manifest.getMainAttributes();
- String value = attribs.getValue(key);
- return value;
- }
-
- Manifest getManifest(String location)
- {
- Manifest manifest;
- try
- {
- File archiveFile = getTestHelper().getTestArchiveFile(location);
- JarFile jarFile = new JarFile(archiveFile);
- manifest = jarFile.getManifest();
- jarFile.close();
- }
- catch (IOException ex)
- {
- throw new IllegalStateException("Cannot get manifest from: " +
location);
-
- }
- return manifest;
- }
-
- OSGiBundle registerBundle(String location, OSGiBundle bundle)
- {
- if (bundle == null)
- throw new IllegalArgumentException("Cannot register null bundle for: "
+ location);
-
- bundles.put(location, bundle);
- return bundle;
- }
-
- void unregisterBundle(OSGiBundle bundle)
- {
- if (bundle == null)
- throw new IllegalArgumentException("Cannot unregister null bundle");
-
- if (bundles.containsValue(bundle))
- {
- Set<Entry<String, OSGiBundle>> entrySet = bundles.entrySet();
- for (Entry<String, OSGiBundle> entry : entrySet)
- {
- if (bundle.equals(entry.getValue()))
- {
- String key = entry.getKey();
- bundles.remove(key);
- break;
- }
- }
- }
- }
-
- VirtualFile toVirtualFile(Archive<?> archive) throws IOException,
MalformedURLException
- {
- ZipExporter exporter = archive.as(ZipExporter.class);
- File target = File.createTempFile("archive_", ".jar");
- exporter.exportZip(target, true);
- target.deleteOnExit();
-
- return AbstractVFS.getChild(target.toURI().toURL());
- }
-
- private void failsafeUninstall(OSGiBundle bundle)
- {
- if (bundle != null)
- {
- try
- {
- if (bundle.getState() != Bundle.UNINSTALLED)
- bundle.uninstall();
- }
- catch (Exception ex)
- {
- log.warn("Cannot uninstall bundle: " + bundle, ex);
- }
- }
- }
-}
Copied:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java
(from rev 102467,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java)
===================================================================
---
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java
(rev 0)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/OSGiRuntimeImpl.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -0,0 +1,451 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.osgi.testing.internal;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.logging.Logger;
+import org.jboss.osgi.jmx.BundleStateMBeanExt;
+import org.jboss.osgi.jmx.FrameworkMBeanExt;
+import org.jboss.osgi.jmx.MBeanProxy;
+import org.jboss.osgi.jmx.ObjectNameFactory;
+import org.jboss.osgi.jmx.PackageStateMBeanExt;
+import org.jboss.osgi.jmx.ServiceStateMBeanExt;
+import org.jboss.osgi.spi.capability.Capability;
+import org.jboss.osgi.spi.util.BundleInfo;
+import org.jboss.osgi.testing.OSGiBundle;
+import org.jboss.osgi.testing.OSGiRuntime;
+import org.jboss.osgi.testing.OSGiRuntimeHelper;
+import org.jboss.osgi.testing.OSGiServiceReference;
+import org.jboss.osgi.vfs.AbstractVFS;
+import org.jboss.osgi.vfs.VirtualFile;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.exporter.ZipExporter;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Version;
+import org.osgi.jmx.framework.BundleStateMBean;
+import org.osgi.jmx.framework.FrameworkMBean;
+import org.osgi.jmx.framework.PackageStateMBean;
+import org.osgi.jmx.framework.ServiceStateMBean;
+
+/**
+ * An abstract implementation of the {@link OSGiRuntime}
+ *
+ * @author Thomas.Diesler(a)jboss.org
+ * @since 25-Sep-2008
+ */
+public abstract class OSGiRuntimeImpl implements OSGiRuntime
+{
+ // Provide logging
+ private static final Logger log = Logger.getLogger(OSGiRuntimeImpl.class);
+
+ private OSGiRuntimeHelper helper;
+ private Map<String, OSGiBundle> bundles = new LinkedHashMap<String,
OSGiBundle>();
+ private List<Capability> capabilities = new ArrayList<Capability>();
+
+ private FrameworkMBean frameworkState;
+ private BundleStateMBean bundleState;
+ private ServiceStateMBean serviceState;
+ private PackageStateMBean packageState;
+
+ public OSGiRuntimeImpl(OSGiRuntimeHelper helper)
+ {
+ this.helper = helper;
+ }
+
+ public OSGiRuntimeHelper getTestHelper()
+ {
+ return helper;
+ }
+
+ public void addCapability(Capability capability) throws BundleException
+ {
+ // Add dependent capabilies
+ for (Capability dependency : capability.getDependencies())
+ addCapability(dependency);
+
+ OSGiServiceReference[] srefs = null;
+
+ // Check if the service provided by the capability exists already
+ String serviceName = capability.getServiceName();
+ if (serviceName != null)
+ srefs = getServiceReferences(serviceName, capability.getFilter());
+
+ if (srefs == null || srefs.length == 0)
+ {
+ log.debug("Add capability: " + capability);
+
+ // Install the capability bundles
+ List<OSGiBundle> installed = new ArrayList<OSGiBundle>();
+ for (BundleInfo info : capability.getBundles())
+ {
+ String location = info.getLocation();
+ String symName = info.getSymbolicName();
+ Version version = info.getVersion();
+ if (bundles.get(location) == null && getBundle(symName, version) ==
null)
+ {
+ OSGiBundle bundle = installBundle(location);
+ installed.add(bundle);
+ }
+ else
+ {
+ log.debug("Skip bundle: " + location);
+ }
+ }
+
+ // Start the capability bundles
+ for (OSGiBundle bundle : installed)
+ {
+ bundle.start();
+ }
+ capabilities.add(capability);
+ }
+ else
+ {
+ log.debug("Skip capability: " + capability);
+ }
+ }
+
+ public void removeCapability(Capability capability)
+ {
+ if (capabilities.remove(capability))
+ {
+ log.debug("Remove capability : " + capability);
+
+ List<BundleInfo> bundleInfos = new
ArrayList<BundleInfo>(capability.getBundles());
+ Collections.reverse(bundleInfos);
+
+ for (BundleInfo info : bundleInfos)
+ {
+ OSGiBundle bundle = bundles.get(info.getLocation());
+ failsafeStop(bundle);
+ }
+
+ for (BundleInfo info : bundleInfos)
+ {
+ OSGiBundle bundle = bundles.remove(info.getLocation());
+ failsafeUninstall(bundle);
+ }
+ }
+
+ List<Capability> dependencies = new
ArrayList<Capability>(capability.getDependencies());
+ Collections.reverse(dependencies);
+
+ // Remove dependent capabilities
+ for (Capability dependency : dependencies)
+ removeCapability(dependency);
+ }
+
+ public OSGiBundle installBundle(String location) throws BundleException
+ {
+ BundleInfo info = BundleInfo.createBundleInfo(location);
+ return installBundle(info);
+ }
+
+ public OSGiBundle installBundle(Archive<?> archive) throws BundleException,
IOException
+ {
+ VirtualFile file = toVirtualFile(archive);
+ return installBundle(file);
+ }
+
+ public OSGiBundle installBundle(VirtualFile virtualFile) throws BundleException
+ {
+ BundleInfo info = BundleInfo.createBundleInfo(virtualFile);
+ return installBundle(info);
+ }
+
+ abstract OSGiBundle installBundle(BundleInfo info) throws BundleException;
+
+ public void shutdown()
+ {
+ log.debug("Start Shutdown");
+
+ // Uninstall the registered bundles
+ ArrayList<String> locations = new ArrayList<String>(bundles.keySet());
+ Collections.reverse(locations);
+
+ while (locations.size() > 0)
+ {
+ String location = locations.remove(0);
+ failsafeUninstall(bundles.remove(location));
+ }
+
+ // Uninstall the capabilities
+ Collections.reverse(capabilities);
+ while (capabilities.size() > 0)
+ {
+ Capability capability = capabilities.get(0);
+ removeCapability(capability);
+ }
+
+ log.debug("End Shutdown");
+ }
+
+ public FrameworkMBean getFrameworkMBean() throws IOException
+ {
+ if (frameworkState == null)
+ {
+ ObjectName objectName = ObjectNameFactory.create(FrameworkMBeanExt.OBJECTNAME);
+ MBeanServerConnection server = getMBeanServer();
+ if (server.isRegistered(objectName))
+ {
+ frameworkState = MBeanProxy.get(server, objectName,
FrameworkMBeanExt.class);
+ }
+ else
+ {
+ objectName = ObjectNameFactory.create(FrameworkMBean.OBJECTNAME);
+ frameworkState = MBeanProxy.get(server, objectName, FrameworkMBean.class);
+ }
+ }
+ return frameworkState;
+ }
+
+ public BundleStateMBean getBundleStateMBean() throws IOException
+ {
+ if (bundleState == null)
+ {
+ MBeanServerConnection server = getMBeanServer();
+ ObjectName objectName =
ObjectNameFactory.create(BundleStateMBeanExt.OBJECTNAME);
+ if (server.isRegistered(objectName))
+ {
+ bundleState = MBeanProxy.get(server, objectName, BundleStateMBeanExt.class);
+ }
+ else
+ {
+ objectName = ObjectNameFactory.create(BundleStateMBean.OBJECTNAME);
+ bundleState = MBeanProxy.get(server, objectName, BundleStateMBean.class);
+ }
+ }
+ return bundleState;
+ }
+
+ public PackageStateMBean getPackageStateMBean() throws IOException
+ {
+ if (packageState == null)
+ {
+ MBeanServerConnection server = getMBeanServer();
+ ObjectName objectName =
ObjectNameFactory.create(PackageStateMBeanExt.OBJECTNAME);
+ if (server.isRegistered(objectName))
+ {
+ packageState = MBeanProxy.get(server, objectName,
PackageStateMBeanExt.class);
+ }
+ else
+ {
+ objectName = ObjectNameFactory.create(PackageStateMBean.OBJECTNAME);
+ packageState = MBeanProxy.get(server, objectName, PackageStateMBean.class);
+ }
+ }
+ return packageState;
+ }
+
+ public ServiceStateMBean getServiceStateMBean() throws IOException
+ {
+ if (serviceState == null)
+ {
+ MBeanServerConnection server = getMBeanServer();
+ ObjectName objectName =
ObjectNameFactory.create(ServiceStateMBeanExt.OBJECTNAME);
+ if (server.isRegistered(objectName))
+ {
+ serviceState = MBeanProxy.get(server, objectName,
ServiceStateMBeanExt.class);
+ }
+ else
+ {
+ objectName = ObjectNameFactory.create(ServiceStateMBean.OBJECTNAME);
+ serviceState = MBeanProxy.get(server, objectName, ServiceStateMBean.class);
+ }
+ }
+ return serviceState;
+ }
+
+ public InitialContext getInitialContext() throws NamingException
+ {
+ return helper.getInitialContext();
+ }
+
+ public String getServerHost()
+ {
+ return helper.getServerHost();
+ }
+
+ public OSGiBundle getBundle(String symbolicName, Version version)
+ {
+ OSGiBundle bundle = getBundle(symbolicName, version, false);
+ return bundle;
+ }
+
+ public OSGiServiceReference getServiceReference(String clazz, long timeout)
+ {
+ int fraktion = 200;
+ timeout = timeout / fraktion;
+ OSGiServiceReference sref = getServiceReference(clazz);
+ while (sref == null && 0 < timeout--)
+ {
+ try
+ {
+ Thread.sleep(fraktion);
+ }
+ catch (InterruptedException e)
+ {
+ // ignore
+ }
+ sref = getServiceReference(clazz);
+ }
+ return sref;
+ }
+
+ OSGiBundle getBundle(String symbolicName, Version version, boolean mustExist)
+ {
+ OSGiBundle bundle = null;
+ List<OSGiBundle> bundles = Arrays.asList(getBundles());
+ for (OSGiBundle aux : bundles)
+ {
+ if (aux.getSymbolicName().equals(symbolicName))
+ {
+ if (version == null || version.equals(aux.getVersion()))
+ {
+ bundle = aux;
+ break;
+ }
+ }
+ }
+
+ if (bundle == null && mustExist == true)
+ throw new IllegalStateException("Cannot obtain bundle: " +
symbolicName + "-" + version + ". We have " + bundles);
+
+ return bundle;
+ }
+
+ String getManifestEntry(String location, String key)
+ {
+ Manifest manifest = getManifest(location);
+ Attributes attribs = manifest.getMainAttributes();
+ String value = attribs.getValue(key);
+ return value;
+ }
+
+ Manifest getManifest(String location)
+ {
+ Manifest manifest;
+ try
+ {
+ File archiveFile = getTestHelper().getTestArchiveFile(location);
+ JarFile jarFile = new JarFile(archiveFile);
+ manifest = jarFile.getManifest();
+ jarFile.close();
+ }
+ catch (IOException ex)
+ {
+ throw new IllegalStateException("Cannot get manifest from: " +
location);
+
+ }
+ return manifest;
+ }
+
+ OSGiBundle registerBundle(String location, OSGiBundle bundle)
+ {
+ if (bundle == null)
+ throw new IllegalArgumentException("Cannot register null bundle for: "
+ location);
+
+ bundles.put(location, bundle);
+ return bundle;
+ }
+
+ void unregisterBundle(OSGiBundle bundle)
+ {
+ if (bundle == null)
+ throw new IllegalArgumentException("Cannot unregister null bundle");
+
+ if (bundles.containsValue(bundle))
+ {
+ Set<Entry<String, OSGiBundle>> entrySet = bundles.entrySet();
+ for (Entry<String, OSGiBundle> entry : entrySet)
+ {
+ if (bundle.equals(entry.getValue()))
+ {
+ String key = entry.getKey();
+ bundles.remove(key);
+ break;
+ }
+ }
+ }
+ }
+
+ VirtualFile toVirtualFile(Archive<?> archive) throws IOException,
MalformedURLException
+ {
+ ZipExporter exporter = archive.as(ZipExporter.class);
+ File target = File.createTempFile("archive_", ".jar");
+ exporter.exportZip(target, true);
+ target.deleteOnExit();
+
+ return AbstractVFS.getRoot(target.toURI().toURL());
+ }
+
+ private void failsafeStop(OSGiBundle bundle)
+ {
+ if (bundle != null)
+ {
+ try
+ {
+ bundle.stop();
+ }
+ catch (Exception ex)
+ {
+ log.warn("Cannot stop bundle: " + bundle, ex);
+ }
+ }
+ }
+
+ private void failsafeUninstall(OSGiBundle bundle)
+ {
+ if (bundle != null)
+ {
+ try
+ {
+ if (bundle.getState() != Bundle.UNINSTALLED)
+ bundle.uninstall();
+ }
+ catch (Exception ex)
+ {
+ log.warn("Cannot uninstall bundle: " + bundle, ex);
+ }
+ }
+ }
+}
Deleted:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java
===================================================================
---
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java 2010-03-09
19:47:32 UTC (rev 102159)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -1,225 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.osgi.testing.internal;
-
-// $Id$
-
-import java.io.IOException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
-import javax.management.MBeanServerConnection;
-import javax.management.ObjectName;
-import javax.management.openmbean.CompositeData;
-import javax.management.openmbean.TabularData;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.jboss.logging.Logger;
-import org.jboss.osgi.jmx.MBeanProxy;
-import org.jboss.osgi.jmx.ObjectNameFactory;
-import org.jboss.osgi.jmx.ServiceStateMBeanExt;
-import org.jboss.osgi.spi.util.BundleInfo;
-import org.jboss.osgi.testing.OSGiBundle;
-import org.jboss.osgi.testing.OSGiRuntime;
-import org.jboss.osgi.testing.OSGiServiceReference;
-import org.jboss.osgi.testing.OSGiTestHelper;
-import org.osgi.framework.BundleException;
-import org.osgi.jmx.framework.BundleStateMBean;
-import org.osgi.jmx.framework.ServiceStateMBean;
-
-/**
- * A remote implementation of the {@link OSGiRuntime}
- *
- * @author Thomas.Diesler(a)jboss.org
- * @since 25-Sep-2008
- */
-public class RemoteRuntime extends OSGiRuntimeImpl
-{
- // Provide logging
- private static final Logger log = Logger.getLogger(RemoteRuntime.class);
-
- private MBeanServerConnection mbeanServer;
-
- public RemoteRuntime(OSGiTestHelper helper)
- {
- super(helper);
- }
-
- OSGiBundle installBundle(BundleInfo info) throws BundleException
- {
- try
- {
- String location = fixVirtualFileURL(info.getRootURL()).toExternalForm();
- long bundleId = getFrameworkMBean().installBundle(location);
- RemoteBundle bundle = new RemoteBundle(this, bundleId);
- return registerBundle(bundle.getLocation(), bundle);
- }
- catch (RuntimeException rte)
- {
- throw rte;
- }
- catch (Exception ex)
- {
- throw new BundleException("Cannot install: " + info, ex);
- }
- }
-
- public void deploy(String location) throws Exception
- {
- URL archiveURL = getTestHelper().getTestArchiveURL(location);
- MainDeployerClient deployer = new MainDeployerClient(this);
- deployer.deploy(archiveURL);
- }
-
- public void undeploy(String location) throws Exception
- {
- URL archiveURL = getTestHelper().getTestArchiveURL(location);
- MainDeployerClient deployer = new MainDeployerClient(this);
- deployer.undeploy(archiveURL);
- }
-
- public OSGiBundle[] getBundles()
- {
- Set<OSGiBundle> bundles = new HashSet<OSGiBundle>();
- try
- {
- TabularData listBundles = getBundleStateMBean().listBundles();
- Iterator<?> iterator = listBundles.values().iterator();
- while (iterator.hasNext())
- {
- CompositeData bundleType = (CompositeData)iterator.next();
- Long bundleId = (Long)bundleType.get(BundleStateMBean.IDENTIFIER);
- try
- {
- bundles.add(new RemoteBundle(this, bundleId));
- }
- catch (IOException ex)
- {
- log.warn("Cannot initialize remote bundle: [" + bundleId +
"] - " + ex.getMessage());
- }
- }
- OSGiBundle[] bundleArr = new OSGiBundle[bundles.size()];
- bundles.toArray(bundleArr);
- return bundleArr;
- }
- catch (RuntimeException rte)
- {
- throw rte;
- }
- catch (Exception ex)
- {
- throw new IllegalStateException("Cannot obtain remote bundles", ex);
- }
- }
-
- public OSGiBundle getBundle(long bundleId)
- {
- for (OSGiBundle bundle : getBundles())
- {
- if (bundleId == bundle.getBundleId())
- return bundle;
- }
- return null;
- }
-
- public OSGiServiceReference getServiceReference(String clazz)
- {
- CompositeData serviceData;
- TabularData propertiesData;
- try
- {
- ServiceStateMBeanExt serviceState = getServiceStateMBeanExt();
- serviceData = serviceState.getService(clazz);
- if (serviceData == null)
- return null;
-
- Long serviceId = (Long)serviceData.get(ServiceStateMBean.IDENTIFIER);
- propertiesData = serviceState.getProperties(serviceId);
- }
- catch (IOException ex)
- {
- throw new IllegalStateException(ex);
- }
- return new RemoteServiceReference(serviceData, propertiesData);
- }
-
- @SuppressWarnings("unchecked")
- public OSGiServiceReference[] getServiceReferences(String clazz, String filter)
- {
- TabularData servicesData;
- List<OSGiServiceReference> srefs;
- try
- {
- ServiceStateMBeanExt serviceState = getServiceStateMBeanExt();
- servicesData = serviceState.getServices(clazz, filter);
- if (servicesData == null)
- return null;
-
- srefs = new ArrayList<OSGiServiceReference>();
- for (CompositeData serviceData :
(Collection<CompositeData>)servicesData.values())
- {
- Long serviceId = (Long)serviceData.get(ServiceStateMBean.IDENTIFIER);
- TabularData propertiesData = serviceState.getProperties(serviceId);
- srefs.add(new RemoteServiceReference(serviceData, propertiesData));
- }
- }
- catch (IOException ex)
- {
- throw new IllegalStateException(ex);
- }
- return srefs.toArray(new OSGiServiceReference[servicesData.size()]);
- }
-
- private ServiceStateMBeanExt getServiceStateMBeanExt()
- {
- ObjectName objectName = ObjectNameFactory.create(ServiceStateMBeanExt.OBJECTNAME);
- return MBeanProxy.get(getMBeanServer(), objectName, ServiceStateMBeanExt.class);
- }
-
- public MBeanServerConnection getMBeanServer()
- {
- if (mbeanServer == null)
- {
- try
- {
- InitialContext iniCtx = getInitialContext();
- mbeanServer =
(MBeanServerConnection)iniCtx.lookup("jmx/invoker/RMIAdaptor");
- }
- catch (NamingException ex)
- {
- throw new IllegalStateException("Cannot obtain
MBeanServerConnection", ex);
- }
- }
- return mbeanServer;
- }
-
- public boolean isRemoteRuntime()
- {
- return true;
- }
-}
Copied:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java
(from rev 102342,
projects/jboss-osgi/projects/testing/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java)
===================================================================
---
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java
(rev 0)
+++
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteRuntime.java 2010-03-16
23:44:55 UTC (rev 102491)
@@ -0,0 +1,226 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.osgi.testing.internal;
+
+// $Id$
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.TabularData;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.logging.Logger;
+import org.jboss.osgi.jmx.MBeanProxy;
+import org.jboss.osgi.jmx.ObjectNameFactory;
+import org.jboss.osgi.jmx.ServiceStateMBeanExt;
+import org.jboss.osgi.spi.util.BundleInfo;
+import org.jboss.osgi.testing.OSGiBundle;
+import org.jboss.osgi.testing.OSGiRuntime;
+import org.jboss.osgi.testing.OSGiServiceReference;
+import org.jboss.osgi.testing.OSGiRuntimeHelper;
+import org.osgi.framework.BundleException;
+import org.osgi.jmx.framework.BundleStateMBean;
+import org.osgi.jmx.framework.ServiceStateMBean;
+
+/**
+ * A remote implementation of the {@link OSGiRuntime}
+ *
+ * @author Thomas.Diesler(a)jboss.org
+ * @since 25-Sep-2008
+ */
+public class RemoteRuntime extends OSGiRuntimeImpl
+{
+ // Provide logging
+ private static final Logger log = Logger.getLogger(RemoteRuntime.class);
+
+ private MBeanServerConnection mbeanServer;
+
+ public RemoteRuntime(OSGiRuntimeHelper helper)
+ {
+ super(helper);
+ }
+
+ OSGiBundle installBundle(BundleInfo info) throws BundleException
+ {
+ try
+ {
+ String location = info.getLocation();
+ String streamURL = info.getRoot().getStreamURL().toExternalForm();
+ long bundleId = getFrameworkMBean().installBundle(location, streamURL);
+ RemoteBundle bundle = new RemoteBundle(this, bundleId);
+ return registerBundle(bundle.getLocation(), bundle);
+ }
+ catch (RuntimeException rte)
+ {
+ throw rte;
+ }
+ catch (Exception ex)
+ {
+ throw new BundleException("Cannot install: " + info, ex);
+ }
+ }
+
+ public void deploy(String location) throws Exception
+ {
+ URL archiveURL = getTestHelper().getTestArchiveURL(location);
+ MainDeployerClient deployer = new MainDeployerClient(this);
+ deployer.deploy(archiveURL);
+ }
+
+ public void undeploy(String location) throws Exception
+ {
+ URL archiveURL = getTestHelper().getTestArchiveURL(location);
+ MainDeployerClient deployer = new MainDeployerClient(this);
+ deployer.undeploy(archiveURL);
+ }
+
+ public OSGiBundle[] getBundles()
+ {
+ Set<OSGiBundle> bundles = new HashSet<OSGiBundle>();
+ try
+ {
+ TabularData listBundles = getBundleStateMBean().listBundles();
+ Iterator<?> iterator = listBundles.values().iterator();
+ while (iterator.hasNext())
+ {
+ CompositeData bundleType = (CompositeData)iterator.next();
+ Long bundleId = (Long)bundleType.get(BundleStateMBean.IDENTIFIER);
+ try
+ {
+ bundles.add(new RemoteBundle(this, bundleId));
+ }
+ catch (IOException ex)
+ {
+ log.warn("Cannot initialize remote bundle: [" + bundleId +
"] - " + ex.getMessage());
+ }
+ }
+ OSGiBundle[] bundleArr = new OSGiBundle[bundles.size()];
+ bundles.toArray(bundleArr);
+ return bundleArr;
+ }
+ catch (RuntimeException rte)
+ {
+ throw rte;
+ }
+ catch (Exception ex)
+ {
+ throw new IllegalStateException("Cannot obtain remote bundles", ex);
+ }
+ }
+
+ public OSGiBundle getBundle(long bundleId)
+ {
+ for (OSGiBundle bundle : getBundles())
+ {
+ if (bundleId == bundle.getBundleId())
+ return bundle;
+ }
+ return null;
+ }
+
+ public OSGiServiceReference getServiceReference(String clazz)
+ {
+ CompositeData serviceData;
+ TabularData propertiesData;
+ try
+ {
+ ServiceStateMBeanExt serviceState = getServiceStateMBeanExt();
+ serviceData = serviceState.getService(clazz);
+ if (serviceData == null)
+ return null;
+
+ Long serviceId = (Long)serviceData.get(ServiceStateMBean.IDENTIFIER);
+ propertiesData = serviceState.getProperties(serviceId);
+ }
+ catch (IOException ex)
+ {
+ throw new IllegalStateException(ex);
+ }
+ return new RemoteServiceReference(serviceData, propertiesData);
+ }
+
+ @SuppressWarnings("unchecked")
+ public OSGiServiceReference[] getServiceReferences(String clazz, String filter)
+ {
+ TabularData servicesData;
+ List<OSGiServiceReference> srefs;
+ try
+ {
+ ServiceStateMBeanExt serviceState = getServiceStateMBeanExt();
+ servicesData = serviceState.getServices(clazz, filter);
+ if (servicesData == null)
+ return null;
+
+ srefs = new ArrayList<OSGiServiceReference>();
+ for (CompositeData serviceData :
(Collection<CompositeData>)servicesData.values())
+ {
+ Long serviceId = (Long)serviceData.get(ServiceStateMBean.IDENTIFIER);
+ TabularData propertiesData = serviceState.getProperties(serviceId);
+ srefs.add(new RemoteServiceReference(serviceData, propertiesData));
+ }
+ }
+ catch (IOException ex)
+ {
+ throw new IllegalStateException(ex);
+ }
+ return srefs.toArray(new OSGiServiceReference[servicesData.size()]);
+ }
+
+ private ServiceStateMBeanExt getServiceStateMBeanExt()
+ {
+ ObjectName objectName = ObjectNameFactory.create(ServiceStateMBeanExt.OBJECTNAME);
+ return MBeanProxy.get(getMBeanServer(), objectName, ServiceStateMBeanExt.class);
+ }
+
+ public MBeanServerConnection getMBeanServer()
+ {
+ if (mbeanServer == null)
+ {
+ try
+ {
+ InitialContext iniCtx = getInitialContext();
+ mbeanServer =
(MBeanServerConnection)iniCtx.lookup("jmx/invoker/RMIAdaptor");
+ }
+ catch (NamingException ex)
+ {
+ throw new IllegalStateException("Cannot obtain
MBeanServerConnection", ex);
+ }
+ }
+ return mbeanServer;
+ }
+
+ public boolean isRemoteRuntime()
+ {
+ return true;
+ }
+}