Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/model/PapyrusModelHelper.java')
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/model/PapyrusModelHelper.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/model/PapyrusModelHelper.java b/plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/model/PapyrusModelHelper.java
new file mode 100644
index 00000000000..8bb7e02f83a
--- /dev/null
+++ b/plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/model/PapyrusModelHelper.java
@@ -0,0 +1,111 @@
+/*****************************************************************************
+ * Copyright (c) 2011 Atos Origin Integration.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tristan Faure (Atos Origin Integration) tristan.faure@atosorigin.com - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.onefile.model;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.papyrus.infra.onefile.Activator;
+import org.osgi.framework.Bundle;
+
+/**
+ * Helper class able to get the correct {@link IPapyrusModelFactory}
+ *
+ * @author tfaure
+ *
+ */
+public class PapyrusModelHelper {
+
+ private static final String MODEL_FACTORY = "modelFactory";
+ static List<Segment> segments;
+
+ static {
+ IConfigurationElement[] elements = Platform
+ .getExtensionRegistry()
+ .getConfigurationElementsFor(Activator.PLUGIN_ID, MODEL_FACTORY);
+ segments = new ArrayList<PapyrusModelHelper.Segment>(elements.length);
+ for (IConfigurationElement e : elements) {
+ Segment s = new PapyrusModelHelper.Segment();
+ try {
+ s.priority = Integer.parseInt(e.getAttribute("priority"));
+ s.bundle = Platform.getBundle(e.getContributor().getName());
+ s.className = e.getAttribute("instance");
+ segments.add(s);
+ } catch (NumberFormatException ex) {
+ // in case of exception the process continue but the stack is
+ // traced
+ ex.printStackTrace();
+ }
+ }
+ // sort elements according to their priorities
+ Collections.sort(segments, new Comparator<Segment>() {
+ public int compare(Segment o1, Segment o2) {
+ Integer val1 = 0;
+ Integer val2 = 0;
+ if (o1 != null) {
+ val1 = o1.priority;
+ }
+ if (o2 != null) {
+ val2 = o2.priority;
+ }
+ return val1.compareTo(val2);
+ }
+ });
+ }
+
+ /**
+ * get the {@link IPapyrusModelFactory} in the current platform
+ *
+ * @return a {@link IPapyrusModelFactory}
+ */
+ public static IPapyrusModelFactory getPapyrusModelFactory() {
+ if (segments == null || segments.size() == 0) {
+ throw new RuntimeException(
+ "Initialisation error, please register extension to "
+ + Activator.PLUGIN_ID + "." + MODEL_FACTORY);
+ }
+ // segments are sorted by priorities, the highest is at the end of the
+ // list
+ return segments.get(segments.size() - 1).getFactory();
+ }
+
+ /**
+ * Utility class to save extension point information
+ *
+ */
+ private static class Segment {
+ public String className;
+ Bundle bundle;
+ int priority;
+ IPapyrusModelFactory instance = null;
+
+ IPapyrusModelFactory getFactory() {
+ if (instance == null) {
+ try {
+ instance = (IPapyrusModelFactory) bundle.loadClass(
+ className).newInstance();
+ } catch (InstantiationException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+ }
+ return instance;
+ }
+ }
+}

Back to the top