Skip to content

Commit 52699d3

Browse files
author
=
committed
trying out a bunch of new stuff including property file resource injection, bootstrap api, property file organisation, ui composition...
1 parent c60f8f4 commit 52699d3

20 files changed

+397
-62
lines changed

src/main/java/com/thenaglecode/algorithms/Configuration.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
* Time: 4:46 PM
1010
*/
1111
public class Configuration {
12-
public static final ConfigurationItem PRIME_GENERATOR_CLASS = new ConfigurationItem("primes.generator.class", "PGSimple1");
13-
public static final ConfigurationItem PRIME_UPTO = new ConfigurationItem("primes.upto", "1000");
14-
public static final ConfigurationItem FILE_PRIMES = new ConfigurationItem("file.primes", "C;/primes.obj");
12+
public static final String PRIME_GENERATOR_CLASS_ID = "primes.generator.class";
13+
public static final String PRIME_UPTO_ID = "primes.upto";
14+
public static final String FILE_PRIMES_ID = "file.primes";
15+
public static final String STANDARD_BASE_PACKAGE_ID = "standard.base.package";
1516

17+
public static final ConfigurationItem PRIME_GENERATOR_CLASS = new ConfigurationItem(PRIME_GENERATOR_CLASS_ID, "PGSimple1");
18+
public static final ConfigurationItem PRIME_UPTO = new ConfigurationItem(PRIME_UPTO_ID, "1000");
19+
public static final ConfigurationItem FILE_PRIMES = new ConfigurationItem(FILE_PRIMES_ID, "C;/primes.obj");
20+
public static final ConfigurationItem STANDARD_BASE_PACKAGE = new ConfigurationItem(STANDARD_BASE_PACKAGE_ID, "com.thenaglecode");
1621

1722
}
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
package com.thenaglecode.algorithms;
22

3-
import com.thenaglecode.algorithms.util.AbstractConfigurationManager;
3+
import com.thenaglecode.algorithms.util.propeties.AbstractConfigurationManager;
44

55
/**
66
* Created with IntelliJ IDEA.
77
* User: jxnagl
88
* Date: 7/25/13
99
* Time: 4:08 PM
10-
* To change this template use File | Settings | File Templates.
1110
*/
1211
public class ConfigurationManager extends AbstractConfigurationManager{
1312

1413
private static ConfigurationManager instance = null;
1514

1615
public static ConfigurationManager getInstance(){
17-
if(instance == null){
18-
instance = new ConfigurationManager();
19-
}
20-
return instance;
16+
return instance == null ? instance = new ConfigurationManager() : instance;
2117
}
2218

2319
/**
2420
* do not ctor
2521
*/
26-
private ConfigurationManager(){
27-
init();
28-
}
29-
30-
private void init() {
31-
//load the properties
32-
}
22+
private ConfigurationManager(){}
3323
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
primes.generator.class=main.java.com.thenaglecode.algorithms.primes.PG7_8
23
primes.upto=10000000
34
file.primes=C:/primes.obj

src/main/java/com/thenaglecode/algorithms/util/AbstractConfigurationManager.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.thenaglecode.algorithms.util;
2+
3+
import com.thenaglecode.algorithms.Configuration;
4+
import com.thenaglecode.algorithms.ConfigurationManager;
5+
import com.thenaglecode.algorithms.util.propeties.PropertyBundleResource;
6+
7+
import java.util.ResourceBundle;
8+
9+
/**
10+
* Created with IntelliJ IDEA.
11+
* User: jxnagl
12+
* Date: 8/2/13
13+
* Time: 4:44 PM
14+
*
15+
* Class for package related operations such as retrieving the fully qualified name of a subsystem.
16+
*/
17+
public class PackageUtil {
18+
@PropertyBundleResource(name = "com.thenaglecode.configuration")
19+
private static ResourceBundle bundle;
20+
21+
public static String buildStandardUrl(String subSystemName) {
22+
String base = bundle.getString(ConfigurationManager.getInstance().getValue(Configuration.STANDARD_BASE_PACKAGE));
23+
return base + "." + subSystemName;
24+
}
25+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.thenaglecode.algorithms.util.propeties;
2+
3+
import com.thenaglecode.algorithms.util.ConfigurationItem;
4+
5+
import java.util.PropertyResourceBundle;
6+
7+
/**
8+
* Created with IntelliJ IDEA.
9+
* User: jxnagl
10+
* Date: 7/25/13
11+
* Time: 4:12 PM
12+
*
13+
* extend this class to create a configuration manager for the residing package. the configuration
14+
* properties fileName specified in the constructor should also reside in the same package as the
15+
* <tt>AbstractConfigurationManager</tt> implementation. if a filename is not specified, the default of
16+
* <tt>"configuration"</tt> is used.
17+
*/
18+
public abstract class AbstractConfigurationManager {
19+
20+
private AbstractConfigurationManager instance;
21+
PropertyResourceBundle bundle;
22+
private String propertyFile;
23+
private static final String DEFAULT_CONFIG_BUDNLE_NAME = "configuration";
24+
25+
public abstract <T extends AbstractConfigurationManager> T getInstance();
26+
27+
/**
28+
* do not ctor
29+
*/
30+
protected AbstractConfigurationManager(){
31+
this(DEFAULT_CONFIG_BUDNLE_NAME);
32+
}
33+
34+
protected AbstractConfigurationManager(String propertyFile){
35+
init(propertyFile);
36+
}
37+
38+
private final String getPropertyFile() {
39+
return propertyFile;
40+
}
41+
42+
private final void setPropertyFile(String propertyFile) {
43+
this.propertyFile = propertyFile;
44+
}
45+
46+
public String getValue(String key){
47+
if(bundle != null){
48+
return bundle.getString(key);
49+
} else {
50+
return null;
51+
}
52+
}
53+
54+
public String getValue(ConfigurationItem configurationItem){
55+
String value = getValue(configurationItem.getKey());
56+
return (value == null) ? configurationItem.getDefaultValue() : value;
57+
}
58+
59+
protected void init(){
60+
init(getPropertyFile());
61+
}
62+
63+
protected void init(String propertyFile) {
64+
setPropertyFile(propertyFile);
65+
String baseName = getClass().getPackage().getName() + "." + propertyFile;
66+
bundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle(baseName);
67+
}
68+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.thenaglecode.algorithms.util.propeties;
2+
3+
import javax.enterprise.util.Nonbinding;
4+
import javax.xml.ws.BindingType;
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* Created with IntelliJ IDEA.
12+
* User: jxnagl
13+
* Date: 8/2/13
14+
* Time: 4:36 PM
15+
*/
16+
17+
@BindingType
18+
@Retention(RetentionPolicy.RUNTIME)
19+
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
20+
ElementType.PARAMETER })
21+
public @interface PropertiesResource {
22+
23+
/**
24+
* The name of the properties file that is visible by the callers class loader.
25+
* Excludes the '.properties' extension.
26+
* @return the name of the properties file without the .properties on the end.
27+
*/
28+
@Nonbinding
29+
public String name();
30+
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thenaglecode.algorithms.util.propeties;
2+
3+
import javax.enterprise.inject.Produces;
4+
import javax.enterprise.inject.spi.InjectionPoint;
5+
import java.io.IOException;
6+
import java.util.PropertyResourceBundle;
7+
import java.util.ResourceBundle;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
10+
11+
/**
12+
* Created with IntelliJ IDEA.
13+
* User: jxnagl
14+
* Date: 8/2/13
15+
* Time: 4:53 PM
16+
*/
17+
public class PropertiesResourceBundleLocator {
18+
19+
private static final Logger logger = Logger.getLogger(PropertiesResourceBundleLocator.class.getName());
20+
21+
@Produces
22+
@PropertyBundleResource(name = "")
23+
ResourceBundle loadPropertiesResource(InjectionPoint ip) throws IOException {
24+
logger.log(Level.FINE, "-- called PropertiesResourceBundle loader");
25+
PropertiesResource annotation = ip.getAnnotated().getAnnotation(PropertiesResource.class);
26+
return PropertyResourceBundle.getBundle(annotation.name());
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thenaglecode.algorithms.util.propeties;
2+
3+
import javax.enterprise.inject.Produces;
4+
import javax.enterprise.inject.spi.InjectionPoint;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.net.URL;
8+
import java.util.Properties;
9+
import java.util.logging.Level;
10+
import java.util.logging.Logger;
11+
12+
/**
13+
* Created with IntelliJ IDEA.
14+
* User: jxnagl
15+
* Date: 8/2/13
16+
* Time: 4:37 PM
17+
*/
18+
public class PropertiesResourceLocator {
19+
20+
private static final Logger logger = Logger.getLogger(PropertiesResourceLocator.class.getName());
21+
22+
@Produces
23+
@PropertiesResource(name = "")
24+
Properties loadProperties(InjectionPoint ip) throws IOException {
25+
logger.log(Level.FINE, "-- called PropertiesResource loader");
26+
PropertiesResource annotation = ip.getAnnotated().getAnnotation(PropertiesResource.class);
27+
String fileName = annotation.name();
28+
Properties props = null;
29+
30+
URL url = null;
31+
url = Thread.currentThread().getContextClassLoader().getResource(fileName);
32+
if(url != null){
33+
props = new Properties();
34+
try (InputStream in = url.openStream()) {
35+
props.load(in);
36+
}
37+
}
38+
return props;
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.thenaglecode.algorithms.util.propeties;
2+
3+
import javax.enterprise.util.Nonbinding;
4+
import javax.xml.ws.BindingType;
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* Created with IntelliJ IDEA.
12+
* User: jxnagl
13+
* Date: 8/2/13
14+
* Time: 4:52 PM
15+
*/
16+
17+
@BindingType
18+
@Retention(RetentionPolicy.RUNTIME)
19+
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
20+
ElementType.PARAMETER })
21+
public @interface PropertyBundleResource {
22+
@Nonbinding
23+
public String name();
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thenaglecode.core;
2+
3+
import com.thenaglecode.algorithms.util.ConfigurationItem;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: jxnagl
8+
* Date: 8/2/13
9+
* Time: 5:47 PM
10+
*/
11+
public class Configuration {
12+
public static final String DEFAULT_BASE_PACKAGE_ID = "default.base.package";
13+
14+
public static final ConfigurationItem DEFAULT_BASE_PACKAGE = new ConfigurationItem(DEFAULT_BASE_PACKAGE_ID, "com.thenaglecode");
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thenaglecode.core;
2+
3+
import com.thenaglecode.algorithms.util.propeties.AbstractConfigurationManager;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: jxnagl
8+
* Date: 7/25/13
9+
* Time: 4:08 PM
10+
*/
11+
public class ConfigurationManager extends AbstractConfigurationManager{
12+
13+
private static ConfigurationManager instance = null;
14+
15+
public static ConfigurationManager getInstance(){
16+
return instance == null ? instance = new ConfigurationManager() : instance;
17+
}
18+
19+
/**
20+
* do not ctor
21+
*/
22+
private ConfigurationManager(){}
23+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# all subsystems should extend this base package
2+
default.base.package=com.thenaglecode
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.thenaglecode.core.ui;
2+
3+
import com.thenaglecode.algorithms.util.ConfigurationItem;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: jxnagl
8+
* Date: 8/2/13
9+
* Time: 5:39 PM
10+
*/
11+
public class Configuration {
12+
public static final String BOOTSTRAP_API_CDN_CSS_ID = "bootstrap.cdn.css";
13+
public static final String BOOTSTRAP_API_CDN_JAVASCRIPT_ID = "bootstrap.cdn.javascript";
14+
15+
public static final ConfigurationItem BOOTSTRAP_API_CDN_CSS = new ConfigurationItem(BOOTSTRAP_API_CDN_CSS_ID, "//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css");
16+
public static final ConfigurationItem BOOTSTRAP_API_CDN_JAVASCRIPT = new ConfigurationItem(BOOTSTRAP_API_CDN_JAVASCRIPT_ID, "//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js");
17+
}

0 commit comments

Comments
 (0)