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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Mar 5 05:30:10 EST 2007


Author: wrzep
Date: 2007-03-05 05:30:09 -0500 (Mon, 05 Mar 2007)
New Revision: 9967

Added:
   labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ProxyUtils.java
   labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/SyncProxy.java
Modified:
   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/ReadOnlyProxy.java
   labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/projects/ProjectsServiceRWImpl.java
Log:
JBLAB-840
thread-safety utils and rw proxy

-Pawel


Added: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ProxyUtils.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ProxyUtils.java	                        (rev 0)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ProxyUtils.java	2007-03-05 10:30:09 UTC (rev 9967)
@@ -0,0 +1,84 @@
+ /*
+  * 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.util.List;
+
+/**
+ * @author Pawel Wrzeszcz (pawel . wrzeszcz [at] jboss . com)
+ */
+
+public class ProxyUtils {
+
+	private static final String[] mutableListOperations = { "add", 
+													  		"addAll",
+													  		"clear",
+													  		"remove",
+													  		"removeAll",
+													  		"reatinAll",
+													  		"set"};
+	
+	public static boolean isSimpleType(Class<? extends Object> type) {
+		
+		boolean result = type.isPrimitive() ||
+				type.getPackage().getName().startsWith("java.lang");
+		
+		//System.out.println("isSimpleType: " + type.getName() + " " +
+				//result);
+		
+		return result;
+	}	
+
+	public static boolean isMutableListOperation(String methodName) {
+
+		for (String op : mutableListOperations) {
+			if (op.equals(methodName)) {
+				return true;
+			}
+		}
+		
+		return false;
+	}
+
+	public static Object wrap(Object result) {
+
+		if ((result == null) || (isSimpleType(result.getClass()))) {
+			
+			return result;
+		}
+		
+		if (result instanceof List) {
+
+			
+			//System.out.println("WRAP as LIST");
+			return ReadOnlyListProxy.newInstance((List) result);
+		}
+		
+		
+		//System.out.println("SIMPLE WRAP");
+		return ReadOnlyProxy.newInstance(result);		
+	}
+	
+	private ProxyUtils() {}
+
+}

Modified: 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	2007-03-05 10:06:38 UTC (rev 9966)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyListProxy.java	2007-03-05 10:30:09 UTC (rev 9967)
@@ -39,7 +39,7 @@
 	
     public static Object newInstance(List obj) {
     
-    		//System,out.println("LIST, NEW INSTANCE " + obj.getClass().getName());
+    		//System.out.println("LIST, NEW INSTANCE " + obj.getClass().getName());
     	
     		return java.lang.reflect.Proxy.newProxyInstance(
     										obj.getClass().getClassLoader(),
@@ -47,33 +47,26 @@
     										new ReadOnlyListProxy(obj));
     }
 
-    private ReadOnlyListProxy(List list) {
+    private ReadOnlyListProxy(List l) {
     
     	
-    		for (Object element : list) {
+    		for (Object element : l) {
     			
-    			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));
-    			}
+    			Object e = ProxyUtils.wrap(element);    			
+    			list.add(e);
 		}
     }
 
     public Object invoke(Object proxy, Method m, Object[] args)
-    		throws Throwable {
+    														throws Throwable {
     
     
-    		//System,out.println("LIST, INVOKE " + m.getDeclaringClass().getName() + " " + m.getName());
+    		//System.out.println("LIST, INVOKE " + m.getDeclaringClass().getName() + " " + m.getName());
     		
     		String methodName = m.getName();
  
-       	//TODO Kill mutable operations
-    		if (false) {
+       	// Kill mutable operations
+    		if (ProxyUtils.isMutableListOperation(methodName)) {
     			throw new IllegalOperationException(m);
     		}
     		
@@ -82,7 +75,7 @@
     		if (methodName.equals("get")) {
     			
     			result = invoke(m, args);
-    			//System,out.println("LIST, GET " + result + " " + args[0] + list.get((Integer) args[0]));
+    			//System.out.println("LIST, GET " + result + " " + args[0] + list.get((Integer) args[0]));
     			
     		} else {
     			result = invoke(m, args);

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-05 10:06:38 UTC (rev 9966)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/ReadOnlyProxy.java	2007-03-05 10:30:09 UTC (rev 9967)
@@ -71,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 from cache: " + methodValues.get(methodName));
     			return methodValues.get(methodName);
     		}
     		
@@ -86,21 +86,9 @@
     		// For getters, store the result in "cache"
     		if (methodName.startsWith("get") || methodName.startsWith("is")) {
 
-    			if ((result != null) && !m.getReturnType().isPrimitive() && 
-    					!m.getReturnType().getPackage().getName().startsWith("java.lang")) {
-    				
-    				//System.out.println("WRAPPING...");
-    				
-    				if (result instanceof List) {
-    				  					
-    					result = ReadOnlyListProxy.newInstance((List) result);
-    				} else {
-    				
-    					result = ReadOnlyProxy.newInstance(result);
-    				}
-    			}
+    			result = ProxyUtils.wrap(result);
     			
-    			//System.out.println("CACHED");
+    			//System.out.print("CACHED ");
     			methodValues.put(methodName, result);
     		}
     		

Added: labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/SyncProxy.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/SyncProxy.java	                        (rev 0)
+++ labs/jbosslabs/trunk/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/proxies/SyncProxy.java	2007-03-05 10:30:09 UTC (rev 9967)
@@ -0,0 +1,94 @@
+ /*
+  * 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 org.jboss.forge.common.projects.project.Project;
+
+/**
+ * @author Pawel Wrzeszcz (pawel . wrzeszcz [at] jboss . com)
+ */
+
+public class SyncProxy implements InvocationHandler {
+
+	private Object obj;
+	
+	
+    public static Object newInstance(Object obj) {
+    
+    		//System.out.println("sync new instance " + obj.getClass().getName());
+    	
+    		return java.lang.reflect.Proxy.newProxyInstance(
+    										obj.getClass().getClassLoader(),
+    										obj.getClass().getInterfaces(),
+    										new SyncProxy(obj));
+    }
+
+    private SyncProxy(Object obj) {
+    		
+    		this.obj = obj;
+    }
+
+    public Object invoke(Object proxy, Method m, Object[] args)
+    		throws Throwable {
+    
+    		//System.out.println("sync invoke " + m.getDeclaringClass().getName() + " " + m.getName());
+    		
+    		if (obj instanceof Project) {
+    			//System.out.println("sync PROJECT: " + ((Project) obj).getId());
+    		}
+    		
+    		Object result;
+    		
+    		synchronized (this) {
+				
+			result = ProxyUtils.wrap(invoke(m, args));
+    		}
+    		
+        return result;
+    }
+
+	private Object invoke(Method m, Object[] args) throws Throwable {
+		
+		Object result;
+	    
+		try {
+	        	 
+			result = m.invoke(obj, 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-ejb3/src/java/org/jboss/forge/ejb3/projects/ProjectsServiceRWImpl.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/projects/ProjectsServiceRWImpl.java	2007-03-05 10:06:38 UTC (rev 9966)
+++ labs/jbosslabs/trunk/portal-extensions/forge-ejb3/src/java/org/jboss/forge/ejb3/projects/ProjectsServiceRWImpl.java	2007-03-05 10:30:09 UTC (rev 9967)
@@ -85,7 +85,7 @@
     
     public ProjectRW getProjectByNameRW(String projectId) {
     	 Projects projectsClass = ProjectsHelper.getProjects(Constants.LABS_PORTAL);
-         Map<String,? extends Project> projects = projectsClass.getProjectsDescriptor().getNewProjects();
+         Map<String,? extends Project> projects = projectsClass.getProjectsDescriptor().getNewProjectsRW();
          //return projects.get(projectId);
          return null;
     }




More information about the jboss-svn-commits mailing list