Author: koen.aers(a)jboss.com
Date: 2011-06-29 16:35:01 -0400 (Wed, 29 Jun 2011)
New Revision: 32460
Modified:
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeInstallations.java
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgePreferenceInitializer.java
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeInstallationsTest.java
Log:
initial ForgeInstallations implementation and test
Modified:
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeInstallations.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeInstallations.java 2011-06-29
20:10:29 UTC (rev 32459)
+++
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeInstallations.java 2011-06-29
20:35:01 UTC (rev 32460)
@@ -1,111 +1,126 @@
package org.jboss.tools.forge.core.preferences;
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.jboss.tools.forge.core.ForgeCorePlugin;
import org.jboss.tools.forge.core.process.ForgeEmbeddedRuntime;
import org.jboss.tools.forge.core.process.ForgeRuntime;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
public class ForgeInstallations {
static final String PREF_FORGE_INSTALLATIONS =
"org.jboss.tools.forge.core.installations";
-// private static List<ForgeRuntime> installations = null;
- private static ForgeRuntime DEFAULT_INSTALLATION = null;
+ public static final ForgeInstallations INSTANCE = new ForgeInstallations();
-// public static ForgeRuntime[] getInstallations() {
-// if (installations == null) {
-// initializeInstallations();
-// }
-// return (ForgeRuntime[])installations.toArray(new
ForgeRuntime[installations.size()]);
-// }
+ List<ForgeRuntime> installations = null;
+ ForgeRuntime defaultInstallation = null;
- public static ForgeRuntime getDefault() {
- if (DEFAULT_INSTALLATION == null) {
+ private ForgeInstallations() {}
+
+ public ForgeRuntime[] getInstallations() {
+ if (installations == null) {
initializeInstallations();
}
- return DEFAULT_INSTALLATION;
+ return (ForgeRuntime[])installations.toArray(new ForgeRuntime[installations.size()]);
}
-// private static IEclipsePreferences getForgeCorePreferences() {
-// return InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID);
-// }
+ public ForgeRuntime getDefault() {
+ if (defaultInstallation == null) {
+ initializeInstallations();
+ }
+ return defaultInstallation;
+ }
-// private static void initializeInstallations() {
-// initializeEmbeddedRuntime();
-// }
+ private IEclipsePreferences getForgeCorePreferences() {
+ return InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID);
+ }
-// private static void initializeEmbeddedRuntime() {
-
-// }
+ private String getForgeInstallationsPreference() {
+ return getForgeCorePreferences().get(
+ PREF_FORGE_INSTALLATIONS,
+ ForgePreferenceInitializer.INITIAL_INSTALLATIONS_PREFERENCE);
+ }
- private static void initializeInstallations() {
- DEFAULT_INSTALLATION = ForgeEmbeddedRuntime.INSTANCE;
+ private void initializeInstallations() {
+ initializeFromXml(getForgeInstallationsPreference());
}
-// private static void initializeInstallations() {
-// String installPrefsXml = getForgeCorePreferences().get(PREF_FORGE_INSTALLATIONS,
null);
-// if (installPrefsXml == null || "".equals(installPrefsXml)) {
-// createInitialInstallations();
-// installPrefsXml = getForgeCorePreferences().get(PREF_FORGE_INSTALLATIONS, null);
-// }
-// initializeFromXml(installPrefsXml);
-// }
-//
-// private static void initializeFromXml(String installPrefsXml) {
-// if (installPrefsXml == null) return;
-// DocumentBuilder documentBuilder = newDocumentBuilder();
-// if (documentBuilder == null) return;
-// InputStream inputStream = createInputStream(installPrefsXml);
-// if (inputStream == null) return;
-// installations = new ArrayList<ForgeRuntime>();
-// Document document = parseInstallations(documentBuilder, inputStream);
-// Element installationsElement = document.getDocumentElement();
-// String defaultInstallationName =
installationsElement.getAttribute("default");
-// NodeList nodeList = installationsElement.getChildNodes();
-// for (int i = 0; i < nodeList.getLength(); i++) {
-// Node node = nodeList.item(i);
-// if (node.getNodeType() == Node.ELEMENT_NODE) {
-// Element element = (Element)node;
-// String name = element.getAttribute("name");
-// String location = element.getAttribute("location");
-// ForgeRuntime newInstallation = new ForgeRuntime(name, location);
-// installations.add(newInstallation);
-// if (name.equals(defaultInstallationName)) {
-// defaultInstallation = newInstallation;
-// }
-// }
-// }
-// }
-//
-// private static Document parseInstallations(DocumentBuilder documentBuilder,
InputStream inputStream) {
-// Document result = null;
-// try {
-// result = documentBuilder.parse(inputStream);
-// } catch (SAXException e) {
-// ForgeUIPlugin.log(e);
-// } catch (IOException e) {
-// ForgeUIPlugin.log(e);
-// }
-// return result;
-// }
-//
-// private static InputStream createInputStream(String string) {
-// InputStream result = null;
-// try {
-// result = new BufferedInputStream(new
ByteArrayInputStream(string.getBytes("UTF8")));
-// } catch (UnsupportedEncodingException e) {
-// ForgeUIPlugin.log(e);
-// }
-// return result;
-// }
-//
-// private static DocumentBuilder newDocumentBuilder() {
-// try {
-// return DocumentBuilderFactory.newInstance().newDocumentBuilder();
-// } catch (ParserConfigurationException e) {
-// ForgeUIPlugin.log(e);
-// return null;
-// }
-// }
-//
+ private void initializeFromXml(String xml) {
+ DocumentBuilder documentBuilder = newDocumentBuilder();
+ if (documentBuilder == null) return;
+ InputStream inputStream = createInputStream(xml);
+ if (inputStream == null) return;
+ installations = new ArrayList<ForgeRuntime>();
+ Document document = parseInstallations(documentBuilder, inputStream);
+ Element installationsElement = document.getDocumentElement();
+ String defaultInstallationName =
installationsElement.getAttribute("default");
+ NodeList nodeList = installationsElement.getChildNodes();
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node node = nodeList.item(i);
+ if (node.getNodeType() == Node.ELEMENT_NODE) {
+ Element element = (Element)node;
+ String type = element.getAttribute("type");
+ ForgeRuntime runtime = null;
+ if ("embedded".equals(type)) {
+ runtime = ForgeEmbeddedRuntime.INSTANCE;
+ }
+ if (runtime == null) continue;
+ installations.add(runtime);
+ if (defaultInstallationName.equals(runtime.getName())) {
+ defaultInstallation = runtime;
+ }
+ }
+ }
+ }
+
+ private Document parseInstallations(DocumentBuilder documentBuilder, InputStream
inputStream) {
+ Document result = null;
+ try {
+ result = documentBuilder.parse(inputStream);
+ } catch (SAXException e) {
+ ForgeCorePlugin.log(e);
+ } catch (IOException e) {
+ ForgeCorePlugin.log(e);
+ }
+ return result;
+ }
+
+ private InputStream createInputStream(String string) {
+ InputStream result = null;
+ try {
+ result = new BufferedInputStream(new
ByteArrayInputStream(string.getBytes("UTF8")));
+ } catch (UnsupportedEncodingException e) {
+ ForgeCorePlugin.log(e);
+ }
+ return result;
+ }
+
+ private DocumentBuilder newDocumentBuilder() {
+ try {
+ return DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ ForgeCorePlugin.log(e);
+ return null;
+ }
+ }
+
// private static Document createEmptyDocument() {
// DocumentBuilder documentBuilder = newDocumentBuilder();
// if (documentBuilder == null) {
Modified:
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgePreferenceInitializer.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgePreferenceInitializer.java 2011-06-29
20:10:29 UTC (rev 32459)
+++
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgePreferenceInitializer.java 2011-06-29
20:35:01 UTC (rev 32460)
@@ -10,7 +10,7 @@
static final String INITIAL_INSTALLATIONS_PREFERENCE =
"<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"no\"?>\n" +
"<forgeInstallations default=\"embedded\">" +
- " <installation name=\"embedded\"/>" +
+ " <installation name=\"embedded\"
type=\"embedded\"/>" +
"</forgeInstallations>";
@Override
Modified:
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeInstallationsTest.java
===================================================================
---
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeInstallationsTest.java 2011-06-29
20:10:29 UTC (rev 32459)
+++
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeInstallationsTest.java 2011-06-29
20:35:01 UTC (rev 32460)
@@ -7,9 +7,47 @@
public class ForgeInstallationsTest {
+ private static final String ALTERNATIVE_FORGE_INSTALLATIONS =
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"no\"?>\n" +
+ "<forgeInstallations default=\"foo\">" +
+ " <installation name=\"embedded\"
type=\"embedded\"/>" +
+ " <installation name=\"foo\" location=\"foofoo\"
type=\"external\"/>" +
+ " <installation name=\"bar\" location=\"barbar\"
type=\"external\"/>" +
+ "</forgeInstallations>";
+
@Test
- public void testGetDefault() {
- assertEquals(ForgeEmbeddedRuntime.INSTANCE, ForgeInstallations.getDefault());
+ public void testGetDefaultInitialCase() {
+ assertEquals(ForgeEmbeddedRuntime.INSTANCE, ForgeInstallations.INSTANCE.getDefault());
}
+ @Test
+ public void testGetInstallationsInitialCase() {
+ assertEquals(1, ForgeInstallations.INSTANCE.getInstallations().length);
+ }
+
+// @Test
+// public void testGetDefaultAlternativeCase() {
+// ForgeInstallations.INSTANCE.defaultInstallation = null;
+// InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID).put(
+// ForgeInstallations.PREF_FORGE_INSTALLATIONS,
+// ALTERNATIVE_FORGE_INSTALLATIONS);
+// ForgeRuntime runtime = ForgeInstallations.INSTANCE.getDefault();
+// assertNotNull(ForgeInstallations.INSTANCE.defaultInstallation);
+// assertEquals("foo", runtime.getName());
+// }
+//
+// @Test
+// public void testGetInstallationsAlternativeCase() {
+// ForgeInstallations.INSTANCE.installations = null;
+// InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID).put(
+// ForgeInstallations.PREF_FORGE_INSTALLATIONS,
+// ALTERNATIVE_FORGE_INSTALLATIONS);
+// ForgeRuntime[] runtimes = ForgeInstallations.INSTANCE.getInstallations();
+// assertNotNull(ForgeInstallations.INSTANCE.installations);
+// assertEquals(3, runtimes.length);
+// }
+
+
+
+
}
Show replies by date