Permalink
Browse files

Configuration now uses also jndi environment parameters.

For example, in tomcat:

<Context path="/openLS"
docBase="......./openLS.war" reloadable="true" >       
        <Environment name="var_name" value="var_value"
type="java.lang.String" />      
</Context>
  • Loading branch information...
Delawen committed Feb 16, 2012
1 parent 169d87a commit f27e279b3f4c647cd57929e713e88fe775608431
@@ -28,7 +28,7 @@
<EndPoint>
<Position>
<gml:Point>
- <gml:pos>3.6507 40.4331</gml:pos>
+ <gml:pos>-3.6507 40.4331</gml:pos>
</gml:Point>
</Position>
</EndPoint>
@@ -1,55 +1,113 @@
package org.gofleet.configuration;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.DatabaseConfiguration;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+@Repository
+/**
+ *
+ * This class retrieves all the configuration from all sources posibles.
+ *
+ * Right now, it looks upon (on this order, lowest configurations may override upper):
+ * <ul>
+ * <li>Database</li>
+ * <li>Server context (likecontext.xml on Tomcat)</li>
+ * </ul>
+ *
+ * @author marias
+ *
+ */
public class Configuration {
private static AbstractConfiguration configuration = null;
private static org.apache.commons.logging.Log log = LogFactory
.getLog(Configuration.class);
@Autowired
- private static org.apache.commons.dbcp.BasicDataSource datasource;
+ private org.apache.commons.dbcp.BasicDataSource dataSource;
- private static AbstractConfiguration getConfiguration() {
- try {
- if (configuration == null)
- configuration = new DatabaseConfiguration(datasource,
+ private AbstractConfiguration getConfiguration() {
+ if (configuration == null) {
+
+ try {
+ configuration = new DatabaseConfiguration(dataSource,
"configuration", "key", "value");
- } catch (Throwable t) {
- log.error("Error loading configuration: " + t);
+ } catch (Throwable t) {
+ log.error("Error loading database configuration", t);
+ }
+
+ try {
+ InitialContext icontext = new InitialContext();
+ Context context = (Context) icontext.lookup("java:comp/env");
+ NamingEnumeration<NameClassPair> propiedadesJDNI = context
+ .list("");
+ while (propiedadesJDNI.hasMoreElements()) {
+ NameClassPair propiety = propiedadesJDNI.nextElement();
+ configuration.addProperty(propiety.getName(),
+ context.lookup(propiety.getName()));
+ log.trace("Configuring '" + propiety.getName() + "' as '"
+ + context.lookup(propiety.getName().toString())
+ + "'");
+ }
+
+ } catch (NamingException e) {
+ log.error("Error loading configuration from context: " + e, e);
+ }
+
}
+
return configuration;
}
- static public String get(String key, String value) {
+ /**
+ * @return the datasource
+ */
+ public org.apache.commons.dbcp.BasicDataSource getDataSource() {
+ return dataSource;
+ }
+
+ /**
+ * @param datasource
+ * the datasource to set
+ */
+ public void setDataSource(org.apache.commons.dbcp.BasicDataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ public String get(String key, String value) {
try {
return getConfiguration().getString(key, value);
} catch (Throwable t) {
return value;
}
}
- static public Boolean get(String key, Boolean value) {
+ public Boolean get(String key, Boolean value) {
try {
return getConfiguration().getBoolean(key, value);
} catch (Throwable t) {
return value;
}
}
-
- static public Double get(String key, Double value) {
+
+ public Double get(String key, Double value) {
try {
return getConfiguration().getDouble(key, value);
} catch (Throwable t) {
return value;
}
}
- static public Integer get(String key, Integer value) {
+ public Integer get(String key, Integer value) {
try {
return getConfiguration().getInteger(key, value);
} catch (Throwable t) {
@@ -32,44 +32,43 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.geotools.referencing.CRS;
-import org.gofleet.configuration.Configuration;
import org.gofleet.openLS.ddbb.GeoCoding;
import org.gofleet.openLS.ddbb.Routing;
import org.gofleet.openLS.util.MoNaVConnector;
import org.gofleet.openLS.util.OSRMConnector;
import org.gofleet.openLS.util.Utils;
-import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.xml.sax.SAXException;
/**
* Copyright (C) 2011, Emergya (http://www.emergya.es)
- *
+ *
* @author <a href="mailto:marias@emergya.es">María Arias</a>
- *
- * This file is part of GoFleet
- *
- * This software is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This software 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * As a special exception, if you link this library with other files to
- * produce an executable, this library does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * This exception does not however invalidate any other reasons why the
- * executable file might be covered by the GNU General Public License.
+ *
+ * This file is part of GoFleet
+ *
+ * This software is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This software 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ * As a special exception, if you link this library with other files to
+ * produce an executable, this library does not by itself cause the
+ * resulting executable to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons why the
+ * executable file might be covered by the GNU General Public License.
*/
@Controller(value = "openLSService")
@Scope("session")
@@ -83,6 +82,24 @@
@Resource
private GeoCoding geoCodingController;
+ @Autowired
+ private org.gofleet.configuration.Configuration configuration;
+
+ /**
+ * @return the configuration
+ */
+ public org.gofleet.configuration.Configuration getConfiguration() {
+ return configuration;
+ }
+
+ /**
+ * @param configuration the configuration to set
+ */
+ public void setConfiguration(
+ org.gofleet.configuration.Configuration configuration) {
+ this.configuration = configuration;
+ }
+
private MoNaVConnector monavConnector = new MoNaVConnector();
private OSRMConnector osrmConnector = new OSRMConnector();
@@ -168,7 +185,8 @@ else if (request instanceof DirectoryRequestType)
/**
* Calls the routing method
- * @param epsg
+ *
+ * @param epsg
*
* @param parameter
* @return
@@ -179,14 +197,21 @@ else if (request instanceof DirectoryRequestType)
List<AbstractResponseParametersType> list = new LinkedList<AbstractResponseParametersType>();
AbstractResponseParametersType arpt = null;
try {
- String conn = Configuration.get("RoutingConnector", "default");
+ String conn = configuration.get("RoutingConnector", "default");
if (conn.equals("PGROUTING"))
arpt = routingController.routePlan(param);
else if (conn.equals("MONAV"))
arpt = monavConnector.routePlan(param);
- else
- arpt = osrmConnector.routePlan(param);
-
+ else {
+
+ String host_port = configuration.get("OSRM_HOST",
+ "localhost:5000");
+ String http = "http";
+ if (configuration.get("OSRM_SSL", "off").equals("on"))
+ http = "https";
+ arpt = osrmConnector.routePlan(param, host_port, http);
+ }
+
} catch (Throwable t) {
LOG.error("Error on routePlan", t);
}
@@ -57,7 +57,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.gofleet.configuration.Configuration;
import org.gofleet.openLS.ddbb.GeoCoding;
import org.gofleet.openLS.ddbb.bean.HBA;
import org.gofleet.openLS.util.GeoUtil;
@@ -83,13 +82,22 @@
@Repository
public class RoutingDAO {
- private static final String GID_ROUTING = Configuration.get("ROUTING_ID",
- "gid");
- private static final String TABLE_ROUTING = Configuration.get(
- "ROUTING_TABLE", "routing");
+ @Autowired
+ private org.gofleet.configuration.Configuration configuration;
+ /**
+ * @return the configuration
+ */
+ public org.gofleet.configuration.Configuration getConfiguration() {
+ return configuration;
+ }
- private static final String EPSG_4326 = Configuration.get("ROUTING_EPSG",
- "EPSG:4326");
+ /**
+ * @param configuration the configuration to set
+ */
+ public void setConfiguration(
+ org.gofleet.configuration.Configuration configuration) {
+ this.configuration = configuration;
+ }
public GeometryFactory gf = new GeometryFactory();
@@ -143,10 +151,10 @@ public DetermineRouteResponseType doInHibernate(Session session)
}
stops.add(GeoUtil.getPoint(wayPointList.getEndPoint(), null));
- consulta.setString("tablename", TABLE_ROUTING);
+ consulta.setString("tablename", configuration.get("ROUTING_TABLE", "routing"));
consulta.setParameterList("stoptable", stops,
GeometryUserType.TYPE);
- consulta.setString("gid", GID_ROUTING);
+ consulta.setString("gid", configuration.get("ROUTING_ID", "gid"));
consulta.setParameter("start",
GeoUtil.getPoint(wayPointList.getStartPoint(), null),
GeometryUserType.TYPE);
@@ -109,14 +109,16 @@ public OSRMConnector() {
* Route plan using osrm server.
*
* @param param
+ * @param host_port
+ * @param http
* @return
* @throws IOException
* @throws ParseException
* @throws JAXBException
* @throws InterruptedException
*/
public AbstractResponseParametersType routePlan(
- DetermineRouteRequestType param) throws IOException, JAXBException,
+ DetermineRouteRequestType param, String host_port, String http) throws IOException, JAXBException,
ParseException, InterruptedException {
DetermineRouteResponseType res = new DetermineRouteResponseType();
@@ -153,12 +155,6 @@ public AbstractResponseParametersType routePlan(
WayPointListType wayPointList = param.getRoutePlan()
.getWayPointList();
- String host_port = Configuration.get("OSRM_HOST",
- "gofre.emergya.es:5000");
- String http = "http";
- if (Configuration.get("OSRM_SSL", "off").equals("on"))
- http = "https";
-
String url = http + "://" + host_port + "/viaroute";
CoordinateReferenceSystem sourceCRS = CRS.decode(EPSG_4326);
Oops, something went wrong.

0 comments on commit f27e279

Please sign in to comment.