Commit 93bbe066 authored by Munilla Christophe's avatar Munilla Christophe

- add some comments

- add some tests
- lighten project : remove unnecessary api and resources-loader modules - move needed classes and interfaces to the core module
- fix bug FraSCAti in Knopflerfish and improve osgi binding : no more FraSCAti instance's ClassLoader replacement
parent edbffc6f
......@@ -38,11 +38,5 @@
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-osgi-resources</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
......@@ -28,26 +28,11 @@ package org.ow2.frascati.osgi.api.service;
import java.io.File;
import java.net.URL;
//import org.ow2.frascati.util.resource.AbstractResource;
/**
* The FraSCAtiOSGiService interface
*/
public interface FraSCAtiOSGiService
{
// /**
// * Load an SCA component embedded in the AbstractResource passed on as a
// * parameter
// *
// * @param resource
// * the AbstractResource containing the composite file to load
// * @param compositeName
// * the composite file name
// * @throws FraSCAtiOSGiNotFoundCompositeException;
// * if no component can be loaded
// *
// */
// String loadSCA(AbstractResource resource, String compositeName)
// throws FraSCAtiOSGiNotFoundCompositeException;
/**
* Load an SCA component embedded in the resource which URL is passed on as a
* parameter
......
<?xml version="1.0"?>
<!--
* OW2 FraSCAti OSGi
* Copyright (c) 2011 - 2012 Inria, University of Lille 1
*
* This library 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 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: frascati@ow2.org
*
* Author: Christophe Munilla
*
* Contributor(s):
-->
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>parent</artifactId>
<version>1.5-SNAPSHOT</version>
</parent>
<artifactId>frascati-osgi-api</artifactId>
<name>OW2 FraSCAti OSGi API</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-assembly-factory</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-osgi-util</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-osgi-resources</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.model</groupId>
<artifactId>osoa-java-api</artifactId>
<version>2.0.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
......@@ -43,12 +43,6 @@
<artifactId>frascati-osgi-api-service</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-osgi-resources</artifactId>
<version>1.5-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
......@@ -32,8 +32,7 @@
<component name="introspect">
<service name="introspector">
<interface.java
interface="org.ow2.frascati.osgi.introspect.api.IntrospectService" />
<interface.java interface="org.ow2.frascati.osgi.introspect.api.IntrospectService" />
</service>
<frascati:implementation.osgi bundle="bundle-frascati-introspector.jar" />
</component>
......
......@@ -69,24 +69,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-osgi-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-osgi-api-service</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-osgi-resources-loader</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.osgi</groupId>
<artifactId>frascati-binding-osgi</artifactId>
......
......@@ -28,7 +28,8 @@ package org.ow2.frascati.osgi.api;
import java.io.File;
import org.osgi.framework.BundleContext;
import org.ow2.frascati.util.resource.AbstractResource;
import org.ow2.frascati.osgi.resource.loader.AbstractResourceClassLoader;
import org.ow2.frascati.parser.api.ParsingContext;
/**
* The FraSCAtiOSGiContext interface
......@@ -45,34 +46,27 @@ public interface FraSCAtiOSGiContext
* from the OSGi context
*/
void registerBindedInterface(String interfaceName, String filter,
ClassLoader bindingCl);
ParsingContext parsingContext);
/**
* Resolve classes before fractal membranes creation process for an exported interface
* towards the OSGi context
*/
void registerExportedInterface(String interfaceName, String properties,
ClassLoader bindingCl);
ParsingContext parsingContext);
/**
* Helper method to create a ProcessingContext using the AbstractResource passed on as
* parameter
* Shortcut to get the FraSCAti instance's ClassLoader
*/
ClassLoader createClassLoader(AbstractResource resource);
ClassLoader getClassLoader();
/**
* Returns the class loader of the FraSCAtiOSGiContxt instance
* Returns the class loader Manager of the FraSCAtiOSGiContext instance
*/
ClassLoader getClassLoader();
AbstractResourceClassLoader getClManager();
/**
* Returns the cache directory of the FraSCAtiOSGiContext instance
*/
File getCacheDir();
// /**
// * This method alerts the FraSCAtiOSGiContext that a binding has been initialized.
// * It's used to clean up some memorized data
// */
// void bindingHintsInitialized();
}
......@@ -29,7 +29,6 @@ import java.util.Map;
import org.objectweb.fractal.api.Component;
import org.osoa.sca.annotations.Service;
import org.ow2.frascati.assembly.factory.api.ProcessingContext;
/**
* The FraSCAtiOSGiRegistry is used to memorized associations between loaded bundles in
......
......@@ -27,6 +27,7 @@
package org.ow2.frascati.osgi.context;
import java.io.File;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
......@@ -42,40 +43,32 @@ import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Scope;
import org.ow2.frascati.assembly.factory.api.ClassLoaderManager;
import org.ow2.frascati.osgi.api.FraSCAtiOSGiContext;
import org.ow2.frascati.osgi.resource.loader.AbstractResourceClassLoader;
import org.ow2.frascati.osgi.security.FraSCAtiOSGiSecurityManager;
import org.ow2.frascati.osgi.util.io.OSGiIOUtils;
import org.ow2.frascati.starter.api.AbstractInitializable;
import org.ow2.frascati.util.FrascatiClassLoader;
import org.ow2.frascati.parser.api.ParsingContext;
import org.ow2.frascati.parser.core.ParsingContextImpl;
import org.ow2.frascati.util.io.IOUtils;
import org.ow2.frascati.util.reflect.ReflectionHelper;
import org.ow2.frascati.util.resource.AbstractResource;
import org.ow2.frascati.util.resource.ResourceAlreadyManagedException;
import org.ow2.frascati.util.resource.loader.AbstractResourceClassLoader;
import org.ow2.frascati.util.resource.loader.AbstractResourceClassLoaderManager;
public class FraSCAtiOSGiContextImpl
extends AbstractInitializable implements FraSCAtiOSGiContext
/**
* Implementation of the FraSCAtiOSGiContext interface
*/
@Scope("COMPOSITE")
public class FraSCAtiOSGiContextImpl implements FraSCAtiOSGiContext
{
// ---------------------------------------------------------------------------
// Internal state.
// --------------------------------------------------------------------------
/**
* The BundleContext assiciated to the FraSCAti instance
* The BundleContext associated to the FraSCAti instance
*/
private BundleContext bundleContext;
/**
* The ClassLoader Manager used for bindings
*/
private AbstractResourceClassLoaderManager clManager;
/**
* the parent ClassLoader
*/
FrascatiClassLoader parentClassLoader;
/**
* The cache directory
*/
......@@ -86,16 +79,47 @@ extends AbstractInitializable implements FraSCAtiOSGiContext
*/
private final Logger log = Logger.getLogger(
FraSCAtiOSGiContextImpl.class.getCanonicalName());
/**
* the AbstractResourceClassLoader manager
*/
private AbstractResourceClassLoader clManager;
/**
* The required classloader manager.
*/
@Reference(name = "frascati-loader")
private ClassLoaderManager classLoaderManager;
private ClassLoaderManager classLoaderManager;
// ---------------------------------------------------------------------------
// Internal methods.
// --------------------------------------------------------------------------
/**
* Initialize and Configure a basic SecurityManager - A security manager is
* needed to use rmi binding
*/
private void configureSecurityManager()
{
log.log(Level.CONFIG, "init FraSCAtiOSGiSecurityManager");
FraSCAtiOSGiSecurityManager frascatiSM = new FraSCAtiOSGiSecurityManager(
System.getSecurityManager(), this);
try
{
URL policyURL = getClass().getClassLoader().getResource("frascati.policy");
if (policyURL != null)
{
String policy = policyURL.toExternalForm();
System.setProperty("java.security.policy", policy);
java.security.Policy.getPolicy().refresh();
}
} catch (Exception e)
{
log.log(Level.WARNING,e.getMessage(),e);
}
System.setSecurityManager(frascatiSM);
}
/**
* The sca syntax is not the same has the osgi one : '!', '|', and '&' are
* replaced by 'not', 'or', and 'and' respectively. This method makes
......@@ -121,69 +145,36 @@ extends AbstractInitializable implements FraSCAtiOSGiContext
}
/**
* Create the classloader which will take the place of the root classloader
* of the FraSCAti instance (this last one will become its parent).
* This substitution is necessary to be able to create osgi bindings without
* using the FraSCAtiOSGiService (new SCA composite loaded using the
* (Web)Explorer for example)
* Define the ClassLoader of the ParsingContext passed on as a parameter.
*
* @param classLoader
* the new ClassLoader of the ParsingContext
* @param parsingContext
* the ParsingContext to parameterized
*/
private void initClassLoader()
{
parentClassLoader = (FrascatiClassLoader) classLoaderManager.getClassLoader();
log.log(Level.INFO, "ClassLoader parentClassLoader : " + parentClassLoader);
private void setParsingContextClassLoader(ParsingContext parsingContext,
ClassLoader classLoader)
{
try
{
clManager = new AbstractResourceClassLoaderManager(
parentClassLoader);
classLoaderManager.setClassLoader(clManager);
Field classLoaderField = ParsingContextImpl.class.getDeclaredField(
"classLoader");
classLoaderField.setAccessible(true);
classLoaderField.set(parsingContext, classLoader);
// Needed to use FraSCAti's Explorer with Equinox OSGi
// implementation
ReflectionHelper classResolver = new ReflectionHelper(clManager,
"org.ow2.frascati.explorer.FrascatiExplorerClassResolver");
if (classResolver.isClassKnown())
{
classResolver.invokeStatic("setClassLoader",
new Class<?>[] { ClassLoader.class },
new Object[] { clManager });
} else
{
log.log(Level.CONFIG, "No FrascatiExplorerClassResolver class "
+ "found : Errors could occure using FraSCAti's Explorer "
+ "in Equinox");
}
} catch (Exception e)
} catch (SecurityException e)
{
log.log(Level.SEVERE,e.getMessage(),e);
}
}
/**
* Initialize and Configure a basic SecurityManager - A security manager is
* needed to use rmi binding
*/
private void configureSecurityManager()
{
log.log(Level.CONFIG, "init FraSCAtiOSGiSecurityManager");
FraSCAtiOSGiSecurityManager frascatiSM = new FraSCAtiOSGiSecurityManager(
System.getSecurityManager(), this);
try
e.printStackTrace();
} catch (NoSuchFieldException e)
{
URL policyURL = getClass().getClassLoader().getResource("frascati.policy");
if (policyURL != null)
{
String policy = policyURL.toExternalForm();
System.setProperty("java.security.policy", policy);
java.security.Policy.getPolicy().refresh();
}
} catch (Exception e)
e.printStackTrace();
} catch (IllegalArgumentException e)
{
log.log(Level.WARNING,e.getMessage(),e);
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
}
System.setSecurityManager(frascatiSM);
}
// ---------------------------------------------------------------------------
......@@ -197,45 +188,10 @@ extends AbstractInitializable implements FraSCAtiOSGiContext
public void init()
{
log.info("init FraSCAtiOSGiContext");
}
/**
* {@inheritDoc}
*
* @see org.ow2.frascati.starter.api.AbstractInitializable#
* doInitialize()
*/
public void doInitialize()
{
getCacheDir();
configureSecurityManager();
initClassLoader();
}
/**
* {@inheritDoc}
*
* @see org.ow2.frascati.osgi.api.FraSCAtiOSGiContext
* #createProcessingContext(org.ow2.frascati.util.resource.AbstractResource)
*/
public ClassLoader createClassLoader(AbstractResource resource)
{
try
{
AbstractResourceClassLoader cl = new AbstractResourceClassLoader(
clManager, clManager, resource, null);
return cl;
} catch (ResourceAlreadyManagedException e)
{
log.log(Level.WARNING,e.getMessage(),e);
} catch (Exception e)
{
log.log(Level.WARNING,e.getMessage(),e);
}
return null;
}
/**
......@@ -245,45 +201,44 @@ extends AbstractInitializable implements FraSCAtiOSGiContext
* #registerBindedInterface(java.lang.String, java.lang.String)
*/
public void registerBindedInterface(String interfaceName, String filter,
ClassLoader bindingCl)
ParsingContext parsingContext)
{
clManager.setResourceLoader(null);
try
{
String normalizedFilter = normalizeFilter(filter);
ServiceReference[] references = bundleContext.getAllServiceReferences(
interfaceName, normalizedFilter);
{
String normalizedFilter = normalizeFilter(filter);
ServiceReference[] references = getBundleContext(
).getAllServiceReferences(interfaceName, normalizedFilter);
if (references != null && references.length > 0)
{
Bundle bundle = references[0].getBundle();
Class<?> interfaceClass = bundle.loadClass(interfaceName);
// needed by JDTCompiler
String jarFilePath = OSGiIOUtils.cacheBundle(bundle,
getCacheDir());
// needed by JDTCompiler
String jarFilePath = OSGiIOUtils.cacheBundle(bundle,getCacheDir());
log.log(Level.CONFIG, "bundle cached : " + jarFilePath);
try
{
URL jarURL = new URL("jar", "", new StringBuilder("file:")
.append(jarFilePath).append("!/").toString());
AbstractResource jarResource = AbstractResource.newResource(jarURL);
AbstractResource jarResource = AbstractResource.newResource(jarURL);
AbstractResourceClassLoader resourceLoader = new AbstractResourceClassLoader(
parentClassLoader, clManager, jarResource, null);
parsingContext.getClassLoader(),
getClManager(),
jarResource);
getClManager().addLoaded(interfaceName, interfaceClass);
getClManager().setDelegate(resourceLoader);
setParsingContextClassLoader(parsingContext, resourceLoader);
clManager.addLoaded(interfaceName, interfaceClass);
clManager.setResourceLoader(resourceLoader);
} catch (ResourceAlreadyManagedException e)
{
log.log(Level.WARNING, e.getMessage(), e);
} catch (MalformedURLException e)
{
log.log(Level.WARNING, e.getMessage(), e);
} catch(Exception e)
{
log.log(Level.WARNING, interfaceName + " not found in the BundleContext");
}
}
} catch (InvalidSyntaxException e)
......@@ -303,17 +258,18 @@ extends AbstractInitializable implements FraSCAtiOSGiContext
* #registerExportedInterface(java.lang.String, java.lang.String)
*/
public void registerExportedInterface(String interfaceName,
String properties, ClassLoader bindingCl)
String properties, ParsingContext parsingContext)
{
clManager.setResourceLoader(null);
String interfaceSimpleName = interfaceName.substring(interfaceName
.lastIndexOf('.') + 1);
String interfaceSimpleName = interfaceName.substring(
interfaceName.lastIndexOf('.') + 1);
String interfacePackageName = interfaceName.substring(0,
interfaceName.lastIndexOf('.'));
String interfaceFileName = new StringBuilder(interfaceName.replace('.',
'/')).append(".class").toString();
String name = interfaceSimpleName;
URL url = bindingCl.getResource(interfaceFileName);
URL url = parsingContext.getClassLoader().getResource(interfaceFileName);
if (url == null)