perfsonar-dev - perfsonar: r2734 - in branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar: base/ant base/auxiliary base/auxiliary/components/configuration/properties base/auxiliary/components/logger/log4j service service/base/container/listeners service/base/engine service/base/web
Subject: perfsonar development work
List archive
perfsonar: r2734 - in branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar: base/ant base/auxiliary base/auxiliary/components/configuration/properties base/auxiliary/components/logger/log4j service service/base/container/listeners service/base/engine service/base/web
Chronological Thread
- From:
- To:
- Subject: perfsonar: r2734 - in branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar: base/ant base/auxiliary base/auxiliary/components/configuration/properties base/auxiliary/components/logger/log4j service service/base/container/listeners service/base/engine service/base/web
- Date: Mon, 27 Aug 2007 10:42:40 -0400
Author: michael.bischoff
Date: 2007-08-27 10:42:39 -0400 (Mon, 27 Aug 2007)
New Revision: 2734
Removed:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/lookupService/
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/ant/antlib.xml
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/AuxiliaryComponentManager.java
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/configuration/properties/PropertiesConfigurationComponent.java
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/logger/log4j/Log4jLoggerComponent.java
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/container/listeners/StartupInitializer.java
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ActionType.java
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ServiceEngine.java
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/web/RequestHandler.java
Log:
fixed threading issues,
fixed consistent lookup of resources,
fixed correct location of configuration files.
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/ant/antlib.xml
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/ant/antlib.xml
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/ant/antlib.xml
2007-08-27 14:42:39 UTC (rev 2734)
@@ -102,7 +102,9 @@
destdir="${basedir}/@{dstdir}"
deprecation="yes"
source="1.5"
- target="1.5">
+ target="1.5"
+ debug="true"
+ debuglevel="lines, vars, and source">
<include name="**/*.java" />
<classpath>
<fileset dir="${basedir}/@{libdir}">
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/AuxiliaryComponentManager.java
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/AuxiliaryComponentManager.java
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/AuxiliaryComponentManager.java
2007-08-27 14:42:39 UTC (rev 2734)
@@ -1,17 +1,19 @@
package org.perfsonar.base.auxiliary;
+import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
-import java.net.URL;
-import java.util.Iterator;
+import java.util.logging.Logger;
import org.perfsonar.base.exceptions.PerfSONARException;
/**
- * AuxiliaryComponentManager is a container for components - modules of
+ * AuxiliaryComponentManager is a register for components - modules of
* the application. It loads components mapping from its configuration file,
* defined in components.properties system property.
*
@@ -20,122 +22,99 @@
*
* @author Maciej Glowiak
* @author Uros Juvan (added componentsIterator)
- *
+ * @author Michael Bischoff
*/
public class AuxiliaryComponentManager {
-
-
+
+ private final static Logger logger =
Logger.getLogger(AuxiliaryComponentManager.class.getName());
+
// -----------------------------------------------------------------
Static
-
/**
* Static reference to Auxiliary Component Manager
*/
- private static AuxiliaryComponentManager componentManager = null;
-
-
+ private static AuxiliaryComponentManager componentManager = new
AuxiliaryComponentManager();
+
+ private static volatile Object initialiseMutex = new Object();
+
+ private static boolean initialised;
+ private static boolean isInitialising;
+
// ----------------------------------------------------- Instance
variables
-
-
/**
* Map of components (component_name, component object reference)
*/
private Map<String,AuxiliaryComponent> components;
-
+
+ private String webInfLocation;
// ------------------------------------------------------------
Constructor
-
-
/**
* Private constructor - object can be created by static getInstance()
* method (singleton pattern) only
*/
- private AuxiliaryComponentManager() {
-
+ private AuxiliaryComponentManager() {
components = new HashMap<String,AuxiliaryComponent>();
-
}
-
-
+
// --------------------------------------------------------- Public
methods
-
-
/**
* Set up the manager. Load mappings from components.properties file
* and creates all component objects
*/
- public void initManager() throws PerfSONARException {
-
-//read components from Configuration file and init them
-
+ public boolean initManager() throws PerfSONARException {
+ //read components from Configuration file and init them
//String configFileName =
"/projects/sonar/perfsonar/conf/components.properties";
- URL urlString =
this.getClass().getClassLoader().getResource("perfsonar/conf/components.properties");
+ URL urlString = lookupFile("/perfsonar/conf/components.properties");
+
+ if(urlString==null || urlString.toString()==null) {
+ throw new PerfSONARException(
+ "AuxiliaryComponentManager.initManager: " +
+ "Missing or unable to locate components.properties file.
Cannot continue"
+ );
+ }
- if(urlString==null || urlString.toString()==null) {
-
- throw new
PerfSONARException("AuxiliaryComponentManager.initManager: " +
- "Missing or unable to locate components.properties file.
Cannot continue");
- }
String configFileName = urlString.getFile();
- System.out.println("AuxiliaryComponentManager.initManager: " +
- "Read config file name as: "+configFileName);
+ logger.info("AuxiliaryComponentManager.initManager: Read config file
name as: "+configFileName);
-
try {
-
//read configfile into properties (name, value)
- ArrayList<ComponentsConfigurationReader.Entry> properties =
- new ComponentsConfigurationReader().
- readFile(configFileName);
+ ArrayList<ComponentsConfigurationReader.Entry> properties = new
ComponentsConfigurationReader().readFile(configFileName);
//for each entry in properties - create and initialize component
for (ComponentsConfigurationReader.Entry e : properties) {
-
String componentName = e.getName();
String componentClassName = e.getValue();
-
- try {
-
- //TODO: remove -- debug
- System.err.println("AUX ComponentManager create
["+componentName+
- "] = "+componentClassName);
-
- AuxiliaryComponent component =
- loadComponent(componentName, componentClassName);
-
- System.err.println("AUX ComponentManager: init
["+componentName+"]");
- //initialize component
-
- component.initComponent();
-
+ try {
+ logger.fine("AUX ComponentManager creating
["+componentName+"] = "+componentClassName);
+ AuxiliaryComponent component =
loadComponent(componentName, componentClassName);
+ logger.finer("AUX ComponentManager: init
["+componentName+"]");
+ component.initComponent();
} catch (PerfSONARException ex) {
- System.err.println(ex.getMessage());
- //do nothing - not critical exception, just load other
- //modules
- }
-
-
- }
-
+
logger.throwing(AuxiliaryComponentManager.class.getName(), "initmanager()",
ex);
+ //do nothing - not critical exception, just load other
modules
+ }
+ }
} catch (FileNotFoundException e) {
- throw new
PerfSONARException("error.common.manager.no_configuration",
- "AuxiliaryComponentManager: Cannot load " +
- "component.properties file. " +
- "Encountered FileNotFound Exception. Message: "
- +e.getMessage());
+ throw new PerfSONARException(
+ "error.common.manager.no_configuration",
+ "AuxiliaryComponentManager: Cannot load
component.properties file, because the file could not be found",
+ e
+ );
} catch (IOException e) {
- throw new
PerfSONARException("error.common.manager.no_configuration",
- "AuxiliaryComponentManager: Cannot load " +
- "component.properties file. " +
- "Encountered IO Exception. Message: "
- +e.getMessage());
+ throw new PerfSONARException(
+ "error.common.manager.no_configuration",
+ "AuxiliaryComponentManager: Cannot load
component.properties file. Encountered IO Exception.",
+ e
+ );
}
-
+
+ return true;
}
+
-
- /**
+ /**
* This method loads a component. It creates Component object, adds
* it to components map and initializes it.
*
@@ -181,19 +160,15 @@
}
-
/**
* Put component into component map
* @param name name of the component
* @param component object
*/
- private void setComponent(String name, AuxiliaryComponent component) {
-
- components.put(name, component);
-
+ protected void setComponent(String name, AuxiliaryComponent component) {
+ components.put(name, component);
}
-
/**
* Return component for specified mapping
* @param name name of the component
@@ -205,7 +180,6 @@
}
-
/**
* Return key,value pair iterator over components Map.
* @return iterator over pair Map.Entry<String, AuxiliaryComponent>
@@ -216,26 +190,29 @@
}
-
// --------------------------------------------------------- static
methods
-
-
/**
* Method creates Auxiliary Component Manager and load all components.
* It's singleton pattern
+ *
+ * TODO remove exception from signature.
*/
- public static AuxiliaryComponentManager getInstance()
- throws PerfSONARException {
-
- if (componentManager == null) {
- componentManager= new AuxiliaryComponentManager();
- componentManager.initManager();
- }
+ public static AuxiliaryComponentManager getInstance() throws
PerfSONARException {
+ // initialation must be lazy because of AxisMessageContext.
+ if(!initialised) {
+ synchronized (initialiseMutex) {
+ // avoid other threads from also initialising
+ // defend against endless loop. (calling
getInstance() within initManager();)
+ if(!initialised && !isInitialising) {
+ isInitialising = true;
+ initialised = componentManager.initManager();
+ isInitialising = false;
+ }
+ }
+ }
return componentManager;
-
}
-
/**
* For standalone applications
*/
@@ -245,6 +222,56 @@
AuxiliaryComponentManager.getInstance();
}
+
+ /**
+ * Profides a consistent way to lookup files within perfsonar.
+ * @return URL containting the path or null if not found.
+ */
+ public static URL lookupFile(String file) {
+ logger.fine("Looking up file: "+file);
+
+ if(file == null) {
+ return null;
+ }
+
+ logger.finer("WEB-INF:"+componentManager.webInfLocation);
+ URL url = null;
+ // try WEB-INF
+ if(componentManager.webInfLocation != null) {
+ if(file.startsWith("/")) {
+ url = componentManager.getClass().getResource(
+ componentManager.webInfLocation + file
+ );
+ } else {
+ url = componentManager.getClass().getResource(
+
componentManager.webInfLocation + "/" + file
+ );
+ }
+ }
+ // return url or try absolute
+ return (url!=null) ? url :
componentManager.getClass().getResource(file);
+ }
+ public static void setWebInfLocation(String location) {
+ logger.fine("WEB-INF location set to: " +location);
+ // normalise
+ String normalisedLocation = slashify(location);
+ logger.finer("location normalised to: "+normalisedLocation);
+
+ if(location.endsWith(File.pathSeparator) || location.endsWith("/")) {
+ componentManager.webInfLocation =
normalisedLocation.substring(0, location.length()-1);
+ } else {
+ componentManager.webInfLocation = normalisedLocation;
+ }
+ }
+ private static String slashify(String path) {
+ String p = path;
+ if (File.separatorChar != '/')
+ p = p.replace(File.separatorChar, '/');
+ if (!p.startsWith("/"))
+ p = "/" + p;
+ return p;
+ }
+
} //AuxiliaryComponentManager
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/configuration/properties/PropertiesConfigurationComponent.java
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/configuration/properties/PropertiesConfigurationComponent.java
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/configuration/properties/PropertiesConfigurationComponent.java
2007-08-27 14:42:39 UTC (rev 2734)
@@ -4,8 +4,10 @@
import java.io.FileInputStream;
import java.net.URL;
import java.util.Properties;
+import java.util.logging.Logger;
import org.perfsonar.base.auxiliary.AuxiliaryComponent;
+import org.perfsonar.base.auxiliary.AuxiliaryComponentManager;
import
org.perfsonar.base.auxiliary.components.configuration.ConfigurationComponent;
import org.perfsonar.base.exceptions.PerfSONARException;
@@ -19,6 +21,7 @@
public class PropertiesConfigurationComponent
implements AuxiliaryComponent, ConfigurationComponent {
+ private Logger logger =
Logger.getLogger(PropertiesConfigurationComponent.class.getName());
// ---------------------------------- class fields
@@ -26,58 +29,46 @@
* Component Name
*/
private String componentName = null;
-
-
+
/**
* Properties
*/
private Properties properties = null;
-
-
+
/**
* File with properties
*/
private File file = null;
-
// ---------------------------------- constructor
-
-
public void initComponent() throws PerfSONARException {
-
// bootstrapped absolute file location in catalina.properties file
- URL fileNameURL =
this.getClass().getClassLoader().getResource("perfsonar/conf/service.properties");
-
- if(fileNameURL==null) {
+ URL fileNameURL =
AuxiliaryComponentManager.lookupFile("/perfsonar/conf/service.properties");
+
+ if(fileNameURL==null) {
+ logger.severe("PropertiesConfigurationComponent.initComponent:" +
+ " Missing or unable to locate service.properties file. Cannot
initialise PropertiesConfigurationComponent.");
-
System.out.println("PropertiesConfigurationComponent.initComponent:" +
- " Missing or unable to locate service.properties file. Cannot
complete request.");
-
throw new
PerfSONARException("PropertiesConfigurationComponent.initComponent:" +
- " Missing or unable to locate service.properties file. Cannot
complete request.");
+ " Missing or unable to locate service.properties file. Cannot
initialise PropertiesConfigurationComponent.");
}
String fileName = fileNameURL.getFile();
+ logger.info("PropertiesConfigurationComponent.initComponent: Read
the file name as: "+fileName);
- System.out.println("PropertiesConfigurationComponent.initComponent:"
+
- "Read the file name as: "+fileName);
-
// create a new file object with the read file name
try {
-
file = new File(fileName);
-
} catch(NullPointerException npe) {
- throw new PerfSONARException("PropertiesConfigurationComponent: "
- + "Unable to create a file object"
- + "referring to service.properties" ,
- npe);
+ throw new PerfSONARException(
+ "PropertiesConfigurationComponent.initComponent: "
+ + "Unable to create a file object referring to
service.properties" ,
+ npe
+ );
}
// load configuration
this.loadConfiguration();
-
-
}
@@ -93,9 +84,7 @@
* @throws PerfSONARException when properties couldn't be read
*/
public Properties getProperties() throws PerfSONARException {
-
return properties;
-
}
@@ -108,18 +97,21 @@
* @throws PerfSONARException when property couldn't be read
*/
public String getProperty(String name) throws PerfSONARException {
-
+ if(getProperties()==null) {
+ throw new PerfSONARException(
+ "PropertiesConfigurationComponent.getProperty: "
+ + "PropertiesConfigurationComponent wasn't initialised
correctly, can't retrieve."
+ );
+ }
+
String property = properties.getProperty(name);
-
if (property == null) {
throw new PerfSONARException(
"PropertiesConfigurationComponent.getProperty: "
+ "Requested Param: "+ name + "not found");
}
-
return property;
-
}
@@ -129,26 +121,19 @@
}
-
public void setComponentName(String componentName) {
-
this.componentName = componentName;
-
}
// ---------------------------------- private methods
-
-
/**
* Operation which loads configuration
* from a property file
*
*/
void loadConfiguration() throws PerfSONARException {
-
try {
-
properties = new Properties();
// create an input stream to read the config file
@@ -159,11 +144,9 @@
// close the input stream
in.close();
-
} catch (Exception e) {
throw new PerfSONARException(e.getMessage());
}
-
}
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/logger/log4j/Log4jLoggerComponent.java
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/logger/log4j/Log4jLoggerComponent.java
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/base/auxiliary/components/logger/log4j/Log4jLoggerComponent.java
2007-08-27 14:42:39 UTC (rev 2734)
@@ -1,13 +1,15 @@
package org.perfsonar.base.auxiliary.components.logger.log4j;
+import java.net.URL;
+
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
-
import org.perfsonar.base.auxiliary.AuxiliaryComponent;
import org.perfsonar.base.auxiliary.AuxiliaryComponentManager;
import org.perfsonar.base.auxiliary.ComponentNames;
import
org.perfsonar.base.auxiliary.components.configuration.ConfigurationComponent;
import org.perfsonar.base.auxiliary.components.logger.LoggerComponent;
+import org.perfsonar.base.exceptions.PerfSONARException;
/**
* This class is responsible for logging functionality.
@@ -35,35 +37,33 @@
ConfigurationComponent configuration;
// ---------------------------------- constructors
+
+ /* Default no arg cosntructor */
+ public Log4jLoggerComponent() {
+ }
-
// ---------------------------------- public methods
-
-
-
-
-
-
- public void initComponent() {
-
+ public void initComponent() throws PerfSONARException {
try {
-
debugLog = Logger.getLogger("sonar-debug");
infoLog = Logger.getLogger("sonar-info");
warnLog = Logger.getLogger("sonar-warn");
errorLog = Logger.getLogger("sonar-error");
fatalLog = Logger.getLogger("sonar-fatal");
- configuration =
(ConfigurationComponent)AuxiliaryComponentManager.
- getInstance().getComponent(ComponentNames.CONFIG);
+ configuration =
(ConfigurationComponent)AuxiliaryComponentManager.getInstance().getComponent(ComponentNames.CONFIG);
- String logconf = configuration.getProperty(
- "service.log.log4j.config");
+ String logconf =
configuration.getProperty("service.log.log4j.config");
+ URL logConfiguration =
AuxiliaryComponentManager.lookupFile(logconf);
- PropertyConfigurator.configure(logconf);
-
+ if(logConfiguration!=null) {
+ PropertyConfigurator.configure(logConfiguration);
+ } else {
+ System.err.println("PerfSONAR:WARNING: perfsonar logging not
configured correctly. (specified logging properties file not found.)");
+ }
} catch(Exception ex) {
+ System.err.println("PerfSONAR:WARNING: perfsonar logging not
configured correctly.");
ex.printStackTrace();
}
@@ -82,69 +82,47 @@
}
-
/* (non-Javadoc)
* @see
org.perfsonar.commons.auxiliary.components.logger.log4j.LoggerComponent#debug(java.lang.String)
*/
public void debug(String message) {
-
debugLog.debug(message);
-
}
-
-
/* (non-Javadoc)
* @see
org.perfsonar.commons.auxiliary.components.logger.log4j.LoggerComponent#info(java.lang.String)
*/
public void info(String message) {
-
infoLog.info(message);
-
}
-
/* (non-Javadoc)
* @see
org.perfsonar.commons.auxiliary.components.logger.log4j.LoggerComponent#warn(java.lang.String)
*/
public void warn(String message) {
-
warnLog.warn(message);
-
}
-
/* (non-Javadoc)
* @see
org.perfsonar.commons.auxiliary.components.logger.log4j.LoggerComponent#error(java.lang.String)
*/
public void error(String message) {
-
errorLog.error(message);
-
}
-
/* (non-Javadoc)
* @see
org.perfsonar.commons.auxiliary.components.logger.log4j.LoggerComponent#fatal(java.lang.String)
*/
public void fatal(String message) {
-
fatalLog.fatal(message);
-
}
public String getComponentName() {
-
return componentName;
-
}
public void setComponentName(String componentName) {
-
this.componentName = componentName;
-
}
-
-
} //Log4jLoggerComponent
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/container/listeners/StartupInitializer.java
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/container/listeners/StartupInitializer.java
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/container/listeners/StartupInitializer.java
2007-08-27 14:42:39 UTC (rev 2734)
@@ -22,27 +22,22 @@
*/
public class StartupInitializer implements ServletContextListener {
-
- // --------------------------------------------------------- Public
methods
-
-
+ // --------------------------------------------------------- Public
methods
/**
* Context initialized event handler.
*
* @param sce servlet context event
*/
public void contextInitialized(ServletContextEvent sce) {
-
+ String location = sce.getServletContext().getRealPath("/WEB-INF/");
+ AuxiliaryComponentManager.setWebInfLocation(location);
+
try {
-
AuxiliaryComponentManager.getInstance();
-
} catch (Exception e) {
-
System.err.println("Error initializing perfSONAR: " + e);
-
- }
-
+ e.printStackTrace();
+ }
}
/**
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ActionType.java
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ActionType.java
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ActionType.java
2007-08-27 14:42:39 UTC (rev 2734)
@@ -117,5 +117,4 @@
public static final String SETUP_DATA_DB = "SETUP_DATA_DB";
-
} //ActionType
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ServiceEngine.java
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ServiceEngine.java
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/engine/ServiceEngine.java
2007-08-27 14:42:39 UTC (rev 2734)
@@ -32,17 +32,13 @@
* @param request Message object containing the request
* @return Message final result of of performed action
*
- * @throws SystemException if the system encountered an internal error
+ * @throws PerfSONARException if the system encountered an internal
error
* while satisfying this request
- * @throws ResourceException if insufficient resources are available or
- * if the user's credentials do not grant him enough
resources
- * @throws DataFormatException if an error was encountered while
getting data
- *
+ *
* @see org.perfsonar.service.commons.engine.ActionType
* @see org.perfsonar.service.commons.engine.ServiceEngineRequest
* @see org.perfsonar.service.commons.engine.ServiceEngineResponse
*
*/
- public Message takeAction(String actionType, Message request)
- throws PerfSONARException;
+ public Message takeAction(String actionType, Message request) throws
PerfSONARException;
}
Modified:
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/web/RequestHandler.java
===================================================================
---
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/web/RequestHandler.java
2007-08-27 14:28:14 UTC (rev 2733)
+++
branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar/service/base/web/RequestHandler.java
2007-08-27 14:42:39 UTC (rev 2734)
@@ -7,19 +7,18 @@
package org.perfsonar.service.base.web;
-import org.ggf.ns.nmwg.base.v2_0.Message;
+import java.net.URL;
+import org.ggf.ns.nmwg.base.v2_0.Message;
import org.perfsonar.base.auxiliary.AuxiliaryComponentManager;
import org.perfsonar.base.auxiliary.ComponentNames;
import
org.perfsonar.base.auxiliary.components.configuration.ConfigurationComponent;
import org.perfsonar.base.auxiliary.components.logger.LoggerComponent;
+import org.perfsonar.base.exceptions.PerfSONARException;
import org.perfsonar.base.util.XMLUtils;
-
import org.perfsonar.service.base.messages.MessageHandler;
import org.perfsonar.service.base.messages.MessageHandlerFactory;
-import org.perfsonar.base.exceptions.PerfSONARException;
import org.perfsonar.service.base.util.ResultCodesUtil;
-
import org.w3c.dom.Document;
@@ -28,37 +27,24 @@
*
* @author Loukik
* @author Maciej Glowiak
+ * @author Michael Bischoff
*/
-
public class RequestHandler {
-
-
// ----------------------------------------------------- constants
-
-
-
private static final String INIT_SERVICE_MESSAGE_TYPE = "InitService";
-
-
private static final String SAX_PARSER_CONFIG =
"service.sax_parser.config";
-
-
// -------------------------------------------- instance variables
+ private AuxiliaryComponentManager componentManager;
+ private ConfigurationComponent configuration;
+ private LoggerComponent logger;
+ private String saxParserConfig;
+ private boolean initialised;
+ private volatile Object initialisationMutex = new Object();
-
-
- private ConfigurationComponent configuration = null;
-
- private LoggerComponent logger = null;
-
-
-
// ---------------------------------- public methods
-
-
/**
* Accepts web service based calls and gives back responses
*
@@ -70,166 +56,162 @@
* are being catched and converted to PerfSONAR result codes
*/
public Document acceptCall(Document request) {
-
- /*
- *
- * The first invocation of AuxiliaryComponentManager.getInstance()
- * creates the static instance of AuxiliaryComponentManager.
- *
- */
-
try {
-
- //get configuration component (and create Aux Comp Manager
-
- configuration =
(ConfigurationComponent)AuxiliaryComponentManager.
- getInstance().getComponent(ComponentNames.CONFIG);
-
- if (configuration == null) throw new PerfSONARException(
- "error.common.no_configuration",
- "Cannot read configuration file");
-
- //get logger component
-
- logger = (LoggerComponent)AuxiliaryComponentManager.
- getInstance().getComponent(ComponentNames.LOGGER);
-
- if (logger == null) throw new PerfSONARException(
- "error.common.no_logger",
- "Cannot find logger component");
-
- logger.info("RequestHandler: Service received a request");
- logger.debug("RequestHandler: " +
- "Calling on parser to parse the request");
-
- //convert input Document into Message - requestMessage
-
- Message requestMessage = null;
- try {
-
- requestMessage = XMLUtils.convertToMessage(
- request,
- configuration.getProperty(SAX_PARSER_CONFIG));
-
- } catch (Exception e) {
-
- throw new PerfSONARException("error.common.parse_error",
- "Parse/validation error, " +
- "Cannot convert request to Message. " +
- "Nested error messsage was "+e.getMessage());
-
- }
-
- //if parsed well
-
- if (requestMessage!=null) {
-
- logger.debug("RequestHandler: " +
- "Message object constructed from request. " +
- "Request is of type: " +
- requestMessage.getType());
-
- /*
- * Check if it is not InitService message only
- */
- if (INIT_SERVICE_MESSAGE_TYPE.
- equalsIgnoreCase(requestMessage.getType())) {
-
- //if so, response that service was set up correctly
- throw new PerfSONARException("service.common.success",
- "Service initialized correctly");
-
- }
-
-
- // Call on the MessageHandlerFactory to get the appropriate
- // message handler for this type of message.
-
- MessageHandler messageHandler =
- MessageHandlerFactory.getMessageHandler(
- requestMessage.getType());
-
- // Call on the mh to "execute" the message and return
- // a response message.
- logger.debug(
- "RequestHandler: " +
- "Calling on MessageHandler ["+
- messageHandler.getClass()+"] to satisfy request");
-
-
- //run proper message handler,
- //which will run ServiceEngine
- //*****************************************************
-
- Message responseMessage = messageHandler.execute(
- requestMessage);
-
- //*****************************************************
-
- // convert response Message into Document
- Document responseDocument = XMLUtils.
- convertMessageToDOM( responseMessage );
-
- //if everything went fine, return response document to Axis
-
- logger.info("RequestHandler: Service sent a successful
response");
-
- return responseDocument;
-
-
- } else {
-
- //if anything went wrong with parsing, throw exception
- logger.error("RequestHandler: " +
- "Message object constructed as null from request");
-
- throw new PerfSONARException("error.common.parse_error",
- "RequestHandler: " +
- "Server could not construct a message object " +
- "from the request due to unknown error. " +
- "Please check request and try again");
- }
-
-
+ if(!initialised) init();
+ checkAuxiliaryComponents();
+ return handleCall(request);
} catch (PerfSONARException pex) {
-
- /*
- * If any error occured
- * response with result/error code
- */
-
+ /* If any error occured response with result/error code */
//create common result code
-
- Message responseMessage =
- ResultCodesUtil.createResultCodeMetadata(
- null,
- pex);
-
+ Message responseMessage =
ResultCodesUtil.createResultCodeMetadata(null, pex);
//convert it into Document
Document responseDocument = null;
try {
+ responseDocument =
XMLUtils.convertMessageToDOM(responseMessage);
+ } catch (PerfSONARException e) {
+ //or send null if impossible
+ logger.fatal(
+ "RequestHandler: Return null response - can
neither create " +
+ "response message nor error code message!"
+ );
+ }
+ logger.info("RequestHandler: Service sent a response with error
information");
+ return responseDocument;
+ }
+ }
- responseDocument = XMLUtils.convertMessageToDOM(
- responseMessage);
+ /**
+ * initialises this RequestHandler by creating and retrieving Aux
components
+ * @throws PerfSONARException
+ */
+ protected void init() throws PerfSONARException {
+ synchronized (initialisationMutex) {
+ if(!initialised) {
+ componentManager =
AuxiliaryComponentManager.getInstance();
+ configuration =
(ConfigurationComponent)componentManager.getComponent(ComponentNames.CONFIG);
+ logger =
(LoggerComponent)componentManager.getComponent(ComponentNames.LOGGER);
+ }
+ }
+ }
- } catch (PerfSONARException e) {
+ /**
+ * handles the call: convert the request and calls a
handleRequestMessage and returns the result.
+ * @param request
+ * @return
+ * @throws PerfSONARException
+ */
+ protected Document handleCall(Document request) throws
PerfSONARException {
+ logger.info("RequestHandler: Service received a request");
+ logger.debug("RequestHandler: Calling on parser to parse the
request");
+ //convert input Document into Message - requestMessage
+ Message requestMessage = convertMessage(request);
- //or send null if impossible
+ if (requestMessage==null) {
+ //something went wrong with parsing, throw exception
+ logger.error("RequestHandler: Message object constructed
as null from request");
+
+ throw new PerfSONARException(
+ "error.common.parse_error",
+ "RequestHandler: Server could not construct a
message object from " +
+ "the request due to unknown error. Please check
request and try again"
+ );
+ }
+
+ return handleRequestMessage(requestMessage);
+ }
- logger.fatal("RequestHandler: " +
- "Return null response - neither can create " +
- "response message nor error code message!");
+ /**
+ * Converts a Document to a Message
+ * @param request
+ * @return a converted requestMessage or null on failure
+ * @throws PerfSONARException
+ */
+ protected Message convertMessage(Document request) throws
PerfSONARException {
+ Message requestMessage = null;
+ try {
+ if(saxParserConfig == null) {
+ saxParserConfig =
attemptSaxParserConfigLookup();
+ }
+ requestMessage = XMLUtils.convertToMessage(request,
saxParserConfig);
+ } catch (Exception e) {
+ throw new PerfSONARException(
+ "error.common.parse_error",
+ "Parse/validation error, Cannot convert request to
Message.",
+ e
+ );
+ }
+ return requestMessage;
+ }
- }
+ protected Document handleRequestMessage(Message requestMessage)
throws PerfSONARException {
+ logger.debug(
+ "RequestHandler: Message object constructed from
request. Request is of type: " + requestMessage.getType()
+ );
- logger.info("RequestHandler: Service sent a response "
- + "with error information");
+ /* Check if it isn't just an InitService message */
+ if
(INIT_SERVICE_MESSAGE_TYPE.equalsIgnoreCase(requestMessage.getType())) {
+ //if so, response that service was set up correctly
+ throw new PerfSONARException(
+ "service.common.success",
+ "Service initialized correctly"
+ );
+ }
+
+ // Call on the MessageHandlerFactory to get the appropriate
+ // message handler for this type of message.
+ MessageHandler messageHandler =
MessageHandlerFactory.getMessageHandler(requestMessage.getType());
- return responseDocument;
+ logger.debug(
+ "RequestHandler: Calling on MessageHandler
["+messageHandler.getClass()+"] to satisfy request"
+ );
- }
+ // Call on the mh to "execute" the message and return
+ // a response message.
+ // run proper message handler, which will run ServiceEngine
+ //*****************************************************
- }
+ Message responseMessage =
messageHandler.execute(requestMessage);
+
+ //*****************************************************
+ // convert response Message into Document
+ Document responseDocument = XMLUtils.convertMessageToDOM(
responseMessage );
+
+ //if everything went fine, return response document to Axis
+ logger.info("RequestHandler: Service sent a successful
response");
+ return responseDocument;
+ }
+
+ protected void checkAuxiliaryComponents() throws PerfSONARException {
+ if (configuration == null) throw new PerfSONARException(
+ "error.common.no_configuration",
+ "Cannot read configuration file"
+ );
+ if (logger == null) throw new PerfSONARException(
+ "error.common.no_logger",
+ "Cannot find logger component"
+ );
+ }
+
+ private String attemptSaxParserConfigLookup() throws
PerfSONARException {
+ String config;
+
+ try {
+ config = configuration.getProperty(SAX_PARSER_CONFIG);
+ } catch (PerfSONARException e) {
+ logger.error("Sax parser config could not be found.
(service.sax_parser.config not specified.)");
+ throw e;
+ }
+
+ URL url = AuxiliaryComponentManager.lookupFile(config);
+ if(url!=null) {
+ return url.getFile();
+ }
+ logger.error("Sax parser config could not be found. (lookup
failed)");
+ throw new PerfSONARException(
+ "error.common.no_configuration",
+ "specified service.sax_parser.config not found"
+ );
+ }
} //RequestHandler
- perfsonar: r2734 - in branches/new-structure/trunk/perfsonar_base/src/main/java/org/perfsonar: base/ant base/auxiliary base/auxiliary/components/configuration/properties base/auxiliary/components/logger/log4j service service/base/container/listeners service/base/engine service/base/web, svnlog, 08/27/2007
Archive powered by MHonArc 2.6.16.