Ver Fonte

Added use of properties files to manage system-specific information.

Each config object should have a corresponding properties file, plus one for
JSP files and one for application-wide settings.  Because of technical limitations
the JSP properties file cannot use namespaces.

Also moved config files into a dev subdirectory.  This will allow for future
seperation between development and production configurations.
Thomas Flucke há 7 anos atrás
pai
commit
2289671c3f

+ 10 - 10
build.xml

@@ -1,15 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
 <project name="ieat2" basedir="." default="war" xmlns:ivy="antlib:org.apache.ivy.ant">
-  
-  <property name="app.name" value="ieat"/>
-  <property name="app.conf.dir" value="conf/app/"/>
-
-  <property name="ant.lib" value="${user.home}/.ant/lib"/>
-  <property name="ant.conf.dir" value="conf/ant/"/>
-
   <property name="lib.dir" value="lib/"/>
   <property name="web.dir" value="web/"/>
+  <property name="conf.dir" value="conf/dev/"/>
+
+  <property name="app.name" value="ieat"/>  
+  <property name="app.conf.dir" value="${conf.dir}/app/"/>
+
+  <property name="ant.lib" value="${user.home}/.ant/lib"/>
+  <property name="ant.conf.dir" value="${conf.dir}/ant/"/>
 
   <property prefix="tomcat" file="${ant.conf.dir}/tomcat.properties"/>
 
@@ -91,11 +91,11 @@
 	<mkdir dir="${build.dir}"/>
 	<war warfile="${build.warfile}" webxml="${app.conf.dir}/web.xml">
       <lib dir="${build.lib}"/>
-      <lib dir="${app.conf.dir}">
-        <exclude name="web.xml"/>
-      </lib>
       <fileset dir="web"/>
       <classes dir="${target.dir}"/>
+      <classes dir="${app.conf.dir}">
+        <exclude name="web.xml"/>
+      </classes>
     </war>
   </target>
   

+ 0 - 0
conf/ant/build.properties → conf/dev/ant/build.properties


+ 0 - 0
conf/ant/compile.properties → conf/dev/ant/compile.properties


+ 0 - 0
conf/ant/ivy.xml → conf/dev/ant/ivy.xml


+ 0 - 0
conf/ant/tomcat.properties → conf/dev/ant/tomcat.properties


+ 0 - 8
conf/app/application.properties → conf/dev/app/application.properties

@@ -3,14 +3,6 @@
 # ieat app
 app.url=localhost:8080/ieat-2.0.0
 
-# mongodb
-spring.data.mongodb.host=localhost
-spring.data.mongodb.port=27017
-spring.data.mongodb.database=ieat
-
-# web resources
-web.resources.timeout = 0 # seconds
-
 # logging
 logging.level.*=error
 #logging.config=log4j.properties

+ 5 - 0
conf/dev/app/db.properties

@@ -0,0 +1,5 @@
+# DB Properties
+
+spring.data.mongodb.host=localhost
+spring.data.mongodb.port=27017
+spring.data.mongodb.database=ieat

+ 2 - 0
conf/dev/app/jsp.properties

@@ -0,0 +1,2 @@
+url=http://localhost:8080/ieat-2.0.0
+ndbKey=CfiHcUnSf0RX0jBuqiWjDK2d2ziOmoZG15CTdhQn

+ 4 - 0
conf/dev/app/web.properties

@@ -0,0 +1,4 @@
+# web resources
+
+# Timeout in seconds
+web.resources.timeout = 0

+ 0 - 0
conf/app/web.xml → conf/dev/app/web.xml


+ 3 - 0
src/name/tflucke/ieat2/configs/DBConfig.java

@@ -3,6 +3,8 @@ package name.tflucke.ieat2.configs;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.config.BeanDefinition;
@@ -23,6 +25,7 @@ import org.mongodb.morphia.annotations.Entity;
  * @since 2.0.0
  */
 @Configuration
+@PropertySource("classpath:db.properties")
 public class DBConfig {
     private static final Logger log = Logger.getLogger(DBConfig.class);
 

+ 24 - 0
src/name/tflucke/ieat2/configs/WebConfig.java

@@ -1,10 +1,17 @@
 package name.tflucke.ieat2.configs;
 
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.Resource;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.config.PropertiesFactoryBean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.web.servlet.ViewResolver;
 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -20,7 +27,10 @@ import org.springframework.http.CacheControl;
  * @since 2.0.0
  */
 @Configuration
+@PropertySource("classpath:web.properties")
 public class WebConfig implements WebMvcConfigurer {
+
+    private static final Log log = LogFactory.getLog(WebConfig.class);
     
     /**
      * Expiration time of web resources in seconds.
@@ -28,6 +38,12 @@ public class WebConfig implements WebMvcConfigurer {
     @Value("${web.resources.timeout:0}")
     private long timeout;
 
+    /**
+     * Properties available to JSP files.
+     */
+    @Resource(name="jspProperties")
+    private Map<String, Object> jspProperties;
+
     /**
      * Allows access to jsp files inside /views
      */
@@ -35,11 +51,19 @@ public class WebConfig implements WebMvcConfigurer {
     public ViewResolver viewResolver() {
         InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
         viewResolver.setViewClass(JstlView.class);
+        viewResolver.setAttributesMap(jspProperties);
         viewResolver.setPrefix("/views/");
         viewResolver.setSuffix(".jsp");
         return viewResolver;
     }
 
+    @Bean
+    public static PropertiesFactoryBean jspProperties() {
+        PropertiesFactoryBean bean = new PropertiesFactoryBean();
+        bean.setLocation(new ClassPathResource("jsp.properties"));
+        return bean;
+    }
+    
     @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
         // Allow /static/** to access js/css files

+ 1 - 1
src/name/tflucke/ieat2/controllers/ViewController.java

@@ -23,7 +23,7 @@ public class ViewController {
      */
     @RequestMapping
     public String defaultPage(ModelMap model) {
-        return "index";
+        return arbitraryFile("index", model);
     }
 
     /**

+ 5 - 5
web/WEB-INF/tags/navigation.tag

@@ -9,18 +9,18 @@
       <a class="navbar-brand" href="#">iEat 2.0</a>
     </div>
     <ul class="nav navbar-nav">
-      <t:navItem title="${title}" href="/ieat-2.0.0">Home</t:navItem>
+      <t:navItem title="${title}" href="${url}">Home</t:navItem>
       <li class="dropdown">
         <a class="dropdown-toggle" data-toggle="dropdown" href="#">
           Food<span class="caret"></span>
         </a>
         <ul class="dropdown-menu">
-          <t:navItem title="${title}" href="/ieat-2.0.0/browseFood">Manage Food</t:navItem>
-          <t:navItem title="${title}" href="/ieat-2.0.0/addFood">Add Food</t:navItem>
+          <t:navItem title="${title}" href="${url}/browseFood">Manage Food</t:navItem>
+          <t:navItem title="${title}" href="${url}/addFood">Add Food</t:navItem>
         </ul>
       </li>
-      <t:navItem title="${title}" href="/ieat-2.0.0/addRecipe">Add Recipe</t:navItem>
-      <t:navItem title="${title}" href="/ieat-2.0.0">Settings</t:navItem>
+      <t:navItem title="${title}" href="${url}/addRecipe">Add Recipe</t:navItem>
+      <t:navItem title="${title}" href="${url}">Settings</t:navItem>
     </ul>
   </div>
 </nav>

+ 3 - 4
web/js/basicFoodEditor.js

@@ -50,17 +50,16 @@ angular.module('basicFoodEditor', ['ndbDatabase', 'ngResource'])
     }])
     // TODO: Only ever used with editBasicFood template.  See if can auto link.
     .controller('BasicFoodEditorController',
-                ['$scope', '$uibModalInstance', 'NDBList', 'BasicFood', 'foodData',
-                 function($scope, $uibModalInstance, NDBList, BasicFood, foodData) {
+                ['$scope', '$uibModalInstance', 'NDBList', 'BasicFood', 'foodData', 'ndbKey',
+                 function($scope, $uibModalInstance, NDBList, BasicFood, foodData, ndbKey) {
                      if (foodData == null)
                      {
                          console.error("No food data to edit!");
                          return;
                      }
 
-                     // TODO: Replace with properties file key.
                      $scope.catagories = NDBList.get({
-                         key: "CfiHcUnSf0RX0jBuqiWjDK2d2ziOmoZG15CTdhQn",
+                         key: ndbKey,
                          type: "g"
                      });
                      

+ 15 - 14
web/views/addFood.jsp

@@ -22,16 +22,16 @@
                   }
                   if ($scope.searchTerm.length >= 3)
                   {
-                      searchTimeout = $timeout(function() {//{ndb.api.key}
-                          // TODO: Replace with properties file key.
-                          NDBSearch.get({key: "CfiHcUnSf0RX0jBuqiWjDK2d2ziOmoZG15CTdhQn",
-                                         "query": $scope.searchTerm
-                                        }, function(data) {
-                                            console.debug(data);
-                                            $scope.searchResults = data;
-                                        }, function (err) {
-                                            console.error(err);
-                                        });
+                      searchTimeout = $timeout(function() {
+                          NDBSearch.get({
+                              key: "${ndbKey}",
+                              "query": $scope.searchTerm
+                          }, function(data) {
+                              console.debug(data);
+                              $scope.searchResults = data;
+                          }, function (err) {
+                              console.error(err);
+                          });
                       }, 100);
                   }
               });
@@ -52,8 +52,8 @@
               
             $scope.promptWindow = function(ndbno) {// {ndb.api.key}
                 var foodRequest = NDBReport.get(
-                    {// TODO: Replace with properties file key.
-                        "key": "CfiHcUnSf0RX0jBuqiWjDK2d2ziOmoZG15CTdhQn",
+                    {
+                        "key": "${ndbKey}",
                         "ndbno": ndbno,
                         "type": "f"
                     }).$promise.then(function(data) {
@@ -66,11 +66,12 @@
                     // TODO: Figure out what these are and how they work
                     //ariaLabelledBy: 'modal-title',
                     //ariaDescribedBy: 'modal-body',
-                    templateUrl: 'static/templates/editBasicFood.html',
+                    templateUrl: '${url}/static/templates/editBasicFood.html',
                     controller: 'BasicFoodEditorController',
                     size: "md",
                     resolve: {
-                        foodData: function() {return foodRequest;}
+                        foodData: function() {return foodRequest;},
+                        ndbKey: function() {return "${ndbKey}";}
                     }
                 });
             };