KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > integrity > RepositoryWrapper


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

17
18 package org.objectweb.jac.aspects.integrity;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import org.aopalliance.intercept.ConstructorInvocation;
24 import org.aopalliance.intercept.MethodInvocation;
25 import org.apache.log4j.Logger;
26 import org.objectweb.jac.core.AspectComponent;
27 import org.objectweb.jac.core.Collaboration;
28 import org.objectweb.jac.core.Interaction;
29 import org.objectweb.jac.core.NameRepository;
30 import org.objectweb.jac.core.ObjectRepository;
31 import org.objectweb.jac.core.Wrapper;
32 import org.objectweb.jac.core.rtti.ClassItem;
33 import org.objectweb.jac.core.rtti.CollectionItem;
34 import org.objectweb.jac.core.rtti.FieldItem;
35
36 /**
37  * This wrapper manages repository collections.
38  */

39
40 public class RepositoryWrapper extends Wrapper {
41     static final Logger logger = Logger.getLogger("integrity.repository");
42     
43     public static final int ADDER = 0;
44     public static final int REMOVER = 1;
45     
46     Object JavaDoc repository;
47     String JavaDoc repositoryName;
48     CollectionItem collection;
49     FieldItem field;
50     int type = ADDER;
51
52     public RepositoryWrapper(
53         AspectComponent ac,
54         String JavaDoc repositoryName,
55         CollectionItem collection,
56         FieldItem field,
57         int type)
58     {
59         super(ac);
60         this.repositoryName = repositoryName;
61         this.collection = collection;
62         this.field = field;
63         this.type=type;
64     }
65
66     /**
67      * Adds the object added to field to the repository
68      */

69     public Object JavaDoc addToRepository(Interaction interaction) {
70         Object JavaDoc ret = doAddToRepository(interaction);
71         if (IntegrityAC.isAddToRepositoryEnabled(collection)) {
72             logger.debug("checking for repository on " + interaction.method);
73             if (repository == null)
74                 repository = NameRepository.get().getObject(repositoryName);
75             Object JavaDoc toAdd = interaction.args[0];
76
77             if (toAdd != null
78                 && repository != null
79                 && !collection.getActualCollection(repository).contains(toAdd)) {
80                 logger.debug("addToRepository(" + repositoryName + ")");
81                 collection.addThroughAdder(repository, toAdd);
82             }
83         } else {
84             logger.debug(collection + " disabled");
85         }
86         return ret;
87     }
88
89     /**
90      * Disables addToRepository
91      */

92     public Object JavaDoc doAddToRepository(Interaction interaction) {
93         IntegrityAC.disableAddToRepository(collection);
94         logger.debug("doAddToRepository " + collection);
95         try {
96             return proceed(interaction);
97         } finally {
98             IntegrityAC.enableAddToRepository(collection);
99         }
100     }
101
102     public Object JavaDoc removeFromRepository(Interaction interaction) {
103         logger.debug("removeFromRepository(" + repositoryName + ")");
104         Object JavaDoc toRemove = interaction.args[0];
105         FieldItem target = collection != null ? collection : field;
106         List JavaDoc constraints = (List JavaDoc) target.getAttribute(IntegrityAC.CONSTRAINTS);
107         if (constraints == null)
108             return proceed(interaction);
109         Iterator JavaDoc it = constraints.iterator();
110         while (it.hasNext()) {
111             Constraint constraint = (Constraint) it.next();
112             Collection JavaDoc objects =
113                 ObjectRepository.getObjectsWhere(
114                     constraint.relation.getClassItem(),
115                     constraint.relation,
116                     toRemove);
117             logger.debug(" " + constraint + " => " + objects);
118             switch (constraint.constraint) {
119                 case Constraint.DELETE_CASCADE :
120                     break;
121                 case Constraint.SET_NULL :
122                     Iterator JavaDoc it2 = objects.iterator();
123                     while (it2.hasNext()) {
124                         Object JavaDoc substance = it2.next();
125                         logger.debug(
126                             " SET_NULL "
127                                 + substance + "." + constraint.relation);
128                         try {
129                             if (constraint.relation
130                                 instanceof CollectionItem) {
131                                 ((CollectionItem) constraint.relation)
132                                     .removeThroughRemover(
133                                         substance,
134                                         toRemove);
135                             } else {
136                                 constraint.relation.setThroughWriter(
137                                     substance,
138                                     null);
139                             }
140                         } catch (Exception JavaDoc e) {
141                             logger.error(
142                                 "Failed to enforce SET_NULL constraint for "
143                                 + constraint.relation + " on " + substance,e);
144                         }
145                     }
146                     break;
147                 case Constraint.FORBIDDEN :
148                     if (objects.size() > 0)
149                         throw new RuntimeException JavaDoc(
150                             "Cannot delete "
151                                 + toRemove + " from " + target.getLongName()
152                                 + " because of constraint " + constraint);
153                     break;
154                 default :
155                     logger.warn(
156                         "Unknown constraint type " + constraint.constraint);
157             }
158         }
159
160         return proceed(interaction);
161     }
162
163     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
164         Interaction interaction = (Interaction) invocation;
165         switch (type) {
166             case ADDER:
167                 return addToRepository(interaction);
168             case REMOVER:
169                 return removeFromRepository(interaction);
170             default:
171                 throw new Exception JavaDoc("Unknown RepositoryWrapper type: "+type);
172         }
173     }
174 }
175
176
Popular Tags