KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > user > UserWrapper


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3                           Renaud Pawlak <renaud@aopsys.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

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 JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 /**
38  * This wrapper is used by the user aspect to implement several
39  * croscutting treatments.
40  */

41
42 public class UserWrapper extends Wrapper {
43
44     public UserWrapper(AspectComponent ac) {
45         super(ac);
46     }
47
48     /** Classes to initialize */
49     Vector JavaDoc classes = new Vector JavaDoc();
50     public void addClass(ClassItem cl) {
51         classes.add(cl);
52     }
53
54     /**
55      * Set user attributes of an object.
56      *
57      * @param wrappee the object to initialize
58      * @param user the user object
59      */

60     protected void setUser(Object JavaDoc wrappee, Object JavaDoc 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 JavaDoc 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 JavaDoc getUser() {
91         UserAC ac = (UserAC) getAspectComponent();
92         Object JavaDoc user = attr(UserAC.USER);
93         if (user == null) {
94             // Get a user from authentication
95
String JavaDoc authuser = (String JavaDoc) attr(AuthenticationAC.USER);
96             if (authuser != null) {
97                 Collection JavaDoc users =
98                     ObjectRepository.getObjects(ac.getUserClass());
99                 Iterator JavaDoc i = users.iterator();
100                 // Find a user whose loginField matches the authenticated username
101
while (i.hasNext()) {
102                     Object JavaDoc testedUser = i.next();
103                     Object JavaDoc 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     /**
116      * Set the user attribute of new objects
117      */

118     public Object JavaDoc setUserOnNew(Interaction interaction) {
119         Log.trace("user", "setUser for " + interaction.method);
120         Object JavaDoc result = proceed(interaction);
121         UserAC ac = (UserAC) getAspectComponent();
122         Object JavaDoc 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     /**
132      * <p>Set user attributes of parameters.</p>
133      */

134     public Object JavaDoc setUser(Interaction interaction) {
135         Log.trace("user", "setUser for " + interaction.method);
136         Object JavaDoc result = proceed(interaction);
137         UserAC ac = (UserAC) getAspectComponent();
138         Object JavaDoc 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 JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
162         return setUser((Interaction) invocation);
163     }
164     public Object JavaDoc construct(ConstructorInvocation invocation)
165         throws Throwable JavaDoc {
166             return setUserOnNew((Interaction)invocation);
167     }
168
169 }
170
Popular Tags