Revision: 3810
http://openutils.svn.sourceforge.net/openutils/?rev=3810&view=rev
Author: diego_schivo
Date: 2012-03-16 11:10:27 +0000 (Fri, 16 Mar 2012)
Log Message:
-----------
MEDIA-279 ImageUtils
Modified Paths:
--------------
magnoliamodules/branches/magnolia44/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java
Modified: magnoliamodules/branches/magnolia44/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java
===================================================================
--- magnoliamodules/branches/magnolia44/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2012-03-16 10:58:18 UTC (rev 3809)
+++ magnoliamodules/branches/magnolia44/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2012-03-16 11:10:27 UTC (rev 3810)
@@ -57,9 +57,9 @@
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
+import javax.jcr.Node;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
-import javax.jcr.lock.Lock;
import net.sourceforge.openutils.mgnlmedia.media.configuration.ImageProcessorsManager;
import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaConfigurationManager;
@@ -72,6 +72,7 @@
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
+import org.apache.jackrabbit.util.Locked;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -121,6 +122,15 @@
private static final String[] extensions = new String[]{"jpg", "jpeg", "gif", "png", "ico" };
+ private static int currentWorkingThreads = 0;
+
+ private static LockUtils locks = new LockUtils(4);
+
+ public static void setMediaThreads(int mediaThreads)
+ {
+ locks = new LockUtils(mediaThreads);
+ }
+
static
{
sdf = new SimpleDateFormat();
@@ -512,8 +522,9 @@
* @throws RepositoryException exception in jcr operations
* @throws IOException exception converting image to jpg
*/
- public static NodeData saveResolution(BufferedImage image, Content saveTo, String name, String extension,
- float quality, boolean forceProgressive) throws RepositoryException, IOException
+ public static NodeData saveResolution(final BufferedImage image, final Content saveTo, final String name,
+ final String extension, final float quality, final boolean forceProgressive) throws RepositoryException,
+ IOException
{
Content resolutions = getResolutionsNode(saveTo);
@@ -530,87 +541,129 @@
resolution = "res-" + image.getWidth() + "x" + image.getHeight();
}
- String originalRes = resolution;
- resolution = getResolutionPath(resolution);
+ final String resolutionNodeName = getResolutionPath(resolution);
+ final Content resolutionsFinal = resolutions;
- // max wait 5 secs
- Lock lock = resolutions.lock(false, true, 5000);
+ Node resolutionsJcrNode = resolutions.getJCRNode();
+ Object ret;
try
{
-
- if (resolutions.hasNodeData(resolution))
+ ret = new Locked()
{
- NodeData nd = resolutions.getNodeData(resolution);
- nd.delete();
- resolutions.save();
- }
- // don't remove deprecated method call, needed for magnolia 4.0 compatibility
- final NodeData nd = resolutions.createNodeData(resolution, PropertyType.BINARY);
- final PipedInputStream stream = new PipedInputStream();
- PipedOutputStream outputstream = new PipedOutputStream(stream);
+ @Override
+ protected Object run(Node resolutionsJcrNode) throws RepositoryException
+ {
+ NodeData ndtemp;
+ boolean existing = false;
+ if (resolutionsFinal.hasNodeData(resolutionNodeName))
+ {
+ ndtemp = resolutionsFinal.getNodeData(resolutionNodeName);
+ existing = true;
+ }
+ else
+ {
+ // don't remove deprecated method call, needed for magnolia 4.0 compatibility
+ ndtemp = resolutionsFinal.createNodeData(resolutionNodeName, PropertyType.BINARY);
+ }
+ log.debug("setting value to {}", ndtemp.getHandle());
- log.debug("setting value to {}", nd.getHandle());
+ final NodeData nd = ndtemp;
+ final PipedInputStream stream = new PipedInputStream();
+ PipedOutputStream outputstream = null;
+ Thread t = null;
+ long count = 0;
- Thread t = new Thread(new Runnable()
- {
-
- /**
- * {@inheritDoc}
- */
- public void run()
- {
try
{
- nd.setValue(stream);
+ outputstream = new PipedOutputStream(stream);
+ t = new Thread(new Runnable()
+ {
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run()
+ {
+ try
+ {
+ nd.setValue(stream);
+ }
+ catch (RepositoryException e)
+ {
+ log.error(e.getMessage(), e);
+ }
+ }
+
+ });
+ t.start();
+
+ count = getStream(image, extension, quality, forceProgressive, outputstream);
}
- catch (RepositoryException e)
+ catch (IOException e)
{
// TODO Auto-generated catch block
- log.error(e.getMessage(), e);
+ e.printStackTrace();
}
- }
- });
- t.start();
+ IOUtils.closeQuietly(outputstream);
+ try
+ {
+ t.join();
+ }
+ catch (InterruptedException e)
+ {
+ log.warn(e.getMessage(), e);
+ }
- long count = getStream(image, extension, quality, forceProgressive, outputstream);
- IOUtils.closeQuietly(outputstream);
- try
- {
- t.join();
- }
- catch (InterruptedException e)
- {
- log.warn(e.getMessage(), e);
- }
+ IOUtils.closeQuietly(stream);
- IOUtils.closeQuietly(stream);
+ String mimetype = "image/" + extension;
+ if ("jpg".equals(extension))
+ {
+ mimetype = "image/jpeg";
+ }
+ nd.setAttribute(ImageUtils.RESOLUTION_PROPERTY, resolutionNodeName);
+ nd.setAttribute(FileProperties.PROPERTY_EXTENSION, extension);
+ nd.setAttribute(FileProperties.PROPERTY_FILENAME, saveTo.getName());
+ nd.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, mimetype);
+ nd.setAttribute(
+ FileProperties.PROPERTY_LASTMODIFIED,
+ GregorianCalendar.getInstance(TimeZone.getDefault()));
+ nd.setAttribute(FileProperties.PROPERTY_WIDTH, "" + image.getWidth());
+ nd.setAttribute(FileProperties.PROPERTY_HEIGHT, "" + image.getHeight());
- String mimetype = "image/" + extension;
- if ("jpg".equals(extension))
- {
- mimetype = "image/jpeg";
- }
- nd.setAttribute(ImageUtils.RESOLUTION_PROPERTY, originalRes);
- nd.setAttribute(FileProperties.PROPERTY_EXTENSION, extension);
- nd.setAttribute(FileProperties.PROPERTY_FILENAME, saveTo.getName());
- nd.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, mimetype);
- nd.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, GregorianCalendar.getInstance(TimeZone.getDefault()));
- nd.setAttribute(FileProperties.PROPERTY_WIDTH, "" + image.getWidth());
- nd.setAttribute(FileProperties.PROPERTY_HEIGHT, "" + image.getHeight());
+ nd.setAttribute(FileProperties.PROPERTY_SIZE, "" + count);
- nd.setAttribute(FileProperties.PROPERTY_SIZE, "" + count);
+ if (existing)
+ {
+ nd.save();
+ }
+ else
+ {
+ resolutionsFinal.save();
+ }
- resolutions.save();
-
- return nd;
+ return nd;
+ }
+ }.with(resolutionsJcrNode, false, 5000);
}
- finally
+ catch (InterruptedException e)
{
- resolutions.unlock();
+ MgnlContext.getHierarchyManager("media").refresh(false);
+ ret = Locked.TIMED_OUT;
}
+ if (ret == Locked.TIMED_OUT)
+ {
+ // do whatever you think is appropriate in this case
+ return null;
+ }
+ else
+ {
+ // get the value
+ return (NodeData) ret;
+ }
}
@@ -793,7 +846,7 @@
public static boolean checkOrCreateResolution(final Content media, final String resolutionTarget,
String nodeDataName, final boolean lazy)
- {
+ {
Content resolutions = getResolutionsNode(media);
@@ -915,23 +968,37 @@
outputextension = "jpg";
}
- BufferedImage original = createBufferedImage(image);
+ BufferedImage original = null;
+ BufferedImage img = null;
+ Map<String, String> params = parseParameters(resolutionstring);
- Map<String, String> params = parseParameters(resolutionstring);
if (lazy)
{
params.put("skipRendering", "true");
}
- BufferedImage img;
- try
+ synchronized (locks.nextLock())
{
- img = ImageUtils.getImageForResolution(original, resolutionstring, params);
+ currentWorkingThreads++;
+ if (log.isDebugEnabled())
+ {
+ log.debug("Current working resizing thread: {}", currentWorkingThreads);
+ }
+ original = createBufferedImage(image);
+
+ try
+ {
+ img = ImageUtils.getImageForResolution(original, resolutionstring, params);
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new RuntimeException(e);
+ }
+ finally
+ {
+ currentWorkingThreads--;
+ }
}
- catch (IllegalArgumentException e)
- {
- throw new RuntimeException(e);
- }
try
{
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|