[Yanel-commits] rev 45781 - public/yanel/trunk/src/core/java/org/wyona/yanel/core

guillaume at wyona.com guillaume at wyona.com
Tue Dec 1 19:45:39 CET 2009


Author: guillaume
Date: 2009-12-01 19:45:38 +0100 (Tue, 01 Dec 2009)
New Revision: 45781

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/RTIbasedResourceConfiguration.java
Modified:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
Log:
Subsumed old-style "RTI" resource configuration object creation
 under new style's using a new adapter class.

Also cleaned up everything and error handling (esp. WRT RTI).

Issue: 4971?
Issue: 7205


Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/RTIbasedResourceConfiguration.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/RTIbasedResourceConfiguration.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/RTIbasedResourceConfiguration.java	2009-12-01 18:45:38 UTC (rev 45781)
@@ -0,0 +1,53 @@
+package org.wyona.yanel.core;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+/**
+ * Adapter for old-style "RTI" resource configuration objects.
+ */
+final class RTIbasedResourceConfiguration extends ResourceConfiguration {
+
+    private static final Logger log = Logger.getLogger(ResourceManager.class);
+
+    private static final Pattern universalNamePattern;
+    static {
+        try {
+            universalNamePattern = Pattern.compile("<\\{([^}]+)\\}([^/]+)/>");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    @SuppressWarnings("deprecation")
+    private final ResourceTypeIdentifier rti;
+
+    @SuppressWarnings("deprecation")
+    private static final String getUniversalNamePart(ResourceTypeIdentifier rti, int partNumber) {
+    	String universalName = rti.getUniversalName();
+        Matcher m = universalNamePattern.matcher(universalName);
+        if (! m.matches()) {
+            throw new IllegalArgumentException(universalName
+             + "is not a valid universal name: it does not match " + m.pattern() + "!");
+        }
+        String part = m.group(partNumber);
+        if (log.isDebugEnabled()) {
+            log.debug("universalName: "+universalName+"\n"+m.pattern()+" part #"+partNumber+": "+part);
+        }
+        return part;
+    }
+
+    @SuppressWarnings("deprecation")
+    public RTIbasedResourceConfiguration(ResourceTypeIdentifier rti) {
+        super(getUniversalNamePart(rti, 2), getUniversalNamePart(rti, 1), rti.properties);
+        this.rti = rti;
+    }
+
+    @SuppressWarnings("deprecation")
+    public final ResourceTypeIdentifier getRTI() {
+        return rti;
+    }
+}

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java	2009-12-01 18:28:28 UTC (rev 45780)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java	2009-12-01 18:45:38 UTC (rev 45781)
@@ -28,7 +28,9 @@
 import org.wyona.yarep.core.NoSuchNodeException;
 
 /**
- *
+ * Service responsible for instantiating {@link Resource} objects for requests
+ *  given some initial resource-type-related configuration
+ *  and some context for the request.
  */
 public class ResourceManager {
 
@@ -36,44 +38,26 @@
     
     protected ResourceTypeRegistry rtRegistry;
     
-    /**
-     *
-     */
-    public ResourceManager() {
-    }
-    
     public void setResourceTypeRegistry(ResourceTypeRegistry registry) {
         this.rtRegistry = registry;
     }
 
     /**
      * Creates a new resource object in the given realm with the given path and the given type.
+     * @deprecated Use {@link #getResource(Environment, Realm, String, ResourceConfiguration)} instead,
+     *  or if you cannot then maybe
+     *  <code>getResource(environment, realm, path, new RTIbasedResourceConfiguration(rti))</code>
+     *  will suit your situation better.
      *
      * @param environment Environment which for example contains request and response
      * @param realm Realm where resource is living
      * @param path Path relative to realm (e.g. yanel.getMap().getPath(realm, request.getServletPath()))
-     * @param rtd Resource type definition
-     * @param rti Resource type identifier (deprecated and contains redudant information to resource type definition. What about properties?)
+     * @param rtd Resource type definition, should be not <samp>null</samp>
+     * @param rti Resource type identifier (deprecated and contains redundant information to resource type definition. What about properties?)
      */
+    @Deprecated
     public Resource getResource(Environment environment, Realm realm, String path, ResourceTypeDefinition rtd, ResourceTypeIdentifier rti) throws Exception {
-        String universalName = rtd.getResourceTypeUniversalName();
-        if (rtd != null) {
-            Resource resource = (Resource) Class.forName(rtd.getResourceTypeClassname()).newInstance();
-
-            resource.setRTD(rtd);
-            resource.setYanel(Yanel.getInstance());
-            resource.setRealm(realm);
-            resource.setPath(path);
-            resource.setRTI(rti);
-            resource.setEnvironment(environment);
-            
-            passParameters(resource, environment.getRequest());
-            
-            return resource;
-        } else {
-            log.error("No resource registered for rti: " + universalName);
-            return null;
-        }
+        return getResource(environment, realm, path, rtd, new RTIbasedResourceConfiguration(rti));
     }
 
     /**
@@ -83,8 +67,17 @@
      */
     public Resource getResource(Environment environment, Realm realm, String path, ResourceConfiguration rc) throws Exception {
         ResourceTypeDefinition rtd = rtRegistry.getResourceTypeDefinition(rc.getUniversalName());
-
         if (rtd != null) {
+            return getResource(environment, realm, path, rtd, rc);
+        }
+        log.error("Resource Type Definition cannot be determined for: " + realm + ", " + path + ", " + rc.getUniversalName());
+        return null;
+    }
+
+    /**
+     * @param rtd not <samp>null</samp>
+     */
+    private Resource getResource(Environment environment, Realm realm, String path, ResourceTypeDefinition rtd, ResourceConfiguration rc) throws Exception {
             try {
                 Resource resource = (Resource) Class.forName(rtd.getResourceTypeClassname()).newInstance();
 
@@ -93,6 +86,12 @@
                 resource.setRealm(realm);
                 resource.setPath(path);
                 resource.setConfiguration(rc);
+
+                // small hack to support old-style "RTI" resource-configuration objects:
+                if (rc instanceof RTIbasedResourceConfiguration) {
+                    resource.setRTI(((RTIbasedResourceConfiguration) rc).getRTI());
+                }
+
                 resource.setEnvironment(environment);
             
                 passParameters(resource, environment.getRequest());
@@ -102,10 +101,6 @@
                 log.error("Resource class not found for " + rtd.getResourceTypeUniversalName());
                 throw e;
             }
-        } else {
-            log.error("Resource Type Definition cannot be determined for: " + realm + ", " + path + ", " + rc.getUniversalName());
-            return null;
-        }
     }
 
     /**
@@ -125,8 +120,8 @@
         if (realm.getRTIRepository().existsNode(PathUtil.getRTIPath(path))) {
             log.warn("DEPRECATED: RTI should be replaced by ResourceConfiguration: " + realm + ", " + path);
             ResourceTypeIdentifier rti = getResourceTypeIdentifier(realm, path);
-            ResourceTypeDefinition rtd = rtRegistry.getResourceTypeDefinition(rti.getUniversalName());
-            return getResource(environment, realm, path, rtd, rti);
+            ResourceConfiguration rc = new RTIbasedResourceConfiguration(rti);
+            return getResource(environment, realm, path, rc);
         } 
 
         // Check on resource configuration map



More information about the Yanel-commits mailing list