Skip to content

Commit

Permalink
use configurable naming & hash strategies in ConfigurableWroManagerFa…
Browse files Browse the repository at this point in the history
…ctory
  • Loading branch information
alexo committed Jun 17, 2012
1 parent 52031f7 commit 4e2cff0
Show file tree
Hide file tree
Showing 21 changed files with 349 additions and 288 deletions.
189 changes: 94 additions & 95 deletions wro4j-core/src/main/java/ro/isdc/wro/manager/WroManager.java

Large diffs are not rendered by default.

Expand Up @@ -28,8 +28,9 @@
import ro.isdc.wro.model.resource.locator.factory.UriLocatorFactory;
import ro.isdc.wro.model.resource.processor.factory.DefaultProcesorsFactory;
import ro.isdc.wro.model.resource.processor.factory.ProcessorsFactory;
import ro.isdc.wro.model.resource.support.hash.HashBuilder;
import ro.isdc.wro.model.resource.support.hash.HashStrategy;
import ro.isdc.wro.model.resource.support.hash.SHA1HashBuilder;
import ro.isdc.wro.model.resource.support.hash.SHA1HashStrategy;
import ro.isdc.wro.model.resource.support.naming.NamingStrategy;
import ro.isdc.wro.model.resource.support.naming.NoOpNamingStrategy;
import ro.isdc.wro.model.transformer.WildcardExpanderModelTransformer;
Expand All @@ -51,7 +52,7 @@ public class BaseWroManagerFactory
private GroupExtractor groupExtractor;
private WroModelFactory modelFactory;
private CacheStrategy<CacheEntry, ContentHashEntry> cacheStrategy;
private HashStrategy hashBuilder;
private HashStrategy hashStrategy;
/**
* A list of model transformers. Allows manager to mutate the model before it is being parsed and
* processed.
Expand All @@ -76,8 +77,8 @@ protected WroManager initialize() {
if (cacheStrategy == null) {
cacheStrategy = newCacheStrategy();
}
if (hashBuilder == null) {
hashBuilder = newHashBuilder();
if (hashStrategy == null) {
hashStrategy = newHashStrategy();
}
if (modelTransformers == null) {
modelTransformers = newModelTransformers();
Expand All @@ -90,12 +91,12 @@ protected WroManager initialize() {
}
//use NoOp as default naming strategy
if (namingStrategy == null) {
namingStrategy = new NoOpNamingStrategy();
namingStrategy = newNamingStrategy();
}

manager.setGroupExtractor(groupExtractor);
manager.setCacheStrategy(cacheStrategy);
manager.setHashBuilder(hashBuilder);
manager.setHashBuilder(hashStrategy);
manager.setUriLocatorFactory(uriLocatorFactory);
manager.setProcessorsFactory(processorsFactory);
manager.setNamingStrategy(namingStrategy);
Expand Down Expand Up @@ -147,7 +148,7 @@ public BaseWroManagerFactory setNamingStrategy(final NamingStrategy namingStrate
public NamingStrategy getNamingStrategy() {
return namingStrategy;
}

/**
* @return default implementation of modelTransformers.
*/
Expand Down Expand Up @@ -180,8 +181,15 @@ protected UriLocatorFactory newUriLocatorFactory() {
/**
* @return {@link HashStrategy} instance.
*/
protected HashStrategy newHashBuilder() {
return new SHA1HashBuilder();
protected HashStrategy newHashStrategy() {
return new SHA1HashStrategy();
}

/**
* @return default {@link NamingStrategy} to be used by this {@link WroManagerFactory}
*/
protected NamingStrategy newNamingStrategy() {
return new NoOpNamingStrategy();
}

/**
Expand Down Expand Up @@ -266,7 +274,7 @@ public BaseWroManagerFactory setModelFactory(final WroModelFactory modelFactory)
* @param hashBuilder the hashBuilder to set
*/
public BaseWroManagerFactory setHashBuilder(final HashStrategy hashBuilder) {
this.hashBuilder = hashBuilder;
this.hashStrategy = hashBuilder;
return this;
}

Expand Down
Expand Up @@ -28,6 +28,10 @@
import ro.isdc.wro.model.resource.processor.ResourcePreProcessor;
import ro.isdc.wro.model.resource.processor.factory.ConfigurableProcessorsFactory;
import ro.isdc.wro.model.resource.processor.factory.ProcessorsFactory;
import ro.isdc.wro.model.resource.support.hash.ConfigurableHashStrategy;
import ro.isdc.wro.model.resource.support.hash.HashStrategy;
import ro.isdc.wro.model.resource.support.naming.ConfigurableNamingStrategy;
import ro.isdc.wro.model.resource.support.naming.NamingStrategy;


/**
Expand Down Expand Up @@ -117,28 +121,68 @@ protected Properties newProperties() {
updatePropertiesWithProcessors(props, ConfigurableProcessorsFactory.PARAM_POST_PROCESSORS);
return props;
}
};
return factory;
}

/**
* Add to properties a new key with value extracted either from filterConfig or from configurable properties file.
*/
private void updatePropertiesWithProcessors(final Properties props, final String paramName) {
final FilterConfig filterConfig = Context.get().getFilterConfig();
// first, retrieve value from init-param for backward compatibility
final String processorsAsString = filterConfig.getInitParameter(paramName);
if (processorsAsString != null) {
props.setProperty(paramName, processorsAsString);
} else {
// retrieve value from configProperties file
final String value = getConfigProperties().getProperty(paramName);
if (value != null) {
props.setProperty(paramName, value);
}
}
/**
* {@inheritDoc}
*/
@Override
protected NamingStrategy newNamingStrategy() {
return new ConfigurableNamingStrategy() {
@Override
protected Properties newProperties() {
final Properties props = new Properties();
updatePropertiesWithProcessors(props, ConfigurableNamingStrategy.KEY);
return props;
}
};
return factory;
}

/**
* {@inheritDoc}
*/
@Override
protected HashStrategy newHashStrategy() {
return new ConfigurableHashStrategy() {
@Override
protected Properties newProperties() {
final Properties props = new Properties();
updatePropertiesWithProcessors(props, ConfigurableHashStrategy.KEY);
return props;

}
};
}

/**
* Add to properties a new key with value extracted either from filterConfig or from configurable properties file.
*/
private void updatePropertiesWithProcessors(final Properties props, final String paramName) {
final FilterConfig filterConfig = Context.get().getFilterConfig();
// first, retrieve value from init-param for backward compatibility
final String processorsAsString = filterConfig.getInitParameter(paramName);
if (processorsAsString != null) {
props.setProperty(paramName, processorsAsString);
} else {
// retrieve value from configProperties file
final String value = getConfigProperties().getProperty(paramName);
if (value != null) {
props.setProperty(paramName, value);
}
}
}

/**
* Use this method rather than accessing the field directly, because it will create a default one if none is provided.
*/
private Properties getConfigProperties() {
if (configProperties == null) {
configProperties = newConfigProperties();
}
return configProperties;
}

/**
* Override this method to provide a different config properties file location. It is very likely that you would like
Expand All @@ -159,18 +203,6 @@ protected Properties newConfigProperties() {
return props;
}


/**
* Use this method rather than accessing the field directly, because it will create a default one if none is provided.
*/
private Properties getConfigProperties() {
if (configProperties == null) {
configProperties = newConfigProperties();
}
return configProperties;
}


/**
* Setter is useful for unit tests.
*/
Expand Down
Expand Up @@ -30,7 +30,7 @@
import ro.isdc.wro.model.resource.processor.factory.ProcessorsFactory;
import ro.isdc.wro.model.resource.processor.factory.SimpleProcessorsFactory;
import ro.isdc.wro.model.resource.support.hash.HashStrategy;
import ro.isdc.wro.model.resource.support.hash.SHA1HashBuilder;
import ro.isdc.wro.model.resource.support.hash.SHA1HashStrategy;
import ro.isdc.wro.model.resource.support.naming.NamingStrategy;
import ro.isdc.wro.model.resource.support.naming.NoOpNamingStrategy;
import ro.isdc.wro.util.ObjectFactory;
Expand All @@ -52,7 +52,7 @@ public class InjectorBuilder {
private UriLocatorFactory uriLocatorFactory = new SimpleUriLocatorFactory();
private ProcessorsFactory processorsFactory = new SimpleProcessorsFactory();
private NamingStrategy namingStrategy = new NoOpNamingStrategy();
private HashStrategy hashBuilder = new SHA1HashBuilder();
private HashStrategy hashStrategy = new SHA1HashStrategy();
private WroModelFactory modelFactory = null;
private GroupExtractor groupExtractor = null;
/**
Expand Down Expand Up @@ -160,7 +160,7 @@ public CacheStrategy<CacheEntry, ContentHashEntry> create() {
});
map.put(HashStrategy.class, new InjectorObjectFactory<HashStrategy>() {
public HashStrategy create() {
return hashBuilder;
return hashStrategy;
}
});
}
Expand All @@ -179,7 +179,7 @@ private InjectorBuilder setWroManager(final WroManager manager) {
modelFactory = manager.getModelFactory();
groupExtractor = manager.getGroupExtractor();
cacheStrategy = manager.getCacheStrategy();
hashBuilder = manager.getHashBuilder();
hashStrategy = manager.getHashStrategy();
modelTransformers = manager.getModelTransformers();
return this;
}
Expand Down
Expand Up @@ -14,7 +14,6 @@

import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.model.resource.support.hash.HashStrategy;
import ro.isdc.wro.model.resource.support.hash.SHA1HashBuilder;
import ro.isdc.wro.util.provider.ProviderFinder;


Expand Down Expand Up @@ -115,10 +114,11 @@ protected final ProviderFinder<P> getProviderFinder() {
}

/**
* @return the class of the provider of type P. Uses {@link ParameterizedType} to compute the class.
* @return the class of the provider of type P. Uses {@link ParameterizedType} to compute the class. Override it to
* support anonymous classes which do not play well with {@link ParameterizedType}'s.
*/
@SuppressWarnings("unchecked")
private Class<P> getProviderClass() {
protected Class<P> getProviderClass() {
final Type type = getClass().getGenericSuperclass();
return (Class<P>) ((ParameterizedType) type).getActualTypeArguments()[1];
}
Expand Down

This file was deleted.

@@ -1,5 +1,14 @@
package ro.isdc.wro.model.resource.support.hash;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Uses CRC32 algorithm for creating fingerprint.
*
Expand All @@ -8,5 +17,30 @@
* @created 17 Jun 2012
*/
public class CRC32HashStrategy
extends CRC32HashBuilder {
implements HashStrategy {
private static final Logger LOG = LoggerFactory.getLogger(CRC32HashStrategy.class);
/**
* A short name of this hashBuilder.
*/
public static final String ALIAS = "CRC32";
/**
* {@inheritDoc}
*/
public String getHash(final InputStream input)
throws IOException {
if (input == null) {
throw new IllegalArgumentException("Content cannot be null!");
}
LOG.debug("creating hash using CRC32 algorithm");
final Checksum checksum = new CRC32();
final byte[] bytes = new byte[1024];
int len = 0;
while ((len = input.read(bytes)) >= 0) {
checksum.update(bytes, 0, len);
}

final String hash = new BigInteger(Long.toString(checksum.getValue())).toString(16);
LOG.debug("CRC32 hash: {}", hash);
return hash;
}
}
Expand Up @@ -53,4 +53,12 @@ protected HashStrategy getDefaultStrategy() {
protected Map<String, HashStrategy> getStrategies(final HashStrategyProvider provider) {
return provider.provideHashStrategies();
}

/**
* {@inheritDoc}
*/
@Override
protected Class<HashStrategyProvider> getProviderClass() {
return HashStrategyProvider.class;
}
}

This file was deleted.

0 comments on commit 4e2cff0

Please sign in to comment.