buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/DiamondsConfig.java
changeset 628 7c4a911dc066
parent 587 85df38eb4012
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
     1 /*
     1 /*
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5  * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:  
    14  * Description:  
    15 *
    15  *
    16 */
    16  */
    17 
       
    18 
       
    19 
    17 
    20 package com.nokia.helium.diamonds;
    18 package com.nokia.helium.diamonds;
    21 
    19 
    22 import org.apache.tools.ant.BuildException;
    20 import java.util.HashMap;
    23 
    21 import java.util.Map;
    24 import java.util.*;
    22 import com.nokia.helium.core.ant.types.Stage;
    25 import org.dom4j.io.SAXReader;
    23 import com.nokia.helium.core.ant.types.TargetMessageTrigger;
    26 import org.dom4j.Document;
    24 import org.apache.tools.ant.Project;
    27 import org.dom4j.Element;
    25 import java.util.Hashtable;
    28 import org.dom4j.Node;
       
    29 import org.dom4j.DocumentException;
       
    30 import org.apache.log4j.Logger;
    26 import org.apache.log4j.Logger;
    31 
    27 
    32 /**
    28 /**
    33  * Loads the configuration information from the xml file.
    29  * Loads the configuration information from the xml file.
    34  * 
    30  * 
    35  */
    31  */
    36 public final class DiamondsConfig {
    32 public final class DiamondsConfig {
    37     private static DiamondsProperties diamondsProperties;
       
    38 
    33 
    39     private static List<Stage> stages;
    34     private static HashMap<String, Stage> stages = new HashMap<String, Stage>();
    40 
    35 
    41     private static Logger log;
    36     private static Logger log = Logger.getLogger(DiamondsConfig.class);
    42 
    37 
    43     private static Map<String, Target> targets;
    38     private static String initialiserTargetName;
       
    39     
       
    40     private static Project project;
       
    41     
       
    42     private static final String DIAMONDS_HOST_PROPERTY = "diamonds.host";
       
    43     private static final String DIAMONDS_PORT_PROPERTY = "diamonds.port";
       
    44     private static final String DIAMONDS_PATH_PROPERTY = "diamonds.path";
       
    45     private static final String DIAMONDS_TSTAMP_PROPERTY = "diamonds.tstamp.format";
       
    46     private static final String DIAMONDS_MAIL_PROPERTY = "diamonds.mail";
       
    47     private static final String DIAMONDS_LDAP_PROPERTY = "diamonds.ldap.server";
       
    48     private static final String DIAMONDS_SMTP_PROPERTY = "diamonds.smtp.server";
       
    49     private static final String DIAMONDS_INITIALIZER_TARGET_PROPERTY = "diamonds.initializer.targetname";
       
    50     private static final String DIAMONDS_CATEGORY_PROPERTY = "diamonds.category";
       
    51     
       
    52     
       
    53     private static final String[] PROPERTY_NAMES = {DIAMONDS_HOST_PROPERTY, DIAMONDS_PORT_PROPERTY, DIAMONDS_PATH_PROPERTY,
       
    54         DIAMONDS_TSTAMP_PROPERTY, DIAMONDS_MAIL_PROPERTY,
       
    55         DIAMONDS_LDAP_PROPERTY, DIAMONDS_SMTP_PROPERTY,
       
    56         DIAMONDS_INITIALIZER_TARGET_PROPERTY, DIAMONDS_CATEGORY_PROPERTY};
    44 
    57 
    45     private static String outputDir;
    58     private static HashMap<String, TargetMessageTrigger> targetMessageList = new HashMap<String, TargetMessageTrigger>();
    46 
       
    47     private static String templateDir;
       
    48     
       
    49     private static String initialiserTargetName;
       
    50 
    59 
    51     private DiamondsConfig() {
    60     private DiamondsConfig() {
    52     }
    61     }
       
    62 
       
    63 
       
    64     @SuppressWarnings("unchecked")
       
    65     private static void initializeMessage(Project prj) {
       
    66         Hashtable<String, Object> references = prj.getReferences();
       
    67         for (String key : references.keySet()) {
       
    68             Object object = references.get(key);
       
    69             log.debug("key: " + key);
       
    70             if (object instanceof TargetMessageTrigger) {
       
    71                 log.debug("found message map:" + object);
       
    72                 log.debug("found key: " + key);
       
    73                 TargetMessageTrigger message = (TargetMessageTrigger)object;
       
    74                 targetMessageList.put(message.getTargetName(), (TargetMessageTrigger)object);
       
    75             }
       
    76         }
       
    77     }
    53     
    78     
    54     /**
    79     @SuppressWarnings("unchecked")
    55      * Method accessed by loggers to load the diamonds specific configuration.
    80     public static void initialize(Project prj) throws DiamondsException {
    56      * 
    81         project = prj;
    57      * @param configFile
    82         log.debug("Diamonds config initialization: project: " + project);
    58      *            - configuration to load
    83         initializeMessage(prj);
    59      * 
    84         for (String property : PROPERTY_NAMES ) {
    60      */
    85             validateProperty(property);
    61     public static void parseConfiguration(String configFile)
       
    62             throws DiamondsException {
       
    63         if (log == null) {
       
    64             log = Logger.getLogger(DiamondsConfig.class);
       
    65         }
    86         }
    66         SAXReader saxReader = new SAXReader();
    87         Hashtable<String, Object> references = prj.getReferences();
    67         Document document = null;
    88         for (String key : references.keySet()) {
    68         try {
    89             Object object = references.get(key); 
    69             log.debug("Reading diamonds configuration.");
    90             if (object instanceof Stage) {
    70             document = saxReader.read(configFile);
    91                 log.debug("stage found: " + key);
    71         } catch (DocumentException e) {
    92                 Stage stageMap = (Stage)object;
    72             // No need to fail the build due to internal Helium configuration errors.
    93                 stageMap.setStageName(key);
    73             log.debug("Diamonds configuration parsing error: "
    94                 stages.put(key, (Stage)object);
    74                     + e.getMessage());
    95             }
    75         }
    96         }
    76         parseConfig(document);
       
    77         diamondsProperties = parseDiamondsProperties(document);
       
    78         stages = parseStages(document);
       
    79         targets = parseTargets(document);
       
    80     }
    97     }
    81 
    98 
    82     /**
    99     private static void validateProperty(String propertyName) throws DiamondsException {
    83      * Parses the general configuration info.
   100         String propertyValue = project.getProperty(propertyName);
    84      * 
   101         if (propertyValue == null) {
    85      * @param document
   102             throw new DiamondsException("required property: " + propertyName + " not defined");
    86      *            - XML config in DOM4J document
       
    87      */
       
    88     private static void parseConfig(Document document) {
       
    89         log.debug("diamonds:DiamondsConfig:parsing general configuration.");
       
    90         Node node = document.selectSingleNode("//output-dir");
       
    91         outputDir = node.valueOf("@path");
       
    92         node = document.selectSingleNode("//template-dir");
       
    93         templateDir = node.valueOf("@path");
       
    94     }
       
    95 
       
    96     /**
       
    97      * Parses the server info.
       
    98      * 
       
    99      * @param document
       
   100      *            - XML config in DOM4J document
       
   101      */
       
   102     private static DiamondsProperties parseDiamondsProperties(Document document) {
       
   103         log.debug("diamonds:DiamondsConfig:parsing diamonds properties.");
       
   104         
       
   105         Map<String, String> propertiesMap = new HashMap<String, String>();
       
   106         
       
   107         loadProperty(document, propertiesMap, "host");
       
   108         loadProperty(document, propertiesMap, "port");
       
   109         loadProperty(document, propertiesMap, "path");
       
   110         loadProperty(document, propertiesMap, "tstampformat");
       
   111         loadProperty(document, propertiesMap, "mail");
       
   112         loadProperty(document, propertiesMap, "ldapserver");
       
   113         loadProperty(document, propertiesMap, "smtpserver");
       
   114         loadProperty(document, propertiesMap, "initialiser-target-name");
       
   115         loadProperty(document, propertiesMap, "category-property");
       
   116         loadProperty(document, propertiesMap, "buildid-property");
       
   117         return new DiamondsProperties(propertiesMap);
       
   118     }
       
   119 
       
   120     /**
       
   121      * Parses the Targets data from config.
       
   122      * 
       
   123      * @param document
       
   124      *            - XML config in DOM4J document
       
   125      * @return list of targets available in the config
       
   126      */
       
   127     @SuppressWarnings("unchecked")
       
   128     private static Map<String, Target> parseTargets(Document document) {
       
   129         log.debug("diamonds:DiamondsConfig:parsing for targets");
       
   130         Map<String, Target> targets = new HashMap<String, Target>();
       
   131         List<Element> stageNodes = document.selectNodes("//target");
       
   132        
       
   133         // Set initialiserTargetName according to target name defined Diamonds config file
       
   134         initialiserTargetName = diamondsProperties.getProperty("initialiser-target-name");
       
   135         targets.put(initialiserTargetName, new Target(initialiserTargetName,"","","",""));
       
   136         for (Element stageNode : stageNodes) {
       
   137             targets.put(stageNode.valueOf("@name"), new Target(stageNode
       
   138                     .valueOf("@name"), stageNode.valueOf("@template-file"),
       
   139                     stageNode.valueOf("@logfile"), stageNode
       
   140                             .valueOf("@ant-properties"), stageNode
       
   141                             .valueOf("@defer")));
       
   142         }
   103         }
   143         return targets;
       
   144     }
       
   145 
       
   146     /**
       
   147      * Parses the stages info.
       
   148      * 
       
   149      * @param document
       
   150      *            - XML config in DOM4J document
       
   151      * @return list of stages in config
       
   152      */
       
   153     @SuppressWarnings("unchecked")
       
   154     private static List<Stage> parseStages(Document document) {
       
   155         List<Stage> stages = new ArrayList<Stage>();
       
   156         List<Element> stageNodes = document.selectNodes("//stage");
       
   157         log.debug("diamonds:DiamondsConfig:parsing for stages");
       
   158         for (Element stage : stageNodes) {
       
   159             stages.add(new Stage(stage.valueOf("@name"), stage
       
   160                     .valueOf("@start"), stage.valueOf("@end"), stage
       
   161                     .valueOf("@logfile")));
       
   162         }
       
   163         return stages;
       
   164     }
   104     }
   165 
   105 
   166     /**
   106     /**
   167      * Helper function to get the stages
   107      * Helper function to get the stages
   168      * 
   108      * 
   169      * @return the stages from config in memory
   109      * @return the stages from config in memory
   170      */
   110      */
   171     static List<Stage> getStages() {
   111     static Map<String, Stage> getStages() {
   172         return stages;
   112         return stages;
   173     }
   113     }
   174 
   114 
   175     /**
   115     /**
   176      * Helper function to get the targets
   116      * Helper function to get the targets
   177      * 
   117      * 
   178      * @return the targets from config in memory
   118      * @return the targets from config in memory
   179      */
   119      */
   180     static Map<String, Target> getTargets() {
   120     static HashMap<String, TargetMessageTrigger> getTargetsMap() {
   181         return targets;
   121         return targetMessageList;
   182     }
   122     }
   183 
   123 
   184     /**
   124     /**
   185      * Returns true if stages exists in config
   125      * Returns true if stages exists in config
   186      * 
   126      * 
   187      * @return the existance of stages in config
   127      * @return the existance of stages in config
   188      */
   128      */
   189     public static boolean isStagesInConfig() {
   129     public static boolean isStagesInConfig() {
   190         return stages != null;
   130         return !stages.isEmpty();
   191     }
   131     }
   192 
   132 
   193     /**
   133     /**
   194      * Returns true if targets exists in config
   134      * Returns true if targets exists in config
   195      * 
   135      * 
   196      * @return the targets from config in memory
   136      * @return the targets from config in memory
   197      */
   137      */
   198     public static boolean isTargetsInConfig() {
   138     public static boolean isTargetsInConfig() {
   199         return targets != null;
   139         return !targetMessageList.isEmpty();
       
   140     }
       
   141 
       
   142     public static String getHost() {
       
   143         return project.getProperty(DIAMONDS_HOST_PROPERTY);
       
   144     }
       
   145 
       
   146     public static String getPort() {
       
   147         return project.getProperty(DIAMONDS_PORT_PROPERTY);
       
   148     }
       
   149 
       
   150     public static String getPath() {
       
   151         return project.getProperty(DIAMONDS_PATH_PROPERTY);
       
   152     }
       
   153 
       
   154     public static String getTimeFormat() {
       
   155         return project.getProperty(DIAMONDS_TSTAMP_PROPERTY);
       
   156     }
       
   157 
       
   158     public static String getMailInfo() {
       
   159         return project.getProperty(DIAMONDS_MAIL_PROPERTY);
       
   160     }
       
   161 
       
   162     public static String getLDAPServer() {
       
   163         return project.getProperty(DIAMONDS_LDAP_PROPERTY);
       
   164     }
       
   165 
       
   166     public static String getSMTPServer() {
       
   167         return project.getProperty(DIAMONDS_SMTP_PROPERTY);
       
   168     }
       
   169 
       
   170     public static String getBuildIdProperty() {
       
   171         return "diamonds.build.id";
       
   172     }
       
   173 
       
   174     public static String getBuildId() {
       
   175         return project.getProperty(getBuildIdProperty());
   200     }
   176     }
   201 
   177 
   202     /**
   178     /**
   203      * Gets the diamonds properties loaded from config
   179      * Gets the initialiserTargetName
   204      * 
   180      * 
   205      * @return the targets from config in memory
   181      * @return the initialiserTargetName
   206      */
   182      */
   207     public static DiamondsProperties getDiamondsProperties() {
   183     public static String getInitializerTargetProperty() {
   208         return diamondsProperties;
   184         return DIAMONDS_INITIALIZER_TARGET_PROPERTY;
   209     }
   185     }
   210 
   186 
   211     /**
   187     public static String getCategory() {
   212      * Gets the output directory
   188         return project.getProperty(DIAMONDS_CATEGORY_PROPERTY);
   213      * 
       
   214      * @return the output directory, loaded from config
       
   215      */
       
   216     static String getOutputDir() {
       
   217         return outputDir;
       
   218     }
       
   219 
       
   220     /**
       
   221      * Gets the output directory
       
   222      * 
       
   223      * @return the output directory, loaded from config
       
   224      */
       
   225     static String getTemplateDir() {
       
   226         return templateDir;
       
   227     }
       
   228     
       
   229     /**
       
   230      * Gets the initialiserTargetName
       
   231      * 
       
   232      * @return the initialiserTargetName, loaded from config
       
   233      */
       
   234     public static String getInitialiserTargetName() {
       
   235         return initialiserTargetName;
       
   236     }
       
   237     
       
   238     /**
       
   239      * Load diamonds property into hashmap.
       
   240      * @param doc
       
   241      * @param hash
       
   242      * @param name
       
   243      * @return
       
   244      */
       
   245     public static void loadProperty(Document document, Map<String, String> hash, String name) {
       
   246         Node node = document.selectSingleNode("//property[@name='" + name + "']");
       
   247         if (node == null) {
       
   248             throw new BuildException("diamonds: DiamondsConfig:'" + name + "' property definition is missing.");
       
   249         }
       
   250         hash.put(name, node.valueOf("@value"));
       
   251     }
   189     }
   252 
   190 
   253 }
   191 }