Commit a112fe4b authored by Munilla Christophe's avatar Munilla Christophe

first changes to provide a generic and extensible FraSCAti in Nuxeo service :...

first changes to provide a generic and extensible FraSCAti in Nuxeo service : define abstract generic FraSCAtiInNuxeoFactory and FraSCAtiInNuxeoServiceProvider - update other modules to use them
parent 7e3a3fca
......@@ -26,30 +26,31 @@
package org.ow2.frascati.isolated;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
//import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The FraSCAtiIsolated class allow to instantiate an isolated FraSCAti
* instance
* The FraSCAtiIsolated class allow to instantiate an isolated FraSCAti
*/
public class FraSCAtiIsolated
{
/**
* FraSCAtiIsolated Logger
*/
Logger log = Logger.getLogger(FraSCAtiIsolated.class.getCanonicalName());
Logger logger = Logger.getLogger(FraSCAtiIsolated.class.getCanonicalName());
/**
* Constant used to set the FraSCAti's libraries path in System's properties
* Constant used to set the FraSCAti's root path in System's properties
*/
public static final String ISOLATED_FRASCATI_LIBRARIES_BASEDIR =
"org.ow2.frascati.isolated.libraries.basedir";
public static final String FRASCATI_ISOLATED_BASEDIR_PROP =
"org.ow2.frascati.isolated.root.basedir";
/**
* The FraSCAti instance object
......@@ -73,38 +74,44 @@ public class FraSCAtiIsolated
private Class<?> frascatiClass = null;
private Class<?> managerClass = null;
/**
* Constructor
*
* Instantiate a new FraSCAti, isolated from the context in which it has been
* launched
*
* @param librariesDirectory
* @param frascatiRootDirectory
* the directory where the FraSCAti's libraries are stored
*
* @throws Exception
*/
public FraSCAtiIsolated(File librariesDirectory, ClassLoader parentClassLoader)
public FraSCAtiIsolated(File frascatiRootDirectory, ClassLoader parentClassLoader)
throws Exception
{
char sep = File.separatorChar;
//if the librariesDirectory is null
if (librariesDirectory == null)
if (frascatiRootDirectory == null)
{
//Use the system's property to find FraSCAti's libraries
String librariesDirectoryProp = System
.getProperty(ISOLATED_FRASCATI_LIBRARIES_BASEDIR);
String frascatiRootDirectoryProp = System.getProperty(
FRASCATI_ISOLATED_BASEDIR_PROP);
if (librariesDirectoryProp == null
|| !(librariesDirectory = new File(librariesDirectoryProp))
.exists())
if (frascatiRootDirectoryProp == null || !(frascatiRootDirectory =
new File(frascatiRootDirectoryProp)).exists())
{
throw new InstantiationException("Enable to instantiate a new"
+ " isolated FraSCAti instance : no libraries directory found");
throw new InstantiationException("Unable to instantiate a new"
+ " isolated FraSCAti instance : no root directory found");
}
}
File frascatiLibDirectory = new File(frascatiRootDirectory,"lib");
if(!frascatiLibDirectory.exists() && !frascatiLibDirectory.mkdir())
{
throw new InstantiationException("Unable to instantiate a new"
+ " isolated FraSCAti instance : no libraries directory found");
}
//Retrieve all libraries and add them to the ClassLoader classpath
File[] libraries = librariesDirectory.listFiles(new FilenameFilter()
File[] libraries = frascatiLibDirectory.listFiles(new FilenameFilter()
{
public boolean accept(File file, String name)
{
......@@ -114,8 +121,29 @@ public class FraSCAtiIsolated
}
return false;
}
});
});
File frascatiConfigDirectory = new File(frascatiRootDirectory,"config");
if(frascatiConfigDirectory.exists())
{
File frascatiConfigFile = new File(frascatiConfigDirectory,
"frascati_boot.properties");
try
{
Properties props = new Properties();
props.loadFromXML(new FileInputStream(frascatiConfigFile));
Enumeration<Object> keys = props.keys();
while (keys.hasMoreElements())
{
String key = (String) keys.nextElement();
String value = props.getProperty(key);
System.setProperty(key, value);
}
} catch (Exception e)
{
logger.log(Level.WARNING, e.getMessage(),e);
}
}
if(parentClassLoader != null)
{
//Use the ClassLoader passed on as a parameter
......@@ -133,7 +161,6 @@ public class FraSCAtiIsolated
{
isolatedCl.addURL(library.toURI().toURL());
}
frascatiClass = isolatedCl.loadClass("org.ow2.frascati.FraSCAti");
// Instantiate the new FraSCAti
......@@ -332,7 +359,7 @@ public class FraSCAtiIsolated
@Override
public void addURL(URL url)
{
// log.debug("adding url to load : " + url);
// logger.debug("adding url to load : " + url);
super.addURL(url);
}
......@@ -371,7 +398,7 @@ public class FraSCAtiIsolated
} catch (ClassNotFoundException e)
{
log.log(Level.CONFIG,
logger.log(Level.CONFIG,
"'" + name + "' class not found using the parent classloader");
}
}
......@@ -384,7 +411,7 @@ public class FraSCAtiIsolated
} catch (ClassNotFoundException e)
{
log.log(Level.CONFIG,
logger.log(Level.CONFIG,
"'" + name + "' class not found using the classloader classpath");
}
if (clazz == null && !regular && getParent() != null)
......
......@@ -59,7 +59,7 @@ public class FraSCAtiIsolatedTest
@Before
public void setUp()
{
File librariesBaseDir = new File("target/test-classes/frascati/lib").getAbsoluteFile();
File librariesBaseDir = new File("target/test-classes/frascati").getAbsoluteFile();
try
{
//The parent of the FraSCAti's ClassLoader is the same as the parent of the
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="org.ow2.frascati.bootstrap">org.ow2.frascati.bootstrap.FraSCAti</entry>
<!--
<entry key="org.ow2.frascati.bootstrap">org.ow2.frascati.bootstrap.FraSCAtiJDTRest</entry>
<entry key="org.ow2.frascati.bootstrap">org.ow2.frascati.bootstrap.FraSCAtiWebExplorer</entry>
-->
</properties>
......@@ -19,6 +19,16 @@
<name>OW2 FraSCAti SCA Component API for Nuxeo</name>
<dependencies>
<dependency>
<groupId>org.ow2.frascati.nuxeo</groupId>
<artifactId>frascati-isolated</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.nuxeo.runtime</groupId>
<artifactId>nuxeo-runtime-bridge</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -23,61 +23,120 @@
* Contributor(s):
*
*/
package org.ow2.frascati.nuxeo;
package org.ow2.frascati.nuxeo.api;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.ow2.frascati.isolated.FraSCAtiIsolated;
import org.ow2.frascati.nuxeo.api.FraSCAtiNuxeoServiceItf;
import org.ow2.frascati.nuxeo.api.FraSCAtiNuxeoServiceProviderItf;
//import org.ow2.frascati.nuxeo.factory.ClassLoaderSingleton;
import org.ow2.frascati.nuxeo.exception.FraSCAtiInNuxeoException;
import org.nuxeo.runtime.bridge.Application;
/**
* Implementation of the FraSCAtiServiceProviderItf interface
* FraSCAtiNuxeo allow to instantiate an new FraSCAti instance in Nuxeo
* Abstract implementation of the FraSCAtiServiceProviderItf interface
* AbstractFraSCAtiInNuxeo allow to instantiate an new FraSCAti instance in Nuxeo
*/
public class FraSCAtiNuxeo extends FraSCAtiIsolated
implements Application, FraSCAtiNuxeoServiceProviderItf
{
Logger log = Logger.getLogger(FraSCAtiNuxeo.class.getCanonicalName());
public abstract class AbstractFraSCAtiInNuxeo<FraSCAtiInNuxeoService extends FraSCAtiInNuxeoServiceItf>
extends FraSCAtiIsolated implements FraSCAtiInNuxeoServiceProviderItf<FraSCAtiInNuxeoService>
{
/**
* Logger
*/
protected Logger logger = Logger.getLogger(getClass().getCanonicalName());
private FraSCAtiNuxeoServiceItf frascatiService;
/**
* the provided FraSCAtiInNuxeoServiceItf implementation instance
*/
private FraSCAtiInNuxeoService frascatiService;
/**
* Constructor
* @throws Exception
*/
public FraSCAtiNuxeo() throws Exception
protected AbstractFraSCAtiInNuxeo(File librariesDirectory, ClassLoader parentClassLoader)
throws FraSCAtiInNuxeoException, Exception
{
super(null,null);
super(librariesDirectory,parentClassLoader);
logger.log(Level.INFO, "FraSCAti In Nuxeo initialisation");
try
{
Class<FraSCAtiInNuxeoService> providedType = getProvidedType();
String serviceName = getProvidedTypeProperty(
FraSCAtiInNuxeoServiceProviderItf.FRASCATI_IN_NUXEO_SERVICE_SERVICE_NAME_PROP);
String componentName = getProvidedTypeProperty(
FraSCAtiInNuxeoServiceProviderItf.FRASCATI_IN_NUXEO_SERVICE_COMPONENT_NAME_PROP);
logger.log(Level.INFO, "Search for service '" +
serviceName + "' [" + providedType.getCanonicalName() + " ] in the '" +
componentName + "' component");
frascatiService = super.getService(providedType, serviceName,
"org.ow2.frascati.FraSCAti/" + componentName);
log.log(Level.INFO, "new FraSCAti instance initialisation");
frascatiService = super.getService(FraSCAtiNuxeoServiceItf.class,
"frascati-nuxeo-service",
"org.ow2.frascati.FraSCAti/frascati-nuxeo");
} catch (Throwable throwable)
{
throw new FraSCAtiInNuxeoException(
"Unable to retrieve the FraSCAtiInNuxeoServiceItf implementing service",
throwable);
}
}
public FraSCAtiNuxeoServiceItf getFraSCAtiNuxeoService()
/**
* Return the type of the provided service
*/
@SuppressWarnings("unchecked")
private Class<FraSCAtiInNuxeoService> getProvidedType()
{
return (Class<FraSCAtiInNuxeoService>)
((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
/**
* Return the value of a public static attribute declared in the provided
* service's implemented interface
*
* @param property
* the search public static attribute name
*/
private String getProvidedTypeProperty(String property)
{
try
{
Field propertyField = getProvidedType().getDeclaredField(property);
return (String) propertyField.get(null);
} catch (Throwable throwable)
{
logger.log(Level.WARNING,throwable.getMessage(),throwable);
}
return null;
}
/**
* {@inheritDoc}
*
* @see org.ow2.frascati.nuxeo.api.FraSCAtiInNuxeoServiceProviderItf#
* getFraSCAtiInNuxeoService()
*/
public FraSCAtiInNuxeoService getFraSCAtiInNuxeoService()
{
return frascatiService;
}
/*
* (non-Javadoc)
/**
* {@inheritDoc}
*
* @see org.nuxeo.runtime.bridge.Application#getService(java.lang.Class)
*/
......@@ -91,8 +150,8 @@ implements Application, FraSCAtiNuxeoServiceProviderItf
return null;
}
/*
* (non-Javadoc)
/**
* {@inheritDoc}
*
* @see org.nuxeo.runtime.bridge.Application#destroy()
*/
......@@ -110,29 +169,18 @@ implements Application, FraSCAtiNuxeoServiceProviderItf
}
mbs.unregisterMBean(new ObjectName(
"org.ow2.frascati.jmx:name=FrascatiJmx"));
} catch (MalformedObjectNameException e)
{
log.log(Level.WARNING,e.getMessage(),e);
} catch (NullPointerException e)
{
log.log(Level.WARNING,e.getMessage(),e);
} catch (MBeanRegistrationException e)
{
log.log(Level.WARNING,e.getMessage(),e);
} catch (InstanceNotFoundException e)
} catch (Throwable throwable)
{
log.log(Level.WARNING,e.getMessage(),e);
logger.log(Level.CONFIG,throwable.getMessage(),throwable);
}
try
{
super.stop();
} catch (Exception e)
} catch (Throwable throwable)
{
log.log(Level.WARNING,e.getMessage(),e);
logger.log(Level.WARNING,throwable.getMessage(),throwable);
}
frascatiService = null;
}
......
......@@ -23,7 +23,7 @@
* Contributor(s):
*
*/
package org.ow2.frascati.nuxeo.factory;
package org.ow2.frascati.nuxeo.api;
import java.io.File;
import java.io.FileInputStream;
......@@ -36,7 +36,6 @@ import java.util.logging.Logger;
import org.nuxeo.common.Environment;
import org.ow2.frascati.nuxeo.FraSCAtiNuxeo;
import org.ow2.frascati.isolated.FraSCAtiIsolated;
import org.nuxeo.runtime.bridge.Application;
......@@ -44,20 +43,27 @@ import org.nuxeo.runtime.bridge.ApplicationDescriptor;
import org.nuxeo.runtime.bridge.ApplicationFactory;
/**
* The FraSCAtiNuxeoFactory is used by the Nuxeo runtime bridge to build a
* The AbstractFraSCAtiInNuxeoFactory is used by the Nuxeo runtime bridge to build a
* FraSCAti instance
*/
public class FraSCAtiNuxeoFactory implements ApplicationFactory
public abstract class AbstractFraSCAtiInNuxeoFactory<FraSCAtiInNuxeoService extends FraSCAtiInNuxeoServiceItf,
FraSCAtiInNuxeo extends AbstractFraSCAtiInNuxeo<FraSCAtiInNuxeoService>>
implements ApplicationFactory
{
// Logger
private static Logger log = Logger.getLogger(FraSCAtiNuxeoFactory.class
.getCanonicalName());
/**
* Return a FraSCAtiInNuxeoServiceProviderItf implementation instance
*/
protected abstract FraSCAtiInNuxeo getFraSCAtiInNuxeoInstance(String librariesDirectory,
ClassLoader parent);
/**
* FraSCAti output directory property
* */
private static final String FRASCATI_OUTPUT_DIRECTORY_PROPERTY =
"org.ow2.frascati.output.directory";
// Logger
protected Logger logger = Logger.getLogger(getClass().getCanonicalName());
/**
* {@inheritDoc}
......@@ -65,78 +71,68 @@ public class FraSCAtiNuxeoFactory implements ApplicationFactory
* @see org.nuxeo.runtime.bridge.ApplicationFactory
* #createApplication(org.nuxeo.runtime.bridge.ApplicationDescriptor)
*/
@Override
public Application createApplication(ApplicationDescriptor desc)
throws java.lang.Exception
throws Exception
{
log.log(Level.INFO, "Create FraSCAtiNuxeo Application");
logger.log(Level.INFO, "Create a FraSCAti In Nuxeo Application");
char sep = File.separatorChar;
URLClassLoader cl = (URLClassLoader) Thread.currentThread()
.getContextClassLoader();
log.log(Level.INFO, "ContextClassLoader found : " + cl);
URLClassLoader urlClassLoader = (URLClassLoader)
Thread.currentThread().getContextClassLoader();
if(logger.isLoggable(Level.CONFIG))
{
logger.log(Level.CONFIG, "ContextClassLoader found : " + urlClassLoader);
}
String home = Environment.getDefault().getHome().getAbsolutePath();
log.log(Level.INFO, "Frascati home dir : " + home);
String outputDir = new StringBuilder(home).append(sep).append(
"tmp").toString();
System.setProperty(FRASCATI_OUTPUT_DIRECTORY_PROPERTY, outputDir);
System.setProperty(FraSCAtiIsolated.ISOLATED_FRASCATI_LIBRARIES_BASEDIR,
new StringBuilder(home).append(sep).append("frascati")
.append(sep).append("lib").toString());
log.log(Level.INFO, "Define FraSCAti default output dir : " + outputDir);
String propertyBootFilePath = new StringBuilder(home).append(sep)
.append("config").append(sep)
.append("frascati_boot.properties").toString();
//Retrieve all configuration properties
String frascatiRootDirPath = System.getProperty(
FraSCAtiInNuxeoServiceProviderItf.FRASCATI_IN_NUXEO_BASEDIR_PROP,
"");
log.log(Level.INFO, "Read frascati_boot.properties file at "
+ propertyBootFilePath);
try
if(frascatiRootDirPath.isEmpty())
{
Properties props = new Properties();
props.loadFromXML(new FileInputStream(
new File(propertyBootFilePath)));
Enumeration<Object> keys = props.keys();
while (keys.hasMoreElements())
{
String key = (String) keys.nextElement();
String value = props.getProperty(key);
System.setProperty(key, value);
}
} catch (Exception e)
frascatiRootDirPath = new StringBuilder(home).append(sep
).append("frascati").toString();
}
String outputdir = System.getProperty(
FraSCAtiInNuxeoServiceProviderItf.FRASCATI_IN_NUXEO_OUTPUT_DIRECTORY_PROP,
"");
if(outputdir.isEmpty())
{
log.log(Level.INFO, "no boot properties found");
}
URL[] urls = cl.getURLs();
outputdir = new StringBuilder(home).append(sep).append(
"tmp").toString();
}
System.setProperty(FRASCATI_OUTPUT_DIRECTORY_PROPERTY, outputdir);
if(logger.isLoggable(Level.CONFIG))
{
logger.log(Level.CONFIG, "Define the '" +
FRASCATI_OUTPUT_DIRECTORY_PROPERTY + "' property : " +
outputdir);
}
URL[] urls = urlClassLoader.getURLs();
if (urls == null || urls.length == 0)
{
log.log(Level.INFO, "No classpath entry found for the IsolatedClassLoader");
logger.log(Level.WARNING, "No classpath entry found to initialize the ClassLoader");
} else if (log.getLevel() == Level.CONFIG)
} else if (logger.isLoggable(Level.CONFIG))
{
for (URL url : urls)
{
log.log(Level.INFO, "Added classpath entry :" + url.toExternalForm());
logger.log(Level.CONFIG, "Added classpath entry :" + url.toExternalForm());
}
}
if (desc != null)
if (desc != null && logger.isLoggable(Level.CONFIG))
{
log.log(Level.INFO,
"ApplicationDescriptor found - required isolated status : "
logger.log(Level.CONFIG, "ApplicationDescriptor found\nisolated status required : "
+ desc.isIsolated());
} else
} else if (desc == null)
{
log.log(Level.WARNING, "No ApplicationDescriptor found");