KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distribution > DeploymentRule


1 /*
2   Copyright (C) 2001 Renaud Pawlak
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.distribution;
19
20 import java.io.Serializable JavaDoc;
21
22 import org.objectweb.jac.core.*;
23 import org.objectweb.jac.core.dist.*;
24 import gnu.regexp.*;
25 import java.util.*;
26
27 /**
28  * A deployment rule parametrizes the deployment scheme of a set
29  * of objects identified by a regular expression.
30  *
31  * <p>Each object that is named by the naming aspect so that it
32  * matches the regular expression will be deployed regarding the
33  * deployment rule.<p>
34  *
35  * The deployment aspect component uses a set of deployment rules
36  * to know how to handle a newly used object.<p>
37  *
38  * @see DeploymentAC
39  * @see Deployment
40  * @see Topology
41  *
42  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
43  */

44
45 public class DeploymentRule implements Serializable JavaDoc {
46
47    /** Stores the readable type of the rule. */
48    String JavaDoc type = "";
49
50    /** Stores the regular expression that represents the objects
51        to be affected by this distribution rule. */

52    RE nameRegexp;
53
54    /** Stores the regular expression that identifies the remote
55        containers where the deployment rule will be applied. */

56    RE contRegexp;
57
58    boolean state=false;
59
60    /** Stores the knowledge style of the replication group. */
61    int knowledgeStyle;
62
63    /** Stores the knowledge graph if needed. */
64    String JavaDoc[] knowledgeGraph;
65
66    /** The objects that have allready been treated. */
67    transient Hashtable treated = new Hashtable();
68
69    AspectComponent ac;
70
71    /**
72     * Creates a new deployment rule in the general case.
73     *
74     * @param type a string that contains a readable representation of
75     * what the rule is doing
76     * @param nameRegexp a regular expression that filters the objects
77     * to which this rule will be applied
78     * @param contRegexp a regular expression that defines a set of
79     * remote container where the objects of the rule will be
80     * deployed
81     * @param state if true, the states of the objects are replicated,
82     * else, it only deploys empty objects */

83
84    public DeploymentRule (AspectComponent ac,
85                           String JavaDoc type,
86                           String JavaDoc nameRegexp,
87                           String JavaDoc contRegexp,
88                           boolean state ) {
89       this.ac = ac;
90       this.type = type;
91       try {
92          this.nameRegexp = new RE(nameRegexp);
93          this.contRegexp = new RE(contRegexp);
94       } catch (REException e) {
95          System.out.println("Regexp construction failed : "+e);
96       }
97       this.state = state;
98    }
99
100    /**
101     * Returns true if the deployment rule must by applied on a given
102     * object.<p>
103     *
104     * @param candidate the tested object
105     * @return true if the candiate matches the rule */

106
107    public boolean isApplicableTo( Object JavaDoc candidate ) {
108       String JavaDoc name = NameRepository.get().getName( candidate );
109       if ( name == null ) return false;
110       if ( nameRegexp.isMatch(name) ) {
111          return true;
112       }
113       return false;
114    }
115
116    /**
117     * Tells if the rule is already applied to a given object.<p>
118     *
119     * @param object the object to test
120     * @return true if the rule is applied to the object */

121
122    public boolean isAppliedTo( Object JavaDoc object ) {
123       if (treated == null) treated = new Hashtable();
124       if (treated.get( object ) != null) return true;
125       if( type.equals( "dynamic client-server" ) ) {
126          return true;
127       }
128       /*if ( ((Wrappee)object).isExtendedBy( consistencyType ) ) {
129          return true;
130       } else {
131          return false;
132          }*/

133       return false;
134    }
135
136    /**
137     * Applies the rule to the given object.<p>
138     *
139     * @param object the object on which the deployment rule will be
140     * applied to */

141
142    public void applyTo( Object JavaDoc object ) {
143       if (treated == null) treated = new Hashtable();
144       treated.put( object, "" );
145       Topology topology = Topology.getPartialTopology( contRegexp );
146
147       Deployment dep = new Deployment(ac,topology);
148       
149       if( state ) {
150          dep.replicate( object );
151       } else {
152          dep.replicateStruct( object );
153       }
154    }
155
156    /**
157     * Returns a readable string representation of the type of this
158     * distribution rule.
159     *
160     * @return the type */

161
162    public String JavaDoc getType() {
163       return type;
164    }
165
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
Popular Tags