geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r350073 - in /geronimo/trunk/modules/system/src: java/org/apache/geronimo/system/configuration/ test/org/apache/geronimo/system/configuration/
Date Wed, 30 Nov 2005 23:25:14 GMT
Author: dain
Date: Wed Nov 30 15:25:09 2005
New Revision: 350073

URL: http://svn.apache.org/viewcvs?rev=350073&view=rev
Log:
Split out LocalAttributeManager xml marshaling code, so it can be easily tested and expanded.

Added:
    geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ConfigurationOverride.java
  (with props)
    geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/GBeanOverride.java
  (with props)
    geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ServerOverride.java
  (with props)
    geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/configuration/ServerOverrideTest.java
  (with props)
Modified:
    geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java

Added: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ConfigurationOverride.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ConfigurationOverride.java?rev=350073&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ConfigurationOverride.java
(added)
+++ geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ConfigurationOverride.java
Wed Nov 30 15:25:09 2005
@@ -0,0 +1,109 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.configuration;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+class ConfigurationOverride {
+    private final String name;
+    private boolean load;
+    private final Map gbeans = new LinkedHashMap();
+
+    public ConfigurationOverride(String name, boolean load) {
+        this.name = name;
+        this.load = load;
+    }
+
+    public ConfigurationOverride(Element element) throws MalformedObjectNameException {
+        name = element.getAttribute("name");
+
+        String loadConfigString = element.getAttribute("load");
+        load = !"false".equals(loadConfigString);
+
+        NodeList gbeans = element.getElementsByTagName("gbean");
+        for (int g = 0; g < gbeans.getLength(); g++) {
+            Element gbeanElement = (Element) gbeans.item(g);
+            GBeanOverride gbean = new GBeanOverride(gbeanElement);
+            addGBean(gbean);
+        }
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public boolean isLoad() {
+        return load;
+    }
+
+    public void setLoad(boolean load) {
+        this.load = load;
+    }
+
+    public GBeanOverride getGBean(String gbeanName) {
+        return (GBeanOverride) gbeans.get(gbeanName);
+    }
+
+    public void addGBean(GBeanOverride gbean) {
+        gbeans.put(gbean.getName(), gbean);
+    }
+
+    public void addGBean(String gbeanName, GBeanOverride gbean) {
+        gbeans.put(gbeanName, gbean);
+    }
+
+    public Map getGBeans() {
+        return gbeans;
+    }
+
+    public GBeanOverride getGBean(ObjectName gbeanName) {
+        return (GBeanOverride) gbeans.get(gbeanName);
+    }
+
+    public void addGBean(ObjectName gbeanName, GBeanOverride gbean) {
+        gbeans.put(gbeanName, gbean);
+    }
+
+    public void writeXml(PrintWriter out) {
+        out.print("  <configuration name=\"" + name + "\"");
+        if (!load) {
+            out.print(" load=\"false\"");
+        }
+        out.println(">");
+
+        // GBeans
+        for (Iterator gb = gbeans.entrySet().iterator(); gb.hasNext();) {
+            Map.Entry gbean = (Map.Entry) gb.next();
+
+            GBeanOverride gbeanOverride = (GBeanOverride) gbean.getValue();
+            gbeanOverride.writeXml(out);
+        }
+
+        out.println("  </configuration>");
+    }
+}

Propchange: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ConfigurationOverride.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ConfigurationOverride.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Added: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/GBeanOverride.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/GBeanOverride.java?rev=350073&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/GBeanOverride.java
(added)
+++ geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/GBeanOverride.java
Wed Nov 30 15:25:09 2005
@@ -0,0 +1,123 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.configuration;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+class GBeanOverride {
+    private final Object name;
+    private boolean load;
+    private final Map attributes = new LinkedHashMap();
+
+    public GBeanOverride(String name, boolean load) {
+        this.name = name;
+        this.load = load;
+    }
+
+    public GBeanOverride(ObjectName name, boolean load) {
+        this.name = name;
+        this.load = load;
+    }
+
+    public GBeanOverride(Element gbean) throws MalformedObjectNameException {
+        String nameString = gbean.getAttribute("name");
+        if (nameString.indexOf(':') > -1) {
+            name = ObjectName.getInstance(nameString);
+        } else {
+            name = nameString;
+        }
+
+        String loadString = gbean.getAttribute("load");
+        load = !"false".equals(loadString);
+
+        NodeList attributes = gbean.getElementsByTagName("attribute");
+        for (int a = 0; a < attributes.getLength(); a++) {
+            Element attribute = (Element) attributes.item(a);
+            String attName = attribute.getAttribute("name");
+            String value = "";
+            NodeList text = attribute.getChildNodes();
+            for (int t = 0; t < text.getLength(); t++) {
+                Node n = text.item(t);
+                if (n.getNodeType() == Node.TEXT_NODE) {
+                    value += n.getNodeValue();
+                }
+            }
+            setAttribute(attName, value.trim());
+        }
+    }
+
+    public Object getName() {
+        return name;
+    }
+
+    public boolean isLoad() {
+        return load;
+    }
+
+    public void setLoad(boolean load) {
+        this.load = load;
+    }
+
+    public Map getAttributes() {
+        return attributes;
+    }
+
+    public String getAttribute(String attributeName) {
+        return (String) attributes.get(attributeName);
+    }
+
+    public void setAttribute(String attributeName, String attributeValue) {
+        attributes.put(attributeName, attributeValue);
+    }
+
+    public void writeXml(PrintWriter out) {
+        String gbeanName;
+        if (name instanceof String) {
+            gbeanName = (String) name;
+        } else {
+            gbeanName = ((ObjectName) name).getCanonicalName();
+        }
+
+        out.print("    <gbean name=\"" + gbeanName + "\"");
+        if (!load) {
+            out.print(" load=\"false\"");
+        }
+        out.println(">");
+
+        // Attribute values
+        for (Iterator att = attributes.entrySet().iterator(); att.hasNext();) {
+            Map.Entry attribute = (Map.Entry) att.next();
+            out.print("      <attribute name=\"" + attribute.getKey() + "\">");
+            out.print((String) attribute.getValue());
+            out.println("</attribute>");
+        }
+
+        out.println("    </gbean>");
+    }
+}

Propchange: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/GBeanOverride.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/GBeanOverride.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Modified: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java?rev=350073&r1=350072&r2=350073&view=diff
==============================================================================
--- geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java
(original)
+++ geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/LocalAttributeManager.java
Wed Nov 30 15:25:09 2005
@@ -27,9 +27,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Iterator;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Timer;
@@ -53,8 +51,6 @@
 import org.apache.geronimo.system.serverinfo.ServerInfo;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
@@ -67,7 +63,7 @@
     private final static Log log = LogFactory.getLog(LocalAttributeManager.class);
 
     private final static String CONFIG_FILE_PROPERTY = "org.apache.geronimo.config.file";
-    
+
     private final static String BACKUP_EXTENSION = ".bak";
     private final static String TEMP_EXTENSION = ".working";
     private final static int SAVE_BUFFER_MS = 5000;
@@ -79,7 +75,7 @@
     private File attributeFile;
     private File backupFile;
     private File tempFile;
-    private final Map configurations = new LinkedHashMap();
+    private ServerOverride serverOverride;
 
     private Timer timer;
     private TimerTask currentTask;
@@ -96,14 +92,14 @@
         return readOnly;
     }
 
-    public Collection setAttributes(URI configurationName, Collection datas) throws InvalidConfigException
{
+    public synchronized Collection setAttributes(URI configurationName, Collection datas)
throws InvalidConfigException {
         String configName = configurationName.toString();
-        ConfigInfo configInfo = (ConfigInfo) configurations.get(configName);
-        if (configInfo != null) {
-            if (configInfo.isLoad()) {
+        ConfigurationOverride configurationOverride = serverOverride.getConfiguration(configName);
+        if (configurationOverride != null) {
+            if (configurationOverride.isLoad()) {
                 for (Iterator iterator = datas.iterator(); iterator.hasNext();) {
                     GBeanData data = (GBeanData) iterator.next();
-                    boolean load = setAttributes(data, configInfo, configName);
+                    boolean load = setAttributes(data, configurationOverride, configName);
                     if (!load) {
                         iterator.remove();
                     }
@@ -119,17 +115,17 @@
      * Set the attributes from the attribute store on a single gbean, and return whether
or not to load the gbean.
      *
      * @param data
-     * @param configInfo
+     * @param configurationOverride
      * @param configName
      * @return true if the gbean should be loaded, false otherwise.
-     * @throws InvalidConfigException
+     * @throws org.apache.geronimo.kernel.config.InvalidConfigException
      */
-    private boolean setAttributes(GBeanData data, ConfigInfo configInfo, String configName)
throws InvalidConfigException {
+    private synchronized boolean setAttributes(GBeanData data, ConfigurationOverride configurationOverride,
String configName) throws InvalidConfigException {
         ObjectName gbeanName = data.getName();
         GBeanInfo gBeanInfo = data.getGBeanInfo();
-        GBeanAttrsInfo attributeMap = configInfo.getGBean(gbeanName);
+        GBeanOverride attributeMap = configurationOverride.getGBean(gbeanName);
         if (attributeMap == null) {
-            attributeMap = configInfo.getGBean(gbeanName.getKeyProperty("name"));
+            attributeMap = configurationOverride.getGBean(gbeanName.getKeyProperty("name"));
         }
         if (attributeMap != null) {
             if (attributeMap.isLoad()) {
@@ -154,7 +150,7 @@
     }
 
 
-    private Object getValue(GAttributeInfo attribute, String value, String configurationName,
ObjectName gbeanName) {
+    private synchronized Object getValue(GAttributeInfo attribute, String value, String configurationName,
ObjectName gbeanName) {
         if (value == null) {
             return null;
         }
@@ -174,21 +170,17 @@
         }
     }
 
-    public synchronized void setValue(String configurationName, ObjectName gbean, GAttributeInfo
attribute, Object value) {
+    public synchronized void setValue(String configurationName, ObjectName gbeanName, GAttributeInfo
attribute, Object value) {
         if (readOnly) {
             return;
         }
-        ConfigInfo config = (ConfigInfo) configurations.get(configurationName);
-        if (config == null) {
-            config = new ConfigInfo(true);
-            configurations.put(configurationName, config);
-        }
-        GBeanAttrsInfo atts = config.getGBean(gbean);
-        if (atts == null) {
-            atts = config.getGBean(gbean.getKeyProperty("name"));
-            if (atts == null) {
-                atts = new GBeanAttrsInfo(true);
-                config.addGBean(gbean, atts);
+        ConfigurationOverride config = serverOverride.getConfiguration(configurationName,
true);
+        GBeanOverride gbeanOverride = config.getGBean(gbeanName);
+        if (gbeanOverride == null) {
+            gbeanOverride = config.getGBean(gbeanName.getKeyProperty("name"));
+            if (gbeanOverride == null) {
+                gbeanOverride = new GBeanOverride(gbeanName, true);
+                config.addGBean(gbeanName, gbeanOverride);
             }
         }
         try {
@@ -202,7 +194,7 @@
                 editor.setValue(value);
                 string = editor.getAsText();
             }
-            Map attrMap = atts.getAttributes();
+            Map attrMap = gbeanOverride.getAttributes();
             attrMap.put(attribute.getName(), string);
             attributeChanged();
         } catch (ClassNotFoundException e) {
@@ -211,80 +203,38 @@
         }
     }
 
-    public synchronized void setShouldLoad(String configurationName, ObjectName gbean, boolean
load) {
+    public synchronized void setShouldLoad(String configurationName, ObjectName gbeanName,
boolean load) {
         if (readOnly) {
             return;
         }
-        ConfigInfo config = (ConfigInfo) configurations.get(configurationName);
-        if (config == null) {
-            config = new ConfigInfo(true);
-            configurations.put(configurationName, config);
+        ConfigurationOverride config = serverOverride.getConfiguration(configurationName,
true);
+
+        GBeanOverride atts = config.getGBean(gbeanName);
+        if (atts == null) {
+            // attempt to lookup by short name
+            atts = config.getGBean(gbeanName.getKeyProperty("name"));
         }
-        GBeanAttrsInfo atts = config.getGBean(gbean);
+
         if (atts == null) {
-            atts = config.getGBean(gbean.getKeyProperty("name"));
-            if (atts == null) {
-                atts = new GBeanAttrsInfo(load);
-                config.addGBean(gbean, atts);
-            } else {
-                atts.setLoad(load);
-            }
+            atts = new GBeanOverride(gbeanName, load);
+            config.addGBean(gbeanName, atts);
         } else {
             atts.setLoad(load);
         }
         attributeChanged();
     }
 
-    public void load() throws IOException {
+    public synchronized void load() throws IOException {
         ensureParentDirectory();
         if (!attributeFile.exists()) {
             return;
         }
-        configurations.clear();
-        Map results = new LinkedHashMap();
         InputSource in = new InputSource(new FileInputStream(attributeFile));
         DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
         try {
             Document doc = dfactory.newDocumentBuilder().parse(in);
             Element root = doc.getDocumentElement();
-            NodeList configs = root.getElementsByTagName("configuration");
-            for (int c = 0; c < configs.getLength(); c++) {
-                Element config = (Element) configs.item(c);
-                String configName = config.getAttribute("name");
-                String loadConfigString = config.getAttribute("load");
-                boolean loadConfig = !"false".equals(loadConfigString);
-                ConfigInfo configInfo = new ConfigInfo(loadConfig);
-                results.put(configName, configInfo);
-                NodeList gbeans = config.getElementsByTagName("gbean");
-                for (int g = 0; g < gbeans.getLength(); g++) {
-                    Element gbean = (Element) gbeans.item(g);
-                    String gbeanName = gbean.getAttribute("name");
-                    String loadGBeanString = gbean.getAttribute("load");
-                    boolean loadGBean = !"false".equals(loadGBeanString);
-                    GBeanAttrsInfo gbeanAttrs = new GBeanAttrsInfo(loadGBean);
-                    if (gbeanName.indexOf(':') > -1) {
-                        ObjectName name = ObjectName.getInstance(gbeanName);
-                        configInfo.addGBean(name, gbeanAttrs);
-                    } else {
-                        configInfo.addGBean(gbeanName, gbeanAttrs);
-                    }
-                    NodeList attributes = gbean.getElementsByTagName("attribute");
-                    for (int a = 0; a < attributes.getLength(); a++) {
-                        Element attribute = (Element) attributes.item(a);
-                        String attName = attribute.getAttribute("name");
-                        String value = "";
-                        NodeList text = attribute.getChildNodes();
-                        for (int t = 0; t < text.getLength(); t++) {
-                            Node n = text.item(t);
-                            if (n.getNodeType() == Node.TEXT_NODE) {
-                                value += n.getNodeValue();
-                            }
-                        }
-                        gbeanAttrs.setAttribute(attName, value.trim());
-                    }
-                }
-            }
-            configurations.putAll(results);
+            serverOverride = new ServerOverride(root);
         } catch (SAXException e) {
             log.error("Unable to read saved manageable attributes", e);
         } catch (ParserConfigurationException e) {
@@ -294,7 +244,7 @@
         }
     }
 
-     public synchronized void save() throws IOException {
+    public synchronized void save() throws IOException {
         if (readOnly) {
             return;
         }
@@ -305,63 +255,47 @@
         if (!tempFile.canWrite()) {
             throw new IOException("Unable to write to manageable attribute working file for
save " + tempFile.getAbsolutePath());
         }
+
+        // write the new configuration to the temp file
         PrintWriter out = new PrintWriter(new FileWriter(tempFile), true);
-        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
-        out.println();
-        out.println("<attributes xmlns=\"http://geronimo.apache.org/xml/ns/attributes\">");
-        for (Iterator it = configurations.entrySet().iterator(); it.hasNext();) {
-            Map.Entry entry = (Map.Entry) it.next();
-            ConfigInfo configInfo = (ConfigInfo) entry.getValue();
-            boolean configLoaded = configInfo.isLoad();
-            out.println("  <configuration name=\"" + entry.getKey() + (configLoaded ?
"\">" : "\" load=\"false\">"));
-            for (Iterator gb = configInfo.getGBeans().entrySet().iterator(); gb.hasNext();)
{
-                Map.Entry gbean = (Map.Entry) gb.next();
-                String gbeanName = gbean.getKey() instanceof String ? (String) gbean.getKey()
: ((ObjectName) gbean.getKey()).getCanonicalName();
-                GBeanAttrsInfo atts = (GBeanAttrsInfo) gbean.getValue();
-                boolean gbeanLoaded = atts.isLoad();
-                out.println("    <gbean name=\"" + gbeanName + (gbeanLoaded ? "\">"
: "\" load=\"false\">"));
-                for (Iterator att = atts.getAttributes().entrySet().iterator(); att.hasNext();)
{
-                    Map.Entry attribute = (Map.Entry) att.next();
-                    out.print("      <attribute name=\"" + attribute.getKey() + "\">");
-                    out.print((String) attribute.getValue());
-                    out.println("</attribute>");
-                }
-                out.println("    </gbean>");
-            }
-            out.println("  </configuration>");
-        }
-        out.println("</attributes>");
+        serverOverride.writeXml(out);
         out.close();
+
+        // delete the current backup file
         if (backupFile.exists()) {
             if (!backupFile.delete()) {
                 throw new IOException("Unable to delete old backup file in order to back
up current manageable attribute working file for save");
             }
         }
+
+        // rename the existing configuration file to the backup file
         if (attributeFile.exists()) {
             if (!attributeFile.renameTo(backupFile)) {
                 throw new IOException("Unable to rename " + attributeFile.getAbsolutePath()
+ " to " + backupFile.getAbsolutePath() + " in order to back up manageable attribute save
file");
             }
         }
+
+        // rename the temp file the the configuration file
         if (!tempFile.renameTo(attributeFile)) {
             throw new IOException("EXTREMELY CRITICAL!  Unable to move manageable attributes
working file to proper file name!  Configuration will revert to defaults unless this is manually
corrected!  (could not rename " + tempFile.getAbsolutePath() + " to " + attributeFile.getAbsolutePath()
+ ")");
         }
     }
 
     //PersistentConfigurationList
-    public boolean isKernelFullyStarted() {
-         return kernelFullyStarted;
-     }
-
-     public void setKernelFullyStarted(boolean kernelFullyStarted) {
-         this.kernelFullyStarted = kernelFullyStarted;
-     }
+    public synchronized boolean isKernelFullyStarted() {
+        return kernelFullyStarted;
+    }
+
+    public synchronized void setKernelFullyStarted(boolean kernelFullyStarted) {
+        this.kernelFullyStarted = kernelFullyStarted;
+    }
 
-    public List restore() throws IOException {
+    public synchronized List restore() throws IOException {
         List configs = new ArrayList();
-        for (Iterator iterator = configurations.entrySet().iterator(); iterator.hasNext();)
{
+        for (Iterator iterator = serverOverride.getConfigurations().entrySet().iterator();
iterator.hasNext();) {
             Map.Entry entry = (Map.Entry) iterator.next();
-            ConfigInfo configInfo = (ConfigInfo) entry.getValue();
-            if (configInfo.isLoad()) {
+            ConfigurationOverride configurationOverride = (ConfigurationOverride) entry.getValue();
+            if (configurationOverride.isLoad()) {
                 String configName = (String) entry.getKey();
                 try {
                     URI configID = new URI(configName);
@@ -374,40 +308,34 @@
         return configs;
     }
 
-    public synchronized void addConfiguration(String configName) {
-        ConfigInfo configInfo = (ConfigInfo) configurations.get(configName);
-        if (configInfo == null) {
-            configInfo = new ConfigInfo(true);
-            configurations.put(configName, configInfo);
-        } else {
-            configInfo.setLoad(true);
-        }
+    public synchronized void addConfiguration(String configurationName) {
+        ConfigurationOverride config = serverOverride.getConfiguration(configurationName,
true);
+        config.setLoad(true);
     }
 
     public synchronized void removeConfiguration(String configName) {
-        ConfigInfo configInfo = (ConfigInfo) configurations.get(configName);
-        if (configInfo == null) {
+        ConfigurationOverride configuration = serverOverride.getConfiguration(configName);
+        if (configuration == null) {
             log.error("Trying to stop unknown configuration: " + configName);
         } else {
-            Map gbeans = configInfo.getGBeans();
-            if (gbeans.isEmpty()) {
-                configurations.remove(configName);
+            if (configuration.getGBeans().isEmpty()) {
+                serverOverride.removeConfiguration(configName);
             } else {
-                configInfo.setLoad(false);
+                configuration.setLoad(false);
             }
         }
     }
 
     //GBeanLifeCycle
-    public void doStart() throws Exception {
+    public synchronized void doStart() throws Exception {
         load();
         if (!readOnly) {
             timer = new Timer();
         }
-        log.info("Started LocalAttributeManager with data on " + configurations.size() +
" configurations");
+        log.info("Started LocalAttributeManager with data on " + serverOverride.getConfigurations().size()
+ " configurations");
     }
 
-    public void doStop() throws Exception {
+    public synchronized void doStop() throws Exception {
         boolean doSave = false;
         synchronized (this) {
             if (timer != null) {
@@ -421,11 +349,11 @@
         if (doSave) {
             save();
         }
-        log.info("Stopped LocalAttributeManager with data on " + configurations.size() +
" configurations");
-        configurations.clear();
+        log.info("Stopped LocalAttributeManager with data on " + serverOverride.getConfigurations().size()
+ " configurations");
+        serverOverride = new ServerOverride();
     }
 
-    public void doFail() {
+    public synchronized void doFail() {
         synchronized (this) {
             if (timer != null) {
                 timer.cancel();
@@ -434,10 +362,10 @@
                 }
             }
         }
-        configurations.clear();
+        serverOverride = new ServerOverride();
     }
 
-    private void ensureParentDirectory() throws IOException {
+    private synchronized void ensureParentDirectory() throws IOException {
         if (attributeFile == null) {
             attributeFile = serverInfo.resolve(configFile);
             tempFile = new File(attributeFile.getAbsolutePath() + TEMP_EXTENSION);
@@ -470,72 +398,6 @@
             }
         };
         timer.schedule(currentTask, SAVE_BUFFER_MS);
-    }
-
-    private static class ConfigInfo {
-        private boolean load;
-        private final Map gbeans = new HashMap();
-
-        public ConfigInfo(boolean load) {
-            this.load = load;
-        }
-
-        public boolean isLoad() {
-            return load;
-        }
-
-        public void setLoad(boolean load) {
-            this.load = load;
-        }
-
-        public GBeanAttrsInfo getGBean(String gbeanName) {
-            return (GBeanAttrsInfo) gbeans.get(gbeanName);
-        }
-
-        public void addGBean(String gbeanName, GBeanAttrsInfo gbean) {
-            gbeans.put(gbeanName, gbean);
-        }
-
-        public Map getGBeans() {
-            return gbeans;
-        }
-
-        public GBeanAttrsInfo getGBean(ObjectName gbeanName) {
-            return (GBeanAttrsInfo) gbeans.get(gbeanName);
-        }
-
-        public void addGBean(ObjectName gbeanName, GBeanAttrsInfo gbean) {
-            gbeans.put(gbeanName, gbean);
-        }
-    }
-
-    private static class GBeanAttrsInfo {
-        private boolean load;
-        private final Map attributes = new HashMap();
-
-        public GBeanAttrsInfo(boolean load) {
-            this.load = load;
-        }
-
-        public boolean isLoad() {
-            return load;
-        }
-
-        public void setLoad(boolean load) {
-            this.load = load;
-        }
-
-        public Map getAttributes() {
-            return attributes;
-        }
-
-        public String getAttribute(String attributeName) {
-            return (String) attributes.get(attributeName);
-        }
-
-        public void setAttribute(String attributeName, String attributeValue) {
-            attributes.put(attributeName, attributeValue);
-        }
     }
 
     public static final GBeanInfo GBEAN_INFO;

Added: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ServerOverride.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ServerOverride.java?rev=350073&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ServerOverride.java
(added)
+++ geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ServerOverride.java
Wed Nov 30 15:25:09 2005
@@ -0,0 +1,83 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.configuration;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import javax.management.MalformedObjectNameException;
+import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @version $Rev$ $Date$
+ */
+class ServerOverride {
+    private final Map configurations = new LinkedHashMap();
+
+    public ServerOverride() {
+    }
+
+    public ServerOverride(Element element) throws MalformedObjectNameException {
+        NodeList configs = element.getElementsByTagName("configuration");
+        for (int i = 0; i < configs.getLength(); i++) {
+            Element configurationElement = (Element) configs.item(i);
+            ConfigurationOverride configuration = new ConfigurationOverride(configurationElement);
+            addConfiguration(configuration);
+        }
+    }
+
+    public ConfigurationOverride getConfiguration(String configurationName) {
+        return getConfiguration(configurationName, false);
+    }
+
+    public ConfigurationOverride getConfiguration(String configurationName, boolean create)
{
+        ConfigurationOverride configuration = (ConfigurationOverride) configurations.get(configurationName);
+        if (create && configuration == null) {
+            configuration = new ConfigurationOverride(configurationName, true);
+            configurations.put(configurationName, configuration);
+        }
+        return configuration;
+    }
+
+    public void addConfiguration(ConfigurationOverride configuration) {
+        configurations.put(configuration.getName(), configuration);
+    }
+
+    public void removeConfiguration(String configurationName) {
+        configurations.remove(configurationName);
+    }
+
+    public Map getConfigurations() {
+        return configurations;
+    }
+
+    public void writeXml(PrintWriter out) {
+        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+        out.println();
+        out.println("<attributes xmlns=\"http://geronimo.apache.org/xml/ns/attributes\">");
+        for (Iterator it = configurations.entrySet().iterator(); it.hasNext();) {
+            Map.Entry entry = (Map.Entry) it.next();
+            ConfigurationOverride configurationOverride = (ConfigurationOverride) entry.getValue();
+            configurationOverride.writeXml(out);
+        }
+        out.println("</attributes>");
+        out.close();
+    }
+}

Propchange: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ServerOverride.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/configuration/ServerOverride.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"

Added: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/configuration/ServerOverrideTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/configuration/ServerOverrideTest.java?rev=350073&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/configuration/ServerOverrideTest.java
(added)
+++ geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/configuration/ServerOverrideTest.java
Wed Nov 30 15:25:09 2005
@@ -0,0 +1,208 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.configuration;
+
+import junit.framework.TestCase;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+import java.util.Map;
+import java.util.Iterator;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ServerOverrideTest extends TestCase {
+    public void testBasics() throws Exception {
+        GBeanOverride pizza = new GBeanOverride("Pizza", true);
+        assertTrue(pizza.isLoad());
+
+        pizza.setLoad(false);
+        assertFalse(pizza.isLoad());
+
+        pizza.setAttribute("cheese", "mozzarella");
+        assertEquals("mozzarella", pizza.getAttribute("cheese"));
+
+        ConfigurationOverride dinnerMenu = new ConfigurationOverride("Dinner Menu", true);
+        assertTrue(dinnerMenu.isLoad());
+
+        dinnerMenu.setLoad(false);
+        assertFalse(dinnerMenu.isLoad());
+
+        dinnerMenu.addGBean(pizza);
+        assertSame(pizza, dinnerMenu.getGBean("Pizza"));
+
+        ServerOverride restaurant = new ServerOverride();
+        restaurant.addConfiguration(dinnerMenu);
+        assertSame(dinnerMenu, restaurant.getConfiguration("Dinner Menu"));
+    }
+
+    public void testGBeanXml() throws Exception {
+        GBeanOverride pizza = new GBeanOverride("Pizza", true);
+        assertCopyIdentical(pizza);
+
+        pizza.setLoad(false);
+        assertCopyIdentical(pizza);
+
+        pizza.setAttribute("cheese", "mozzarella");
+        assertCopyIdentical(pizza);
+
+        pizza.setAttribute("size", "x-large");
+        assertCopyIdentical(pizza);
+    }
+
+    public void testConfigurationXml() throws Exception {
+        ConfigurationOverride dinnerMenu = new ConfigurationOverride("Dinner Menu", true);
+        assertCopyIdentical(dinnerMenu);
+
+        dinnerMenu.setLoad(false);
+        assertCopyIdentical(dinnerMenu);
+
+        GBeanOverride pizza = new GBeanOverride("Pizza", false);
+        pizza.setAttribute("cheese", "mozzarella");
+        pizza.setAttribute("size", "x-large");
+        dinnerMenu.addGBean(pizza);
+        assertCopyIdentical(dinnerMenu);
+
+        GBeanOverride garlicCheeseBread = new GBeanOverride("Garlic Cheese Bread", true);
+        dinnerMenu.addGBean(garlicCheeseBread);
+        assertCopyIdentical(dinnerMenu);
+    }
+
+    public void testServerXml() throws Exception {
+        ServerOverride restaurant = new ServerOverride();
+        assertCopyIdentical(restaurant);
+
+        ConfigurationOverride dinnerMenu = new ConfigurationOverride("Dinner Menu", false);
+        GBeanOverride pizza = new GBeanOverride("Pizza", false);
+        pizza.setAttribute("cheese", "mozzarella");
+        pizza.setAttribute("size", "x-large");
+        dinnerMenu.addGBean(pizza);
+        GBeanOverride garlicCheeseBread = new GBeanOverride("Garlic Cheese Bread", true);
+        dinnerMenu.addGBean(garlicCheeseBread);
+        assertCopyIdentical(restaurant);
+
+        ConfigurationOverride drinkMenu = new ConfigurationOverride("Drink Menu", false);
+        GBeanOverride beer = new GBeanOverride("Beer", true);
+        drinkMenu.addGBean(beer);
+        GBeanOverride wine = new GBeanOverride("Wine", true);
+        drinkMenu.addGBean(wine);
+        assertCopyIdentical(restaurant);
+    }
+
+    private void assertCopyIdentical(ServerOverride server) throws Exception {
+        ServerOverride copy = copy(server);
+        assertIdentical(server, copy);
+    }
+
+    private void assertCopyIdentical(ConfigurationOverride configuration) throws Exception
{
+        ConfigurationOverride copy = copy(configuration);
+        assertIdentical(configuration, copy);
+    }
+
+    private void assertCopyIdentical(GBeanOverride gbean) throws Exception {
+        GBeanOverride copy = copy(gbean);
+        assertIdentical(gbean, copy);
+    }
+
+    private void assertIdentical(ServerOverride expected, ServerOverride actual) {
+        assertNotNull(expected);
+        assertNotNull(actual);
+        assertNotSame(expected, actual);
+
+        Map expectedGBeans = expected.getConfigurations();
+        Map actualGBeans = actual.getConfigurations();
+        assertEquals(expectedGBeans.size(), actualGBeans.size());
+
+        for (Iterator iterator = expectedGBeans.entrySet().iterator(); iterator.hasNext();)
{
+            Map.Entry entry = (Map.Entry) iterator.next();
+            Object name = entry.getKey();
+            ConfigurationOverride expectedConfiguration = (ConfigurationOverride) entry.getValue();
+            ConfigurationOverride actualConfiguration = (ConfigurationOverride) actualGBeans.get(name);
+            assertIdentical(expectedConfiguration, actualConfiguration);
+        }
+    }
+
+    private void assertIdentical(ConfigurationOverride expected, ConfigurationOverride actual)
{
+        assertNotNull(expected);
+        assertNotNull(actual);
+        assertNotSame(expected, actual);
+        assertEquals(expected.getName(), actual.getName());
+        assertEquals(expected.isLoad(), actual.isLoad());
+
+        Map expectedGBeans = expected.getGBeans();
+        Map actualGBeans = actual.getGBeans();
+        assertEquals(expectedGBeans.size(), actualGBeans.size());
+
+        for (Iterator iterator = expectedGBeans.entrySet().iterator(); iterator.hasNext();)
{
+            Map.Entry entry = (Map.Entry) iterator.next();
+            Object name = entry.getKey();
+            GBeanOverride expectedGBean = (GBeanOverride) entry.getValue();
+            GBeanOverride actualGBean = (GBeanOverride) actualGBeans.get(name);
+            assertIdentical(expectedGBean, actualGBean);
+        }
+    }
+
+    private void assertIdentical(GBeanOverride expected, GBeanOverride actual) {
+        assertNotNull(expected);
+        assertNotNull(actual);
+        assertNotSame(expected, actual);
+        assertEquals(expected.getName(), actual.getName());
+        assertEquals(expected.isLoad(), actual.isLoad());
+        assertEquals(expected.getAttributes(), actual.getAttributes());
+    }
+
+    private ServerOverride copy(ServerOverride server) throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        server.writeXml(new PrintWriter(out, true));
+        System.out.println(new String(out.toByteArray()));
+        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        Element element = parseXml(in);
+        ServerOverride copy = new ServerOverride(element);
+        return copy;
+    }
+
+    private ConfigurationOverride copy(ConfigurationOverride configuration) throws Exception
{
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        configuration.writeXml(new PrintWriter(out, true));
+        System.out.println(new String(out.toByteArray()));
+        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        Element element = parseXml(in);
+        ConfigurationOverride copy = new ConfigurationOverride(element);
+        return copy;
+    }
+
+    private GBeanOverride copy(GBeanOverride gbean) throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        gbean.writeXml(new PrintWriter(out, true));
+        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        Element element = parseXml(in);
+        GBeanOverride copy = new GBeanOverride(element);
+        return copy;
+    }
+
+    private Element parseXml(InputStream in) throws Exception {
+        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+        Document doc = documentBuilderFactory.newDocumentBuilder().parse(in);
+        return doc.getDocumentElement();
+    }
+}

Propchange: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/configuration/ServerOverrideTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/configuration/ServerOverrideTest.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Id"



Mime
View raw message