001    /*
002      Copyright (C) 2001 Renaud Pawlak
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
017    
018    package org.objectweb.jac.aspects.distribution;
019    
020    import java.io.Serializable;
021    
022    import org.objectweb.jac.core.*;
023    import org.objectweb.jac.core.dist.*;
024    import gnu.regexp.*;
025    import java.util.*;
026    
027    /**
028     * A deployment rule parametrizes the deployment scheme of a set
029     * of objects identified by a regular expression.
030     *
031     * <p>Each object that is named by the naming aspect so that it
032     * matches the regular expression will be deployed regarding the
033     * deployment rule.<p>
034     *
035     * The deployment aspect component uses a set of deployment rules
036     * to know how to handle a newly used object.<p>
037     *
038     * @see DeploymentAC
039     * @see Deployment
040     * @see Topology
041     *
042     * @author <a href="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
043     */
044    
045    public class DeploymentRule implements Serializable {
046    
047       /** Stores the readable type of the rule. */
048       String type = "";
049    
050       /** Stores the regular expression that represents the objects
051           to be affected by this distribution rule. */
052       RE nameRegexp;
053    
054       /** Stores the regular expression that identifies the remote
055           containers where the deployment rule will be applied. */
056       RE contRegexp;
057    
058       boolean state=false;
059    
060       /** Stores the knowledge style of the replication group. */
061       int knowledgeStyle;
062    
063       /** Stores the knowledge graph if needed. */
064       String[] knowledgeGraph;
065    
066       /** The objects that have allready been treated. */
067       transient Hashtable treated = new Hashtable();
068    
069       AspectComponent ac;
070    
071       /**
072        * Creates a new deployment rule in the general case.
073        *
074        * @param type a string that contains a readable representation of
075        * what the rule is doing 
076        * @param nameRegexp a regular expression that filters the objects
077        * to which this rule will be applied 
078        * @param contRegexp a regular expression that defines a set of
079        * remote container where the objects of the rule will be
080        * deployed 
081        * @param state if true, the states of the objects are replicated,
082        * else, it only deploys empty objects */
083    
084       public DeploymentRule (AspectComponent ac,
085                              String type,
086                              String nameRegexp,
087                              String contRegexp,
088                              boolean state ) {
089          this.ac = ac;
090          this.type = type;
091          try {
092             this.nameRegexp = new RE(nameRegexp);
093             this.contRegexp = new RE(contRegexp);
094          } catch (REException e) {
095             System.out.println("Regexp construction failed : "+e);
096          }
097          this.state = state;
098       }                           
099    
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 candidate ) {
108          String 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 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 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 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