001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-06-14 16:42:05 +0000 (Thu, 14 Jun 2012) $' 
007 * '$Revision: 29943 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.configuration;
031
032import java.io.File;
033import java.io.FileWriter;
034
035import org.kepler.build.modules.Module;
036import org.kepler.util.DotKeplerManager;
037
038/**
039 * A class that implements the ConfigurationWriter interface for the commons
040 * framework.
041 */
042public class GeneralConfigurationWriter implements ConfigurationWriter
043{      
044  /**
045   * constructor
046   */
047  public GeneralConfigurationWriter()
048  {
049     
050  }
051  
052  /**
053   * write (serialize) a given configuration property
054   */
055  public void writeConfiguration(RootConfigurationProperty property)
056    throws ConfigurationManagerException
057  {
058    if(!property.isDirty())
059    { //there is nothing to write here since nothing changed
060      return;
061    }
062    
063    String filename = property.getFile().getName();
064    Module m = property.getModule();
065    
066    //first check to see if the configurations directory exists in .kepler
067    
068    File modConfDir = DotKeplerManager.getInstance().getModuleConfigurationDirectory(m.getName());
069    ///File modConfDir = DotKeplerManager.getInstance().getModuleConfigurationDirectory(m.getStemName());
070    if(!modConfDir.exists())
071    { //create the dir
072      if(!modConfDir.mkdirs())
073      {
074          throw new ConfigurationManagerException("Unable to create directory " + modConfDir);
075      }
076    }
077    
078    //now we can save the property to .kepler/configurations
079    try
080    {
081      File configFile = new File(modConfDir, filename);
082      //get the first child of the root because that's actually the config
083      //info we want.
084      ConfigurationProperty cp = property.getProperty(0);
085      String xml = cp.getXML();
086      //System.out.println("xml: " + xml);
087      FileWriter fw = new FileWriter(configFile);
088      fw.write(xml, 0, xml.length());
089      fw.flush();
090      fw.close();
091    }
092    catch(Exception e)
093    {
094      throw new ConfigurationManagerException("Error serializing configuration " + 
095        filename + " to directory " + modConfDir.getAbsolutePath() +
096        " : " + e.getMessage());
097    }
098  }
099}