Author: rob.stryker(a)jboss.com
Date: 2009-04-14 01:41:07 -0400 (Tue, 14 Apr 2009)
New Revision: 14715
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathCategory.java
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathModel.java
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathQuery.java
Log:
JBIDE-4046 - race condition in server wizard
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathCategory.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathCategory.java 2009-04-14
03:22:57 UTC (rev 14714)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathCategory.java 2009-04-14
05:41:07 UTC (rev 14715)
@@ -21,11 +21,14 @@
*/
package org.jboss.ide.eclipse.as.core.extensions.descriptors;
+import java.util.ArrayList;
import java.util.HashMap;
-import java.util.Iterator;
+import java.util.List;
+import org.eclipse.core.runtime.Path;
import org.eclipse.wst.server.core.IServer;
import org.jboss.ide.eclipse.as.core.server.internal.ServerAttributeHelper;
+import org.jboss.tools.jmx.core.IMemento;
/**
* A class representing an XPath Category, which
@@ -34,15 +37,61 @@
*
*/
public class XPathCategory {
+ @Deprecated private static final String DELIMITER = ",";
+ @Deprecated private static final String QUERY_LIST =
+ "org.jboss.ide.eclipse.as.core.model.descriptor.QueryList";
+ @Deprecated private static final String QUERY =
+ "org.jboss.ide.eclipse.as.core.model.descriptor.Query";
+
+
protected String name; // cannot include delimiter from the model, comma
protected IServer server;
+ protected IMemento memento;
protected HashMap<String, XPathQuery> children;
+ @Deprecated
public XPathCategory(String name, IServer server) {
this.name = name;
this.server = server;
+ children = new HashMap<String, XPathQuery>();
+ XPathQuery[] queries = loadQueries_LEGACY(this, server);
+ for( int i = 0; i < queries.length; i++ ) {
+ children.put(queries[i].getName(), queries[i]);
+ }
}
+ private static XPathQuery[] loadQueries_LEGACY(XPathCategory category, IServer server)
{
+ ServerAttributeHelper helper = ServerAttributeHelper.createHelper(server);
+ String list = helper.getAttribute(QUERY_LIST + "." +
category.getName().replace(' ', '_'), (String)null);
+ if( list == null )
+ return new XPathQuery[] {};
+ String[] queriesByName = list.split(DELIMITER);
+ List<String> queryAsStringValues;
+ ArrayList<XPathQuery> returnList = new ArrayList<XPathQuery>();
+ for( int i = 0; i < queriesByName.length; i++ ) {
+ queryAsStringValues = helper.getAttribute(QUERY + "." +
queriesByName[i].replace(' ', '_'), (List)null);
+ if( queryAsStringValues != null ) {
+ XPathQuery q =new
XPathQuery(queriesByName[i].substring(queriesByName[i].indexOf(Path.SEPARATOR)+1),
queryAsStringValues);
+ q.setCategory(category);
+ returnList.add(q);
+ }
+ }
+ return (XPathQuery[]) returnList.toArray(new XPathQuery[returnList.size()]);
+ }
+
+ public XPathCategory(IServer server, IMemento memento) {
+ this.server = server;
+ this.name = memento.getString("name");
+ IMemento[] queryMementos = memento.getChildren("query");
+ children = new HashMap<String, XPathQuery>();
+ for( int i = 0; i < queryMementos.length; i++ ) {
+ String name = queryMementos[i].getString("name");
+ XPathQuery child = new XPathQuery(queryMementos[i]);
+ children.put(name, child);
+ child.setCategory(this);
+ }
+ }
+
public String getName() { return this.name; }
public IServer getServer() { return this.server; }
@@ -54,13 +103,6 @@
* Lazily load the queries upon request
*/
public XPathQuery[] getQueries() {
- if( children == null ) {
- children = new HashMap<String, XPathQuery>();
- XPathQuery[] queries = XPathModel.getDefault().loadQueries(this, server);
- for( int i = 0; i < queries.length; i++ ) {
- children.put(queries[i].getName(), queries[i]);
- }
- }
return children.values().toArray(new XPathQuery[children.size()]);
}
@@ -87,8 +129,6 @@
* Save these queries to its server object
*/
public void save() {
- ServerAttributeHelper helper = ServerAttributeHelper.createHelper(server);
- XPathModel.getDefault().saveCategory(this, server, helper);
- helper.save();
+ XPathModel.getDefault().save(server);
}
}
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathModel.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathModel.java 2009-04-14
03:22:57 UTC (rev 14714)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathModel.java 2009-04-14
05:41:07 UTC (rev 14715)
@@ -21,13 +21,15 @@
*/
package org.jboss.ide.eclipse.as.core.extensions.descriptors;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.List;
import java.util.Properties;
import org.eclipse.core.runtime.FileLocator;
@@ -44,6 +46,8 @@
import org.jboss.ide.eclipse.as.core.server.UnitedServerListener;
import org.jboss.ide.eclipse.as.core.server.internal.LocalJBossServerRuntime;
import org.jboss.ide.eclipse.as.core.server.internal.ServerAttributeHelper;
+import org.jboss.tools.jmx.core.IMemento;
+import org.jboss.tools.jmx.core.util.XMLMemento;
/**
* The class representing the model for all xpath storage and searching
@@ -58,12 +62,9 @@
private static final String DELIMITER = ",";
private static final String CATEGORY_LIST =
"org.jboss.ide.eclipse.as.core.model.descriptor.Categories";
- private static final String QUERY_LIST =
- "org.jboss.ide.eclipse.as.core.model.descriptor.QueryList";
- private static final String QUERY =
- "org.jboss.ide.eclipse.as.core.model.descriptor.Query";
- private static final String DEFAULTS_SET =
"org.jboss.ide.eclipse.as.core.model.descriptor.DefaultsSet";
-
+ private static final IPath STATE_LOCATION =
JBossServerCorePlugin.getDefault().getStateLocation();
+ private static final String XPATH_FILE_NAME = "xpaths.xml";
+
/* Singleton */
private static XPathModel instance;
public static XPathModel getDefault() {
@@ -79,31 +80,26 @@
}
public void serverAdded(IServer server) {
- final ServerAttributeHelper helper = new ServerAttributeHelper(server,
server.createWorkingCopy());
- if( !helper.getAttribute(DEFAULTS_SET, false)) {
- final IServer server2 = server;
- new Job("Add Server XPath Details") {
- protected IStatus run(IProgressMonitor monitor) {
-
- if(server2==null || server2.getRuntime()==null) {
- return Status.OK_STATUS; // server has no runtime so we can't set this up.
- }
-
- LocalJBossServerRuntime ajbsr = (LocalJBossServerRuntime)
- server2.getRuntime().loadAdapter(LocalJBossServerRuntime.class, null);
- if(ajbsr != null ) {
- IPath loc = server2.getRuntime().getLocation();
- IPath configFolder =
loc.append(IJBossServerConstants.SERVER).append(ajbsr.getJBossConfiguration());
- loadDefaults(server2, configFolder.toOSString());
- helper.setAttribute(DEFAULTS_SET, true);
- helper.save();
- save(server2);
- }
- return Status.OK_STATUS;
+ final IServer server2 = server;
+ new Job("Add Server XPath Details") {
+ protected IStatus run(IProgressMonitor monitor) {
+
+ if(server2==null || server2.getRuntime()==null) {
+ return Status.OK_STATUS; // server has no runtime so we can't set this up.
}
- }.schedule();
- }
+ LocalJBossServerRuntime ajbsr = (LocalJBossServerRuntime)
+ server2.getRuntime().loadAdapter(LocalJBossServerRuntime.class, null);
+ if(ajbsr != null ) {
+ IPath loc = server2.getRuntime().getLocation();
+ IPath configFolder =
loc.append(IJBossServerConstants.SERVER).append(ajbsr.getJBossConfiguration());
+ ArrayList<XPathCategory> defaults = loadDefaults(server2,
configFolder.toOSString());
+ serverToCategories.put(server2.getId(), defaults);
+ save(server2);
+ }
+ return Status.OK_STATUS;
+ }
+ }.schedule();
}
public XPathQuery getQuery(IServer server, IPath path) {
@@ -172,79 +168,81 @@
/*
* Loading and saving is below
*/
- private XPathCategory[] load(IServer server) {
- ServerAttributeHelper helper = ServerAttributeHelper.createHelper(server);
- String list = helper.getAttribute(CATEGORY_LIST, (String)null);
- if( list == null )
- return new XPathCategory[] {};
- String[] byName = list.split(DELIMITER);
- XPathCategory[] cats = new XPathCategory[byName.length];
- for( int i = 0; i < byName.length; i++ ) {
- cats[i] = new XPathCategory(byName[i], server);
- }
- return cats;
+ protected File getFile(IServer server) {
+ return STATE_LOCATION.append(server.getId().replace(' ',
'_')).append(XPATH_FILE_NAME).toFile();
}
-
+
public void save(IServer server) {
if( !serverToCategories.containsKey(server.getId()))
return;
-
- ServerAttributeHelper helper = ServerAttributeHelper.createHelper(server);
+ XMLMemento memento = XMLMemento.createWriteRoot("xpaths");
XPathCategory[] categories = getCategories(server);
- String list = "";
for( int i = 0; i < categories.length; i++ ) {
- if( i != 0 )
- list += DELIMITER;
- list += categories[i].getName();
- saveCategory(categories[i], server, helper);
+ XMLMemento child = (XMLMemento)memento.createChild("category");
+ saveCategory(categories[i], server, child);
}
- helper.setAttribute(CATEGORY_LIST, list);
- helper.save();
+ try {
+ memento.save(new FileOutputStream(getFile(server)));
+ } catch( IOException ioe) {
+ // TODO LOG
+ }
}
- public void saveCategory(XPathCategory category, IServer server, ServerAttributeHelper
helper) {
+ public void saveCategory(XPathCategory category, IServer server, XMLMemento memento) {
+ memento.putString("name", category.getName());
if( category.queriesLoaded()) {
XPathQuery[] queries = category.getQueries();
- String val = "";
for( int i = 0; i < queries.length; i++ ) {
- if( i != 0 )
- val += DELIMITER;
- val += category.getName() + Path.SEPARATOR + queries[i].getName();
- saveQuery(queries[i], category, server, helper);
+ XMLMemento child = (XMLMemento)memento.createChild("query");
+ saveQuery(queries[i], category, server, child);
}
- helper.setAttribute(QUERY_LIST + "." + category.getName().replace('
', '_'), val);
}
}
- private void saveQuery(XPathQuery query, XPathCategory category, IServer server,
ServerAttributeHelper helper) {
- ArrayList<String> list = new ArrayList<String>();
- list.add(query.getBaseDir());
- list.add(query.getFilePattern() == null ? EMPTY_STRING : query.getFilePattern());
- list.add(query.getXpathPattern() == null ? EMPTY_STRING : query.getXpathPattern());
- list.add(query.getAttribute() == null ? EMPTY_STRING : query.getAttribute());
- helper.setAttribute(QUERY + "." + category.getName().replace(' ',
'_') + Path.SEPARATOR + query.getName().replace(' ', '_'), list);
+ private void saveQuery(XPathQuery query, XPathCategory category,
+ IServer server, XMLMemento memento) {
+ memento.putString("name", query.getName());
+ memento.putString("dir", query.getBaseDir());
+ memento.putString("filePattern", query.getFilePattern());
+ memento.putString("xpathPattern", query.getXpathPattern());
+ memento.putString("attribute", query.getAttribute());
}
- public XPathQuery[] loadQueries(XPathCategory category, IServer server) {
- ServerAttributeHelper helper = ServerAttributeHelper.createHelper(server);
- String list = helper.getAttribute(QUERY_LIST + "." +
category.getName().replace(' ', '_'), (String)null);
- if( list == null )
- return new XPathQuery[] {};
- String[] queriesByName = list.split(DELIMITER);
- List<String> queryAsStringValues;
- ArrayList<XPathQuery> returnList = new ArrayList<XPathQuery>();
- for( int i = 0; i < queriesByName.length; i++ ) {
- queryAsStringValues = helper.getAttribute(QUERY + "." +
queriesByName[i].replace(' ', '_'), (List)null);
- if( queryAsStringValues != null ) {
- XPathQuery q =new
XPathQuery(queriesByName[i].substring(queriesByName[i].indexOf(Path.SEPARATOR)+1),
queryAsStringValues);
- q.setCategory(category);
- returnList.add(q);
+ private XPathCategory[] load(IServer server) {
+ if( getFile(server).exists())
+ return loadXML(server);
+ return load_LEGACY(server);
+ }
+
+ private XPathCategory[] loadXML(IServer server) {
+ XPathCategory[] categories = null;
+ try {
+ File file = getFile(server);
+ XMLMemento memento = XMLMemento.createReadRoot(new FileInputStream(file));
+ IMemento[] categoryMementos = memento.getChildren("category");
+ categories = new XPathCategory[categoryMementos.length];
+ for( int i = 0; i < categoryMementos.length; i++ ) {
+ categories[i] = new XPathCategory(server, categoryMementos[i]);
}
+ } catch( IOException ioe) {
+ // TODO LOG
}
- return (XPathQuery[]) returnList.toArray(new XPathQuery[returnList.size()]);
+ return categories == null ? new XPathCategory[] { } : categories;
}
+ private XPathCategory[] load_LEGACY(IServer server) {
+ ServerAttributeHelper helper = ServerAttributeHelper.createHelper(server);
+ String list = helper.getAttribute(CATEGORY_LIST, (String)null);
+ if( list == null )
+ return new XPathCategory[] {};
+ String[] byName = list.split(DELIMITER);
+ XPathCategory[] cats = new XPathCategory[byName.length];
+ for( int i = 0; i < byName.length; i++ ) {
+ cats[i] = new XPathCategory(byName[i], server);
+ }
+ return cats;
+ }
/*
* Loading the defaults for the server
@@ -262,12 +260,12 @@
rtToPortsFile.put("org.jboss.ide.eclipse.as.runtime.eap.43", new
Path("properties").append("jboss.eap.43.default.ports.properties"));
}
- public void loadDefaults(IServer server, String configFolder) {
+ private static ArrayList<XPathCategory> loadDefaults(IServer server, String
configFolder) {
ArrayList<XPathCategory> retVal = new ArrayList<XPathCategory>();
Path p = (Path)rtToPortsFile.get(server.getRuntime().getRuntimeType().getId());
- if( p == null ) return;
+ if( p == null ) return retVal;
URL url = FileLocator.find(JBossServerCorePlugin.getDefault().getBundle(), p, null);
- if( url == null ) return;
+ if( url == null ) return retVal;
Properties pr = new Properties();
try {
@@ -292,8 +290,7 @@
new Status(IStatus.ERROR, JBossServerCorePlugin.PLUGIN_ID,
"Error loading default xpaths", e));
}
-
- serverToCategories.put(server.getId(), retVal);
+ return retVal;
}
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathQuery.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathQuery.java 2009-04-14
03:22:57 UTC (rev 14714)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/descriptors/XPathQuery.java 2009-04-14
05:41:07 UTC (rev 14715)
@@ -34,6 +34,7 @@
import org.jaxen.XPath;
import org.jaxen.dom4j.Dom4jXPath;
import
org.jboss.ide.eclipse.as.core.extensions.descriptors.XPathFileResult.XPathResultNode;
+import org.jboss.tools.jmx.core.IMemento;
/**
* A simple value object to hold the XPath query data
@@ -64,6 +65,14 @@
protected transient XPathCategory category;
protected transient XMLDocumentRepository repository = null;
+ public XPathQuery(IMemento memento) {
+ this.name = memento.getString("name");
+ this.baseDir = memento.getString("dir");
+ this.filePattern = memento.getString("filePattern");
+ this.xpathPattern = memento.getString("xpathPattern");
+ this.attribute = memento.getString("attribute");
+ }
+
public XPathQuery(String name, List list) {
this.name = name;
this.baseDir = list.get(0).equals(XPathModel.EMPTY_STRING) ? null :
(String)list.get(0);
@@ -71,6 +80,7 @@
this.xpathPattern = list.get(2).equals(XPathModel.EMPTY_STRING) ? null :
(String)list.get(2);
this.attribute = list.size() < 3 || list.get(3).equals(XPathModel.EMPTY_STRING) ?
null : (String)list.get(3);
}
+
public XPathQuery(String name, String baseDir, String filePattern, String xpathPattern,
String attribute) {
this.name = name;
this.baseDir = baseDir;