Commit 2a65ece1 authored by Philippe Merle's avatar Philippe Merle

Improved the code quality according to the Sonar checking reports:

* Declared 'Map' instead of 'HashMap' fields.
* Switched from 'protected' to 'private' fields and added needed getter methods.
* Added a 'final' modifier to methods not overridden.
* Removed string constant duplications.
* Removed local variable declarations when they are immediately returned.
* Avoided to catch Throwable.
* Declared a protected constructor for utility classes.
* Removed unnecessary 'public' modifier for interface methods.
* Added 'final' modifier to all constants.
* Avoided to throw Error instances.
* Added brackets for if, if/else, while statements.
parent 2c621229
......@@ -188,7 +188,7 @@ public class FactoryCommandLine {
*
* @param args - Command line arguments
*/
public static void main(String[] args) {
public static void main(String[] args) throws FrascatiException {
// The factory command line used to retrieve options value.
FactoryCommandLine cmd = null;
......
/***
* OW2 FraSCAti Examples: Ohloh Proxy
* Copyright (C) 2009-2010 INRIA, USTL
* Copyright (C) 2009-2010 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
......@@ -24,24 +24,16 @@
*/
package org.ow2.frascati.examples.ohloh.test;
import org.junit.Before;
import org.junit.Test;
import org.ow2.frascati.assembly.factory.Launcher;
import org.ow2.frascati.util.FrascatiException;
public class OhlohTestCase {
protected Launcher launcher;
@Before
public void init() throws FrascatiException {
// Load the composite
launcher = new Launcher("ohloh-proxy");
}
@Test
public void testService() {
public void testService() throws FrascatiException {
Launcher launcher = new Launcher("ohloh-proxy");
String result = launcher.call("ohloh", "getProjectInXml", String.class, "frascati");
System.out.println(result);
}
......
......@@ -115,7 +115,7 @@ public class FraSCAti
/**
* The OW2 FraSCAti composite instance.
*/
protected Component frascatiComposite;
private Component frascatiComposite;
//---------------------------------------------------------------------------
// Internal methods.
......@@ -125,20 +125,19 @@ public class FraSCAti
* @see AbstractFractalLoggeable#newException(String)
*/
@Override
protected FrascatiException newException(String message) {
protected final FrascatiException newException(String message) {
return new FrascatiException(message);
}
/**
* Load and instantiate a Java class.
*/
protected static <T> T loadAndInstantiate(String classname) throws Exception
@SuppressWarnings("unchecked")
protected static <T> T loadAndInstantiate(String classname)
throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(classname);
Object instance = clazz.newInstance();
@SuppressWarnings("unchecked")
T result = (T)instance;
return result;
return (T)clazz.newInstance();
}
/**
......@@ -147,7 +146,7 @@ public class FraSCAti
* @return The assembly factory component
* @throws FrascatiException
*/
protected void initFrascatiComposite() throws FrascatiException
protected final void initFrascatiComposite() throws FrascatiException
{
// Instantiate the OW2 FraSCAti bootstrap factory.
Factory bootstrapFactory;
......@@ -201,7 +200,7 @@ public class FraSCAti
/**
* Get an SCA service of the OW2 FraSCAti composite.
*/
protected <T> T getFrascatiService(String serviceName, Class<T> clazz)
protected final <T> T getFrascatiService(String serviceName, Class<T> clazz)
throws FrascatiException
{
return getService(this.frascatiComposite, serviceName, clazz);
......@@ -234,7 +233,7 @@ public class FraSCAti
/**
* Get the CompositeManager service of the OW2 FraSCAti Assembly Factory.
*/
public CompositeManager getCompositeManager()
public final CompositeManager getCompositeManager()
throws FrascatiException
{
return getFrascatiService(COMPOSITE_MANAGER, CompositeManager.class);
......@@ -250,7 +249,7 @@ public class FraSCAti
* @return the SCA composite component or null if an error occurs.
* @throws FrascatiException
*/
public Component getComposite(String composite)
public final Component getComposite(String composite)
throws FrascatiException
{
return getCompositeManager().getComposite(composite);
......@@ -268,7 +267,7 @@ public class FraSCAti
* @return the SCA composite component
* @throws FrascatiException
*/
public Component getComposite(String composite, URL[] libs)
public final Component getComposite(String composite, URL[] libs)
throws FrascatiException
{
return getCompositeManager().getComposite(composite, libs);
......@@ -277,7 +276,7 @@ public class FraSCAti
/**
*
*/
public Component[] getContribution(String contribution)
public final Component[] getContribution(String contribution)
throws FrascatiException
{
return getCompositeManager().getContribution(contribution);
......@@ -286,7 +285,7 @@ public class FraSCAti
/**
*
*/
public ProcessingContext newProcessingContext()
public final ProcessingContext newProcessingContext()
throws FrascatiException
{
return newProcessingContext(ProcessingMode.all);
......@@ -295,7 +294,7 @@ public class FraSCAti
/**
*
*/
public ProcessingContext newProcessingContext(ProcessingMode processingMode)
public final ProcessingContext newProcessingContext(ProcessingMode processingMode)
throws FrascatiException
{
ProcessingContext processingContext = new ProcessingContextImpl(getClassLoader());
......@@ -306,7 +305,7 @@ public class FraSCAti
/**
*
*/
public Component processComposite(String composite, ProcessingContext processingContext)
public final Component processComposite(String composite, ProcessingContext processingContext)
throws FrascatiException
{
return getCompositeManager().processComposite(new QName(composite), processingContext);
......@@ -315,14 +314,12 @@ public class FraSCAti
/**
* Get an SCA service of an SCA component.
*/
public <T> T getService(Component component, String serviceName, Class<T> clazz)
@SuppressWarnings("unchecked")
public final <T> T getService(Component component, String serviceName, Class<T> clazz)
throws FrascatiException
{
try {
Object itf = component.getFcInterface(serviceName);
@SuppressWarnings("unchecked")
T service = (T)itf;
return service;
return (T)component.getFcInterface(serviceName);
} catch(NoSuchInterfaceException e) {
severe(new FrascatiException("Cannot retrieve the '" + serviceName +
"' service of an SCA composite!", e));
......@@ -337,7 +334,7 @@ public class FraSCAti
* The name of the SCA composite to close.
* @throws FrascatiException
*/
public void close(Component composite) throws FrascatiException {
public final void close(Component composite) throws FrascatiException {
// Closing the SCA domain.
try {
log.info("Closing the SCA composite '" + composite + "'.");
......@@ -351,7 +348,7 @@ public class FraSCAti
/**
* Get the ClassLoaderManager service of the OW2 FraSCAti Assembly Factory.
*/
public ClassLoaderManager getClassLoaderManager()
public final ClassLoaderManager getClassLoaderManager()
throws FrascatiException
{
return getFrascatiService(CLASSLOADER_MANAGER, ClassLoaderManager.class);
......@@ -364,7 +361,7 @@ public class FraSCAti
* - URLClassLoader to be used by the Factory
* @throws FrascatiException
*/
public void setClassLoader(URLClassLoader classloader)
public final void setClassLoader(URLClassLoader classloader)
throws FrascatiException
{
getClassLoaderManager().setClassLoader(classloader);
......@@ -376,7 +373,7 @@ public class FraSCAti
* @return - ClassLoader currently used by the Factory
* @throws FrascatiException
*/
public ClassLoader getClassLoader()
public final ClassLoader getClassLoader()
throws FrascatiException
{
return getClassLoaderManager().getClassLoader();
......@@ -385,7 +382,7 @@ public class FraSCAti
/**
* Get the MembraneGeneration service of the OW2 FraSCAti Assembly Factory.
*/
public MembraneGeneration getMembraneGeneration()
public final MembraneGeneration getMembraneGeneration()
throws FrascatiException
{
return getFrascatiService(MEMBRANE_GENERATION, MembraneGeneration.class);
......
......@@ -47,6 +47,10 @@ import org.ow2.frascati.util.FrascatiException;
*/
public class Launcher extends AbstractLoggeable {
//---------------------------------------------------------------------------
// Internal state.
// --------------------------------------------------------------------------
/**
* Name of the SCA composite to launch
*/
......@@ -80,10 +84,7 @@ public class Launcher extends AbstractLoggeable {
scaComposite = FraSCAti.newFraSCAti().getComposite(compositeName);
this.compositeName = compositeName;
} catch (FrascatiException fe) {
log.severe("Unable to launch " + compositeName + ": "
+ fe.getMessage());
scaComposite = null;
throw fe;
severe(compositeName, fe);
}
}
......@@ -100,10 +101,7 @@ public class Launcher extends AbstractLoggeable {
scaComposite = f.getComposite(compositeName);
this.compositeName = compositeName;
} catch (FrascatiException fe) {
log.severe("Unable to launch " + compositeName + ": "
+ fe.getMessage());
scaComposite = null;
throw fe;
severe(compositeName, fe);
}
}
......@@ -121,30 +119,40 @@ public class Launcher extends AbstractLoggeable {
try {
scaComposite = f.getComposite(compositeName,urls);
this.compositeName = compositeName;
} catch (FrascatiException fe) {
log.severe("Unable to launch " + compositeName + ": "
+ fe.getMessage());
scaComposite = null;
throw fe;
} catch (FrascatiException fe) {
severe(compositeName, fe);
}
}
//---------------------------------------------------------------------------
// Internal methods
// --------------------------------------------------------------------------
/**
* Log FraSCAti exceptions.
*/
private void severe(String compositeName, FrascatiException fe)
throws FrascatiException
{
scaComposite = null;
log.severe("Unable to launch composite '" + compositeName + "': " + fe.getMessage());
throw fe;
}
//---------------------------------------------------------------------------
// Public methods
// --------------------------------------------------------------------------
/**
* Launch the composite
* @throws FrascatiException
*/
public void launch() throws FrascatiException {
public final void launch() throws FrascatiException {
if (compositeName != null) {
try {
scaComposite = FraSCAti.newFraSCAti().getComposite(compositeName);
} catch (FrascatiException fe) {
log.severe("Unable to launch " + compositeName + ": "
+ fe.getMessage());
scaComposite = null;
throw fe;
severe(compositeName, fe);
}
} else {
log.severe("FraSCAti launcher: composite name must be set!");
......@@ -157,7 +165,7 @@ public class Launcher extends AbstractLoggeable {
* @param compositeName the composite to launch
* @throws FrascatiException
*/
public void launch(String compositeName)
public final void launch(String compositeName)
throws FrascatiException {
this.compositeName = compositeName;
launch();
......@@ -177,8 +185,9 @@ public class Launcher extends AbstractLoggeable {
* parameters for <code>methodName</code>
* @return the method result or null
*/
public <T> T call(String serviceName, String methodName,
Class<T> responseType, Object... params) {
@SuppressWarnings("unchecked")
public final <T> T call(String serviceName, String methodName,
Class<T> responseType, Object... params) throws FrascatiException {
try {
Object o = TinfiDomain
.getService(scaComposite, Object.class, serviceName);
......@@ -202,30 +211,26 @@ public class Launcher extends AbstractLoggeable {
log.info("Call service '" + serviceName + "' of " + compositeName
+ " - Method '" + methodName
+ (withParams ? ("' - Params: " + paramList) : "'"));
Object ret = m.invoke(o, params);
@SuppressWarnings("unchecked")
T r = (T) ret;
return r;
return (T) m.invoke(o, params);
} catch (NoSuchInterfaceException e) {
log.severe("Unable to get the service " + serviceName);
throw new Error("Unable to get the service " + serviceName, e);
warning(new FrascatiException("Unable to get the service '" + serviceName + "'", e));
return null;
} catch (SecurityException e) {
log.severe("Unable to get the method " + methodName);
throw new Error("Unable to get the method " + methodName, e);
warning(new FrascatiException("Unable to get the method '" + methodName + "'", e));
return null;
} catch (NoSuchMethodException e) {
log.severe("The service " + serviceName
+ " does not provide the method " + methodName + params);
throw new Error("The service " + serviceName
+ " does not provide the method " + methodName + params, e);
warning(new FrascatiException("The service '" + serviceName
+ "' does not provide the method " + methodName + params, e));
return null;
} catch (IllegalArgumentException e) {
log.warning(e.getMessage());
throw new Error(e);
warning(new FrascatiException(e));
return null;
} catch (IllegalAccessException e) {
log.warning(e.getMessage());
throw new Error(e);
warning(new FrascatiException(e));
return null;
} catch (InvocationTargetException e) {
log.warning(e.getMessage());
throw new Error(e);
warning(new FrascatiException(e));
return null;
}
}
......@@ -233,7 +238,7 @@ public class Launcher extends AbstractLoggeable {
* Close the launched composite.
*
*/
public void close()
public final void close()
{
try {
log.info("Closing the SCA composite '" + compositeName + "'...");
......
......@@ -71,6 +71,9 @@ import org.ow2.frascati.util.FrascatiException;
*/
public class ManifestLauncher {
protected ManifestLauncher() {
}
public static void main(String[] args) throws IOException {
// mainComposite : the name of the main composite file
// mainService: the name of the main service
......
......@@ -43,14 +43,14 @@ public interface ClassLoaderManager {
*
* @return the used class loader.
*/
public URLClassLoader getClassLoader();
URLClassLoader getClassLoader();
/**
* Set the class loader used by the OW2 FraSCAti Assembly Factory.
*
* @param classLoader the class loader to use.
*/
public void setClassLoader(URLClassLoader classLoader);
void setClassLoader(URLClassLoader classLoader);
/**
* Load libraries into the class loader of the OW2 FraSCAti Assembly Factory.
......@@ -58,6 +58,6 @@ public interface ClassLoaderManager {
* @param urls URLs of the libraries to load.
* @throws ManagerException thrown when a problem obscurs.
*/
public void loadLibraries(URL[] urls) throws ManagerException;
void loadLibraries(URL[] urls) throws ManagerException;
}
......@@ -51,7 +51,7 @@ public interface CompositeManager {
* @throws ManagerException thrown when a problem occurs.
* @since 1.1
*/
public Component[] getContribution(String contribution) throws ManagerException;
Component[] getContribution(String contribution) throws ManagerException;
/**
* Load an SCA composite and create the FraSCAti composite instance.
......@@ -62,7 +62,7 @@ public interface CompositeManager {
* @return the resulting FraSCAti composite instance.
* @throws ManagerException thrown when a problem occurs.
*/
public Component getComposite(String composite) throws ManagerException;
Component getComposite(String composite) throws ManagerException;
/**
* Load an SCA composite and create the FraSCAti composite instance.
......@@ -74,7 +74,7 @@ public interface CompositeManager {
* @return the resulting FraSCAti composite instance.
* @throws ManagerException thrown when a problem occurs.
*/
public Component getComposite(String composite, URL[] libs) throws ManagerException;
Component getComposite(String composite, URL[] libs) throws ManagerException;
/**
* Load an SCA composite and create the associate FraSCAti composite instance.
......@@ -85,7 +85,7 @@ public interface CompositeManager {
* @return the resulting FraSCAti composite instance.
* @throws ManagerException thrown when a problem occurs.
*/
public Component getComposite(QName qname) throws ManagerException;
Component getComposite(QName qname) throws ManagerException;
/**
* Load an SCA composite and create the associate FraSCAti composite instance.
......@@ -96,7 +96,7 @@ public interface CompositeManager {
* @return the resulting FraSCAti composite instance.
* @throws ManagerException thrown when a problem occurs.
*/
public Component processComposite(QName qname, ProcessingContext processingContext) throws ManagerException;
Component processComposite(QName qname, ProcessingContext processingContext) throws ManagerException;
/**
* Register a composite to the top level domain composite.
......@@ -104,7 +104,7 @@ public interface CompositeManager {
* @param composite composite to register to the top level domain composite.
* @throws ManagerException thrown when a problem occurs.
*/
public void addComposite(Component composite) throws ManagerException;
void addComposite(Component composite) throws ManagerException;
/**
* Remove a composite from the top level domain composite.
......@@ -112,27 +112,27 @@ public interface CompositeManager {
* @param name name of the composite to remove.
* @throws ManagerException thrown when a problem occurs.
*/
public void removeComposite(String name) throws ManagerException;
void removeComposite(String name) throws ManagerException;
/**
* Get all the composite names contained by the top level domain composite.
*
* @return all the composite names contained by the top level domain composite.
*/
public String[] getCompositeNames();
String[] getCompositeNames();
/**
* Get all composites contained by the top level domain composite.
*
* @return all composites contained by the top level domain composite.
*/
public Component[] getComposites();
Component[] getComposites();
/**
* Get the top level domain composite.
*
* @return the top level domain composite.
*/
public Component getTopLevelDomainComposite();
Component getTopLevelDomainComposite();
}
......@@ -42,7 +42,7 @@ public interface Processor<ElementType> {
*
* @return the processor ID.
*/
public String getProcessorID();
String getProcessorID();
/**
* Check an element.
......@@ -51,7 +51,7 @@ public interface Processor<ElementType> {
* @param processingContext the processing context to use.
* @throws ProcessorException thrown when a problem obscurs.
*/
public void check(ElementType element,
void check(ElementType element,
ProcessingContext processingContext) throws ProcessorException;
/**
......@@ -61,7 +61,7 @@ public interface Processor<ElementType> {
* @param processingContext the processing context to use.
* @throws ProcessorException thrown when a problem obscurs.
*/
public void generate(ElementType element,
void generate(ElementType element,
ProcessingContext processingContext) throws ProcessorException;
/**
......@@ -72,7 +72,7 @@ public interface Processor<ElementType> {
* @return an instantiated element.
* @throws ProcessorException thrown when a problem obscurs.
*/
public void instantiate(ElementType element,
void instantiate(ElementType element,
ProcessingContext processingContext) throws ProcessorException;
/**
......@@ -82,7 +82,7 @@ public interface Processor<ElementType> {
* @param processingContext the processing context to use.
* @throws ProcessorException thrown when a problem obscurs.
*/
public void complete(ElementType element,
void complete(ElementType element,
ProcessingContext processingContext) throws ProcessorException;
}
......@@ -41,14 +41,14 @@ public class ProcessorException extends FrascatiException {
/**
* The element associated to this exception.
*/
protected Object element;
private Object element;
/**
* Get the element associated to this exception.
*
* @return the element associated to this exception.
*/
public Object getElement() {
public final Object getElement() {
return this.element;
}
......
......@@ -61,7 +61,7 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
// but keys are Multiplicity instead of String
// and key null values are defined also.
final private static Map<Multiplicity,Boolean> contingencies =
private static final Map<Multiplicity,Boolean> CONTINGENCIES =
new HashMap<Multiplicity,Boolean>() {
private static final long serialVersionUID = 1306190174200744738L;
{
......@@ -72,7 +72,7 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
put(Multiplicity._1N, TypeFactory.MANDATORY);
}};
final private static Map<Multiplicity, Boolean> cardinalities =
private static final Map<Multiplicity, Boolean> CARDINALITIES =
new HashMap<Multiplicity, Boolean>() {
private static final long serialVersionUID = 1306190174200744738L;
{
......@@ -91,7 +91,7 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
* @see AbstractIntentProcessor#addIntentHandler(EObjectType, SCABasicIntentController, IntentHandler)
*/
@Override
protected void addIntentHandler(EObjectType baseReference,
protected final void addIntentHandler(EObjectType baseReference,
SCABasicIntentController intentController,
IntentHandler intentHandler)
throws NoSuchInterfaceException
......@@ -102,7 +102,7 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
/**
* @see org.ow2.frascati.assembly.factory.api.Processor#check(ElementType, ProcessingContext)
*/
protected void checkBaseReference(EObjectType baseReference, ProcessingContext processingContext)
protected final void checkBaseReference(EObjectType baseReference, ProcessingContext processingContext)
throws ProcessorException
{
// Check the base reference name.
......@@ -119,13 +119,13 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
// Is this composite an intent (service Intent of interface IntentHandler)?
// Check the base reference interface.
checkMustBeDefined(baseReference, "sca:interface", baseReference.getInterface(), interfaceProcessor, processingContext);
checkMustBeDefined(baseReference, SCA_INTERFACE, baseReference.getInterface(), getInterfaceProcessor(), processingContext);
// TODO check callback
// interfaceProcessor.check(baseReference.getCallback(), processingContext);
// Check the base reference bindings.
check(baseReference, "sca:binding", baseReference.getBinding(), bindingProcessor, processingContext);
check(baseReference, SCA_BINDING, baseReference.getBinding(), getBindingProcessor(), processingContext);
}
/**
......@@ -135,7 +135,7 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
throws ProcessorException
{
// Generate the base reference interface.
generate(baseReference, "sca:interface", baseReference.getInterface(), interfaceProcessor, processingContext);
generate(baseReference, SCA_INTERFACE, baseReference.getInterface(), getInterfaceProcessor(), processingContext);
// Corresponding Java interface class name.
String interfaceClassName = processingContext.getData(baseReference.getInterface(), JavaClass.class)
......@@ -147,10 +147,10 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
Multiplicity multiplicity = baseReference.getMultiplicity();
// Indicates if the interface is mandatory or optional.
boolean contingency = contingencies.get(multiplicity);
boolean contingency = CONTINGENCIES.get(multiplicity);
// indicates if component interface is single or collection.
boolean cardinality = cardinalities.get(multiplicity);
boolean cardinality = CARDINALITIES.get(multiplicity);
// Create the Fractal interface type.
InterfaceType interfaceType;
......@@ -159,7 +159,7 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
+ " and cardinality = " + cardinality;
try {
logDo(baseReference, logMessage);
interfaceType = typeFactory.createInterfaceType(interfaceName, interfaceClassName,
interfaceType = getTypeFactory().createInterfaceType(interfaceName, interfaceClassName,
TypeFactory.CLIENT, contingency, cardinality);
logDone(baseReference, logMessage);
} catch (FactoryException te) {
......@@ -174,14 +174,14 @@ public abstract class AbstractBaseReferencePortIntentProcessor<EObjectType exten
/**
* @see org.ow2.frascati.assembly.factory.api.Processor#complete(ElementType, ProcessingContext)
*/
protected void completeBaseReference(EObjectType baseReference, ProcessingContext processingContext)
protected final void completeBaseReference(EObjectType baseReference, ProcessingContext processingContext)