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

michi at wyona.com michi at wyona.com
Thu Dec 7 19:53:08 CET 2006


Author: michi
Date: 2006-12-07 19:53:07 +0100 (Thu, 07 Dec 2006)
New Revision: 20663

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeIdentifier.java
Log:
more classes from Josias added

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java	2006-12-07 18:42:01 UTC (rev 20662)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java	2006-12-07 18:53:07 UTC (rev 20663)
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.wyona.yanel.core;
+
+import java.io.Reader;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.log4j.Category;
+import org.wyona.yanel.core.map.Map;
+import org.wyona.yanel.core.map.Realm;
+import org.wyona.yarep.core.NoSuchNodeException;
+
+/**
+ *
+ */
+public class ResourceManager {
+
+    private static Category log = Category.getInstance(ResourceTypeRegistry.class);
+    
+    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.
+     */
+    public Resource getResource(HttpServletRequest request, HttpServletResponse response, 
+            Realm realm, Path 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.setRequest(request);
+            resource.setResponse(response);
+            
+            return resource;
+        } else {
+            log.error("No resource registered for rti: " + universalName);
+            return null;
+        }
+    }
+
+    /**
+     * Creates a new resource object in the given realm and with the given path.
+     */
+    public Resource getResource(HttpServletRequest request, HttpServletResponse response, 
+            Realm realm, Path path) throws Exception {
+        ResourceTypeIdentifier rti = getResourceTypeIdentifier(realm, path);
+        ResourceTypeDefinition rtd = rtRegistry.getResourceTypeDefinition(rti.getUniversalName());
+        Resource resource = getResource(request, response, realm, path, rtd, rti);
+        return resource;
+    }
+
+    /**
+     * Returns the abstraction of the rti file for the given path in the realm.
+     * TODO: move this method to some RTIManager class ?
+     */
+    public ResourceTypeIdentifier getResourceTypeIdentifier(Realm realm, Path path) throws Exception {
+        log.debug("Original path: " + path);
+        try {
+            Reader reader = realm.getRTIRepository().getReader(path.getRTIPath());
+            return new ResourceTypeIdentifier(reader);
+        } catch(NoSuchNodeException e) {
+            log.warn(e.getMessage());
+            log.warn("TODO: Implement chain of responsibility ...");
+            return new ResourceTypeIdentifier("<{http://www.wyona.org/yanel/resource/1.0}file/>", null);
+        } 
+    }
+    
+
+}

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeIdentifier.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeIdentifier.java	2006-12-07 18:42:01 UTC (rev 20662)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeIdentifier.java	2006-12-07 18:53:07 UTC (rev 20663)
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.wyona.yanel.core;
+
+import java.io.BufferedReader;
+import java.io.Reader;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Category;
+
+/**
+ * Abstraction of an rti file.
+ * Convention:
+ * - first line contains resource type identifier string: <{http://www.wyona.org/yanel/resource/1.0}file/>
+ * - all remaining lines contain properties in the following format:
+ *        key:value
+ * - lines starting with '#' are ignored
+ * - lines which don't contain a ':' are ignored
+ *        
+ * TODO: this class should be renamed
+ */
+public class ResourceTypeIdentifier {
+
+    private Category log = Category.getInstance(ResourceTypeDefinition.class);
+
+    protected Map properties;
+    protected String universalName;
+    
+    public ResourceTypeIdentifier(Reader reader) throws Exception {
+        BufferedReader br = new BufferedReader(reader);
+
+        this.universalName = br.readLine();
+        this.properties = new HashMap();
+        
+        String property;
+        while ((property = br.readLine()) != null) {
+            int colonIndex = property.indexOf(":");
+            if (colonIndex > 0 && !property.trim().startsWith("#")) {
+                String name = property.substring(0, colonIndex).trim();
+                String value = property.substring(colonIndex + 1).trim();
+                this.properties.put(name, value);
+            }
+        }
+    }
+    
+    public ResourceTypeIdentifier(String universalName, Map properties) {
+        if (properties == null) {
+            this.properties = new HashMap();
+        } else {
+            this.properties = properties;
+        }
+        this.universalName = universalName;
+    }
+    
+    public String getUniversalName() {
+        return universalName;
+    }
+    
+    /**
+     * @param key
+     * @return value for this key or null if no value exists for this key.
+     */
+    public String getProperty(String key) {
+        return (String)properties.get(key);
+    }
+    
+    public boolean containsKey(String key) {
+        return properties.containsKey(key);
+    }
+
+
+}




More information about the Yanel-commits mailing list