1 18 19 package org.objectweb.jac.aspects.user; 20 21 import org.aopalliance.intercept.ConstructorInvocation; 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.objectweb.jac.aspects.authentication.AuthenticationAC; 24 import org.objectweb.jac.aspects.gui.GuiAC; 25 import org.objectweb.jac.core.AspectComponent; 26 import org.objectweb.jac.core.Interaction; 27 import org.objectweb.jac.core.ObjectRepository; 28 import org.objectweb.jac.core.Wrapper; 29 import org.objectweb.jac.core.rtti.ClassItem; 30 import org.objectweb.jac.core.rtti.ClassRepository; 31 import org.objectweb.jac.core.rtti.FieldItem; 32 import org.objectweb.jac.util.Log; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import java.util.Vector ; 36 37 41 42 public class UserWrapper extends Wrapper { 43 44 public UserWrapper(AspectComponent ac) { 45 super(ac); 46 } 47 48 49 Vector classes = new Vector (); 50 public void addClass(ClassItem cl) { 51 classes.add(cl); 52 } 53 54 60 protected void setUser(Object wrappee, Object user) { 61 UserAC ac = (UserAC) getAspectComponent(); 62 ClassItem classItem = ClassRepository.get().getClass(wrappee); 63 FieldItem[] fields = classItem.getFields(); 64 for (int i = 0; i < fields.length; i++) { 65 if (fields[i].getTypeItem() == ac.getUserClass()) { 66 if (fields[i].getThroughAccessor(wrappee) == null) { 67 Log.trace( 68 "user", 69 "initializing field " 70 + fields[i].getName() 71 + " with " 72 + GuiAC.toString(user)); 73 try { 74 fields[i].setThroughWriter(wrappee, user); 75 } catch (Exception e) { 76 Log.error( 77 "Failed to set user field " 78 + wrappee 79 + "." 80 + fields[i] 81 + " with " 82 + user); 83 e.printStackTrace(); 84 } 85 } 86 } 87 } 88 } 89 90 protected Object getUser() { 91 UserAC ac = (UserAC) getAspectComponent(); 92 Object user = attr(UserAC.USER); 93 if (user == null) { 94 String authuser = (String ) attr(AuthenticationAC.USER); 96 if (authuser != null) { 97 Collection users = 98 ObjectRepository.getObjects(ac.getUserClass()); 99 Iterator i = users.iterator(); 100 while (i.hasNext()) { 102 Object testedUser = i.next(); 103 Object id = 104 ac.getLoginField().getThroughAccessor(testedUser); 105 if (authuser.equals(id)) { 106 user = testedUser; 107 break; 108 } 109 } 110 } 111 } 112 return user; 113 } 114 115 118 public Object setUserOnNew(Interaction interaction) { 119 Log.trace("user", "setUser for " + interaction.method); 120 Object result = proceed(interaction); 121 UserAC ac = (UserAC) getAspectComponent(); 122 Object user = getUser(); 123 124 Log.trace("user", "user = " + user + "(" + GuiAC.toString(user) + ")"); 125 if (user != null) { 126 setUser(interaction.wrappee, user); 127 } 128 return result; 129 } 130 131 134 public Object setUser(Interaction interaction) { 135 Log.trace("user", "setUser for " + interaction.method); 136 Object result = proceed(interaction); 137 UserAC ac = (UserAC) getAspectComponent(); 138 Object user = getUser(); 139 140 Log.trace("user", "user = " + user + "(" + GuiAC.toString(user) + ")"); 141 if (user != null) { 142 for (int i = 0; i < interaction.args.length; i++) { 143 if (classes 144 .contains( 145 ClassRepository.get().getClass(interaction.args[i]))) { 146 Log.trace( 147 "user", 148 "Init param " 149 + i 150 + "(" 151 + interaction.args[i] 152 + ") of " 153 + interaction.method); 154 setUser(interaction.args[i], user); 155 } 156 } 157 } 158 return result; 159 } 160 161 public Object invoke(MethodInvocation invocation) throws Throwable { 162 return setUser((Interaction) invocation); 163 } 164 public Object construct(ConstructorInvocation invocation) 165 throws Throwable { 166 return setUserOnNew((Interaction)invocation); 167 } 168 169 } 170 | Popular Tags |