[jboss-svn-commits] JBL Code SVN: r9914 - in labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects: proxies and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Mar 2 17:22:18 EST 2007


Author: wrzep
Date: 2007-03-02 17:22:18 -0500 (Fri, 02 Mar 2007)
New Revision: 9914

Added:
   labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyListProxy.java
Modified:
   labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsDescriptor.java
   labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyProxy.java
Log:
JBLAB-840
ReadOnlyListProxy

-Pawel


Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsDescriptor.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsDescriptor.java	2007-03-02 21:46:59 UTC (rev 9913)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsDescriptor.java	2007-03-02 22:22:18 UTC (rev 9914)
@@ -364,8 +364,7 @@
                    ProjectRW newProjectObj = (ProjectRW)un.unmarshal(
                        isf.getInputSource(projectId+"/" + ProjectsHelper.PROJECT_DESC));
                    
-                   Project newProject = newProjectObj; 
-                	   			//(Project) ReadOnlyProxy.newInstance(newProjectObj);
+                   Project newProject = (Project) ReadOnlyProxy.newInstance(newProjectObj);
                    
                    newProjects.put(projectId, newProject);
                    

Added: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyListProxy.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyListProxy.java	                        (rev 0)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyListProxy.java	2007-03-02 22:22:18 UTC (rev 9914)
@@ -0,0 +1,114 @@
+ /*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+
+package org.jboss.forge.common.projects.proxies;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import org.jboss.forge.common.exceptions.IllegalOperationException;
+
+/**
+ * @author Pawel Wrzeszcz (pawel . wrzeszcz [at] jboss . com)
+ */
+
+public class ReadOnlyListProxy implements InvocationHandler {
+
+	private List<Object> list = new ArrayList<Object>();
+	
+    public static Object newInstance(List obj) {
+    
+    		//System,out.println("LIST, NEW INSTANCE " + obj.getClass().getName());
+    	
+    		return java.lang.reflect.Proxy.newProxyInstance(
+    										obj.getClass().getClassLoader(),
+    										obj.getClass().getInterfaces(),
+    										new ReadOnlyListProxy(obj));
+    }
+
+    private ReadOnlyListProxy(List list) {
+    
+    	
+    		for (Object element : list) {
+    			
+    			if ((element == null) || element.getClass().isPrimitive() ||
+    					element.getClass().getPackage().getName().startsWith("java.lang") ) {
+    				
+    				//System,out.println("LIST, simple add " + element);
+    				this.list.add(element);
+    			} else {
+    				//System,out.println("LIST, wrapped add " + element);
+    				this.list.add(ReadOnlyProxy.newInstance(element));
+    			}
+		}
+    }
+
+    public Object invoke(Object proxy, Method m, Object[] args)
+    		throws Throwable {
+    
+    
+    		//System,out.println("LIST, INVOKE " + m.getDeclaringClass().getName() + " " + m.getName());
+    		
+    		String methodName = m.getName();
+ 
+       	//TODO Kill mutable operations
+    		if (false) {
+    			throw new IllegalOperationException(m);
+    		}
+    		
+    		Object result;
+    		
+    		if (methodName.equals("get")) {
+    			
+    			result = invoke(m, args);
+    			//System,out.println("LIST, GET " + result + " " + args[0] + list.get((Integer) args[0]));
+    			
+    		} else {
+    			result = invoke(m, args);
+    		}
+    		
+    		return result;
+    }
+
+	private Object invoke(Method m, Object[] args) throws Throwable {
+		
+		Object result;
+	    
+		try {
+	        	 
+			result = m.invoke(list, args);
+	        	 	
+	    } catch (InvocationTargetException e) {
+	        	 
+	     	 throw e.getTargetException();
+	     	 
+	    } catch (Exception e) {
+	        	 
+	    	 	throw new RuntimeException("Unexpected invocation exception: " + e.getMessage());
+	    } 
+	         
+	    return result;
+	}
+	
+}

Modified: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyProxy.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyProxy.java	2007-03-02 21:46:59 UTC (rev 9913)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyProxy.java	2007-03-02 22:22:18 UTC (rev 9914)
@@ -26,6 +26,7 @@
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.jboss.forge.common.exceptions.IllegalOperationException;
@@ -43,7 +44,7 @@
 	
     public static Object newInstance(Object obj) {
     
-    		System.out.println("NEW INSTANCE " + obj.getClass().getName());
+    		//System.out.println("NEW INSTANCE " + obj.getClass().getName());
     	
     		return java.lang.reflect.Proxy.newProxyInstance(
     										obj.getClass().getClassLoader(),
@@ -59,13 +60,10 @@
     public Object invoke(Object proxy, Method m, Object[] args)
     		throws Throwable {
     
-    		System.out.println("INVOKE " + m.getDeclaringClass().getName() + " " + m.getName());
+    		//System.out.println("INVOKE " + m.getDeclaringClass().getName() + " " + m.getName());
     		
-    		return invoke(m, args);
-    		
-    		/*
     		if (obj instanceof Project) {
-    			System.out.println("PROJECT: " + ((Project) obj).getId());
+    			//System.out.println("PROJECT: " + ((Project) obj).getId());
     		}
     		
     		String methodName = m.getName();
@@ -73,7 +71,7 @@
     		// Check "cache" (only getter results are stored there)	
     		if (methodValues.containsKey(methodName)) {
     		
-    			System.out.println("RET " + methodValues.get(methodName));
+    			//System.out.println("RET " + methodValues.get(methodName));
     			return methodValues.get(methodName);
     		}
     		
@@ -91,20 +89,24 @@
     			if ((result != null) && !m.getReturnType().isPrimitive() && 
     					!m.getReturnType().getPackage().getName().startsWith("java.lang")) {
     				
-    				System.out.println("WRAPPING...");
+    				//System.out.println("WRAPPING...");
     				
-    				result = ReadOnlyProxy.newInstance(result);
+    				if (result instanceof List) {
+    				  					
+    					result = ReadOnlyListProxy.newInstance((List) result);
+    				} else {
+    				
+    					result = ReadOnlyProxy.newInstance(result);
+    				}
     			}
     			
-        		// TODO Collections handling
-    			System.out.println("CACHED");
+    			//System.out.println("CACHED");
     			methodValues.put(methodName, result);
     		}
     		
-    		System.out.println("RET " + result);
+    		//System.out.println("RET " + result);
     		
         return result;
-        */
     }
 
 	private Object invoke(Method m, Object[] args) throws Throwable {




More information about the jboss-svn-commits mailing list