Browse Source

Refactored App to use AppConfig for initial configuration. Updated ResourceLoader with logging. Added a default properties file, and a PropertiesService to load that properties file. Updated ImageHashDTO to include some fields. Added a HashService to manage generating hashes. moved the about file to an about.info file so we can automatically process it.

master
Drew Short 11 years ago
parent
commit
3699870198
  1. 4
      .gitignore
  2. 24
      pom.xml
  3. 19
      src/includes/startCLI.sh
  4. 36
      src/main/java/com/sothr/imagetools/App.java
  5. 20
      src/main/java/com/sothr/imagetools/AppCLI.java
  6. 41
      src/main/java/com/sothr/imagetools/AppConfig.java
  7. 10
      src/main/java/com/sothr/imagetools/util/ResourceLoader.java
  8. 21
      src/main/resources/default.properties
  9. 3
      src/main/resources/documents/about
  10. 5
      src/main/resources/documents/about.info
  11. 6
      src/main/scala/com/sothr/imagetools/dto/ImageHashDTO.scala
  12. 17
      src/main/scala/com/sothr/imagetools/hash/HashService.scala
  13. 5
      src/main/scala/com/sothr/imagetools/image/Image.scala
  14. 33
      src/main/scala/com/sothr/imagetools/util/PropertiesService.scala

4
.gitignore

@ -18,3 +18,7 @@ target/
# Build Name and Version Files
name.info
version.info
# Copied README.md and LICENSE files
src/includes/README.md
src/includes/LICENSE

24
pom.xml

@ -32,6 +32,7 @@
<lib.scalatest.version>2.0</lib.scalatest.version>
<lib.log4j.version>1.2.17</lib.log4j.version>
<lib.slf4j.version>1.7.5</lib.slf4j.version>
<lib.grizzled-slf4j.version>1.0.1</lib.grizzled-slf4j.version>
<lib.scala-library.version>2.10.0</lib.scala-library.version>
<lib.scalafx.version>8.0.0-M1</lib.scalafx.version>
</properties>
@ -64,6 +65,11 @@
<artifactId>slf4j-log4j12</artifactId>
<version>${lib.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.clapper</groupId>
<artifactId>grizzled-slf4j_2.10</artifactId>
<version>${lib.grizzled-slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
@ -73,6 +79,22 @@
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.info</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.info</exclude>
</excludes>
</resource>
</resources>
<pluginManagement>
<plugins>
@ -227,6 +249,8 @@
<tasks>
<copy file="${project.build.directory}/version.info" toFile="${basedir}/version.info" overwrite="true" />
<copy file="${project.build.directory}/name.info" toFile="${basedir}/name.info" overwrite="true" />
<copy file="${basedir}/LICENSE" toFile="${basedir}/src/includes/LICENSE" overwrite="true" />
<copy file="${basedir}/README.md" toFile="${basedir}/src/includes/README.md" overwrite="true" />
</tasks>
</configuration>
<goals>

19
src/includes/startCLI.sh

@ -0,0 +1,19 @@
#!/bin/bash
echo "Welcome to Image Tools version: ${project.version}"
command=""
correct=false
while true; do
read -p "Please enter and commandline arguments you would like to include: " args
command="-cp ${project.name}-${project.version}-jfx.jar:lib/* com.sothr.imagetools.AppCLI $args"
echo "Is \"$command\" accurate? (yes/no)"
select yn in "Yes" "No"; do
case $yn in
Yes ) correct=true; break;;
No ) break;;
esac
done
case $correct in
true ) break;;
esac
done
java $command

36
src/main/java/com/sothr/imagetools/App.java

@ -7,17 +7,14 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.List;
/**
* Hello world!
*
* Image Tools
*/
public class App extends Application
{
@ -28,7 +25,7 @@ public class App extends Application
public static void main( String[] args )
{
configLogging("./log4j.properties");
AppConfig.configLogging();
try {
//try to run the UI
launch(args);
@ -38,30 +35,13 @@ public class App extends Application
}
}
private static void configLogging(String propertiesLocation) {
//Logging Config
File file = new File(propertiesLocation);
if (file.exists()) {
PropertyConfigurator.configure(propertiesLocation);
} else {
//Simple error logging configuration
Properties defaultProps = new Properties();
defaultProps.setProperty("log4j.rootLogger","ERROR, A1");
//Rolling Error logger
defaultProps.setProperty("log4j.appender.A1","org.apache.log4j.RollingFileAppender");
defaultProps.setProperty("log4j.appender.A1.File","Image-Tools.err");
defaultProps.setProperty("log4j.appender.A1.MaxFileSize","100KB");
defaultProps.setProperty("log4j.appender.A1.MaxBackupIndex","1");
defaultProps.setProperty("log4j.appender.A1.layout","org.apache.log4j.EnhancedPatternLayout");
defaultProps.setProperty("log4j.appender.A1.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n");
PropertyConfigurator.configure(defaultProps);
}
logger = LoggerFactory.getLogger(App.class);
}
@Override
public void init() throws Exception{
configLogging("./log4j.properties");
AppConfig.configLogging();
logger = LoggerFactory.getLogger(this.getClass());
logger.info("Initializing Image Tools");
List<String> parameters = this.getParameters().getRaw();
logger.info(String.format("Application was called with '%s' parameters", parameters.toString()));
super.init();
}

20
src/main/java/com/sothr/imagetools/AppCLI.java

@ -0,0 +1,20 @@
package com.sothr.imagetools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* CLI interface for Image Tools
*/
class AppCLI {
private static Logger logger;
public static void main(String[] args) {
AppConfig.configLogging();
logger = LoggerFactory.getLogger(AppCLI.class);
logger.info("Started Image Tools CLI");
System.out.println("Hello World");
}
}

41
src/main/java/com/sothr/imagetools/AppConfig.java

@ -0,0 +1,41 @@
package com.sothr.imagetools;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Properties;
class AppConfig {
private static Logger logger;
private static final String LOGSETTINGSFILE = "./log4j.properties";
public static void configLogging() {
//Logging Config
File file = new File(LOGSETTINGSFILE);
Boolean fromFile = false;
if (file.exists()) {
fromFile = true;
PropertyConfigurator.configure(LOGSETTINGSFILE);
} else {
//Simple error logging configuration
Properties defaultProps = new Properties();
defaultProps.setProperty("log4j.rootLogger","ERROR, A1");
//Rolling Error logger
defaultProps.setProperty("log4j.appender.A1","org.apache.log4j.RollingFileAppender");
defaultProps.setProperty("log4j.appender.A1.File","Image-Tools.err");
defaultProps.setProperty("log4j.appender.A1.MaxFileSize","100KB");
defaultProps.setProperty("log4j.appender.A1.MaxBackupIndex","1");
defaultProps.setProperty("log4j.appender.A1.layout","org.apache.log4j.EnhancedPatternLayout");
defaultProps.setProperty("log4j.appender.A1.layout.ConversionPattern","%d{yy-MM-dd HH:mm:ss} %-5p [%c{3.}] - %m%n");
PropertyConfigurator.configure(defaultProps);
}
logger = LoggerFactory.getLogger(AppConfig.class);
String message = fromFile ? "From File" : "From Defaults";
logger.info(String.format("Configured Logger %s", message));
}
}

10
src/main/java/com/sothr/imagetools/util/ResourceLoader.java

@ -1,5 +1,7 @@
package com.sothr.imagetools.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.net.URL;
@ -10,17 +12,23 @@ public class ResourceLoader {
private static final ResourceLoader instance = new ResourceLoader();
private ResourceLoader() {}
private Logger logger = LoggerFactory.getLogger(this.getClass());
private ResourceLoader() {
logger.info("Created Resource Loader");
}
public static ResourceLoader get() {
return instance;
}
public URL getResource(String location) {
logger.debug(String.format("Attempting to load resource: %s", location));
return Thread.currentThread().getContextClassLoader().getResource(location);
}
public InputStream getResourceStream(String location) {
logger.debug(String.format("Attempting to get stream for resource: %s",location));
return Thread.currentThread().getContextClassLoader().getResourceAsStream(location);
}

21
src/main/resources/default.properties

@ -0,0 +1,21 @@
#Default Properties File
#Image Tools version: ${project.version}
version=${project.version}
#Default Image Settings
#images must be 90% similar
image.differenceThreshold=0.90
#control generation of hashes for new images.
image.ahash=true
image.ahashWeight=0.70
image.dhash=true
image.dhashWeight=0.85
#set to false if hashing images is taking too long
image.phash=true
image.phashWeight=1.0
#Default Thumbnail Settings
#Directory where to store thumbnails
thumbnail.directory=/cache/thumbnails/
#Size of the thumbnail to generate and store
thumbnail.size=128

3
src/main/resources/documents/about

@ -1,3 +0,0 @@
This is a simple about script. It demonstrates loading the about text from a file.
It supports simple text, and nothing fancy.

5
src/main/resources/documents/about.info

@ -0,0 +1,5 @@
Image Tools Version: ${project.version}
This is a simple about script. It demonstrates loading the about text from a file.
It supports simple text, and nothing fancy.

6
src/main/scala/com/sothr/imagetools/dto/ImageHashDTO.scala

@ -1,5 +1,9 @@
package com.sothr.imagetools.dto
class ImageHashDTO {
class ImageHashDTO(val ahash:Long, val dhash:Long, val phash:Long) {
def getAhash():Long = this.ahash
def getDhash():Long = this.dhash
def getPhash():Long = this.phash
}

17
src/main/scala/com/sothr/imagetools/hash/HashService.scala

@ -0,0 +1,17 @@
package com.sothr.imagetools.hash
object HashService {
def getAhash(imageData:Array[Array[Integer]]):Long = {
return 0L
}
def getDhash(imageData:Array[Array[Integer]]):Long = {
return 0L
}
def getPhash(imageData:Array[Array[Integer]]):Long = {
return 0L
}
}

5
src/main/scala/com/sothr/imagetools/image/Image.scala

@ -3,11 +3,12 @@ package com.sothr.imagetools.image
import scala.collection.Traversable
import com.sothr.imagetools.dto.ImageHashDTO
abstract class Image(val imagePath:String, val thumbnailPath:String) {
abstract class Image(val imagePath:String, val thumbnailPath:String, protected var hashes:ImageHashDTO = null) {
protected val imageType:ImageType = ImageType.SingleFrameImage
def getHashes():ImageHashDTO
def getHashes():ImageHashDTO = this.hashes
def setHashes(newHashes:ImageHashDTO) = { this.hashes = newHashes }
def isSimilarTo(otherImage:Image):Boolean

33
src/main/scala/com/sothr/imagetools/util/PropertiesService.scala

@ -0,0 +1,33 @@
package com.sothr.imagetools.util
import java.util.Properties
import grizzled.slf4j.Logging
/*
* Service for loading and interacting with the properties file
*/
object PropertiesService extends Logging {
val properties:Properties = new Properties()
/*
* Load the properties file from the specified location
*/
def loadProperties(location:String) = {
info(s"Attempting to load properties from: $location")
val inputStream = ResourceLoader.get.getResourceStream(location)
val splitLocation = location.split("""\.""")
if (splitLocation(splitLocation.length) equals "xml") {
properties.loadFromXML(inputStream)
} else if (splitLocation(splitLocation.length) equals "properties") {
properties.load(inputStream);
} else {
error("Unable to load the properties file because it is not in the .properties or .xml format")
}
}
def saveProperties(location:String) = {
}
}
Loading…
Cancel
Save