This commit seems to be causing problems in continuum and elsewhere,  but
not in my env.  See comments in the JIRA (1293) Investigating.


On 12/02/2008, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> Author: kelvingoodson
> Date: Tue Feb 12 03:13:53 2008
> New Revision: 620763
>
> URL: http://svn.apache.org/viewvc?rev=620763&view=rev
> Log:
> Fix for TUSCANY-1293
>
> Added:
>
>     incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/
>     
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/SdoBundleActivator.java
> (with props)
>
>     
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/
>     
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/ClassLoaderTestCase.java
> (with props)
>     
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/OSGiTestCase.java
> (with props)
>     
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/TestBundleActivator.java
> (with props)
>     incubator/tuscany/java/sdo/impl/src/test/resources/osgi/
>     incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.impl.mf
>
>     incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.osgi.test.mf
> Modified:
>     incubator/tuscany/java/sdo/impl/pom.xml
>
>     incubator/tuscany/java/sdo/impl/src/main/resources/META-INF/MANIFEST.MF
>     incubator/tuscany/java/sdo/lib/src/main/resources/META-INF/MANIFEST.MF
>     incubator/tuscany/java/sdo/sdo-api/pom.xml
>
>     
> incubator/tuscany/java/sdo/sdo-api/src/main/java/commonj/sdo/impl/HelperProvider.java
>
> Modified: incubator/tuscany/java/sdo/impl/pom.xml
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/pom.xml?rev=620763&r1=620762&r2=620763&view=diff
>
> ==============================================================================
> --- incubator/tuscany/java/sdo/impl/pom.xml (original)
> +++ incubator/tuscany/java/sdo/impl/pom.xml Tue Feb 12 03:13:53 2008
> @@ -103,6 +103,13 @@
>              <version>3.2.1</version>
>          </dependency>
>
> +        <!-- dependencies for OSGi support -->
> +        <dependency>
> +            <groupId>org.apache.felix</groupId>
> +            <artifactId>org.apache.felix.main</artifactId>
> +            <version>1.0.1</version>
> +        </dependency>
> +
>         <!-- dependencies for test cases -->
>          <dependency>
>              <groupId>junit</groupId>
>
> Added:
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/SdoBundleActivator.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/SdoBundleActivator.java?rev=620763&view=auto
>
> ==============================================================================
> ---
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/SdoBundleActivator.java
> (added)
> +++
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/SdoBundleActivator.java
> Tue Feb 12 03:13:53 2008
> @@ -0,0 +1,22 @@
> +package org.apache.tuscany.sdo.osgi;
> +
> +import org.osgi.framework.BundleActivator;
> +import org.osgi.framework.BundleContext;
> +
> +import commonj.sdo.impl.HelperProvider;
> +
> +public class SdoBundleActivator implements BundleActivator {
> +
> +
> +       public void start(BundleContext bundleContext) throws Exception {
> +
> +               HelperProvider.setDefaultInstance(this.getClass
> ().getClassLoader());
> +
> +       }
> +
> +       public void stop(BundleContext bundleContext) throws Exception {
> +
> +       }
> +
> +
> +}
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/SdoBundleActivator.java
>
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/osgi/SdoBundleActivator.java
>
> ------------------------------------------------------------------------------
>     svn:keywords = Rev Date
>
> Modified:
> incubator/tuscany/java/sdo/impl/src/main/resources/META-INF/MANIFEST.MF
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/resources/META-INF/MANIFEST.MF?rev=620763&r1=620762&r2=620763&view=diff
>
> ==============================================================================
> ---
> incubator/tuscany/java/sdo/impl/src/main/resources/META-INF/MANIFEST.MF
> (original)
> +++
> incubator/tuscany/java/sdo/impl/src/main/resources/META-INF/MANIFEST.MF Tue
> Feb 12 03:13:53 2008
> @@ -11,15 +11,19 @@
> Bundle-SymbolicName: org.apache.tuscany.sdo.impl
> Bundle-Version: 1.0.0
> Bundle-Vendor: Apache Software Foundation
> -Require-Bundle: org.eclipse.emf.common,
> +Require-Bundle-FIXME: org.eclipse.emf.common,
>   org.eclipse.emf.ecore,
>   org.eclipse.emf.ecore.change,
>   org.eclipse.emf.ecore.xmi,
>   org.eclipse.xsd,
>   org.apache.tuscany.sdo.spec;visibility:=reexport
> +Import-Package: org.osgi.framework
> Export-Package: commonj.sdo.impl,
>   org.apache.tuscany.sdo,
>   org.apache.tuscany.sdo.helper,
>   org.apache.tuscany.sdo.impl,
>   org.apache.tuscany.sdo.test,
> + org.apache.tuscany.sdo.codegen,
> + org.apache.tuscany.sdo.model,
>   org.apache.tuscany.sdo.util
> +Bundle-Activator: org.apache.tuscany.sdo.osgi.SdoBundleActivator
>
> Added:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/ClassLoaderTestCase.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/ClassLoaderTestCase.java?rev=620763&view=auto
>
> ==============================================================================
> ---
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/ClassLoaderTestCase.java
> (added)
> +++
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/ClassLoaderTestCase.java
> Tue Feb 12 03:13:53 2008
> @@ -0,0 +1,192 @@
> +/**
> + *
> + *  Licensed to the Apache Software Foundation (ASF) under one
> + *  or more contributor license agreements.  See the NOTICE file
> + *  distributed with this work for additional information
> + *  regarding copyright ownership.  The ASF licenses this file
> + *  to you under the Apache License, Version 2.0 (the
> + *  "License"); you may not use this file except in compliance
> + *  with the License.  You may obtain a copy of the License at
> + *
> + *    http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing,
> + *  software distributed under the License is distributed on an
> + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> + *  KIND, either express or implied.  See the License for the
> + *  specific language governing permissions and limitations
> + *  under the License.
> + */
> +package org.apache.tuscany.sdo.test.osgi;
> +
> +
> +import java.io.File;
> +import java.io.IOException;
> +import java.lang.reflect.Method;
> +import java.net.URL;
> +import java.net.URLClassLoader;
> +import java.util.HashSet;
> +
> +import org.apache.tuscany.sdo.test.AllTests;
> +import commonj.sdo.impl.HelperProvider;
> +
> +import junit.framework.Assert;
> +import junit.framework.TestCase;
> +import junit.framework.TestResult;
> +import junit.framework.TestSuite;
> +
> +/*
> + * This test runs the SDO implementation test suite in a
> multi-classloader environment
> + */
> +public class ClassLoaderTestCase extends TestCase {
> +
> +       private ClassLoader contextClassLoader;
> +
> +       protected void setUp() throws Exception {
> +
> +               contextClassLoader = Thread.currentThread
> ().getContextClassLoader();
> +               super.setUp();
> +       }
> +
> +       protected void tearDown() throws Exception {
> +               super.tearDown();
> +               Thread.currentThread
> ().setContextClassLoader(contextClassLoader);
> +       }
> +
> +       // Load the test class using a separate test classloader which
> +       // loads SDO API, LIB and IMPL using different classloaders
> +       // Run the SDO test suite under this multi-classloader environment
> +       // Third party libraries and test classes/resources are available
> +       // on the thread context classloader when this test is run
> +       public void test() throws Exception {
> +               ClassLoader testClassLoader = createTestClassLoader();
> +
> +               Class testClass = testClassLoader.loadClass(this.getClass
> ().getName());
> +               Method testMethod = testClass.getMethod("runSDOTest",
> null);
> +               Object testObj = testClass.newInstance();
> +               testMethod.invoke(testObj, null);
> +
> +
> +       }
> +
> +
> +       public void runSDOTest() throws Exception {
> +
> +               HelperProvider.setDefaultInstance(this.getClass
> ().getClassLoader());
> +
> +               TestSuite allTests = AllTests.suite();
> +               TestResult testResult = new TestResult();
> +               allTests.run(testResult);
> +               Assert.assertEquals(0, testResult.errorCount());
> +    }
> +
> +
> +       // Get all the URLs for a classloader, remove these from
> dependentJars
> +       private URL[] getClassLoaderURLs(
> +                       URL[] classPathEntries,
> +                       HashSet dependentJars,
> +                       String[] jarList)
> +       throws IOException {
> +
> +               String pathSeparator = "/";
> +       HashSet classPathEntrySet;
> +
> +        classPathEntrySet = new HashSet();
> +
> +        for (int i = 0; i < classPathEntries.length; i++) {
> +
> +               URL classPathEntry = classPathEntries[i];
> +               String classPathEntryStr = classPathEntry.getPath();
> +               if (jarList != null) {
> +                       for (int k = 0; k < jarList.length; k++) {
> +                               String jarName = "tuscany-" + jarList[k];
> +                           String alternateJarName = "tuscany-sdo-" +
> jarList[k];
> +                           String folderName = pathSeparator + jarList[k]
> + pathSeparator;
> +                               if (classPathEntryStr.indexOf(jarName) >=
> 0 ||
> +                                               
> classPathEntryStr.indexOf(alternateJarName)
> >=0 ||
> +                                               
> classPathEntryStr.indexOf(folderName)
> >=0) {
> +
> +                                       classPathEntrySet.add
> (classPathEntry);
> +                                       dependentJars.remove
> (classPathEntry);
> +                               }
> +                       }
> +               }
> +        }
> +        return (URL [])classPathEntrySet.toArray(new URL[
> classPathEntrySet.size()]);
> +       }
> +
> +
> +       // Create the test classloader. It uses separate classloaders to
> load SDO API, SDO LIB,
> +       // SDO IMPL and 3rd part libraries
> +       private ClassLoader createTestClassLoader() throws Exception {
> +
> +               String[] sdoApiJars  = {"sdo-api"};
> +               String[] sdoLibJars  = {"lib"};
> +               String[] sdoImplJars = {"impl"};
> +
> +               URL[] sdoApiUrls;
> +               URL[] sdoLibUrls;
> +               URL[] sdoImplUrls;
> +               URL[] dependencyUrls;
> +
> +               if (!(this.getClass().getClassLoader() instanceof
> URLClassLoader))
> +                       return this.getClass().getClassLoader();
> +
> +               HashSet dependentJars = new HashSet();
> +               URL[] classPathEntries =
> ((URLClassLoader)this.getClass().getClassLoader()).getURLs();
> +               for (int i = 0; i < classPathEntries.length; i++) {
> +               dependentJars.add(classPathEntries[i]);
> +               }
> +               sdoApiUrls = getClassLoaderURLs(classPathEntries,
> dependentJars, sdoApiJars);
> +               sdoLibUrls = getClassLoaderURLs(classPathEntries,
> dependentJars, sdoLibJars);
> +               sdoImplUrls = getClassLoaderURLs(classPathEntries,
> dependentJars, sdoImplJars);
> +               dependencyUrls = (URL [])dependentJars.toArray(new URL[
> dependentJars.size()]);
> +
> +           ClassLoader dependencyLoader = new
> URLClassLoader(dependencyUrls, null);
> +           ClassLoader sdoApiLoader = new URLClassLoader(sdoApiUrls,
> dependencyLoader);
> +           ClassLoader sdoLibClassLoader = new URLClassLoader(sdoLibUrls,
> sdoApiLoader);
> +           ClassLoader sdoImplClassLoader = new
> URLClassLoader(sdoImplUrls, sdoLibClassLoader);
> +
> +
> +           TestClassLoader testClassLoader = new TestClassLoader(
> +                       new ClassLoader[] {sdoApiLoader,
> sdoLibClassLoader, sdoImplClassLoader, dependencyLoader}
> +                       );
> +
> +
> +           // Test classes and 3rd party libraries should be on the
> context classloader
> +           URL testUrl = new File("./target/test-classes").toURL();
> +           ClassLoader contextClassLoader = new URLClassLoader(new
> URL[]{testUrl}, dependencyLoader);
> +           Thread.currentThread
> ().setContextClassLoader(contextClassLoader);
> +
> +           return testClassLoader;
> +       }
> +
> +
> +
> +       private class TestClassLoader extends ClassLoader {
> +
> +               ClassLoader[] parentLoaders;
> +
> +               private TestClassLoader(ClassLoader[] parentLoaders) {
> +                       this.parentLoaders = parentLoaders;
> +               }
> +
> +               public Class loadClass(String className) throws
> ClassNotFoundException {
> +
> +                       Class clazz = findLoadedClass(className);
> +                       if (clazz != null)
> +                               return clazz;
> +
> +                       for (int i = 0; i < parentLoaders.length; i++) {
> +                               try {
> +                                       return
> parentLoaders[i].loadClass(className);
> +                               } catch (Exception e) {
> +                                 // speculative load using parent class
> loader failed,  but that's OK
> +                               }
> +                       }
> +                       return super.loadClass(className);
> +               }
> +
> +
> +       }
> +}
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/ClassLoaderTestCase.java
>
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/ClassLoaderTestCase.java
>
> ------------------------------------------------------------------------------
>     svn:keywords = Rev Date
>
> Added:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/OSGiTestCase.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/OSGiTestCase.java?rev=620763&view=auto
>
> ==============================================================================
> ---
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/OSGiTestCase.java
> (added)
> +++
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/OSGiTestCase.java
> Tue Feb 12 03:13:53 2008
> @@ -0,0 +1,353 @@
> +/**
> + *
> + *  Licensed to the Apache Software Foundation (ASF) under one
> + *  or more contributor license agreements.  See the NOTICE file
> + *  distributed with this work for additional information
> + *  regarding copyright ownership.  The ASF licenses this file
> + *  to you under the Apache License, Version 2.0 (the
> + *  "License"); you may not use this file except in compliance
> + *  with the License.  You may obtain a copy of the License at
> + *
> + *    http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing,
> + *  software distributed under the License is distributed on an
> + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> + *  KIND, either express or implied.  See the License for the
> + *  specific language governing permissions and limitations
> + *  under the License.
> + */
> +package org.apache.tuscany.sdo.test.osgi;
> +
> +import java.io.ByteArrayInputStream;
> +import java.io.ByteArrayOutputStream;
> +import java.io.File;
> +import java.io.FileInputStream;
> +import java.io.FilenameFilter;
> +import java.net.URL;
> +import java.net.URLClassLoader;
> +import java.util.ArrayList;
> +import java.util.List;
> +import java.util.Properties;
> +import java.util.jar.Attributes;
> +import java.util.jar.JarEntry;
> +import java.util.jar.JarInputStream;
> +import java.util.jar.JarOutputStream;
> +import java.util.jar.Manifest;
> +import java.util.zip.ZipEntry;
> +
> +import org.apache.felix.framework.Felix;
> +import org.apache.felix.main.Main;
> +import org.osgi.framework.Bundle;
> +import org.osgi.framework.BundleContext;
> +
> +import junit.framework.TestCase;
> +
> +/*
> + * This test runs the SDO implementation test suite under a Felix OSGi
> runtime
> + */
> +public class OSGiTestCase extends TestCase {
> +
> +  private Felix felix;
> +  private BundleContext bundleContext;
> +  private ClassLoader contextClassLoader;
> +
> +  protected void setUp() throws Exception {
> +
> +    contextClassLoader = Thread.currentThread().getContextClassLoader();
> +
> +    super.setUp();
> +
> +    // Start a Felix OSGi runtime
> +    File profileDir = new File(".felix");
> +    if (profileDir.isDirectory())
> +      deleteDirectory(profileDir);
> +    else
> +      profileDir.delete();
> +    profileDir.mkdir();
> +
> +    Properties props = Main.loadConfigProperties();
> +    props.put("felix.cache.profiledir", profileDir.getAbsolutePath());
> +    props.put("felix.embedded.execution", "true");
> +    props.put("org.osgi.framework.system.packages",
> +        "org.osgi.framework; version=1.3.0,"
> +            + "org.osgi.service.packageadmin; version=1.2.0, "
> +            + "org.osgi.service.startlevel; version=1.0.0, "
> +            + "org.osgi.service.url; version=1.0.0, " + "javax.xml, "
> +            + "javax.xml.parsers, " + "org.xml.sax, " + "
> org.xml.sax.helpers, "
> +            + "org.w3c.dom, " + "org.w3c.dom.events, " + "
> javax.xml.stream, "
> +            + "javax.xml.transform, " + "javax.xml.transform.dom, "
> +            + "javax.xml.transform.stream, " + "org.objectweb.asm, "
> +            + "junit.framework");
> +
> +    List activators = new ArrayList();
> +    Felix felix = new Felix(props, activators);
> +    felix.start();
> +    bundleContext = felix.getBundleContext();
> +  }
> +
> +  protected void tearDown() throws Exception {
> +
> +    super.tearDown();
> +    Thread.currentThread().setContextClassLoader(contextClassLoader);
> +
> +    if (felix != null) {
> +      felix.stop();
> +      felix = null;
> +    }
> +  }
> +
> +  // Install SDO spec, SDO lib, SDO impl, and its EMF dependencies
> +  // Create a test bundle containing all the SDO tests, and run the
> entire
> +  // test suite inside an OSGi container
> +  public void test() throws Exception {
> +
> +    ArrayList bundles = new ArrayList();
> +    FilenameFilter jarFileFilter = new JarFileFilter();
> +
> +    File apiDir = new File("../sdo-api/target");
> +    File[] apiJars = apiDir.listFiles(jarFileFilter);
> +    for (int i = 0; i < apiJars.length; i++) {
> +      Bundle bundle = bundleContext
> +          .installBundle(apiJars[i].toURL().toString());
> +      bundles.add(bundle);
> +    }
> +
> +    File libDir = new File("../lib/target");
> +    File[] libJars = libDir.listFiles(jarFileFilter);
> +    for (int i = 0; i < libJars.length; i++) {
> +      Bundle bundle = bundleContext
> +          .installBundle(libJars[i].toURL().toString());
> +      bundles.add(bundle);
> +    }
> +
> +    if (!(contextClassLoader instanceof URLClassLoader))
> +      return;
> +
> +    URL[] classPathURLs = ((URLClassLoader)
> contextClassLoader).getURLs();
> +    for (int i = 0; i < classPathURLs.length; i++) {
> +      String url = classPathURLs[i].toString();
> +      if (url.indexOf("eclipse") > 0 && url.endsWith(".jar")) {
> +        Bundle bundle = installEclipseBundle(classPathURLs[i]);
> +        if (bundle != null)
> +          bundles.add(bundle);
> +      }
> +    }
> +
> +    // When this test is run during the build, sdo.impl.jar would not yet
> have
> +    // been created
> +    // Create this bundle - use the manifest file provided in the test
> +    // directory, which works
> +    // with Felix.
> +    Bundle implBundle = installBundle("file:sdo.impl",
> +        "../impl/target/test-classes/osgi/sdo.impl.mf",
> +        new String[] { "../impl/target/classes" });
> +    bundles.add(implBundle);
> +
> +    // Start all the installed bundles
> +    for (int i = 0; i < bundles.size(); i++) {
> +      Bundle bundle = (Bundle) bundles.get(i);
> +      try {
> +        bundle.start();
> +      } catch (Exception e) {
> +        e.printStackTrace();
> +        System.out.println("Could not start bundle " + bundle);
> +        // don't stop on first failure, so we document all failures
> +        // any failure will be re-triggered by the testBundle.start()
> call below
> +      }
> +    }
> +
> +    // Install the test bundle - it contains all the test classes. The
> bundle
> +    // activator for
> +    // this class runs the entire test suite
> +    Bundle testBundle = installBundle("file:sdo.osgi.test",
> +        "../impl/target/test-classes/osgi/sdo.osgi.test.mf",
> +        new String[] { "../impl/target/test-classes" });
> +
> +    TestClassLoader testClassLoader = new TestClassLoader(testBundle,
> +        contextClassLoader);
> +    Thread.currentThread().setContextClassLoader(testClassLoader);
> +
> +    // The test suite is run inside an OSGi container by this call.
> +    testBundle.start();
> +  }
> +
> +  // Delete any old Felix configuration files left over from previous
> runs
> +  private static void deleteDirectory(File dir) {
> +
> +    File[] files = dir.listFiles();
> +    for (int i = 0; i < files.length; i++) {
> +      if (files[i].isDirectory())
> +        deleteDirectory(files[i]);
> +      else
> +        files[i].delete();
> +    }
> +    dir.delete();
> +
> +  }
> +
> +  // Create and install a bundle with the specified manifest file
> +  // The bundle contains all files from the list of directories specified
> +  public Bundle installBundle(String bundleLocation, String
> manifestFileName,
> +      String[] dirNames) throws Exception {
> +
> +    ByteArrayOutputStream out = new ByteArrayOutputStream();
> +
> +    File manifestFile = new File(manifestFileName);
> +    Manifest manifest = new Manifest();
> +    manifest.read(new FileInputStream(manifestFile));
> +
> +    JarOutputStream jarOut = new JarOutputStream(out, manifest);
> +
> +    for (int i = 0; i < dirNames.length; i++) {
> +      File dir = new File(dirNames[i]);
> +      addFilesToJar(dir, dirNames[i], jarOut);
> +    }
> +
> +    jarOut.close();
> +    out.close();
> +
> +    ByteArrayInputStream inStream = new ByteArrayInputStream(
> out.toByteArray());
> +    return bundleContext.installBundle(bundleLocation, inStream);
> +
> +  }
> +
> +  // Add all the files from a build directory into a jar file
> +  // This method is used to create bundles on the fly
> +  private void addFilesToJar(File dir, String rootDirName,
> +      JarOutputStream jarOut) throws Exception {
> +
> +    if (dir.getName().equals(".svn"))
> +      return;
> +
> +    File[] files = dir.listFiles();
> +
> +    for (int i = 0; i < files.length; i++) {
> +
> +      if (files[i].isDirectory()) {
> +        addFilesToJar(files[i], rootDirName, jarOut);
> +        continue;
> +      }
> +      if (files[i].getName().endsWith("MANIFEST.MF"))
> +        continue;
> +
> +      String entryName = files[i].getPath().substring(rootDirName.length()
> + 1);
> +      entryName = entryName.replaceAll("\\\\", "/");
> +      ZipEntry ze = new ZipEntry(entryName);
> +
> +      jarOut.putNextEntry(ze);
> +      FileInputStream file = new FileInputStream(files[i]);
> +      byte[] fileContents = new byte[file.available()];
> +      file.read(fileContents);
> +      jarOut.write(fileContents);
> +    }
> +  }
> +
> +  // Install a bundle corresponding to an jar file from Eclipse (eg. EMF
> jars)
> +  // These bundle manifest entries use Require-Bundle of
> +  // eclipse core runtime. The Plugin class from the Eclipse runtime
> +  // is loaded by the bundle activator of some of these bundles.
> +  // Since this test is run under Felix, remove the dependency on
> +  // Eclipse runtime by removing the Require-Bundle and
> +  // Bundle-Activator entries from the manifest
> +  private Bundle installEclipseBundle(URL jarURL) throws Exception {
> +
> +    JarInputStream jarInput = new JarInputStream(jarURL.openStream());
> +
> +    Manifest manifest = jarInput.getManifest();
> +    if (manifest == null) {
> +      manifest = new Manifest();
> +      ZipEntry entry;
> +      while ((entry = jarInput.getNextEntry()) != null) {
> +        if (entry.getName().equals("META-INF/MANIFEST.MF")) {
> +          byte bytes[] = new byte[(int) entry.getSize()];
> +          jarInput.read(bytes);
> +          manifest.read(new ByteArrayInputStream(bytes));
> +        }
> +      }
> +      jarInput.close();
> +      jarInput = new JarInputStream(jarURL.openStream());
> +    }
> +    if (manifest == null
> +        || manifest.getMainAttributes() == null
> +        || !manifest.getMainAttributes().containsKey(
> +            new Attributes.Name("Bundle-SymbolicName"))) {
> +
> +      return null;
> +    }
> +    manifest.getMainAttributes().remove(new Attributes.Name
> ("Require-Bundle"));
> +    manifest.getMainAttributes()
> +        .remove(new Attributes.Name("Bundle-Activator"));
> +    manifest.getMainAttributes().put(
> +        new Attributes.Name("DynamicImport-Package"), "*");
> +
> +    ByteArrayOutputStream out = new ByteArrayOutputStream();
> +
> +    JarOutputStream jarOut = new JarOutputStream(out, manifest);
> +    ZipEntry entry;
> +    byte bytes[] = new byte[1024];
> +    while ((entry = jarInput.getNextEntry()) != null) {
> +      if (!entry.getName().equals("META-INF/MANIFEST.MF")) {
> +        jarOut.putNextEntry((JarEntry) entry);
> +        int len;
> +        while ((len = jarInput.read(bytes)) != -1) {
> +          jarOut.write(bytes, 0, len);
> +        }
> +        jarOut.closeEntry();
> +      }
> +      jarInput.closeEntry();
> +    }
> +    jarOut.close();
> +    out.close();
> +    jarInput.close();
> +
> +    ByteArrayInputStream byteStream = new ByteArrayInputStream(out
> +        .toByteArray());
> +
> +    return bundleContext.installBundle(jarURL.toString(), byteStream);
> +
> +  }
> +
> +  // Filter used to list jar files from a directory
> +  private class JarFileFilter implements FilenameFilter {
> +
> +    public boolean accept(File dir, String name) {
> +      if (name.endsWith(".jar") && !name.endsWith("javadoc.jar"))
> +        return true;
> +      else
> +        return false;
> +    }
> +
> +  }
> +
> +  // Test classloader - used as context classloader
> +  private static class TestClassLoader extends ClassLoader {
> +
> +    Bundle testBundle;
> +
> +    private TestClassLoader(Bundle testBundle, ClassLoader
> parentClassLoader) {
> +      super(parentClassLoader);
> +      this.testBundle = testBundle;
> +    }
> +
> +    public Class loadClass(String className) throws
> ClassNotFoundException {
> +      Class clazz = findLoadedClass(className);
> +      if (clazz != null)
> +        return clazz;
> +
> +      try {
> +        return testBundle.loadClass(className);
> +      } catch (Exception e) {
> +      }
> +      return super.loadClass(className);
> +    }
> +
> +    public URL getResource(String resName) {
> +      URL resource = testBundle.getResource(resName);
> +      if (resource == null)
> +        resource = super.getResource(resName);
> +      return resource;
> +    }
> +
> +  }
> +}
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/OSGiTestCase.java
>
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/OSGiTestCase.java
>
> ------------------------------------------------------------------------------
>     svn:keywords = Rev Date
>
> Added:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/TestBundleActivator.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/TestBundleActivator.java?rev=620763&view=auto
>
> ==============================================================================
> ---
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/TestBundleActivator.java
> (added)
> +++
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/TestBundleActivator.java
> Tue Feb 12 03:13:53 2008
> @@ -0,0 +1,66 @@
> +package org.apache.tuscany.sdo.test.osgi;
> +/**
> + *
> + *  Licensed to the Apache Software Foundation (ASF) under one
> + *  or more contributor license agreements.  See the NOTICE file
> + *  distributed with this work for additional information
> + *  regarding copyright ownership.  The ASF licenses this file
> + *  to you under the Apache License, Version 2.0 (the
> + *  "License"); you may not use this file except in compliance
> + *  with the License.  You may obtain a copy of the License at
> + *
> + *    http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing,
> + *  software distributed under the License is distributed on an
> + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> + *  KIND, either express or implied.  See the License for the
> + *  specific language governing permissions and limitations
> + *  under the License.
> + */
> +import java.util.Enumeration;
> +
> +import junit.framework.Assert;
> +import junit.framework.Test;
> +import junit.framework.TestFailure;
> +import junit.framework.TestResult;
> +
> +import org.apache.tuscany.sdo.AllTests;
> +import org.osgi.framework.BundleActivator;
> +import org.osgi.framework.BundleContext;
> +
> +
> +/*
> + * Bundle activator for running the SDO test suite under OSGi
> + * The SDO test suite is run inside an OSGi container when the bundle is
> started.
> + */
> +public class TestBundleActivator implements BundleActivator {
> +
> +       public void start(BundleContext bundleContext) throws Exception {
> +               runSDOTests();
> +       }
> +
> +       public void stop(BundleContext bundleContext) throws Exception {
> +
> +       }
> +
> +
> +       public void runSDOTests() throws Exception {
> +
> +               Test allTests = AllTests.suite();
> +               TestResult testResult = new TestResult();
> +               allTests.run(testResult);
> +               System.out.println("Runs " + testResult.runCount() + ",
> Errors: "
> +                               + testResult.errorCount());
> +
> +               Enumeration e = testResult.errors();
> +               while (e.hasMoreElements()) {
> +                       TestFailure f = (TestFailure) e.nextElement();
> +                       System.out.println(f);
> +                       System.out.println(f.exceptionMessage());
> +               }
> +
> +               Assert.assertEquals(0, testResult.errorCount());
> +
> +       }
> +}
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/TestBundleActivator.java
>
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange:
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/osgi/TestBundleActivator.java
>
> ------------------------------------------------------------------------------
>     svn:keywords = Rev Date
>
> Added: incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.impl.mf
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.impl.mf?rev=620763&view=auto
>
> ==============================================================================
> --- incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.impl.mf
> (added)
> +++ incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.impl.mf
> Tue Feb 12 03:13:53 2008
> @@ -0,0 +1,42 @@
> +Manifest-Version: 1.0
> +Extension-Name: tuscany-sdo-impl
> +Specification-Title: Tuscany SDO Implementation
> +Specification-Vendor: Apache Software Foundation
> +Implementation-Vendor: Apache Software Foundation
> +Implementation-Vendor-Id: org.apache
> +Implementation-Title: tuscany-sdo-impl
> +Implementation-Version: incubating-M3
> +Bundle-ManifestVersion: 2
> +Bundle-Name: Tuscany SDO Implementation
> +Bundle-SymbolicName: org.apache.tuscany.sdo.impl
> +Bundle-Version: 1.0.0
> +Bundle-Vendor: Apache Software Foundation
> +Require-Bundle: org.eclipse.emf.common,
> + org.eclipse.emf.ecore,
> + org.eclipse.emf.ecore.change,
> + org.eclipse.emf.ecore.xmi,
> + org.eclipse.xsd,
> + org.apache.tuscany.sdo.lib;visibility:=reexport,
> + org.apache.tuscany.sdo.spec;visibility:=reexport
> +Import-Package: org.osgi.framework,
> + org.xml.sax,
> + org.xml.sax.helpers,
> + org.w3c.dom,
> + org.w3c.dom.events,
> + javax.xml,
> + javax.xml.parsers,
> + javax.xml.stream,
> + javax.xml.transform,
> + javax.xml.transform.dom,
> + javax.xml.transform.stream,
> + org.objectweb.asm
> +Export-Package:
> + org.apache.tuscany.sdo,
> + org.apache.tuscany.sdo.helper,
> + org.apache.tuscany.sdo.impl,
> + org.apache.tuscany.sdo.util,
> + org.apache.tuscany.sdo.codegen,
> + org.apache.tuscany.sdo.model,
> + org.apache.tuscany.sdo.model.impl,
> + org.apache.tuscany.sdo.model.internal
> +Bundle-Activator: org.apache.tuscany.sdo.osgi.SdoBundleActivator
>
> Added:
> incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.osgi.test.mf
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.osgi.test.mf?rev=620763&view=auto
>
> ==============================================================================
> ---
> incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.osgi.test.mf
> (added)
> +++
> incubator/tuscany/java/sdo/impl/src/test/resources/osgi/sdo.osgi.test.mf Tue
> Feb 12 03:13:53 2008
> @@ -0,0 +1,38 @@
> +Manifest-Version: 1.0
> +Bundle-ManifestVersion: 2
> +Bundle-Name: Tuscany SDO OSGi Tests
> +Bundle-SymbolicName: org.apache.tuscany.sdo.osgi.test
> +Bundle-Version: 1.0.0
> +Bundle-Vendor: Apache Software Foundation
> +Import-Package: org.osgi.framework,
> +  junit.framework,
> +  javax.xml,
> +  javax.xml.parsers,
> +  org.xml.sax,
> +  org.xml.sax.helpers,
> +  org.w3c.dom,
> +  org.w3c.dom.events,
> +  javax.xml.stream,
> +  javax.xml.transform,
> +  javax.xml.transform.dom,
> +  javax.xml.transform.stream,
> +  org.eclipse.emf.common,
> +  org.eclipse.emf.common.archive,
> +  org.eclipse.emf.common.command,
> +  org.eclipse.emf.common.notify,
> +  org.eclipse.emf.common.util,
> +  org.eclipse.emf.ecore,
> +  org.eclipse.emf.ecore.impl,
> +  org.eclipse.emf.ecore.plugin,
> +  org.eclipse.emf.ecore.resource,
> +  org.eclipse.emf.ecore.util,
> +  org.eclipse.emf.ecore.xml.namespace,
> +  org.eclipse.emf.ecore.xml.namespace.util,
> +  org.eclipse.emf.ecore.xml.type,
> +  org.eclipse.emf.ecore.xml.type.util,
> +  org.eclipse.xsd
> +Export-Package: org.apache.tuscany.sdo.osgi
> +Require-Bundle: org.apache.tuscany.sdo.lib;visibility:=reexport,
> +  org.apache.tuscany.sdo.impl;visibility:=reexport,
> +  org.apache.tuscany.sdo.spec;visibility:=reexport
> +Bundle-Activator: org.apache.tuscany.sdo.test.osgi.TestBundleActivator
>
> Modified:
> incubator/tuscany/java/sdo/lib/src/main/resources/META-INF/MANIFEST.MF
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/lib/src/main/resources/META-INF/MANIFEST.MF?rev=620763&r1=620762&r2=620763&view=diff
>
> ==============================================================================
> --- incubator/tuscany/java/sdo/lib/src/main/resources/META-INF/MANIFEST.MF
> (original)
> +++ incubator/tuscany/java/sdo/lib/src/main/resources/META-INF/MANIFEST.MF
> Tue Feb 12 03:13:53 2008
> @@ -13,4 +13,5 @@
> Bundle-Vendor: Apache Software Foundation
> Require-Bundle: org.apache.tuscany.sdo.spec;visibility:=reexport
> Export-Package: org.apache.tuscany.sdo.api,
> - org.apache.tuscany.sdo.rtlib
> + org.apache.tuscany.sdo.spi,
> + org.apache.tuscany.sdo.lib
>
> Modified: incubator/tuscany/java/sdo/sdo-api/pom.xml
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sdo-api/pom.xml?rev=620763&r1=620762&r2=620763&view=diff
>
> ==============================================================================
> --- incubator/tuscany/java/sdo/sdo-api/pom.xml (original)
> +++ incubator/tuscany/java/sdo/sdo-api/pom.xml Tue Feb 12 03:13:53 2008
> @@ -103,10 +103,11 @@
>                  <configuration>
>                      <osgiManifest>
>                          <bundleName>${pom.name}</bundleName>
> +                        <bundleVersion>${specVersion}</bundleVersion>
>                          <bundleDescription>${pom.description
> }</bundleDescription>
>                          <bundleVendor>${pom.organization.name
> }</bundleVendor>
>                          <bundleLocalization>plugin</bundleLocalization>
> -                        <bundleSymbolicName>commonj.sdo
> </bundleSymbolicName>
> +                        <bundleSymbolicName>org.apache.tuscany.sdo.spec
> </bundleSymbolicName>
>                          <exportPackage>
>                              commonj.sdo;version="${specVersion}",
> commonj.sdo.helper;version="${specVersion},
>                              commonj.sdo.impl;version="${specVersion}"
>
> Modified:
> incubator/tuscany/java/sdo/sdo-api/src/main/java/commonj/sdo/impl/HelperProvider.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sdo-api/src/main/java/commonj/sdo/impl/HelperProvider.java?rev=620763&r1=620762&r2=620763&view=diff
>
> ==============================================================================
> ---
> incubator/tuscany/java/sdo/sdo-api/src/main/java/commonj/sdo/impl/HelperProvider.java
> (original)
> +++
> incubator/tuscany/java/sdo/sdo-api/src/main/java/commonj/sdo/impl/HelperProvider.java
> Tue Feb 12 03:13:53 2008
> @@ -49,7 +49,7 @@
>       * to load the HelperProvider class itself and if no default
> implementation is available
>       * this field will be set to null.
>       */
> -    public static final HelperProvider INSTANCE;
> +    public static HelperProvider INSTANCE;
>
>      /**
>       * The name of the resource that is used for service location.
> @@ -71,6 +71,15 @@
>              provider = null;
>          }
>          INSTANCE = provider;
> +    }
> +
> +    public static synchronized void setDefaultInstance(ClassLoader cl) {
> +       if (INSTANCE == null) {
> +            try {
> +                INSTANCE = getInstance(cl);
> +            } catch (NoHelperProviderException e) {
> +            }
> +       }
>      }
>
>      /**
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to