org/objectweb/proactive/core/xml/VariableContract.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.xml;
00032 
00033 import java.io.FileInputStream;
00034 import java.io.Serializable;
00035 import java.util.HashMap;
00036 import java.util.Iterator;
00037 import java.util.Properties;
00038 import java.util.regex.Matcher;
00039 import java.util.regex.Pattern;
00040 
00041 import org.apache.log4j.Logger;
00042 import org.objectweb.proactive.core.descriptor.xml.VariablesHandler;
00043 import org.objectweb.proactive.core.util.log.Loggers;
00044 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00045 import org.xml.sax.SAXException;
00046 
00047 
00053 public class VariableContract implements Serializable {
00054     static Logger logger = ProActiveLogger.getLogger(Loggers.DEPLOYMENT);
00055     public static VariableContract xmlproperties = null;
00056     public static final Lock lock = new Lock();
00057     private boolean closed;
00058     
00059     private static final Pattern variablePattern = Pattern.compile("(\\$\\{(.*?)\\})");
00060     private static final Pattern legalPattern = Pattern.compile("^\\$\\{[\\w\\.]+\\}$");
00061     
00062     private class PropertiesDatas {
00063         public String value;
00064         public VariableContractType type;
00065         public String setFrom; //Descriptor, Program
00066 
00067         public String toString() {
00068             StringBuilder sb = new StringBuilder();
00069 
00070             sb.append(value).append(" type=").append(type).append(" setFrom=")
00071               .append(setFrom);
00072             return sb.toString();
00073         }
00074     }
00075 
00076     private HashMap list;
00077 
00082     public VariableContract() {
00083         list = new HashMap();
00084         closed = false;
00085     }
00086 
00090     public void close() {
00091                 closed = true;
00092         }
00093 
00099     public boolean isClosed() {
00100         return closed;
00101     }
00102 
00115     public void setVariableFromProgram(String name, String value,
00116         VariableContractType type) {
00117         setVariableFrom(name, value, type, "Program");
00118         setFromJavaProperty(name, type);
00119     }
00120 
00125     public void setJavaPropertiesValues(){
00126         //before closing we set the JavaProperties values
00127                 java.util.Iterator it = list.keySet().iterator();
00128                 while (it.hasNext()) {
00129                         String name = (String) it.next();
00130                         PropertiesDatas data = (PropertiesDatas) list.get(name);
00131                         setFromJavaProperty(name, data.type);
00132                 }// while
00133     }
00134     
00141     public void setFromJavaProperty(String name, VariableContractType type){
00142         
00143         if(!type.hasSetAbility("JavaProperty")) return;
00144         
00145         try {
00146                         String value = System.getProperty(name);
00147                         if(logger.isDebugEnabled()){
00148                                 logger.debug("Found java property "+name+"="+value);
00149                         }
00150                         if (value == null) value="";
00151                         setVariableFrom(name, value, type, "JavaProperty");
00152                 } catch (Exception ex) {
00153                         if (logger.isDebugEnabled())
00154                                 logger.debug("Unable to get java property: " + name);
00155                 }
00156     }
00157     
00167     private void setVariableFrom(String name, String value,
00168         VariableContractType type, String from) {
00169         if (logger.isDebugEnabled()) {
00170             logger.debug("Setting from " + from + ": " + type + " " + name +
00171                 "=" + value);
00172         }
00173 
00174         if (closed) {
00175             throw new IllegalArgumentException(
00176                 "Variable Contract is Closed. Variables can no longer be set");
00177         }
00178 
00179         checkGenericLogic(name, value, type);
00180 
00181         if ((value.length() > 0) && !type.hasSetAbility(from)) {
00182             throw new IllegalArgumentException("Variable " + name +
00183                 " can not be set from " + from + " for type: " + type);
00184         }
00185 
00186         if ((value.length() <= 0) && !type.hasSetEmptyAbility(from)) {
00187             throw new IllegalArgumentException("Variable " + name +
00188                 " can not be set empty from " + from + " for type: " + type);
00189         }
00190 
00191         if (list.containsKey(name)) {
00192             PropertiesDatas var = (PropertiesDatas) list.get(name);
00193 
00194             if (!type.hasPriority(var.setFrom, from)) {
00195                 if (logger.isDebugEnabled()) {
00196                     logger.debug("Skipping, lower priority (" + from + " < " +
00197                         var.setFrom + ") for type: " + type);
00198                 }
00199                 return;
00200             }
00201         }
00202 
00203         unsafeAdd(name, value, type, from);
00204     }
00205 
00212     public void setVariableFromProgram(HashMap map, VariableContractType type)
00213         throws NullPointerException {
00214         if ((map == null) || (type == null)) {
00215             throw new NullPointerException("Null arguments");
00216         }
00217 
00218         String name;
00219         java.util.Iterator it = map.keySet().iterator();
00220         while (it.hasNext()) {
00221             name = (String) it.next();
00222             setVariableFromProgram(name, (String) map.get(name), type);
00223         }
00224     }
00225 
00233     public void setDescriptorVariable(String name, String value,
00234         VariableContractType type) {
00235         
00236         setVariableFrom(name, value, type, "Descriptor");
00237         setFromJavaProperty(name, type);
00238     }
00239     
00245     public void load(String file) throws org.xml.sax.SAXException {
00246         Properties properties = new Properties();
00247         if (logger.isDebugEnabled()) {
00248             logger.debug("Loading propeties file:" + file);
00249         }
00250 
00251         // Open the file
00252         try {
00253             FileInputStream stream = new FileInputStream(file);
00254             properties.load(stream);
00255         } catch (Exception ex) {
00256             if (logger.isDebugEnabled()) {
00257                 logger.debug("Curret Working Directory: " +
00258                     System.getProperty("user.dir"));
00259             }
00260 
00261             throw new org.xml.sax.SAXException(
00262                 "Tag property cannot open file : [" + file + "]");
00263         }
00264 
00265         String name;
00266         String value;
00267         Iterator it = properties.keySet().iterator();
00268         while (it.hasNext()) {
00269             name = (String) it.next();
00270             value = properties.getProperty(name);
00271             setDescriptorVariable(name, value,
00272                 VariableContractType.DescriptorVariable);
00273         }
00274     }
00275 
00280     public void loadXML(String file) {
00281         if (logger.isDebugEnabled()) {
00282             logger.debug("Loading XML variable file:" + file);
00283         }
00284         VariablesHandler.createVariablesHandler(file, this);
00285     }
00286 
00292     public String getValue(String name) {
00293         if (list.containsKey(name)) {
00294             PropertiesDatas var = (PropertiesDatas) list.get(name);
00295 
00296             return var.value;
00297         }
00298 
00299         return null;
00300     }
00301 
00308     public String transform(String text) throws SAXException {
00309         if(text==null) return null;
00310         
00311         Matcher m=variablePattern.matcher(text);
00312         StringBuffer sb=new StringBuffer();
00313         while(m.find()){
00314 
00315                 if(!isLegalName(m.group(1)))
00316                         throw new SAXException("Error, malformed variable:"+m.group(1));
00317                 
00318                 String name=m.group(2);
00319                 String value=getValue(name);
00320 
00321                 if(value==null || value.length()<=0)
00322                         throw new SAXException("Error, variable value not found: "+name+"=?");
00323                         
00324                 if(logger.isDebugEnabled()){
00325                         logger.debug("Matched:"+name+" = "+value);
00326                         //logger.debug(m);
00327                 }
00328                 m.appendReplacement(sb, value);
00329         }
00330         m.appendTail(sb);
00331         
00332         return sb.toString();
00333     }
00334 
00341     private void checkGenericLogic(String name, String value,
00342         VariableContractType type) {
00343 
00344         /*
00345          * Generic Logical checks
00346          */
00347         if (name == null) {
00348             throw new NullPointerException("Variable Name is null.");
00349         }
00350 
00351         if (name.length() <= 0) {
00352             throw new IllegalArgumentException("Variable Name is empty.");
00353         }
00354 
00355         if (!isLegalName("${"+name+"}")) {
00356             throw new IllegalArgumentException("Illegal variable name:"+name);
00357         }
00358 
00359         
00360         if (value == null) {
00361             throw new NullPointerException("Variable Value is null.");
00362         }
00363 
00364         if (type == null) {
00365             throw new NullPointerException("Variable Type is null.");
00366         }
00367         
00368         if (list.containsKey(name) &&
00369                 !((PropertiesDatas) list.get(name)).type.equals(type)) {
00370             throw new IllegalArgumentException("Variable " + name +
00371                 " is already defined with type: " +
00372                 ((PropertiesDatas) list.get(name)).type);
00373         }
00374 
00375     }
00376 
00385     private void unsafeAdd(String name, String value,
00386         VariableContractType type, String setFrom)
00387         throws NullPointerException, IllegalArgumentException {
00388         if (name == null) {
00389             throw new NullPointerException("XML Variable Name is null.");
00390         }
00391         if (name.length() <= 0) {
00392             throw new IllegalArgumentException("XML Variable Name is empty.");
00393         }
00394         if (value == null) {
00395             throw new NullPointerException("XML Variable Value is null.");
00396         }
00397 
00398         PropertiesDatas data;
00399         if (list.containsKey(name)) {
00400             data = (PropertiesDatas) list.get(name);
00401             if (logger.isDebugEnabled()) {
00402                 logger.debug("...Modifying variable registry: "+name+"="+value);
00403             }
00404         } else {
00405             data = new PropertiesDatas();
00406             if (logger.isDebugEnabled()) {
00407                 logger.debug("...Creating new registry for variable: "+name+"="+value);
00408             }
00409         }
00410 
00411         data.type = type;
00412         data.value = value;
00413         data.setFrom = setFrom;
00414         list.put(name, data);
00415     }
00416 
00422     public boolean checkContract() {
00423         boolean retval = true;
00424         String name;
00425         java.util.Iterator it = list.keySet().iterator();
00426         while (it.hasNext()) {
00427             name = (String) it.next();
00428             PropertiesDatas data = (PropertiesDatas) list.get(name);
00429 
00430             if(data.value.length()<=0){
00431                 logger.error(data.type.getEmptyErrorMessage(name));
00432                 retval=false;
00433             }
00434         }
00435 
00436         return retval;
00437     }
00438 
00439     public String toString() {
00440         StringBuilder sb = new StringBuilder();
00441 
00442         PropertiesDatas var;
00443         String name;
00444         java.util.Iterator it = list.keySet().iterator();
00445         while (it.hasNext()) {
00446             name = (String) it.next();
00447             var = (PropertiesDatas) list.get(name);
00448 
00449             sb.append(name).append("=").append(var).append("\n");
00450         }
00451 
00452         return sb.toString();
00453     }
00454     
00460         public boolean isLegalName(String var){
00461                 Matcher  m = legalPattern.matcher(var);
00462                 return m.matches();
00463         }
00464     
00465 /*
00466     public void setDescriptorVariableOLD(String name, String value,
00467         VariableContractType type) {
00468         if (logger.isDebugEnabled()) {
00469             logger.debug("Setting from descriptor: " + type + " " + name + "=" +
00470                 value + ".");
00471         }
00472 
00473         checkGenericLogic(name, value, type);
00474 
00475         /* DescriptorVariable
00476          *  -Priority: XML
00477          *  -Set Ability: XML
00478          *  -Default: XML
00479      
00480         if (type.equals(VariableContractType.DescriptorVariable) &&
00481                 (value.length() <= 0)) {
00482             throw new IllegalArgumentException("Variable " + name +
00483                 " value must be specified for type: " + type);
00484         }
00485 
00486         /* ProgramVariable
00487          *  -Priority: Program
00488          *  -Set Ability: Program
00489          *  -Default: Program
00490        
00491         if (type.equals(VariableContractType.ProgramVariable)) {
00492             if (value.length() > 0) {
00493                 throw new IllegalArgumentException("Variable " + name +
00494                     " can not be set from descriptor for type: " + type);
00495             }
00496 
00497             if (list.containsKey(name)) {
00498                 if (logger.isDebugEnabled()) {
00499                     logger.debug("Skipping " + type + " " + name + "=" + value +
00500                         ". Program already set variable with value.");
00501                 }
00502                 return;
00503             }
00504         }
00505 
00506         /* DescriptorDefaultVariable
00507          *  -Priority: Program
00508          *  -Set Ability: Program, XML
00509          *  -Default: XML
00510       
00511         if (type.equals(VariableContractType.DescriptorDefaultVariable)) {
00512             if (value.length() < 0) {
00513                 throw new IllegalArgumentException("Variable " + name +
00514                     " value must be specified for type: " + type);
00515             }
00516 
00517             //Priority is lost if Program set the variable
00518             if (list.containsKey(name)) {
00519                 PropertiesDatas var = (PropertiesDatas) list.get(name);
00520 
00521                 //skipe if program already set a value
00522                 if (var.setFrom.equals("Program") && (var.value.length() > 0)) {
00523                     if (logger.isDebugEnabled()) {
00524                         logger.debug("Skipping variable " + name + " type: " +
00525                             type +
00526                             ". Program already set variable with higher priority");
00527                     }
00528                     return;
00529                 }
00530             }
00531         }
00532 
00533         /* ProgramDefaultVariable
00534          *  -Priority: XML
00535          *  -Set Ability: Program, XML
00536          *  -Default: Program
00537      
00538         if (type.equals(VariableContractType.ProgramDefaultVariable) &&
00539                 (value.length() <= 0)) {
00540             if (logger.isDebugEnabled()) {
00541                 logger.debug("Variable " + name +
00542                     ", not setting to empty value");
00543             }
00544             return;
00545         }
00546 
00547         /* JavaPropertyVariable
00548          *  -Priority: JavaProperty
00549          *  -Set Ability: JavaProperty
00550          *  -Default: JavaProperty
00551 
00552         if (type.equals(VariableContractType.JavaPropertyVariable)) {
00553             String prop_value = null;
00554 
00555             if (value.length() <= 0) {
00556                 throw new IllegalArgumentException("Variable " + name +
00557                     " value can not be empty for type " + type);
00558             }
00559 
00560             try {
00561                 prop_value = System.getProperty(value);
00562                 if (prop_value == null) {
00563                     throw new Exception();
00564                 }
00565             } catch (Exception ex) {
00566                 throw new IllegalArgumentException(
00567                     "Unable to get System Property: " + value);
00568             }
00569 
00570             value = prop_value;
00571         }
00572 
00573         //defineVariable(type, name);
00574         unsafeAdd(name, value, type, "Descriptor");
00575     }
00576     */
00577 /*
00578     public void setVariableFromProgramOLD(String name, String value,
00579         VariableContractType type)
00580         throws NullPointerException, IllegalArgumentException {
00581         if (logger.isDebugEnabled()) {
00582             logger.debug("Setting from program:    " + type + " " + name + "=" +
00583                 value);
00584         }
00585 
00586         checkGenericLogic(name, value, type);
00587 
00588         // DescriptorVariable        *  -Priority: XML         *  -Set Ability: XML        *  -Default: XML         
00589         if (type.equals(VariableContractType.DescriptorVariable)) {
00590             if (value.length() > 0) {
00591                 throw new IllegalArgumentException("Variable " + name +
00592                     " can not be set from program for type: " + type);
00593             }
00594 
00595             if (list.containsKey(name)) {
00596                 if (logger.isDebugEnabled()) {
00597                     logger.debug("Skipping " + type + " " + name + "=" + value +
00598                         ". Descriptor already set variable with value.");
00599                 }
00600                 return;
00601             }
00602         }
00603 
00604         // ProgramVariable         *  -Priority: Program         *  -Set Ability: Program         *  -Default: Program         
00605         if (type.equals(VariableContractType.ProgramVariable) &&
00606                 (value.length() <= 0)) {
00607             throw new IllegalArgumentException("Variable " + name +
00608                 " value must be specified for type: " + type);
00609         }
00610 
00611         // DescriptorDefaultVariable         *  -Priority: Program         *  -Set Ability: Program, XML         *  -Default: XML         
00612         if (type.equals(VariableContractType.DescriptorDefaultVariable) &&
00613                 (value.length() <= 0)) {
00614             if (logger.isDebugEnabled()) {
00615                 logger.debug("Variable " + name +
00616                     ", not setting to empty value");
00617             }
00618             return;
00619         }
00620 
00621         // ProgramDefaultVariable          *  -Priority: XML         *  -Set Ability: Program, XML         *  -Default: Program         
00622         if (type.equals(VariableContractType.ProgramDefaultVariable)) {
00623             if (value.length() < 0) {
00624                 throw new IllegalArgumentException("Variable " + name +
00625                     " value must be specified for type: " + type);
00626             }
00627 
00628             if (list.containsKey(name)) {
00629                 PropertiesDatas var = (PropertiesDatas) list.get(name);
00630 
00631                 //skipe if descriptor already set a value
00632                 if (var.setFrom.equals("Descriptor") &&
00633                         (var.value.length() > 0)) {
00634                     if (logger.isDebugEnabled()) {
00635                         logger.debug("Skipping variable " + name + " type: " +
00636                             type +
00637                             ". Descriptor already set variable with higher priority");
00638                     }
00639                     return;
00640                 }
00641             }
00642         }
00643 
00644         // JavaPropertyVariable         *  -Priority: JavaProperty         *  -Set Ability: JavaProperty         *  -Default: JavaProperty         
00645         if (type.equals(VariableContractType.JavaPropertyVariable)) {
00646             String prop_value = null;
00647 
00648             if (value.length() <= 0) {
00649                 throw new IllegalArgumentException("Variable " + name +
00650                     " value can not be empty for type " + type);
00651             }
00652 
00653             try {
00654                 prop_value = System.getProperty(value);
00655                 if (prop_value == null) {
00656                     throw new Exception();
00657                 }
00658             } catch (Exception ex) {
00659                 throw new IllegalArgumentException(
00660                     "Unable to get System Property: " + value);
00661             }
00662 
00663             value = prop_value;
00664         }
00665 
00666         //defineVariable(type, name);
00667         unsafeAdd(name, value, type, "Program");
00668     }
00669 */
00676     static public class Lock {
00677         private boolean locked;
00678 
00679         private Lock() {
00680             locked = false;
00681         }
00682 
00687         public synchronized void release() {
00688             locked = false;
00689             notify();
00690         }
00691 
00695         public synchronized void aquire() {
00696             while (locked) {
00697                 try {
00698                     wait();
00699                 } catch (InterruptedException e) {
00700                 }
00701             }
00702 
00703             locked = true;
00704         }
00705     }
00706 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1