Author: mir
Date: Tue Feb 23 14:51:46 2010
New Revision: 915361

URL: http://svn.apache.org/viewvc?rev=915361&view=rev
Log:
CLEREZZA-126: applied review comments

Modified:
    
incubator/clerezza/issues/CLEREZZA-126/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java

Modified: 
incubator/clerezza/issues/CLEREZZA-126/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-126/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java?rev=915361&r1=915360&r2=915361&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-126/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-126/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java
 Tue Feb 23 14:51:46 2010
@@ -20,8 +20,11 @@
 
 import java.net.URI;
 import java.net.URL;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.QueryParam;
@@ -30,6 +33,7 @@
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
+import org.apache.clerezza.jaxrs.utils.RedirectUtil;
 import org.apache.clerezza.platform.config.PlatformConfig;
 import org.apache.clerezza.platform.graphprovider.content.ContentGraphProvider;
 import org.apache.clerezza.rdf.core.Literal;
@@ -46,6 +50,8 @@
 import org.apache.felix.scr.annotations.Service;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -57,10 +63,10 @@
  * @author mir
  */
 @Component
-...@service(value = Object.class)
+...@service(Object.class)
 @Property(name = "javax.ws.rs", boolValue = true)
 @Path("thumbnail-service")
-public class ThumbnailService {
+public class ThumbnailService implements BundleListener{
 
        @Reference
        ContentGraphProvider cgProvider;
@@ -70,9 +76,19 @@
        private BundleContext bundleContext;
        private String STATICWEB_PATH = 
"/org/apache/clerezza/web/resources/style/staticweb/";
        private String BASE_PATH = STATICWEB_PATH + "images/icons/mediatype/";
+       private Bundle cachedStyleBundle = null;
+       private Map<MediaType, String> mediaTypeIconUriCache =
+                       Collections.synchronizedMap(new HashMap<MediaType, 
String>());
 
        protected void activate(ComponentContext context) {
                bundleContext = context.getBundleContext();
+               bundleContext.addBundleListener(this);
+       }
+
+       protected void deactivate(ComponentContext context) {
+               bundleContext.removeBundleListener(this);
+               bundleContext = null;
+               mediaTypeIconUriCache.clear();
        }
 
        /**
@@ -114,14 +130,18 @@
                if (mediaTypes.hasNext()) {
                        MediaType mediaType = 
MediaType.valueOf(LiteralFactory.getInstance().createObject(
                                        String.class, (TypedLiteral) 
mediaTypes.next()));
-                       URI iconUri = getMediaTypeIconUri(mediaType, 
uriInfo.getBaseUri());
-                       return Response.seeOther(iconUri).build();
+                       String iconUri = mediaTypeIconUriCache.get(mediaType);
+                       if (iconUri == null) {
+                               iconUri = getMediaTypeIconUri(mediaType);
+                               mediaTypeIconUriCache.put(mediaType, iconUri);
+                       }
+                       return RedirectUtil.createSeeOtherResponse(iconUri, 
uriInfo);
                }
-               return Response.seeOther(getDefaultIconUri(getStyleBundle(),
-                               uriInfo.getBaseUri())).build();
+               return RedirectUtil.createSeeOtherResponse(
+                               getDefaultIconUrl(getStyleBundle()), uriInfo);
        }
 
-       private URI getMediaTypeIconUri(MediaType mediaType, URI baseUri) {
+       private String getMediaTypeIconUri(MediaType mediaType) {
                Bundle styleBundle = getStyleBundle();
                if (styleBundle == null) {
                        throw new RuntimeException("no style bundle found");
@@ -129,21 +149,21 @@
                String path = BASE_PATH + mediaType.getType() + "/";
                Enumeration entries = styleBundle.findEntries(path,
                                mediaType.getSubtype() + ".*", false);
-               URI iconUri = createIconUri(entries, baseUri);
+               String iconUri = createIconUri(entries);
                if (iconUri != null) {
                        return iconUri;
                }
                entries = styleBundle.findEntries(path, "any.*", false);
-               iconUri = createIconUri(entries, baseUri);
+               iconUri = createIconUri(entries);
                if (iconUri != null) {
                        return iconUri;
                }
-               return getDefaultIconUri(styleBundle, baseUri);
+               return getDefaultIconUrl(styleBundle);
        }
 
-       private URI getDefaultIconUri(Bundle bundle, URI baseUri) {
+       private String getDefaultIconUrl(Bundle bundle) {
                Enumeration entries = bundle.findEntries(BASE_PATH, "any.*", 
false);
-               URI iconUri = createIconUri(entries, baseUri);
+               String iconUri = createIconUri(entries);
                if (iconUri != null) {
                        return iconUri;
                } else {
@@ -151,12 +171,10 @@
                }
        }
 
-       private URI createIconUri(Enumeration entries, URI baseUri) {
+       private String createIconUri(Enumeration entries) {
                if (entries != null && entries.hasMoreElements()) {
                        URL iconUrl = (URL) entries.nextElement();
-                       String relativeUri = iconUrl.getPath().
-                                       replace(STATICWEB_PATH, "style/");
-                       return URI.create(baseUri.toString() + relativeUri);
+                       return iconUrl.getPath().replace(STATICWEB_PATH, 
"style/");
                }
                return null;
        }
@@ -218,13 +236,28 @@
                }
        }
 
-       private Bundle getStyleBundle() {
+       private synchronized Bundle getStyleBundle() {
+               if (cachedStyleBundle != null) {
+                       return cachedStyleBundle;
+               }
                Bundle[] bundles = bundleContext.getBundles();
                for (Bundle bundle : bundles) {
-                       if 
(bundle.getSymbolicName().equals("org.apache.clerezza.web.resources.style")) {
+                       URL staticWebPathURL = bundle.getEntry(STATICWEB_PATH);
+                       if (staticWebPathURL != null ) {
+                               cachedStyleBundle = bundle;
                                return bundle;
                        }
                }
                return null;
        }
+
+       @Override
+       public synchronized void bundleChanged(BundleEvent be) {
+               if (be.getType() == BundleEvent.UNINSTALLED && 
+                               be.getBundle().equals(cachedStyleBundle)) {
+                       cachedStyleBundle = null;
+                       cachedStyleBundle = getStyleBundle();
+                       mediaTypeIconUriCache.clear();
+               }
+       }
 }


Reply via email to